import { ChatMessage, CompletionOptions, LLMOptions, ModelProvider, } from "../../index.js"; import { stripImages } from "../images.js"; import { BaseLLM } from "../index.js"; import { streamSse } from "../stream.js"; const NON_CHAT_MODELS = [ "text-davinci-002", "text-davinci-003", "code-davinci-002", "text-ada-001", "text-babbage-001", "text-curie-001", "davinci", "curie", "babbage", "ada", ]; const CHAT_ONLY_MODELS = [ "gpt-3.5-turbo", "gpt-3.5-turbo-0613", "gpt-3.5-turbo-16k", "gpt-4", "gpt-4-turbo", "gpt-4o", "gpt-35-turbo-16k", "gpt-35-turbo-0613", "gpt-35-turbo", "gpt-4-32k", "gpt-4-turbo-preview", "gpt-4-vision", "gpt-4-0125-preview", "gpt-4-1106-preview", "gpt-4o-mini", ]; class OpenAI extends BaseLLM { public useLegacyCompletionsEndpoint: boolean | undefined = undefined; maxStopWords: number | undefined = undefined; constructor(options: LLMOptions) { super(options); this.useLegacyCompletionsEndpoint = options.useLegacyCompletionsEndpoint; this.apiVersion = options.apiVersion ?? "2023-07-01-preview"; } static providerName: ModelProvider = "openai"; static defaultOptions: Partial = { apiBase: "https://api.openai.com/v1/", }; protected _convertMessage(message: ChatMessage) { if (typeof message.content === "string") { return message; } else if (!message.content.some((item) => item.type !== "text")) { // If no multi-media is in the message, just send as text // for compatibility with OpenAI "compatible" servers // that don't support multi-media format return { ...message, content: message.content.map((item) => item.text).join(""), }; } const parts = message.content.map((part) => { const msg: any = { type: part.type, text: part.text, }; if (part.type === "imageUrl") { msg.image_url = { ...part.imageUrl, detail: "low" }; msg.type = "image_url"; } return msg; }); return { ...message, content: parts, }; } protected _convertModelName(model: string): string { return model; } private isO1Model(model?: string): boolean { return ( !!model && (model.startsWith("o1-preview") || model.startsWith("o1-mini")) ); } protected _convertArgs(options: any, messages: ChatMessage[]) { const url = new URL(this.apiBase!); const finalOptions: any = { messages: messages.map(this._convertMessage), model: this._convertModelName(options.model), max_tokens: options.maxTokens, temperature: options.temperature, top_p: options.topP, frequency_penalty: options.frequencyPenalty, presence_penalty: options.presencePenalty, stream: options.stream ?? true, stop: // Jan + Azure OpenAI don't truncate and will throw an error this.maxStopWords !== undefined ? options.stop?.slice(0, this.maxStopWords) : url.host === "api.deepseek.com" ? options.stop?.slice(0, 16) : url.port === "1337" || url.host === "api.openai.com" || url.host === "api.groq.com" || this.apiType === "azure" ? options.stop?.slice(0, 4) : options.stop, }; // OpenAI o1-preview and o1-mini: if (this.isO1Model(options.model)) { // a) use max_completion_tokens instead of max_tokens finalOptions.max_completion_tokens = options.maxTokens; finalOptions.max_tokens = undefined; // b) don't support streaming currently finalOptions.stream = false; // c) don't support system message finalOptions.messages = finalOptions.messages?.filter( (message: any) => message?.role !== "system", ); } return finalOptions; } protected _getHeaders() { return { "Content-Type": "application/json", Authorization: `Bearer ${this.apiKey}`, "api-key": this.apiKey ?? "", // For Azure }; } protected async _complete( prompt: string, options: CompletionOptions, ): Promise { let completion = ""; for await (const chunk of this._streamChat( [{ role: "user", content: prompt }], options, )) { completion += chunk.content; } return completion; } protected _getEndpoint( endpoint: "chat/completions" | "completions" | "models", ) { if (this.apiType === "azure") { return new URL( `openai/deployments/${this.engine}/${endpoint}?api-version=${this.apiVersion}`, this.apiBase, ); } if (!this.apiBase) { throw new Error( "No API base URL provided. Please set the 'apiBase' option in config.json", ); } return new URL(endpoint, this.apiBase); } protected async *_streamComplete( prompt: string, options: CompletionOptions, ): AsyncGenerator { for await (const chunk of this._streamChat( [{ role: "user", content: prompt }], options, )) { yield stripImages(chunk.content); } } protected async *_legacystreamComplete( prompt: string, options: CompletionOptions, ): AsyncGenerator { const args: any = this._convertArgs(options, []); args.prompt = prompt; args.messages = undefined; const response = await this.fetch(this._getEndpoint("completions"), { method: "POST", headers: this._getHeaders(), body: JSON.stringify({ ...args, stream: true, }), }); for await (const value of streamSse(response)) { if (value.choices?.[0]?.text && value.finish_reason !== "eos") { yield value.choices[0].text; } } } protected async *_streamChat( messages: ChatMessage[], options: CompletionOptions, ): AsyncGenerator { if ( !CHAT_ONLY_MODELS.includes(options.model) && this.supportsCompletions() && (NON_CHAT_MODELS.includes(options.model) || this.useLegacyCompletionsEndpoint || options.raw) ) { for await (const content of this._legacystreamComplete( stripImages(messages[messages.length - 1]?.content || ""), options, )) { yield { role: "assistant", content, }; } return; } const body = this._convertArgs(options, messages); // Empty messages cause an error in LM Studio body.messages = body.messages.map((m: any) => ({ ...m, content: m.content === "" ? " " : m.content, })) as any; const response = await this.fetch(this._getEndpoint("chat/completions"), { method: "POST", headers: this._getHeaders(), body: JSON.stringify(body), }); // Handle non-streaming response if (body.stream === false) { const data = await response.json(); yield data.choices[0].message; return; } for await (const value of streamSse(response)) { if (value.choices?.[0]?.delta?.content) { yield value.choices[0].delta; } } } protected async *_streamFim( prefix: string, suffix: string, options: CompletionOptions, ): AsyncGenerator { const endpoint = new URL("fim/completions", this.apiBase); const resp = await this.fetch(endpoint, { method: "POST", body: JSON.stringify({ model: options.model, prompt: prefix, suffix, max_tokens: options.maxTokens, temperature: options.temperature, top_p: options.topP, frequency_penalty: options.frequencyPenalty, presence_penalty: options.presencePenalty, stop: options.stop, stream: true, }), headers: { "Content-Type": "application/json", Accept: "application/json", "x-api-key": this.apiKey ?? "", Authorization: `Bearer ${this.apiKey}`, }, }); for await (const chunk of streamSse(resp)) { yield chunk.choices[0].delta.content; } } async listModels(): Promise { const response = await this.fetch(this._getEndpoint("models"), { method: "GET", headers: this._getHeaders(), }); const data = await response.json(); return data.data.map((m: any) => m.id); } } export default OpenAI;