import axios, { AxiosInstance, CreateAxiosDefaults } from 'axios'; import { AuthenticationError } from './exceptions'; import { get_balance, get_image, get_model, get_models, get_file, get_files, get_batch, get_batches, get_batch_status, post_ai_check, post_auth, post_batch, post_chat, post_embeddings, post_files, post_files_delete, post_token, post_tokens_count, stream_chat, stream_chat_readable, } from './api'; import { AccessToken, AICheckResult, Balance, Batch, BATCH_METHOD, BatchRequest, BatchStatus, BatchStatuses, Chat, ChatCompletion, ChatCompletionChunk, DeletedFile, Embeddings, Image, Model, Models, Token, Tokens, UploadedFile, UploadedFiles, WithXHeaders, } from './interfaces'; import { getDefaultSettings, Settings } from './settings'; import { EventEmitter } from 'events'; import { isRunningInBrowser } from './api/utils'; const GIGACHAT_MODEL = 'GigaChat'; export interface GigaChatClientConfig { /** Адрес относительно которого выполняются запросы */ baseUrl?: string; /** Адрес для запроса токена доступа OAuth 2.0 */ authUrl?: string; /** Авторизационные данные */ credentials?: string; /** Версия API, к которой предоставляется доступ */ scope?: string; /** JWE токен */ accessToken?: string; /** Название модели, от которой нужно получить ответ */ model?: string; /** Параметр цензуры */ profanityCheck?: boolean; /** Имя пользователя */ user?: string; /** Пароль */ password?: string; /** Таймаут в секундах */ timeout?: number; /** Детализация запросов в консоли */ verbose?: boolean; /** Флаги, включающие особенные фичи */ flags?: string[]; /** HTTPS Agent, который прокидывается в Axios клиент */ httpsAgent?: any; /** Включает работу библиотеку в браузере */ dangerouslyAllowBrowser?: boolean; } export interface DetectedImage { uuid?: string; postfix?: string; } export interface DetectedVideo { uuid?: string; cover_uuid?: string; postfix?: string; } export function detectImage(message: string): DetectedImage | null { const regex = /.+?)"\sfuse="true"\/>(?.*)?/ms; const match = regex.exec(message); if (!match || !match.groups) return null; return { uuid: match.groups['uuid'], postfix: match.groups['postfix'], }; } export function detectVideo(message: string): DetectedVideo | null { const regex = /.+?)"\ssrc="(?.+?)"\sfuse="true"\/>(?.+)?/ms; const match = regex.exec(message); if (!match || !match.groups) return null; return { uuid: match.groups['uuid'], cover_uuid: match.groups['cover_uuid'], postfix: match.groups['postfix'], }; } export class GigaChat { public _client: AxiosInstance; public _authClient: AxiosInstance; public _settings: Settings; protected _accessToken?: AccessToken; constructor(config: GigaChatClientConfig) { this._settings = { ...getDefaultSettings(), ...config } as Settings; if (!this._settings.dangerouslyAllowBrowser && isRunningInBrowser()) { throw new Error( 'Скорее всего вы пытаетесь запустить библиотеку GigaChat в браузере. Такое использование выключено по умолчанию, так как при таком использовании вы рискуете разоблачить ваш токен GigaChat третьим лицам! Чтобы включить использование библиотеки проставьте параметр dangerouslyAllowBrowser. ', ); } if (this._settings.accessToken) { this._accessToken = { access_token: this._settings.accessToken, expires_at: 0, }; } this._client = axios.create(this._getAxiosConfig()); this._authClient = axios.create(this._getAuthAxiosConfig()); if (this._settings.accessToken) { this._accessToken = { access_token: this._settings.accessToken, expires_at: 0, }; } } protected get token(): string | undefined { return this._accessToken?.access_token; } protected get useAuth(): boolean { return Boolean(this._settings.credentials || (this._settings.user && this._settings.password)); } protected checkValidityToken(): boolean { /** Проверить время завершения действия токена */ return !!this._accessToken; } protected resetToken(): void { /** Сбросить токен */ this._accessToken = undefined; } protected parseChat(payload: Chat | Record | string): Chat { let chat: Chat; if (typeof payload === 'string') { chat = { messages: [{ role: 'user', content: payload }], }; } else { chat = payload as Chat; } chat.model = chat.model ?? this._settings.model ?? GIGACHAT_MODEL; chat.profanity_check = chat.profanity_check ?? this._settings.profanityCheck; chat.flags = chat.flags ?? this._settings.flags; return chat; } private _getAxiosConfig(): CreateAxiosDefaults { return { baseURL: this._settings.baseUrl, timeout: this._settings.timeout * 1000, httpsAgent: this._settings.httpsAgent, validateStatus: () => true, }; } private _getAuthAxiosConfig(): CreateAxiosDefaults { return { httpsAgent: this._settings.httpsAgent, timeout: this._settings.timeout * 1000, validateStatus: () => true, }; } public async updateToken(): Promise { // if (authorization_cvar.get() !== undefined) { // return; // } if (this._settings.credentials) { const credData = await post_auth(this._authClient, { url: this._settings.authUrl, credentials: this._settings.credentials, scope: this._settings.scope, }); if ('tok' in credData) { // @ts-ignore this._accessToken = this._buildAccessToken(credData); } else { this._accessToken = credData; } console.info('OAUTH UPDATE TOKEN'); } else if (this._settings.user && this._settings.password) { const token = await post_token(this._client, { user: this._settings.user, password: this._settings.password, }); this._accessToken = this._buildAccessToken(token); console.info('UPDATE TOKEN'); } } private async _decorator(call: () => Promise): Promise { if (this.useAuth) { if (this.checkValidityToken()) { try { return await call(); } catch (error) { if (error instanceof AuthenticationError) { console.warn('AUTHENTICATION ERROR'); this.resetToken(); } else { throw error; } } } await this.updateToken(); } return await call(); } public async tokensCount(input: string[], model?: string): Promise { if (!model) { model = this._settings.model || GIGACHAT_MODEL; } return this._decorator(() => post_tokens_count(this._client, { input, model: model || this._settings.model || GIGACHAT_MODEL, accessToken: this.token, }), ); } public async aiCheck(input: string, model: string): Promise { return this._decorator(() => post_ai_check(this._client, { input, model, accessToken: this.token, }), ); } public async balance(): Promise { return this._decorator(() => get_balance(this._client, { accessToken: this.token })); } public async embeddings(texts: string[], model: string = 'Embeddings'): Promise { return this._decorator(() => post_embeddings(this._client, { accessToken: this.token, input: texts, model, }), ); } public async embeddingsBatch(payload: BatchRequest[]): Promise { return this._decorator(() => post_batch(this._client, { batch: payload, method: BATCH_METHOD.embedder, accessToken: this.token, }), ); } public async getModels(): Promise { return this._decorator(() => get_models(this._client, { accessToken: this.token })); } public async getModel(model: string): Promise { return this._decorator(() => get_model(this._client, { model, accessToken: this.token, }), ); } public async getImage(fileId: string): Promise { return this._decorator(() => get_image(this._client, { fileId, accessToken: this.token, }), ); } public async uploadFile(file: File, purpose: 'general' | 'assistant' = 'general'): Promise { return this._decorator(() => post_files(this._client, { file, purpose, accessToken: this.token })); } public async getFile(file: string): Promise { return this._decorator(() => get_file(this._client, { file, accessToken: this.token })); } public async getFiles(): Promise { return this._decorator(() => get_files(this._client, { accessToken: this.token })); } public async deleteFile(file: string): Promise { return this._decorator(() => post_files_delete(this._client, { file, accessToken: this.token })); } public async chat(payload: Chat | Record | string): Promise { const chat = this.parseChat(payload); return this._decorator(() => post_chat(this._client, { chat, accessToken: this.token, }), ); } public async chatsBatch(payload: BatchRequest[]) { const batch = payload.map((task) => ({ ...task, request: this.parseChat(task.request) })); return this._decorator(() => post_batch(this._client, { batch, method: BATCH_METHOD.chatCompletions, accessToken: this.token, }), ); } public async getBatch( fileId: string, ): Promise & WithXHeaders> { return this._decorator(() => get_batch(this._client, { fileId, accessToken: this.token })); } public async getBatches(): Promise { return this._decorator(() => get_batches(this._client, { accessToken: this.token })); } public async getBatchStatus(batchId: string): Promise { return this._decorator(() => get_batch_status(this._client, { batchId, accessToken: this.token, }), ); } public async stream_readable( payload: Chat | Record | string, abortSignal?: AbortSignal, ): Promise { const chat = this.parseChat(payload); return this._decorator(() => stream_chat_readable( this._client, { chat, accessToken: this.token, }, this._settings.dangerouslyAllowBrowser, abortSignal, ), ); } public async *stream( payload: Chat | Record | string, abortSignal?: AbortSignal, ): AsyncIterable { const chat = this.parseChat(payload); if (this.useAuth) { if (this.checkValidityToken()) { try { const stream = await stream_chat( this._client, { chat, accessToken: this.token }, this._settings.dangerouslyAllowBrowser, abortSignal, ); for await (const chunk of stream) { yield chunk; } return; } catch (error) { if (error instanceof AuthenticationError) { console.warn('AUTHENTICATION ERROR'); this.resetToken(); } else { throw error; } } } await this.updateToken(); } const stream = await stream_chat( this._client, { chat, accessToken: this.token }, this._settings.dangerouslyAllowBrowser, abortSignal, ); for await (const chunk of stream) { yield chunk; } } private _buildAccessToken(token: Token): AccessToken { return { access_token: token.tok, expires_at: token.exp, }; } } export default GigaChat;