From 1157a970a6dcc77cd0edb2c25169d40969289f03 Mon Sep 17 00:00:00 2001 From: Alexandre Payot <18074599+payoto@users.noreply.github.com> Date: Tue, 26 Dec 2023 03:25:07 +0100 Subject: [PATCH] Improve API support for self hosted OpenAI-like models (#701) * Support explicit disabling of templates Allows a user to specify `"template": "none"` in config.json to disable the chat templates and send the entire chat history as a List of message rather than a single formatted message. * OpenAI calls to complete go to v1/completions This commit directs - formatted messages to `v1/completions` - JSON conversations to `v1/chat/completions` --- core/index.d.ts | 1 + core/llm/index.ts | 5 +++-- core/llm/llms/OpenAI.ts | 45 ++++++++++++++++++++++++++++++++--------- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/core/index.d.ts b/core/index.d.ts index d0863cbfd..3b81aef68 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -299,6 +299,7 @@ type TemplateType = | "phind" | "anthropic" | "chatml" + | "none" | "deepseek"; type ModelProvider = diff --git a/core/llm/index.ts b/core/llm/index.ts index adfcd2e68..41d180d11 100644 --- a/core/llm/index.ts +++ b/core/llm/index.ts @@ -92,6 +92,7 @@ function autodetectTemplateFunction( anthropic: anthropicTemplateMessages, chatml: chatmlTemplateMessages, deepseek: deepseekTemplateMessages, + none: null, }; return mapping[templateType]; @@ -262,9 +263,9 @@ export abstract class BaseLLM implements ILLM { .join("\n"); return `Settings: ${settings} - + ############################################ - + ${prompt}`; } diff --git a/core/llm/llms/OpenAI.ts b/core/llm/llms/OpenAI.ts index 3f682728b..9ab66b145 100644 --- a/core/llm/llms/OpenAI.ts +++ b/core/llm/llms/OpenAI.ts @@ -37,25 +37,52 @@ class OpenAI extends BaseLLM { options: CompletionOptions ): Promise { let completion = ""; - for await (const chunk of this._streamChat( - [{ role: "user", content: prompt }], + for await (const chunk of this._streamComplete( + prompt, options )) { - completion += chunk.content; + completion += chunk; } - return completion; } + private _getCompletionUrl() { + if (this.apiType === "azure") { + return `${this.apiBase}/openai/deployments/${this.engine}/completions?api-version=${this.apiVersion}`; + } else { + return this.apiBase + "/v1/completions"; + } + } protected async *_streamComplete( prompt: string, options: CompletionOptions ): AsyncGenerator { - for await (const chunk of this._streamChat( - [{ role: "user", content: prompt }], - options - )) { - yield chunk.content; + const response = await this.fetch(this._getCompletionUrl(), { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${this.apiKey}`, + "api-key": this.apiKey || "", // For Azure + }, + body: JSON.stringify({ + ...{ + prompt, + model: options.model, + max_tokens: options.maxTokens, + temperature: options.temperature, + top_p: options.topP, + frequency_penalty: options.frequencyPenalty, + presence_penalty: options.presencePenalty, + stop: options.stop, + }, + stream: true, + }), + }); + + for await (const value of streamSse(response)) { + if (value.choices?.[0]?.text) { + yield value.choices[0].text; + } } }