import { ChatCompletionCreateParams, ChatCompletionChunk, ChatCompletion, CreateEmbeddingResponse, EmbeddingCreateParams } from "openai/resources"; import { OpenAILikeInterface } from "../interfaces/OpenAILike"; import { ChatCompletionStream, streamingResponseHandle } from "../utils/stream"; import { Logger } from "../utils/Logger"; const MINI_MAX_API_URL = "https://api.minimax.chat" export class MiniMaxAPIService implements OpenAILikeInterface { apiKey: string constructor(apiKey: string) { this.apiKey = apiKey } async chatComplete(params: ChatCompletionCreateParams & { mask_sensitive_info?: boolean }): Promise | ChatCompletion> { const response = await fetch(MINI_MAX_API_URL + '/v1/text/chatcompletion_v2', { method: "POST", headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + this.apiKey }, body: JSON.stringify(params) }) if (!response.ok || response.status !== 200) { return Promise.reject(response.status + ":" + response.statusText) } if (params.stream) { const stream = new ChatCompletionStream() streamingResponseHandle(response, (data) => { if (data === null) { // stream.end() return } try { const json = JSON.parse(data) const chuck: ChatCompletionChunk = { id: json.id, model: params.model, created: Date.now(), object: "chat.completion.chunk", choices: json.choices.map((choice: any) => { return { index: choice.index, delta: { role: choice.delta?.role || choice.message?.role, content: choice.delta?.content || choice.message?.content, tool_calls: choice.delta?.tool_calls || choice.message?.tool_calls }, finish_reason: choice.finish_reason } }), } stream.write(chuck) // Logger.debug("----------", json.choices[0].finish_reason) if (json.choices[0].message) { setTimeout(() => { stream.end() }, 500) } } catch (err) { Logger.error("mini max chat complete stream parse error", err) } }) return stream } else { const json = await response.json() Logger.debug("mini max chat complete response", json) if (json.base_resp.status_code !== 0) { return Promise.reject(json.base_resp.status_msg) } const data = json return { id: data.id || "", object: "chat.completion", model: params.model, created: Date.now(), choices: data.choices.map((choice: any) => { return { index: choice.index || 0, message: { role: choice.message.role, content: choice.message.content, tool_calls: choice.tool_calls }, finish_reason: choice.finish_reason, } }), usage: { prompt_tokens: data.usage.prompt_tokens || 0, total_tokens: data.usage.total_tokens || 0, completion_tokens: data.usage.completion_tokens || 0 } } as ChatCompletion } } embeddings(params: EmbeddingCreateParams): Promise { throw new Error("Method not implemented."); } }