continue-custom/core/llm/llms/OpenAI.ts
Nate Sesti 53b61288ec
🎨 Refactor core (#1281)
* 🎨 refactor out react context

* 🎨 refactor local storage

* 🎨 refactor IdeMessenger

* 🔥 removing unused files

* 🚚 move scripts

* ✅ setup VSCode integration testing

* 🎨 fix test-related stuff

* 🚧 testing

* 🎨 tweak terminal cmd+L

* 🧑‍💻 install biome in core

* 🎨 run biome check on core

* 🎨 run biome check in vscode

* 🎨 run biome check in binary

* 🧑‍💻 global biome config

* 🎨 run biome check on core

* 🎨 run biome check in vscode

* 🎨 fix a few biome warnings

* 🚧 WIP on protocols

* 🚧 progress, organizing, about to remove webviewCore protocol

* 🚧 now running

* 🚧 move indexing code to core

* 🚧 WIP on JetBrains indexing implementation

* 🎨 finish listDir

* 🚧 gui fixes

* 🏷️ update IMessenger.on return type

* 🐛 fix jetbrains types + IDE detection

* 🧑‍💻 set up debugging for binary with TcpMessenger

* 👷 fix prepackage.js

* 🧑‍💻 turn off debug mode for intellij

* 🐛 merge fixes

* 🐛 fixes after refactor

* 🐛 merge fixes

* 💄 increase font size in JB

* 🔀 merge changes

* 🩹 small fixes

* 🐛 fix jetbrains build

* 🩹 more jetbrains fixes

* 🎨 jetbrains improvements

* 🐛 fix lancedb prob in jetbrains build

* 👷 update jetbrains build process

* ✨ intellij problems context provider

* 👷 add script utils file

* 💚 fix jetbrains build regression

* 🎨 dynamic import transformers.js

* 🩹 small jetbrains updates

* ✨ folder context provider in jetbrains

* 🎨 many more jetbrains improvements

* 🔀 merge fixes

* ⚡️ small improvements

* 🎨 tell users transformers.js not supported

* 🎨 trial updates

* 📝 update jetbrains readmej

* ⚡️ only use smaller context for local autocomplete models

* ⚡️ improve bracket filter

* ✨ global .continue/.prompts folder

* 💄 improved model setup process

* 🥅 display VSCode req errs
2024-05-24 19:38:13 -07:00

240 lines
5.9 KiB
TypeScript

import {
ChatMessage,
CompletionOptions,
LLMOptions,
ModelProvider,
} from "../../index.js";
import { stripImages } from "../countTokens.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",
];
class OpenAI extends BaseLLM {
public useLegacyCompletionsEndpoint: boolean | undefined = undefined;
protected maxStopWords: number | undefined = undefined;
constructor(options: LLMOptions) {
super(options);
this.useLegacyCompletionsEndpoint = options.useLegacyCompletionsEndpoint;
}
static providerName: ModelProvider = "openai";
static defaultOptions: Partial<LLMOptions> = {
apiBase: "https://api.openai.com/v1/",
};
protected _convertMessage(message: ChatMessage) {
if (typeof message.content === "string") {
return message;
}
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: "low" };
msg.type = "image_url";
}
return msg;
});
return {
...message,
content: parts,
};
}
protected _convertModelName(model: string): string {
return model;
}
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:
// Jan + Azure OpenAI don't truncate and will throw an error
this.maxStopWords !== undefined
? options.stop?.slice(0, this.maxStopWords)
: url.port === "1337" ||
url.host === "api.openai.com" ||
url.host === "api.groq.com" ||
this.apiType === "azure"
? options.stop?.slice(0, 4)
: options.stop,
};
return finalOptions;
}
protected _getHeaders() {
return {
"Content-Type": "application/json",
Authorization: `Bearer ${this.apiKey}`,
"api-key": this.apiKey ?? "", // For Azure
};
}
protected async _complete(
prompt: string,
options: CompletionOptions,
): Promise<string> {
let completion = "";
for await (const chunk of this._streamChat(
[{ role: "user", content: prompt }],
options,
)) {
completion += chunk.content;
}
return completion;
}
private _getEndpoint(
endpoint: "chat/completions" | "completions" | "models",
) {
if (this.apiType === "azure") {
return new URL(
`openai/deployments/${this.engine}/${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,
options: CompletionOptions,
): AsyncGenerator<string> {
for await (const chunk of this._streamChat(
[{ role: "user", content: prompt }],
options,
)) {
yield stripImages(chunk.content);
}
}
protected async *_legacystreamComplete(
prompt: string,
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,
}),
});
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[],
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(
stripImages(messages[messages.length - 1]?.content || ""),
options,
)) {
yield {
role: "assistant",
content,
};
}
return;
}
const body = {
...this._convertArgs(options, messages),
stream: true,
};
// Empty messages cause an error in LM Studio
body.messages = body.messages.map((m) => ({
...m,
content: m.content === "" ? " " : m.content,
})) as any;
const response = await this.fetch(this._getEndpoint("chat/completions"), {
method: "POST",
headers: this._getHeaders(),
body: JSON.stringify(body),
});
for await (const value of streamSse(response)) {
if (value.choices?.[0]?.delta?.content) {
yield value.choices[0].delta;
}
}
}
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;