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; + } } }