381 lines
10 KiB
TypeScript
381 lines
10 KiB
TypeScript
import {
|
|
ChatMessage,
|
|
CompletionOptions,
|
|
LLMOptions,
|
|
ModelProvider,
|
|
Tool,
|
|
} from "../../index.js";
|
|
import { renderChatMessage } from "../../util/messageContent.js";
|
|
import { BaseLLM } from "../index.js";
|
|
import { streamSse } from "../stream.js";
|
|
|
|
const NON_CHAT_MODELS = [
|
|
"text-davinci-002",
|
|
"text-davinci-003",
|
|
"code-davinci-002",
|
|
"text-ada-001",
|
|
"text-babbage-001",
|
|
"text-curie-001",
|
|
"davinci",
|
|
"curie",
|
|
"babbage",
|
|
"ada",
|
|
];
|
|
|
|
const CHAT_ONLY_MODELS = [
|
|
"gpt-3.5-turbo",
|
|
"gpt-3.5-turbo-0613",
|
|
"gpt-3.5-turbo-16k",
|
|
"gpt-4",
|
|
"gpt-4-turbo",
|
|
"gpt-4o",
|
|
"gpt-35-turbo-16k",
|
|
"gpt-35-turbo-0613",
|
|
"gpt-35-turbo",
|
|
"gpt-4-32k",
|
|
"gpt-4-turbo-preview",
|
|
"gpt-4-vision",
|
|
"gpt-4-0125-preview",
|
|
"gpt-4-1106-preview",
|
|
"gpt-4o-mini",
|
|
"o1-preview",
|
|
"o1-mini",
|
|
];
|
|
|
|
class OpenAI extends BaseLLM {
|
|
public useLegacyCompletionsEndpoint: boolean | undefined = undefined;
|
|
|
|
constructor(options: LLMOptions) {
|
|
super(options);
|
|
this.useLegacyCompletionsEndpoint = options.useLegacyCompletionsEndpoint;
|
|
this.apiVersion = options.apiVersion ?? "2023-07-01-preview";
|
|
}
|
|
|
|
static providerName: ModelProvider = "openai";
|
|
static defaultOptions: Partial<LLMOptions> = {
|
|
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;
|
|
}
|
|
|
|
private isO1Model(model?: string): boolean {
|
|
return (
|
|
!!model && (model.startsWith("o1-preview") || model.startsWith("o1-mini"))
|
|
);
|
|
}
|
|
|
|
protected supportsPrediction(model: string): boolean {
|
|
return ["gpt-4o-mini", "gpt-4o"].includes(model);
|
|
}
|
|
|
|
private convertTool(tool: Tool): any {
|
|
return {
|
|
type: tool.type,
|
|
function: {
|
|
name: tool.function.name,
|
|
description: tool.function.description,
|
|
parameters: tool.function.parameters,
|
|
strict: tool.function.strict,
|
|
},
|
|
};
|
|
}
|
|
|
|
protected _convertArgs(options: CompletionOptions, messages: ChatMessage[]) {
|
|
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,
|
|
};
|
|
|
|
// OpenAI o1-preview and o1-mini:
|
|
if (this.isO1Model(options.model)) {
|
|
// a) use max_completion_tokens instead of max_tokens
|
|
finalOptions.max_completion_tokens = options.maxTokens;
|
|
finalOptions.max_tokens = undefined;
|
|
|
|
// b) don't support system message
|
|
finalOptions.messages = finalOptions.messages?.filter(
|
|
(message: any) => message?.role !== "system",
|
|
);
|
|
}
|
|
|
|
if (options.prediction && 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 = options.prediction;
|
|
}
|
|
|
|
return finalOptions;
|
|
}
|
|
|
|
protected _getHeaders() {
|
|
return {
|
|
"Content-Type": "application/json",
|
|
Authorization: `Bearer ${this.apiKey}`,
|
|
"api-key": this.apiKey ?? "", // For Azure
|
|
};
|
|
}
|
|
|
|
protected async _complete(
|
|
prompt: string,
|
|
signal: AbortSignal,
|
|
options: CompletionOptions,
|
|
): Promise<string> {
|
|
let completion = "";
|
|
for await (const chunk of this._streamChat(
|
|
[{ role: "user", content: prompt }],
|
|
signal,
|
|
options,
|
|
)) {
|
|
completion += chunk.content;
|
|
}
|
|
|
|
return completion;
|
|
}
|
|
|
|
protected _getEndpoint(
|
|
endpoint: "chat/completions" | "completions" | "models",
|
|
) {
|
|
if (this.apiType === "azure") {
|
|
return new URL(
|
|
`openai/deployments/${this.deployment}/${endpoint}?api-version=${this.apiVersion}`,
|
|
this.apiBase,
|
|
);
|
|
}
|
|
if (!this.apiBase) {
|
|
throw new Error(
|
|
"No API base URL provided. Please set the 'apiBase' option in config.json",
|
|
);
|
|
}
|
|
|
|
return new URL(endpoint, this.apiBase);
|
|
}
|
|
|
|
protected async *_streamComplete(
|
|
prompt: string,
|
|
signal: AbortSignal,
|
|
options: CompletionOptions,
|
|
): AsyncGenerator<string> {
|
|
for await (const chunk of this._streamChat(
|
|
[{ role: "user", content: prompt }],
|
|
signal,
|
|
options,
|
|
)) {
|
|
yield renderChatMessage(chunk);
|
|
}
|
|
}
|
|
|
|
protected async *_legacystreamComplete(
|
|
prompt: string,
|
|
signal: AbortSignal,
|
|
options: CompletionOptions,
|
|
): AsyncGenerator<string> {
|
|
const args: any = this._convertArgs(options, []);
|
|
args.prompt = prompt;
|
|
args.messages = undefined;
|
|
|
|
const response = await this.fetch(this._getEndpoint("completions"), {
|
|
method: "POST",
|
|
headers: this._getHeaders(),
|
|
body: JSON.stringify({
|
|
...args,
|
|
stream: true,
|
|
}),
|
|
signal,
|
|
});
|
|
|
|
for await (const value of streamSse(response)) {
|
|
if (value.choices?.[0]?.text && value.finish_reason !== "eos") {
|
|
yield value.choices[0].text;
|
|
}
|
|
}
|
|
}
|
|
|
|
protected async *_streamChat(
|
|
messages: ChatMessage[],
|
|
signal: AbortSignal,
|
|
options: CompletionOptions,
|
|
): AsyncGenerator<ChatMessage> {
|
|
if (
|
|
!CHAT_ONLY_MODELS.includes(options.model) &&
|
|
this.supportsCompletions() &&
|
|
(NON_CHAT_MODELS.includes(options.model) ||
|
|
this.useLegacyCompletionsEndpoint ||
|
|
options.raw)
|
|
) {
|
|
for await (const content of this._legacystreamComplete(
|
|
renderChatMessage(messages[messages.length - 1]),
|
|
signal,
|
|
options,
|
|
)) {
|
|
yield {
|
|
role: "assistant",
|
|
content,
|
|
};
|
|
}
|
|
return;
|
|
}
|
|
|
|
const body = this._convertArgs(options, messages);
|
|
// Empty messages cause an error in LM Studio
|
|
body.messages = body.messages.map((m: any) => ({
|
|
...m,
|
|
content: m.content === "" ? " " : m.content,
|
|
// We call it toolCalls, they call it tool_calls
|
|
tool_calls: m.toolCalls,
|
|
tool_call_id: m.toolCallId,
|
|
})) as any;
|
|
const response = await this.fetch(this._getEndpoint("chat/completions"), {
|
|
method: "POST",
|
|
headers: this._getHeaders(),
|
|
body: JSON.stringify(body),
|
|
signal,
|
|
});
|
|
|
|
// Handle non-streaming response
|
|
if (body.stream === false) {
|
|
const data = await response.json();
|
|
yield data.choices[0].message;
|
|
return;
|
|
}
|
|
|
|
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,
|
|
},
|
|
}),
|
|
),
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
protected async *_streamFim(
|
|
prefix: string,
|
|
suffix: string,
|
|
signal: AbortSignal,
|
|
options: CompletionOptions,
|
|
): AsyncGenerator<string> {
|
|
const endpoint = new URL("fim/completions", this.apiBase);
|
|
const resp = await this.fetch(endpoint, {
|
|
method: "POST",
|
|
body: JSON.stringify({
|
|
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,
|
|
}),
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Accept: "application/json",
|
|
"x-api-key": this.apiKey ?? "",
|
|
Authorization: `Bearer ${this.apiKey}`,
|
|
},
|
|
signal,
|
|
});
|
|
for await (const chunk of streamSse(resp)) {
|
|
yield chunk.choices[0].delta.content;
|
|
}
|
|
}
|
|
|
|
async listModels(): Promise<string[]> {
|
|
const response = await this.fetch(this._getEndpoint("models"), {
|
|
method: "GET",
|
|
headers: this._getHeaders(),
|
|
});
|
|
|
|
const data = await response.json();
|
|
return data.data.map((m: any) => m.id);
|
|
}
|
|
}
|
|
|
|
export default OpenAI;
|