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`
This commit is contained in:
parent
6b17de49ed
commit
1157a970a6
3 changed files with 40 additions and 11 deletions
1
core/index.d.ts
vendored
1
core/index.d.ts
vendored
|
|
@ -299,6 +299,7 @@ type TemplateType =
|
|||
| "phind"
|
||||
| "anthropic"
|
||||
| "chatml"
|
||||
| "none"
|
||||
| "deepseek";
|
||||
|
||||
type ModelProvider =
|
||||
|
|
|
|||
|
|
@ -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}`;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -37,25 +37,52 @@ class OpenAI extends BaseLLM {
|
|||
options: CompletionOptions
|
||||
): Promise<string> {
|
||||
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<string> {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue