diff --git a/.github/workflows/build_publish.yaml b/.github/workflows/build_publish.yaml index 2a2ca98..d14313c 100644 --- a/.github/workflows/build_publish.yaml +++ b/.github/workflows/build_publish.yaml @@ -14,7 +14,6 @@ on: pull_request: jobs: - lint_and_test: runs-on: ubuntu-latest @@ -76,6 +75,4 @@ jobs: run: | echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}" >> .npmrc npm version ${{ github.ref_name }} - sed -i 's/VERSION = '\''0.0.1'\''/VERSION = '\''${{ github.ref_name }}'\''/g' src/client.js - npm publish - + npm publish diff --git a/package-lock.json b/package-lock.json index ccd3293..8255cd4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@mistralai/mistralai", - "version": "0.0.1", + "version": "0.2.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@mistralai/mistralai", - "version": "0.0.1", + "version": "0.2.0", "license": "ISC", "dependencies": { "node-fetch": "^2.6.7" diff --git a/package.json b/package.json index 4419619..d1b9820 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@mistralai/mistralai", - "version": "0.0.1", + "version": "0.2.0", "description": "", "author": "bam4d@mistral.ai", "license": "ISC", @@ -12,7 +12,9 @@ "test": "node --experimental-vm-modules node_modules/.bin/jest" }, "jest": { - "testPathIgnorePatterns": ["examples"] + "testPathIgnorePatterns": [ + "examples" + ] }, "repository": { "type": "git", diff --git a/src/client.d.ts b/src/client.d.ts index 37549ef..5b63e9c 100644 --- a/src/client.d.ts +++ b/src/client.d.ts @@ -1,162 +1,173 @@ -declare module '@mistralai/mistralai' { - export interface ModelPermission { - id: string; - object: 'model_permission'; - created: number; - allow_create_engine: boolean; - allow_sampling: boolean; - allow_logprobs: boolean; - allow_search_indices: boolean; - allow_view: boolean; - allow_fine_tuning: boolean; - organization: string; - group: string | null; - is_blocking: boolean; - } - - export interface Model { - id: string; - object: 'model'; - created: number; - owned_by: string; - root: string | null; - parent: string | null; - permission: ModelPermission[]; - } - - export interface ListModelsResponse { - object: 'list'; - data: Model[]; - } - - export interface Function { - name: string; - description: string; - parameters: object; - } - - export interface FunctionCall { - name: string; - arguments: string; - } - - export interface ToolCalls { - id: string; - function: FunctionCall; - } - - export interface ResponseFormat { - type: 'json_object'; - } - - export interface TokenUsage { - prompt_tokens: number; - completion_tokens: number; - total_tokens: number; - } - - export interface ChatCompletionResponseChoice { - index: number; - message: { - role: string; - content: string; - tool_calls: null | ToolCalls[]; - }; - finish_reason: string; - } - - export interface ChatCompletionResponseChunkChoice { - index: number; - delta: { - role?: string; - content?: string; - tool_calls?: ToolCalls[]; - }; - finish_reason: string; - } - - export interface ChatCompletionResponse { - id: string; - object: 'chat.completion'; - created: number; - model: string; - choices: ChatCompletionResponseChoice[]; - usage: TokenUsage; - } - - export interface ChatCompletionResponseChunk { - id: string; - object: 'chat.completion.chunk'; - created: number; - model: string; - choices: ChatCompletionResponseChunkChoice[]; - usage: TokenUsage | null; - } - - export interface Embedding { - id: string; - object: 'embedding'; - embedding: number[]; - } - - export interface EmbeddingResponse { - id: string; - object: 'list'; - data: Embedding[]; - model: string; - usage: TokenUsage; - } - - export interface Message { - role: string; - content: string | string[] - } - - export interface Tool { - type: 'function'; - function: Function; - } - - export interface ChatRequest { - model: string; - messages: Array; - tools?: Array; - temperature?: number; - maxTokens?: number; - topP?: number; - randomSeed?: number; - /** - * @deprecated use safePrompt instead - */ - safeMode?: boolean; - safePrompt?: boolean; - toolChoice?: 'auto' | 'any' | 'none'; - responseFormat?: ResponseFormat; - } - - export interface ChatRequestOptions { - signal?: AbortSignal - } - - class MistralClient { - apiKey: string - endpoint: string - maxRetries: number - timeout: number - - constructor(apiKey?: string, endpoint?: string, maxRetries?: number, timeout?: number); - - listModels(): Promise; - - chat(request: ChatRequest, options?: ChatRequestOptions): Promise; - - chatStream(request: ChatRequest, options?: ChatRequestOptions): AsyncGenerator; - - embeddings(options: { - model: string; - input: string | string[]; - }): Promise; - } - - export default MistralClient; +declare module "@mistralai/mistralai" { + export interface ModelPermission { + id: string; + object: "model_permission"; + created: number; + allow_create_engine: boolean; + allow_sampling: boolean; + allow_logprobs: boolean; + allow_search_indices: boolean; + allow_view: boolean; + allow_fine_tuning: boolean; + organization: string; + group: string | null; + is_blocking: boolean; + } + + export interface Model { + id: string; + object: "model"; + created: number; + owned_by: string; + root: string | null; + parent: string | null; + permission: ModelPermission[]; + } + + export interface ListModelsResponse { + object: "list"; + data: Model[]; + } + + export interface Function { + name: string; + description: string; + parameters: object; + } + + export interface FunctionCall { + name: string; + arguments: string; + } + + export interface ToolCalls { + id: string; + function: FunctionCall; + } + + export interface ResponseFormat { + type: "json_object"; + } + + export interface TokenUsage { + prompt_tokens: number; + completion_tokens: number; + total_tokens: number; + } + + export interface ChatCompletionResponseChoice { + index: number; + message: { + role: string; + content: string; + tool_calls: null | ToolCalls[]; + }; + finish_reason: string; + } + + export interface ChatCompletionResponseChunkChoice { + index: number; + delta: { + role?: string; + content?: string; + tool_calls?: ToolCalls[]; + }; + finish_reason: string; + } + + export interface ChatCompletionResponse { + id: string; + object: "chat.completion"; + created: number; + model: string; + choices: ChatCompletionResponseChoice[]; + usage: TokenUsage; + } + + export interface ChatCompletionResponseChunk { + id: string; + object: "chat.completion.chunk"; + created: number; + model: string; + choices: ChatCompletionResponseChunkChoice[]; + usage: TokenUsage | null; + } + + export interface Embedding { + id: string; + object: "embedding"; + embedding: number[]; + } + + export interface EmbeddingResponse { + id: string; + object: "list"; + data: Embedding[]; + model: string; + usage: TokenUsage; + } + + export interface Message { + role: string; + content: string | string[]; + } + + export interface Tool { + type: "function"; + function: Function; + } + + export interface ChatRequest { + model: string; + messages: Array; + tools?: Array; + temperature?: number; + maxTokens?: number; + topP?: number; + randomSeed?: number; + /** + * @deprecated use safePrompt instead + */ + safeMode?: boolean; + safePrompt?: boolean; + toolChoice?: "auto" | "any" | "none"; + responseFormat?: ResponseFormat; + } + + export interface ChatRequestOptions { + signal?: AbortSignal; + } + + class MistralClient { + apiKey: string; + endpoint: string; + maxRetries: number; + timeout: number; + + constructor( + apiKey?: string, + endpoint?: string, + maxRetries?: number, + timeout?: number + ); + + listModels(): Promise; + + chat( + request: ChatRequest, + options?: ChatRequestOptions + ): Promise; + + chatStream( + request: ChatRequest, + options?: ChatRequestOptions + ): AsyncGenerator; + + embeddings(options: { + model: string; + input: string | string[]; + }): Promise; + } + + export default MistralClient; } diff --git a/src/client.js b/src/client.js index d429d54..0db5943 100644 --- a/src/client.js +++ b/src/client.js @@ -1,11 +1,12 @@ -const VERSION = '0.0.3'; +const VERSION = '0.2.0'; const RETRY_STATUS_CODES = [429, 500, 502, 503, 504]; const ENDPOINT = 'https://api.mistral.ai'; // We can't use a top level await if eventually this is to be converted // to typescript and compiled to commonjs, or similarly using babel. const configuredFetch = Promise.resolve( - globalThis.fetch ?? import('node-fetch').then((m) => m.default)); + globalThis.fetch ?? import('node-fetch').then((m) => m.default), +); /** * MistralAPIError @@ -21,7 +22,7 @@ class MistralAPIError extends Error { super(message); this.name = 'MistralAPIError'; } -}; +} /** * @param {Array} signals to merge @@ -34,9 +35,13 @@ function combineSignals(signals) { return; } - signal.addEventListener('abort', () => { - controller.abort(signal.reason); - }, {once: true}); + signal.addEventListener( + 'abort', + () => { + controller.abort(signal.reason); + }, + {once: true}, + ); if (signal.aborted) { controller.abort(signal.reason); @@ -106,8 +111,10 @@ class MistralClient { 'Authorization': `Bearer ${this.apiKey}`, }, body: method !== 'get' ? JSON.stringify(request) : null, - signal: combineSignals( - [AbortSignal.timeout(this.timeout * 1000), signal]), + signal: combineSignals([ + AbortSignal.timeout(this.timeout * 1000), + signal, + ]), }; for (let attempts = 0; attempts < this.maxRetries; attempts++) { @@ -149,12 +156,12 @@ class MistralClient { ); // eslint-disable-next-line max-len await new Promise((resolve) => - setTimeout(resolve, Math.pow(2, (attempts + 1)) * 500), + setTimeout(resolve, Math.pow(2, attempts + 1) * 500), ); } else { throw new MistralAPIError( `HTTP error! status: ${response.status} ` + - `Response: \n${await response.text()}`, + `Response: \n${await response.text()}`, ); } } catch (error) { @@ -165,7 +172,7 @@ class MistralClient { if (attempts === this.maxRetries - 1) throw error; // eslint-disable-next-line max-len await new Promise((resolve) => - setTimeout(resolve, Math.pow(2, (attempts + 1)) * 500), + setTimeout(resolve, Math.pow(2, attempts + 1) * 500), ); } } @@ -204,9 +211,7 @@ class MistralClient { ) { // if modelDefault and model are undefined, throw an error if (!model && !this.modelDefault) { - throw new MistralAPIError( - 'You must provide a model name', - ); + throw new MistralAPIError('You must provide a model name'); } return { model: model ?? this.modelDefault, @@ -259,19 +264,22 @@ class MistralClient { * default timeout signal * @return {Promise} */ - chat = async function({ - model, - messages, - tools, - temperature, - maxTokens, - topP, - randomSeed, - safeMode, - safePrompt, - toolChoice, - responseFormat, - }, {signal} = {}) { + chat = async function( + { + model, + messages, + tools, + temperature, + maxTokens, + topP, + randomSeed, + safeMode, + safePrompt, + toolChoice, + responseFormat, + }, + {signal} = {}, + ) { const request = this._makeChatCompletionRequest( model, messages, @@ -322,19 +330,22 @@ class MistralClient { * default timeout signal * @return {Promise} */ - chatStream = async function* ({ - model, - messages, - tools, - temperature, - maxTokens, - topP, - randomSeed, - safeMode, - safePrompt, - toolChoice, - responseFormat, - }, {signal} = {}) { + chatStream = async function* ( + { + model, + messages, + tools, + temperature, + maxTokens, + topP, + randomSeed, + safeMode, + safePrompt, + toolChoice, + responseFormat, + }, + {signal} = {}, + ) { const request = this._makeChatCompletionRequest( model, messages,