From 38131a0e7fa205939c47d4d854aa15b39fcf8958 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Thu, 7 Nov 2024 02:28:57 -0800 Subject: [PATCH 1/3] openai prediction take 1 --- core/index.d.ts | 1 + core/llm/llms/OpenAI.ts | 24 +++++++++++++++++++++++- extensions/vscode/config_schema.json | 5 +++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/core/index.d.ts b/core/index.d.ts index 5b0b892f6..d73385dbe 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -794,6 +794,7 @@ interface BaseCompletionOptions { keepAlive?: number; raw?: boolean; stream?: boolean; + predictionContent?: string; } export interface ModelCapability { diff --git a/core/llm/llms/OpenAI.ts b/core/llm/llms/OpenAI.ts index b626a9698..110e1e8ff 100644 --- a/core/llm/llms/OpenAI.ts +++ b/core/llm/llms/OpenAI.ts @@ -95,7 +95,14 @@ class OpenAI extends BaseLLM { ); } - protected _convertArgs(options: any, messages: ChatMessage[]) { + private supportsPrediction(model: string): boolean { + return [ + "gpt-4o-mini", + "gpt-4o" + ].includes(model); + } + + protected _convertArgs(options: CompletionOptions, messages: ChatMessage[]) { const url = new URL(this.apiBase!); const finalOptions: any = { messages: messages.map(this._convertMessage), @@ -135,6 +142,21 @@ class OpenAI extends BaseLLM { ); } + if (options.predictionContent && this.supportsPrediction(options.model)) { + if (finalOptions.presence_penalty) { // prediction doesn't support > 0 + finalOptions.presence_penalty = undefined; + } + if (finalOptions.frequency_penalty) { // prediction doesn't support > 0 + finalOptions.frequency_penalty = undefined; + } + finalOptions.max_completion_tokens = undefined; + + finalOptions.prediction = { + "type": "content", + "content": options.predictionContent + }; + } + return finalOptions; } diff --git a/extensions/vscode/config_schema.json b/extensions/vscode/config_schema.json index f18bdbb6a..3f4b90b19 100644 --- a/extensions/vscode/config_schema.json +++ b/extensions/vscode/config_schema.json @@ -65,6 +65,11 @@ "title": "Ollama keep_alive", "description": "The number of seconds after no requests are made to unload the model from memory. Defaults to 60*30 = 30min", "type": "integer" + }, + "predictionContent": { + "title": "Prediction", + "description": "(gpt-4o/gpt-4o-mini only) Prediction output which can greatly improve response times when large parts of the model response are known ahead of time. This is most common when you are regenerating a file with only minor changes to most of the content.", + "type": "string" } } }, From b3b7b5b9cab7048d8f84b51d6eb56d8b7408d60c Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Thu, 7 Nov 2024 02:46:29 -0800 Subject: [PATCH 2/3] prediction full implementation --- core/index.d.ts | 10 +++++- core/llm/llms/OpenAI.ts | 7 ++-- extensions/vscode/config_schema.json | 50 ++++++++++++++++++++++++++-- 3 files changed, 59 insertions(+), 8 deletions(-) diff --git a/core/index.d.ts b/core/index.d.ts index d73385dbe..ef8b7c128 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -780,6 +780,14 @@ export interface CustomCommand { description: string; } +interface Prediction { + type: "content" + content: string | { + type: "text" + text: string + }[] +} + interface BaseCompletionOptions { temperature?: number; topP?: number; @@ -794,7 +802,7 @@ interface BaseCompletionOptions { keepAlive?: number; raw?: boolean; stream?: boolean; - predictionContent?: string; + prediction?: Prediction; } export interface ModelCapability { diff --git a/core/llm/llms/OpenAI.ts b/core/llm/llms/OpenAI.ts index 110e1e8ff..168e0afff 100644 --- a/core/llm/llms/OpenAI.ts +++ b/core/llm/llms/OpenAI.ts @@ -142,7 +142,7 @@ class OpenAI extends BaseLLM { ); } - if (options.predictionContent && this.supportsPrediction(options.model)) { + if (options.prediction && this.supportsPrediction(options.model)) { if (finalOptions.presence_penalty) { // prediction doesn't support > 0 finalOptions.presence_penalty = undefined; } @@ -151,10 +151,7 @@ class OpenAI extends BaseLLM { } finalOptions.max_completion_tokens = undefined; - finalOptions.prediction = { - "type": "content", - "content": options.predictionContent - }; + finalOptions.prediction = options.prediction; } return finalOptions; diff --git a/extensions/vscode/config_schema.json b/extensions/vscode/config_schema.json index 3f4b90b19..e3736a3bb 100644 --- a/extensions/vscode/config_schema.json +++ b/extensions/vscode/config_schema.json @@ -66,10 +66,56 @@ "description": "The number of seconds after no requests are made to unload the model from memory. Defaults to 60*30 = 30min", "type": "integer" }, - "predictionContent": { + "prediction": { "title": "Prediction", "description": "(gpt-4o/gpt-4o-mini only) Prediction output which can greatly improve response times when large parts of the model response are known ahead of time. This is most common when you are regenerating a file with only minor changes to most of the content.", - "type": "string" + "type": "object", + "properties": { + "type": { + "type": "string", + "enum": [ + "content" + ], + "description": "Currently only 'content' type supported" + }, + "content": { + "description": "Predicted output (usually contents of a file)", + "anyOf": [ + { + "type": "string", + "description": "Predicted output (usually contents of a file)" + }, + { + "type": "array", + "description": "Array of predicted output text items (usually pieces of a file)", + "items": { + "type": "object", + "properties": { + "type": { + "type": "string", + "enum": [ + "text" + ], + "description": "Currently only 'text' supported" + }, + "text": { + "type": "string", + "description": "Predicted output item text" + } + }, + "required": [ + "type", + "text" + ] + } + } + ] + } + }, + "required": [ + "type", + "content" + ] } } }, From 0e166fcf32ee637f5776c954432c720da9854ab3 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Thu, 7 Nov 2024 09:18:34 -0800 Subject: [PATCH 3/3] remove prediction from config_schema.json --- extensions/vscode/config_schema.json | 51 ---------------------------- 1 file changed, 51 deletions(-) diff --git a/extensions/vscode/config_schema.json b/extensions/vscode/config_schema.json index e3736a3bb..f18bdbb6a 100644 --- a/extensions/vscode/config_schema.json +++ b/extensions/vscode/config_schema.json @@ -65,57 +65,6 @@ "title": "Ollama keep_alive", "description": "The number of seconds after no requests are made to unload the model from memory. Defaults to 60*30 = 30min", "type": "integer" - }, - "prediction": { - "title": "Prediction", - "description": "(gpt-4o/gpt-4o-mini only) Prediction output which can greatly improve response times when large parts of the model response are known ahead of time. This is most common when you are regenerating a file with only minor changes to most of the content.", - "type": "object", - "properties": { - "type": { - "type": "string", - "enum": [ - "content" - ], - "description": "Currently only 'content' type supported" - }, - "content": { - "description": "Predicted output (usually contents of a file)", - "anyOf": [ - { - "type": "string", - "description": "Predicted output (usually contents of a file)" - }, - { - "type": "array", - "description": "Array of predicted output text items (usually pieces of a file)", - "items": { - "type": "object", - "properties": { - "type": { - "type": "string", - "enum": [ - "text" - ], - "description": "Currently only 'text' supported" - }, - "text": { - "type": "string", - "description": "Predicted output item text" - } - }, - "required": [ - "type", - "text" - ] - } - } - ] - } - }, - "required": [ - "type", - "content" - ] } } },