From 0b894699a0222e19bf12a0870364b81dcd70ecfb Mon Sep 17 00:00:00 2001 From: Nate Date: Wed, 11 Dec 2024 10:31:40 -0800 Subject: [PATCH] fix o1 --- core/llm/index.ts | 16 ++++++++++++++-- core/llm/llms/OpenAI.ts | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/core/llm/index.ts b/core/llm/index.ts index 82e413ae8..55f0ac58f 100644 --- a/core/llm/index.ts +++ b/core/llm/index.ts @@ -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, diff --git a/core/llm/llms/OpenAI.ts b/core/llm/llms/OpenAI.ts index 82afb8cea..9f56eb2be 100644 --- a/core/llm/llms/OpenAI.ts +++ b/core/llm/llms/OpenAI.ts @@ -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,