This commit is contained in:
Nate 2024-12-11 10:31:40 -08:00
parent 353237caf9
commit 0b894699a0
2 changed files with 46 additions and 2 deletions

View file

@ -1,6 +1,10 @@
import { fetchwithRequestOptions } from "@continuedev/fetch";
import { findLlmInfo } from "@continuedev/llm-info";
import { BaseLlmApi, constructLlmApi } from "@continuedev/openai-adapters";
import {
BaseLlmApi,
ChatCompletionCreateParams,
constructLlmApi,
} from "@continuedev/openai-adapters";
import Handlebars from "handlebars";
import {
@ -659,6 +663,12 @@ export abstract class BaseLLM implements ILLM {
return { role: "assistant" as const, content: completion };
}
protected modifyChatBody(
body: ChatCompletionCreateParams,
): ChatCompletionCreateParams {
return body;
}
async *streamChat(
_messages: ChatMessage[],
signal: AbortSignal,
@ -695,9 +705,11 @@ export abstract class BaseLLM implements ILLM {
}
} else {
if (this.shouldUseOpenAIAdapter("streamChat") && this.openaiAdapter) {
let body = toChatBody(messages, completionOptions);
body = this.modifyChatBody(body);
const stream = this.openaiAdapter.chatCompletionStream(
{
...toChatBody(messages, completionOptions),
...body,
stream: true,
},
signal,

View file

@ -212,6 +212,38 @@ class OpenAI extends BaseLLM {
}
}
protected modifyChatBody(
body: ChatCompletionCreateParams,
): ChatCompletionCreateParams {
body.stop = body.stop?.slice(0, this.getMaxStopWords());
// OpenAI o1-preview and o1-mini:
if (this.isO1Model(body.model)) {
// a) use max_completion_tokens instead of max_tokens
body.max_completion_tokens = body.max_tokens;
body.max_tokens = undefined;
// b) don't support system message
body.messages = body.messages?.filter(
(message: any) => message?.role !== "system",
);
}
if (body.prediction && this.supportsPrediction(body.model)) {
if (body.presence_penalty) {
// prediction doesn't support > 0
body.presence_penalty = undefined;
}
if (body.frequency_penalty) {
// prediction doesn't support > 0
body.frequency_penalty = undefined;
}
body.max_completion_tokens = undefined;
}
return body;
}
protected async *_legacystreamComplete(
prompt: string,
signal: AbortSignal,