refactor out OpenAI converters
This commit is contained in:
parent
99afbbe85d
commit
1bd3eb5401
9 changed files with 204 additions and 113 deletions
|
|
@ -1,34 +1,16 @@
|
|||
import { LLMOptions, ModelProvider, ChatMessage } from "../../index.js";
|
||||
import { LLMOptions, ModelProvider } from "../../index.js";
|
||||
|
||||
import OpenAI from "./OpenAI.js";
|
||||
|
||||
class Nvidia extends OpenAI {
|
||||
// NVIDIA NIMs currently limits the number of stops for Starcoder 2 to 4
|
||||
// https://docs.api.nvidia.com/nim/reference/bigcode-starcoder2-7b-infer
|
||||
protected maxStopWordsStarCoder = 4;
|
||||
maxStopWords = 4;
|
||||
static providerName: ModelProvider = "nvidia";
|
||||
static defaultOptions: Partial<LLMOptions> = {
|
||||
apiBase: "https://integrate.api.nvidia.com/v1/",
|
||||
useLegacyCompletionsEndpoint: false,
|
||||
};
|
||||
|
||||
protected _convertArgs(options: any, messages: ChatMessage[]) {
|
||||
const url = new URL(this.apiBase!);
|
||||
const finalOptions = {
|
||||
messages: messages.map(this._convertMessage),
|
||||
model: this._convertModelName(options.model),
|
||||
max_tokens: options.maxTokens,
|
||||
temperature: options.temperature,
|
||||
top_p: options.topP,
|
||||
frequency_penalty: options.frequencyPenalty,
|
||||
presence_penalty: options.presencePenalty,
|
||||
stop: options.model.includes("starcoder2")
|
||||
? options.stop?.slice(0, this.maxStopWordsStarCoder)
|
||||
: options.stop,
|
||||
};
|
||||
|
||||
return finalOptions;
|
||||
}
|
||||
}
|
||||
|
||||
export default Nvidia;
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
import { ChatCompletionCreateParams } from "openai/resources/index";
|
||||
import {
|
||||
ChatMessage,
|
||||
CompletionOptions,
|
||||
|
|
@ -7,6 +8,10 @@ import {
|
|||
} from "../../index.js";
|
||||
import { renderChatMessage } from "../../util/messageContent.js";
|
||||
import { BaseLLM } from "../index.js";
|
||||
import {
|
||||
fromChatCompletionChunk,
|
||||
toChatBody,
|
||||
} from "../openaiTypeConverters.js";
|
||||
import { streamSse } from "../stream.js";
|
||||
|
||||
const NON_CHAT_MODELS = [
|
||||
|
|
@ -56,44 +61,6 @@ class OpenAI extends BaseLLM {
|
|||
apiBase: "https://api.openai.com/v1/",
|
||||
};
|
||||
|
||||
protected _convertMessage(message: ChatMessage) {
|
||||
if (message.role === "tool") {
|
||||
return {
|
||||
role: "tool",
|
||||
content: message.content,
|
||||
tool_call_id: message.toolCallId,
|
||||
};
|
||||
}
|
||||
|
||||
if (typeof message.content === "string") {
|
||||
return message;
|
||||
} else if (!message.content.some((item) => item.type !== "text")) {
|
||||
// If no multi-media is in the message, just send as text
|
||||
// for compatibility with OpenAI "compatible" servers
|
||||
// that don't support multi-media format
|
||||
return {
|
||||
...message,
|
||||
content: message.content.map((item) => item.text).join(""),
|
||||
};
|
||||
}
|
||||
|
||||
const parts = message.content.map((part) => {
|
||||
const msg: any = {
|
||||
type: part.type,
|
||||
text: part.text,
|
||||
};
|
||||
if (part.type === "imageUrl") {
|
||||
msg.image_url = { ...part.imageUrl, detail: "auto" };
|
||||
msg.type = "image_url";
|
||||
}
|
||||
return msg;
|
||||
});
|
||||
return {
|
||||
...message,
|
||||
content: parts,
|
||||
};
|
||||
}
|
||||
|
||||
protected _convertModelName(model: string): string {
|
||||
return model;
|
||||
}
|
||||
|
|
@ -120,33 +87,32 @@ class OpenAI extends BaseLLM {
|
|||
};
|
||||
}
|
||||
|
||||
protected _convertArgs(options: CompletionOptions, messages: ChatMessage[]) {
|
||||
protected getMaxStopWords(): number {
|
||||
const url = new URL(this.apiBase!);
|
||||
const tools = options.tools?.map(this.convertTool);
|
||||
|
||||
const finalOptions: any = {
|
||||
messages: messages.map(this._convertMessage),
|
||||
model: this._convertModelName(options.model),
|
||||
max_tokens: options.maxTokens,
|
||||
temperature: options.temperature,
|
||||
top_p: options.topP,
|
||||
frequency_penalty: options.frequencyPenalty,
|
||||
presence_penalty: options.presencePenalty,
|
||||
stream: options.stream ?? true,
|
||||
stop:
|
||||
// Jan + Azure OpenAI don't truncate and will throw an error
|
||||
this.maxStopWords !== undefined
|
||||
? options.stop?.slice(0, this.maxStopWords)
|
||||
: url.host === "api.deepseek.com"
|
||||
? options.stop?.slice(0, 16)
|
||||
: url.port === "1337" ||
|
||||
url.host === "api.openai.com" ||
|
||||
url.host === "api.groq.com" ||
|
||||
this.apiType === "azure"
|
||||
? options.stop?.slice(0, 4)
|
||||
: options.stop,
|
||||
tools,
|
||||
};
|
||||
if (this.maxStopWords !== undefined) {
|
||||
return this.maxStopWords;
|
||||
} else if (url.host === "api.deepseek.com") {
|
||||
return 16;
|
||||
} else if (
|
||||
url.port === "1337" ||
|
||||
url.host === "api.openai.com" ||
|
||||
url.host === "api.groq.com" ||
|
||||
this.apiType === "azure"
|
||||
) {
|
||||
return 4;
|
||||
} else {
|
||||
return Infinity;
|
||||
}
|
||||
}
|
||||
|
||||
protected _convertArgs(
|
||||
options: CompletionOptions,
|
||||
messages: ChatMessage[],
|
||||
): ChatCompletionCreateParams {
|
||||
const finalOptions = toChatBody(messages, options);
|
||||
|
||||
finalOptions.stop = options.stop?.slice(0, this.getMaxStopWords());
|
||||
|
||||
// OpenAI o1-preview and o1-mini:
|
||||
if (this.isO1Model(options.model)) {
|
||||
|
|
@ -309,26 +275,9 @@ class OpenAI extends BaseLLM {
|
|||
}
|
||||
|
||||
for await (const value of streamSse(response)) {
|
||||
if (value.choices?.[0]?.delta?.content) {
|
||||
yield {
|
||||
role: "assistant",
|
||||
content: value.choices[0].delta.content,
|
||||
};
|
||||
} else if (value.choices?.[0]?.delta?.tool_calls) {
|
||||
yield {
|
||||
role: "assistant",
|
||||
content: "",
|
||||
toolCalls: value.choices?.[0]?.delta?.tool_calls.map(
|
||||
(tool_call: any) => ({
|
||||
id: tool_call.id,
|
||||
type: tool_call.type,
|
||||
function: {
|
||||
name: tool_call.function.name,
|
||||
arguments: tool_call.function.arguments,
|
||||
},
|
||||
}),
|
||||
),
|
||||
};
|
||||
const chunk = fromChatCompletionChunk(value);
|
||||
if (chunk) {
|
||||
yield chunk;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
126
core/llm/openaiTypeConverters.ts
Normal file
126
core/llm/openaiTypeConverters.ts
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
import {
|
||||
ChatCompletion,
|
||||
ChatCompletionChunk,
|
||||
ChatCompletionCreateParams,
|
||||
ChatCompletionMessageParam,
|
||||
} from "openai/resources/index";
|
||||
|
||||
import { ChatMessage, CompletionOptions } from "..";
|
||||
|
||||
export function toChatMessage(
|
||||
message: ChatMessage,
|
||||
): ChatCompletionMessageParam {
|
||||
if (message.role === "tool") {
|
||||
return {
|
||||
role: "tool",
|
||||
content: message.content,
|
||||
tool_call_id: message.toolCallId,
|
||||
};
|
||||
}
|
||||
|
||||
if (typeof message.content === "string") {
|
||||
return {
|
||||
role: message.role,
|
||||
content: message.content,
|
||||
};
|
||||
} else if (!message.content.some((item) => item.type !== "text")) {
|
||||
// If no multi-media is in the message, just send as text
|
||||
// for compatibility with OpenAI-"compatible" servers
|
||||
// that don't support multi-media format
|
||||
return {
|
||||
...message,
|
||||
content: message.content.map((item) => item.text).join(""),
|
||||
};
|
||||
}
|
||||
|
||||
const parts = message.content.map((part) => {
|
||||
const msg: any = {
|
||||
type: part.type,
|
||||
text: part.text,
|
||||
};
|
||||
if (part.type === "imageUrl") {
|
||||
msg.image_url = { ...part.imageUrl, detail: "auto" };
|
||||
msg.type = "image_url";
|
||||
}
|
||||
return msg;
|
||||
});
|
||||
|
||||
return {
|
||||
...message,
|
||||
content: parts,
|
||||
};
|
||||
}
|
||||
|
||||
export function toChatBody(
|
||||
messages: ChatMessage[],
|
||||
options: CompletionOptions,
|
||||
): ChatCompletionCreateParams {
|
||||
const tools = options.tools?.map((tool) => ({
|
||||
type: tool.type,
|
||||
function: {
|
||||
name: tool.function.name,
|
||||
description: tool.function.description,
|
||||
parameters: tool.function.parameters,
|
||||
strict: tool.function.strict,
|
||||
},
|
||||
}));
|
||||
|
||||
return {
|
||||
messages: messages.map(toChatMessage),
|
||||
model: options.model,
|
||||
max_tokens: options.maxTokens,
|
||||
temperature: options.temperature,
|
||||
top_p: options.topP,
|
||||
frequency_penalty: options.frequencyPenalty,
|
||||
presence_penalty: options.presencePenalty,
|
||||
stream: options.stream ?? true,
|
||||
stop: options.stop,
|
||||
tools,
|
||||
prediction: options.prediction,
|
||||
};
|
||||
}
|
||||
|
||||
export function fromChatResponse(response: ChatCompletion): ChatMessage {
|
||||
const message = response.choices[0].message;
|
||||
const toolCall = message.tool_calls?.[0];
|
||||
if (toolCall) {
|
||||
return {
|
||||
role: "assistant",
|
||||
content: "",
|
||||
toolCalls: message.tool_calls,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
role: "assistant",
|
||||
content: message.content ?? "",
|
||||
};
|
||||
}
|
||||
|
||||
export function fromChatCompletionChunk(
|
||||
chunk: ChatCompletionChunk,
|
||||
): ChatMessage | undefined {
|
||||
const delta = chunk.choices?.[0]?.delta;
|
||||
|
||||
if (delta?.content) {
|
||||
return {
|
||||
role: "assistant",
|
||||
content: delta.content,
|
||||
};
|
||||
} else if (delta?.tool_calls) {
|
||||
return {
|
||||
role: "assistant",
|
||||
content: "",
|
||||
toolCalls: delta?.tool_calls.map((tool_call: any) => ({
|
||||
id: tool_call.id,
|
||||
type: tool_call.type,
|
||||
function: {
|
||||
name: tool_call.function.name,
|
||||
arguments: tool_call.function.arguments,
|
||||
},
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
43
core/package-lock.json
generated
43
core/package-lock.json
generated
|
|
@ -14,6 +14,7 @@
|
|||
"@aws-sdk/credential-providers": "^3.620.1",
|
||||
"@continuedev/config-types": "^1.0.13",
|
||||
"@continuedev/llm-info": "^1.0.2",
|
||||
"@continuedev/openai-adapters": "../packages/openai-adapters",
|
||||
"@modelcontextprotocol/sdk": "^1.0.0",
|
||||
"@mozilla/readability": "^0.5.0",
|
||||
"@octokit/rest": "^20.0.2",
|
||||
|
|
@ -46,7 +47,7 @@
|
|||
"node-html-markdown": "^1.3.0",
|
||||
"ollama": "^0.4.6",
|
||||
"onnxruntime-node": "1.14.0",
|
||||
"openai": "^4.20.1",
|
||||
"openai": "^4.76.0",
|
||||
"p-limit": "^6.1.0",
|
||||
"partial-json": "^0.1.7",
|
||||
"pg": "^8.11.3",
|
||||
|
|
@ -102,6 +103,25 @@
|
|||
"node": ">=20.11.0"
|
||||
}
|
||||
},
|
||||
"../packages/openai-adapters": {
|
||||
"name": "@continuedev/openai-adapters",
|
||||
"version": "1.0.9",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"@azure/openai": "^1.0.0-beta.12",
|
||||
"@continuedev/config-types": "^1.0.5",
|
||||
"@continuedev/fetch": "^1.0.3",
|
||||
"node-fetch": "^3.3.2",
|
||||
"openai": "^4.67.0",
|
||||
"zod": "^3.23.8"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/follow-redirects": "^1.14.4",
|
||||
"cross-env": "^7.0.3",
|
||||
"ts-jest": "^29.2.3",
|
||||
"ts-node": "^10.9.2"
|
||||
}
|
||||
},
|
||||
"node_modules/@75lb/deep-merge": {
|
||||
"version": "1.1.2",
|
||||
"resolved": "https://registry.npmjs.org/@75lb/deep-merge/-/deep-merge-1.1.2.tgz",
|
||||
|
|
@ -3383,6 +3403,10 @@
|
|||
"resolved": "https://registry.npmjs.org/@continuedev/llm-info/-/llm-info-1.0.2.tgz",
|
||||
"integrity": "sha512-B5VFcZlwlwQg6htbIjCjj3z/pj3I929BzgWowiLzxYC40ag+v3e/hM9EDGWCEdNIT+PDMchAwS67iNB+WhE7qg=="
|
||||
},
|
||||
"node_modules/@continuedev/openai-adapters": {
|
||||
"resolved": "../packages/openai-adapters",
|
||||
"link": true
|
||||
},
|
||||
"node_modules/@esbuild/android-arm": {
|
||||
"version": "0.17.19",
|
||||
"resolved": "https://registry.npmjs.org/@esbuild/android-arm/-/android-arm-0.17.19.tgz",
|
||||
|
|
@ -12606,9 +12630,9 @@
|
|||
}
|
||||
},
|
||||
"node_modules/openai": {
|
||||
"version": "4.47.1",
|
||||
"resolved": "https://registry.npmjs.org/openai/-/openai-4.47.1.tgz",
|
||||
"integrity": "sha512-WWSxhC/69ZhYWxH/OBsLEirIjUcfpQ5+ihkXKp06hmeYXgBBIUCa9IptMzYx6NdkiOCsSGYCnTIsxaic3AjRCQ==",
|
||||
"version": "4.76.0",
|
||||
"resolved": "https://registry.npmjs.org/openai/-/openai-4.76.0.tgz",
|
||||
"integrity": "sha512-QBGIetjX1C9xDp5XGa/3mPnfKI9BgAe2xHQX6PmO98wuW9qQaurBaumcYptQWc9LHZZq7cH/Y1Rjnsr6uUDdVw==",
|
||||
"dependencies": {
|
||||
"@types/node": "^18.11.18",
|
||||
"@types/node-fetch": "^2.6.4",
|
||||
|
|
@ -12616,11 +12640,18 @@
|
|||
"agentkeepalive": "^4.2.1",
|
||||
"form-data-encoder": "1.7.2",
|
||||
"formdata-node": "^4.3.2",
|
||||
"node-fetch": "^2.6.7",
|
||||
"web-streams-polyfill": "^3.2.1"
|
||||
"node-fetch": "^2.6.7"
|
||||
},
|
||||
"bin": {
|
||||
"openai": "bin/cli"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"zod": "^3.23.8"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"zod": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/openai/node_modules/@types/node": {
|
||||
|
|
|
|||
|
|
@ -45,6 +45,7 @@
|
|||
"@aws-sdk/credential-providers": "^3.620.1",
|
||||
"@continuedev/config-types": "^1.0.13",
|
||||
"@continuedev/llm-info": "^1.0.2",
|
||||
"@continuedev/openai-adapters": "../packages/openai-adapters",
|
||||
"@modelcontextprotocol/sdk": "^1.0.0",
|
||||
"@mozilla/readability": "^0.5.0",
|
||||
"@octokit/rest": "^20.0.2",
|
||||
|
|
@ -77,7 +78,7 @@
|
|||
"node-html-markdown": "^1.3.0",
|
||||
"ollama": "^0.4.6",
|
||||
"onnxruntime-node": "1.14.0",
|
||||
"openai": "^4.20.1",
|
||||
"openai": "^4.76.0",
|
||||
"p-limit": "^6.1.0",
|
||||
"partial-json": "^0.1.7",
|
||||
"pg": "^8.11.3",
|
||||
|
|
|
|||
3
extensions/vscode/package-lock.json
generated
3
extensions/vscode/package-lock.json
generated
|
|
@ -99,6 +99,7 @@
|
|||
"@aws-sdk/credential-providers": "^3.620.1",
|
||||
"@continuedev/config-types": "^1.0.13",
|
||||
"@continuedev/llm-info": "^1.0.2",
|
||||
"@continuedev/openai-adapters": "../packages/openai-adapters",
|
||||
"@modelcontextprotocol/sdk": "^1.0.0",
|
||||
"@mozilla/readability": "^0.5.0",
|
||||
"@octokit/rest": "^20.0.2",
|
||||
|
|
@ -131,7 +132,7 @@
|
|||
"node-html-markdown": "^1.3.0",
|
||||
"ollama": "^0.4.6",
|
||||
"onnxruntime-node": "1.14.0",
|
||||
"openai": "^4.20.1",
|
||||
"openai": "^4.76.0",
|
||||
"p-limit": "^6.1.0",
|
||||
"partial-json": "^0.1.7",
|
||||
"pg": "^8.11.3",
|
||||
|
|
|
|||
3
gui/package-lock.json
generated
3
gui/package-lock.json
generated
|
|
@ -108,6 +108,7 @@
|
|||
"@aws-sdk/credential-providers": "^3.620.1",
|
||||
"@continuedev/config-types": "^1.0.13",
|
||||
"@continuedev/llm-info": "^1.0.2",
|
||||
"@continuedev/openai-adapters": "../packages/openai-adapters",
|
||||
"@modelcontextprotocol/sdk": "^1.0.0",
|
||||
"@mozilla/readability": "^0.5.0",
|
||||
"@octokit/rest": "^20.0.2",
|
||||
|
|
@ -140,7 +141,7 @@
|
|||
"node-html-markdown": "^1.3.0",
|
||||
"ollama": "^0.4.6",
|
||||
"onnxruntime-node": "1.14.0",
|
||||
"openai": "^4.20.1",
|
||||
"openai": "^4.76.0",
|
||||
"p-limit": "^6.1.0",
|
||||
"partial-json": "^0.1.7",
|
||||
"pg": "^8.11.3",
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ They are concerned with:
|
|||
- Cache behavior
|
||||
- max stop words
|
||||
- use legacy completions endpoint?
|
||||
- anything else that couldn't possibly be guess by the client since it won't know the endpoint behind the proxy
|
||||
|
||||
## Supported APIs
|
||||
|
||||
|
|
|
|||
|
|
@ -20,7 +20,6 @@ export const RequestOptionsSchema = z.object({
|
|||
// Base config objects
|
||||
export const BaseConfig = z.object({
|
||||
provider: z.string(),
|
||||
model: z.string(),
|
||||
requestOptions: RequestOptionsSchema.optional(),
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue