🎨 use new URL everywhere instead of strings

This commit is contained in:
Nate Sesti 2024-03-12 00:40:12 -07:00
parent 36f62daa66
commit 0c6f8d9605
16 changed files with 50 additions and 47 deletions

View file

@ -2,7 +2,7 @@ import { EmbedOptions } from "../..";
import BaseEmbeddingsProvider from "./BaseEmbeddingsProvider";
async function embedOne(chunk: string, options: EmbedOptions) {
const resp = await fetch(`${options.apiBase}/api/embeddings`, {
const resp = await fetch(new URL("api/embeddings", options.apiBase), {
method: "POST",
body: JSON.stringify({
model: options.model,
@ -19,7 +19,7 @@ async function embedOne(chunk: string, options: EmbedOptions) {
class OllamaEmbeddingsProvider extends BaseEmbeddingsProvider {
static defaultOptions: Partial<EmbedOptions> | undefined = {
apiBase: "http://localhost:11434",
apiBase: "http://localhost:11434/",
};
get id(): string {

View file

@ -4,7 +4,7 @@ import { fetchWithExponentialBackoff } from "./util";
class OpenAIEmbeddingsProvider extends BaseEmbeddingsProvider {
static defaultOptions: Partial<EmbedOptions> | undefined = {
apiBase: "https://api.openai.com/v1",
apiBase: "https://api.openai.com/v1/",
model: "text-embedding-3-small",
};
@ -13,15 +13,10 @@ class OpenAIEmbeddingsProvider extends BaseEmbeddingsProvider {
}
async embed(chunks: string[]) {
let apiBase = this.options.apiBase;
if (apiBase?.endsWith("/")) {
apiBase = apiBase.slice(0, -1);
}
return await Promise.all(
chunks.map(async (chunk) => {
const resp = await fetchWithExponentialBackoff(
`${apiBase}/embeddings`,
new URL("embeddings", this.options.apiBase).toString(),
{
method: "POST",
body: JSON.stringify({

View file

@ -113,8 +113,8 @@ export abstract class BaseLLM implements ILLM {
this.llmRequestHook = options.llmRequestHook;
this.apiKey = options.apiKey;
this.apiBase = options.apiBase;
if (this.apiBase?.endsWith("/")) {
this.apiBase = this.apiBase.slice(0, -1);
if (this.apiBase && !this.apiBase.endsWith("/")) {
this.apiBase = this.apiBase + "/";
}
this.engine = options.engine;

View file

@ -16,7 +16,7 @@ class Anthropic extends BaseLLM {
model: "claude-3-opus-20240229",
maxTokens: 4096,
},
apiBase: "https://api.anthropic.com/v1",
apiBase: "https://api.anthropic.com/v1/",
};
constructor(options: LLMOptions) {
@ -69,7 +69,7 @@ class Anthropic extends BaseLLM {
messages: ChatMessage[],
options: CompletionOptions,
): AsyncGenerator<ChatMessage> {
const response = await this.fetch(this.apiBase + "/messages", {
const response = await this.fetch(new URL("messages", this.apiBase), {
method: "POST",
headers: {
"Content-Type": "application/json",

View file

@ -4,7 +4,7 @@ import OpenAI from "./OpenAI";
class DeepInfra extends OpenAI {
static providerName: ModelProvider = "deepinfra";
static defaultOptions: Partial<LLMOptions> = {
apiBase: "https://api.deepinfra.com/v1/openai",
apiBase: "https://api.deepinfra.com/v1/openai/",
};
}

View file

@ -14,7 +14,7 @@ class GooglePalm extends BaseLLM {
static defaultOptions: Partial<LLMOptions> = {
model: "gemini-pro",
apiBase: "https://generativelanguage.googleapis.com",
apiBase: "https://generativelanguage.googleapis.com/",
};
protected async *_streamComplete(
@ -82,7 +82,10 @@ class GooglePalm extends BaseLLM {
messages: ChatMessage[],
options: CompletionOptions,
): AsyncGenerator<ChatMessage> {
const apiURL = `${this.apiBase}/v1/models/${options.model}:streamGenerateContent?key=${this.apiKey}`;
const apiURL = new URL(
`v1/models/${options.model}:streamGenerateContent?key=${this.apiKey}`,
this.apiBase,
);
const body = {
contents: messages.map((msg) => {
return {
@ -157,7 +160,10 @@ class GooglePalm extends BaseLLM {
msgList.push({ content: message.content });
}
const apiURL = `${this.apiBase}/v1beta2/models/${options.model}:generateMessage?key=${this.apiKey}`;
const apiURL = new URL(
`v1beta2/models/${options.model}:generateMessage?key=${this.apiKey}`,
this.apiBase,
);
const body = { prompt: { messages: msgList } };
const response = await this.fetch(apiURL, {
method: "POST",

View file

@ -5,13 +5,13 @@ import { streamSse } from "../stream";
class HuggingFaceTGI extends BaseLLM {
static providerName: ModelProvider = "huggingface-tgi";
static defaultOptions: Partial<LLMOptions> = {
apiBase: "http://localhost:8080",
apiBase: "http://localhost:8080/",
};
constructor(options: LLMOptions) {
super(options);
this.fetch(`${this.apiBase}/info`, {
this.fetch(new URL("info", this.apiBase), {
method: "GET",
}).then(async (response) => {
if (response.status !== 200) {
@ -48,13 +48,16 @@ class HuggingFaceTGI extends BaseLLM {
): AsyncGenerator<string> {
const args = this._convertArgs(options, prompt);
const response = await this.fetch(`${this.apiBase}/generate_stream`, {
method: "POST",
headers: {
"Content-Type": "application/json",
const response = await this.fetch(
new URL("generate_stream", this.apiBase),
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ inputs: prompt, parameters: args }),
},
body: JSON.stringify({ inputs: prompt, parameters: args }),
});
);
for await (const value of streamSse(response)) {
yield value.token.text;

View file

@ -4,7 +4,7 @@ import OpenAI from "./OpenAI";
class LMStudio extends OpenAI {
static providerName: ModelProvider = "lmstudio";
static defaultOptions: Partial<LLMOptions> = {
apiBase: "http://localhost:1234/v1",
apiBase: "http://localhost:1234/v1/",
};
}

View file

@ -5,7 +5,7 @@ import { streamSse } from "../stream";
class LlamaCpp extends BaseLLM {
static providerName: ModelProvider = "llama.cpp";
static defaultOptions: Partial<LLMOptions> = {
apiBase: "http://127.0.0.1:8080",
apiBase: "http://127.0.0.1:8080/",
};
private _convertArgs(options: CompletionOptions, prompt: string) {
@ -30,7 +30,7 @@ class LlamaCpp extends BaseLLM {
...this.requestOptions?.headers,
};
const resp = await this.fetch(`${this.apiBase}/completion`, {
const resp = await this.fetch(new URL("completion", this.apiBase), {
method: "POST",
headers,
body: JSON.stringify({

View file

@ -4,7 +4,7 @@ import OpenAI from "./OpenAI";
class Mistral extends OpenAI {
static providerName: ModelProvider = "mistral";
static defaultOptions: Partial<LLMOptions> = {
apiBase: "https://api.mistral.ai/v1",
apiBase: "https://api.mistral.ai/v1/",
model: "mistral-small",
};
}

View file

@ -11,7 +11,7 @@ import { streamResponse } from "../stream";
class Ollama extends BaseLLM {
static providerName: ModelProvider = "ollama";
static defaultOptions: Partial<LLMOptions> = {
apiBase: "http://localhost:11434",
apiBase: "http://localhost:11434/",
model: "codellama-7b",
};

View file

@ -41,7 +41,7 @@ class OpenAI extends BaseLLM {
static providerName: ModelProvider = "openai";
static defaultOptions: Partial<LLMOptions> = {
apiBase: "https://api.openai.com/v1",
apiBase: "https://api.openai.com/v1/",
};
protected _convertMessage(message: ChatMessage) {
@ -95,22 +95,21 @@ class OpenAI extends BaseLLM {
}
private _getEndpoint(
endpoint: "/chat/completions" | "/completions" | "/models",
endpoint: "chat/completions" | "completions" | "models",
) {
if (this.apiType === "azure") {
return `${this.apiBase}/openai/deployments/${this.engine}${endpoint}?api-version=${this.apiVersion}`;
return new URL(
`openai/deployments/${this.engine}${endpoint}?api-version=${this.apiVersion}`,
this.apiBase,
);
} else {
let url = this.apiBase;
if (!url) {
if (!this.apiBase) {
throw new Error(
"No API base URL provided. Please set the 'apiBase' option in config.json",
);
}
if (url.endsWith("/")) {
url = url.slice(0, -1);
}
return url + endpoint;
return new URL(endpoint, this.apiBase);
}
}
@ -134,7 +133,7 @@ class OpenAI extends BaseLLM {
args.prompt = prompt;
delete args.messages;
const response = await this.fetch(this._getEndpoint("/completions"), {
const response = await this.fetch(this._getEndpoint("completions"), {
method: "POST",
headers: {
"Content-Type": "application/json",
@ -184,7 +183,7 @@ class OpenAI extends BaseLLM {
...m,
content: m.content === "" ? " " : m.content,
})) as any;
const response = await this.fetch(this._getEndpoint("/chat/completions"), {
const response = await this.fetch(this._getEndpoint("chat/completions"), {
method: "POST",
headers: {
"Content-Type": "application/json",
@ -202,7 +201,7 @@ class OpenAI extends BaseLLM {
}
async listModels(): Promise<string[]> {
const response = await this.fetch(this._getEndpoint("/models"), {
const response = await this.fetch(this._getEndpoint("models"), {
method: "GET",
headers: {
Authorization: `Bearer ${this.apiKey}`,

View file

@ -4,7 +4,7 @@ import OpenAI from "./OpenAI";
class TextGenWebUI extends OpenAI {
static providerName: ModelProvider = "text-gen-webui";
static defaultOptions: Partial<LLMOptions> = {
apiBase: "http://localhost:5000/v1",
apiBase: "http://localhost:5000/v1/",
};
}

View file

@ -10,7 +10,7 @@ import OpenAI from "./OpenAI";
class Together extends OpenAI {
static providerName: ModelProvider = "together";
static defaultOptions: Partial<LLMOptions> = {
apiBase: "https://api.together.xyz/v1",
apiBase: "https://api.together.xyz/v1/",
};
private static MODEL_IDS: { [name: string]: string } = {

View file

@ -78,7 +78,7 @@ If you haven't already installed Continue, you can do that [here](https://market
"title": "Code Llama",
"provider": "openai",
"model": "codellama-7b",
"apiBase": "http://localhost:8000/v1"
"apiBase": "http://localhost:8000/v1/"
}
]
}

View file

@ -808,7 +808,7 @@ export const PROVIDER_INFO: { [key: string]: ModelInfo } = {
icon: "lmstudio.png",
tags: [ModelProviderTag["Local"], ModelProviderTag["Open-Source"]],
params: {
apiBase: "http://localhost:1234/v1",
apiBase: "http://localhost:1234/v1/",
},
packages: [
{
@ -899,7 +899,7 @@ After it's up and running, you can start using Continue.`,
collectInputFor: [
{
...apiBaseInput,
defaultValue: "http://localhost:8000/v1",
defaultValue: "http://localhost:8000/v1/",
},
...completionParamsInputs,
],