use openai adapters in BaseLLM
This commit is contained in:
parent
1bd3eb5401
commit
f0334fac80
6 changed files with 153 additions and 29 deletions
|
|
@ -1,4 +1,5 @@
|
|||
import { findLlmInfo } from "@continuedev/llm-info";
|
||||
import { BaseLlmApi, constructLlmApi } from "@continuedev/openai-adapters";
|
||||
import Handlebars from "handlebars";
|
||||
|
||||
import {
|
||||
|
|
@ -41,6 +42,13 @@ import {
|
|||
countTokens,
|
||||
pruneRawPromptFromTop,
|
||||
} from "./countTokens.js";
|
||||
import {
|
||||
fromChatCompletionChunk,
|
||||
LlmApiRequestType,
|
||||
toChatBody,
|
||||
toCompleteBody,
|
||||
toFimBody,
|
||||
} from "./openaiTypeConverters.js";
|
||||
import CompletionOptionsForModels from "./templates/options.js";
|
||||
|
||||
export abstract class BaseLLM implements ILLM {
|
||||
|
|
@ -120,6 +128,8 @@ export abstract class BaseLLM implements ILLM {
|
|||
|
||||
private _llmOptions: LLMOptions;
|
||||
|
||||
private openaiAdapter: BaseLlmApi;
|
||||
|
||||
constructor(_options: LLMOptions) {
|
||||
this._llmOptions = _options;
|
||||
|
||||
|
|
@ -196,6 +206,13 @@ export abstract class BaseLLM implements ILLM {
|
|||
this.apiType = options.apiType;
|
||||
this.region = options.region;
|
||||
this.projectId = options.projectId;
|
||||
|
||||
this.openaiAdapter = constructLlmApi({
|
||||
provider: this.providerName as any,
|
||||
apiKey: this.apiKey,
|
||||
apiBase: this.apiBase,
|
||||
requestOptions: this.requestOptions,
|
||||
});
|
||||
}
|
||||
|
||||
listModels(): Promise<string[]> {
|
||||
|
|
@ -431,6 +448,15 @@ export abstract class BaseLLM implements ILLM {
|
|||
throw new Error("Not implemented");
|
||||
}
|
||||
|
||||
protected useOpenAIAdapterFor: (LlmApiRequestType | "*")[] = [];
|
||||
|
||||
private shouldUseOpenAIAdapter(requestType: LlmApiRequestType) {
|
||||
return (
|
||||
this.useOpenAIAdapterFor.includes(requestType) ||
|
||||
this.useOpenAIAdapterFor.includes("*")
|
||||
);
|
||||
}
|
||||
|
||||
async *streamFim(
|
||||
prefix: string,
|
||||
suffix: string,
|
||||
|
|
@ -452,14 +478,29 @@ export abstract class BaseLLM implements ILLM {
|
|||
}
|
||||
|
||||
let completion = "";
|
||||
for await (const chunk of this._streamFim(
|
||||
prefix,
|
||||
suffix,
|
||||
signal,
|
||||
completionOptions,
|
||||
)) {
|
||||
completion += chunk;
|
||||
yield chunk;
|
||||
|
||||
if (this.shouldUseOpenAIAdapter("streamFim")) {
|
||||
const stream = this.openaiAdapter.fimStream(
|
||||
toFimBody(prefix, suffix, completionOptions),
|
||||
);
|
||||
for await (const chunk of stream) {
|
||||
const result = fromChatCompletionChunk(chunk);
|
||||
if (result) {
|
||||
const content = renderChatMessage(result);
|
||||
completion += content;
|
||||
yield content;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for await (const chunk of this._streamFim(
|
||||
prefix,
|
||||
suffix,
|
||||
signal,
|
||||
completionOptions,
|
||||
)) {
|
||||
completion += chunk;
|
||||
yield chunk;
|
||||
}
|
||||
}
|
||||
|
||||
this._logTokensGenerated(
|
||||
|
|
@ -509,13 +550,24 @@ export abstract class BaseLLM implements ILLM {
|
|||
|
||||
let completion = "";
|
||||
try {
|
||||
for await (const chunk of this._streamComplete(
|
||||
prompt,
|
||||
signal,
|
||||
completionOptions,
|
||||
)) {
|
||||
completion += chunk;
|
||||
yield chunk;
|
||||
if (this.shouldUseOpenAIAdapter("streamComplete")) {
|
||||
for await (const chunk of this.openaiAdapter.completionStream({
|
||||
...toCompleteBody(prompt, completionOptions),
|
||||
stream: true,
|
||||
})) {
|
||||
const content = chunk.choices[0]?.text ?? "";
|
||||
completion += content;
|
||||
yield content;
|
||||
}
|
||||
} else {
|
||||
for await (const chunk of this._streamComplete(
|
||||
prompt,
|
||||
signal,
|
||||
completionOptions,
|
||||
)) {
|
||||
completion += chunk;
|
||||
yield chunk;
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
this._logTokensGenerated(completionOptions.model, prompt, completion);
|
||||
|
|
@ -561,7 +613,16 @@ export abstract class BaseLLM implements ILLM {
|
|||
}
|
||||
}
|
||||
|
||||
const completion = await this._complete(prompt, signal, completionOptions);
|
||||
let completion: string;
|
||||
if (this.shouldUseOpenAIAdapter("complete")) {
|
||||
const result = await this.openaiAdapter.completionNonStream({
|
||||
...toCompleteBody(prompt, completionOptions),
|
||||
stream: false,
|
||||
});
|
||||
completion = result.choices[0].text;
|
||||
} else {
|
||||
completion = await this._complete(prompt, signal, completionOptions);
|
||||
}
|
||||
|
||||
this._logTokensGenerated(completionOptions.model, prompt, completion);
|
||||
|
||||
|
|
@ -619,13 +680,26 @@ export abstract class BaseLLM implements ILLM {
|
|||
yield { role: "assistant", content: chunk };
|
||||
}
|
||||
} else {
|
||||
for await (const chunk of this._streamChat(
|
||||
messages,
|
||||
signal,
|
||||
completionOptions,
|
||||
)) {
|
||||
completion += chunk.content;
|
||||
yield chunk;
|
||||
if (this.shouldUseOpenAIAdapter("streamChat")) {
|
||||
const stream = this.openaiAdapter.chatCompletionStream({
|
||||
...toChatBody(messages, completionOptions),
|
||||
stream: true,
|
||||
});
|
||||
for await (const chunk of stream) {
|
||||
const result = fromChatCompletionChunk(chunk);
|
||||
if (result) {
|
||||
yield result;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for await (const chunk of this._streamChat(
|
||||
messages,
|
||||
signal,
|
||||
completionOptions,
|
||||
)) {
|
||||
completion += chunk.content;
|
||||
yield chunk;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
|
|
|
|||
|
|
@ -3,8 +3,10 @@ import {
|
|||
ChatCompletionChunk,
|
||||
ChatCompletionCreateParams,
|
||||
ChatCompletionMessageParam,
|
||||
CompletionCreateParams,
|
||||
} from "openai/resources/index";
|
||||
|
||||
import { FimCreateParamsStreaming } from "@continuedev/openai-adapters/dist/apis/base";
|
||||
import { ChatMessage, CompletionOptions } from "..";
|
||||
|
||||
export function toChatMessage(
|
||||
|
|
@ -80,6 +82,42 @@ export function toChatBody(
|
|||
};
|
||||
}
|
||||
|
||||
export function toCompleteBody(
|
||||
prompt: string,
|
||||
options: CompletionOptions,
|
||||
): CompletionCreateParams {
|
||||
return {
|
||||
prompt,
|
||||
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,
|
||||
};
|
||||
}
|
||||
|
||||
export function toFimBody(
|
||||
prefix: string,
|
||||
suffix: string,
|
||||
options: CompletionOptions,
|
||||
): FimCreateParamsStreaming {
|
||||
return {
|
||||
model: options.model,
|
||||
prompt: prefix,
|
||||
suffix,
|
||||
max_tokens: options.maxTokens,
|
||||
temperature: options.temperature,
|
||||
top_p: options.topP,
|
||||
frequency_penalty: options.frequencyPenalty,
|
||||
presence_penalty: options.presencePenalty,
|
||||
stop: options.stop,
|
||||
stream: true,
|
||||
};
|
||||
}
|
||||
|
||||
export function fromChatResponse(response: ChatCompletion): ChatMessage {
|
||||
const message = response.choices[0].message;
|
||||
const toolCall = message.tool_calls?.[0];
|
||||
|
|
@ -124,3 +162,13 @@ export function fromChatCompletionChunk(
|
|||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export type LlmApiRequestType =
|
||||
| "chat"
|
||||
| "streamChat"
|
||||
| "complete"
|
||||
| "streamComplete"
|
||||
| "streamFim"
|
||||
| "embed"
|
||||
| "rerank"
|
||||
| "list";
|
||||
|
|
|
|||
2
core/package-lock.json
generated
2
core/package-lock.json
generated
|
|
@ -112,7 +112,7 @@
|
|||
"@continuedev/config-types": "^1.0.5",
|
||||
"@continuedev/fetch": "^1.0.3",
|
||||
"node-fetch": "^3.3.2",
|
||||
"openai": "^4.67.0",
|
||||
"openai": "^4.76.0",
|
||||
"zod": "^3.23.8"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
|
|
|||
8
packages/openai-adapters/package-lock.json
generated
8
packages/openai-adapters/package-lock.json
generated
|
|
@ -13,7 +13,7 @@
|
|||
"@continuedev/config-types": "^1.0.5",
|
||||
"@continuedev/fetch": "^1.0.3",
|
||||
"node-fetch": "^3.3.2",
|
||||
"openai": "^4.67.0",
|
||||
"openai": "^4.76.0",
|
||||
"zod": "^3.23.8"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
|
@ -3582,9 +3582,9 @@
|
|||
}
|
||||
},
|
||||
"node_modules/openai": {
|
||||
"version": "4.67.0",
|
||||
"resolved": "https://registry.npmjs.org/openai/-/openai-4.67.0.tgz",
|
||||
"integrity": "sha512-jdsPSEdZbUNVtvEFE/eeL4FjKavyVMJJEdGMZk9vExglqUrblEcFxi3LK2WhskhrYKAU1MgJAI+dK9pDcA5z5w==",
|
||||
"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",
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@
|
|||
"@continuedev/config-types": "^1.0.5",
|
||||
"@continuedev/fetch": "^1.0.3",
|
||||
"node-fetch": "^3.3.2",
|
||||
"openai": "^4.67.0",
|
||||
"openai": "^4.76.0",
|
||||
"zod": "^3.23.8"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
|
|
|||
|
|
@ -100,3 +100,5 @@ export {
|
|||
type CompletionCreateParamsNonStreaming,
|
||||
type CompletionCreateParamsStreaming,
|
||||
} from "openai/resources/index.mjs";
|
||||
|
||||
export type { BaseLlmApi } from "./apis/base.js";
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue