diff --git a/core/indexing/embeddings/OllamaEmbeddingsProvider.ts b/core/indexing/embeddings/OllamaEmbeddingsProvider.ts index e95ddad4f..3393cc111 100644 --- a/core/indexing/embeddings/OllamaEmbeddingsProvider.ts +++ b/core/indexing/embeddings/OllamaEmbeddingsProvider.ts @@ -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 | undefined = { - apiBase: "http://localhost:11434", + apiBase: "http://localhost:11434/", }; get id(): string { diff --git a/core/indexing/embeddings/OpenAIEmbeddingsProvider.ts b/core/indexing/embeddings/OpenAIEmbeddingsProvider.ts index 1d806d865..0710da682 100644 --- a/core/indexing/embeddings/OpenAIEmbeddingsProvider.ts +++ b/core/indexing/embeddings/OpenAIEmbeddingsProvider.ts @@ -4,7 +4,7 @@ import { fetchWithExponentialBackoff } from "./util"; class OpenAIEmbeddingsProvider extends BaseEmbeddingsProvider { static defaultOptions: Partial | 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({ diff --git a/core/llm/index.ts b/core/llm/index.ts index 1fcf8c2aa..e6df8e2ba 100644 --- a/core/llm/index.ts +++ b/core/llm/index.ts @@ -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; diff --git a/core/llm/llms/Anthropic.ts b/core/llm/llms/Anthropic.ts index e64d58d89..8bb642648 100644 --- a/core/llm/llms/Anthropic.ts +++ b/core/llm/llms/Anthropic.ts @@ -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 { - 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", diff --git a/core/llm/llms/DeepInfra.ts b/core/llm/llms/DeepInfra.ts index a3723afb6..1a6952cfa 100644 --- a/core/llm/llms/DeepInfra.ts +++ b/core/llm/llms/DeepInfra.ts @@ -4,7 +4,7 @@ import OpenAI from "./OpenAI"; class DeepInfra extends OpenAI { static providerName: ModelProvider = "deepinfra"; static defaultOptions: Partial = { - apiBase: "https://api.deepinfra.com/v1/openai", + apiBase: "https://api.deepinfra.com/v1/openai/", }; } diff --git a/core/llm/llms/GooglePalm.ts b/core/llm/llms/GooglePalm.ts index 3c62a49ca..780eeac19 100644 --- a/core/llm/llms/GooglePalm.ts +++ b/core/llm/llms/GooglePalm.ts @@ -14,7 +14,7 @@ class GooglePalm extends BaseLLM { static defaultOptions: Partial = { 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 { - 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", diff --git a/core/llm/llms/HuggingFaceTGI.ts b/core/llm/llms/HuggingFaceTGI.ts index a4893b8eb..f7f759e1e 100644 --- a/core/llm/llms/HuggingFaceTGI.ts +++ b/core/llm/llms/HuggingFaceTGI.ts @@ -5,13 +5,13 @@ import { streamSse } from "../stream"; class HuggingFaceTGI extends BaseLLM { static providerName: ModelProvider = "huggingface-tgi"; static defaultOptions: Partial = { - 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 { 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; diff --git a/core/llm/llms/LMStudio.ts b/core/llm/llms/LMStudio.ts index 4279abaf1..5a63701e1 100644 --- a/core/llm/llms/LMStudio.ts +++ b/core/llm/llms/LMStudio.ts @@ -4,7 +4,7 @@ import OpenAI from "./OpenAI"; class LMStudio extends OpenAI { static providerName: ModelProvider = "lmstudio"; static defaultOptions: Partial = { - apiBase: "http://localhost:1234/v1", + apiBase: "http://localhost:1234/v1/", }; } diff --git a/core/llm/llms/LlamaCpp.ts b/core/llm/llms/LlamaCpp.ts index eb947af11..9c5349619 100644 --- a/core/llm/llms/LlamaCpp.ts +++ b/core/llm/llms/LlamaCpp.ts @@ -5,7 +5,7 @@ import { streamSse } from "../stream"; class LlamaCpp extends BaseLLM { static providerName: ModelProvider = "llama.cpp"; static defaultOptions: Partial = { - 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({ diff --git a/core/llm/llms/Mistral.ts b/core/llm/llms/Mistral.ts index 385eda897..03a2b3a07 100644 --- a/core/llm/llms/Mistral.ts +++ b/core/llm/llms/Mistral.ts @@ -4,7 +4,7 @@ import OpenAI from "./OpenAI"; class Mistral extends OpenAI { static providerName: ModelProvider = "mistral"; static defaultOptions: Partial = { - apiBase: "https://api.mistral.ai/v1", + apiBase: "https://api.mistral.ai/v1/", model: "mistral-small", }; } diff --git a/core/llm/llms/Ollama.ts b/core/llm/llms/Ollama.ts index 8d88c1535..4e02a16cf 100644 --- a/core/llm/llms/Ollama.ts +++ b/core/llm/llms/Ollama.ts @@ -11,7 +11,7 @@ import { streamResponse } from "../stream"; class Ollama extends BaseLLM { static providerName: ModelProvider = "ollama"; static defaultOptions: Partial = { - apiBase: "http://localhost:11434", + apiBase: "http://localhost:11434/", model: "codellama-7b", }; diff --git a/core/llm/llms/OpenAI.ts b/core/llm/llms/OpenAI.ts index e3c50c5c5..ea9dad075 100644 --- a/core/llm/llms/OpenAI.ts +++ b/core/llm/llms/OpenAI.ts @@ -41,7 +41,7 @@ class OpenAI extends BaseLLM { static providerName: ModelProvider = "openai"; static defaultOptions: Partial = { - 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 { - const response = await this.fetch(this._getEndpoint("/models"), { + const response = await this.fetch(this._getEndpoint("models"), { method: "GET", headers: { Authorization: `Bearer ${this.apiKey}`, diff --git a/core/llm/llms/TextGenWebUI.ts b/core/llm/llms/TextGenWebUI.ts index 2482df838..ce7e65ebd 100644 --- a/core/llm/llms/TextGenWebUI.ts +++ b/core/llm/llms/TextGenWebUI.ts @@ -4,7 +4,7 @@ import OpenAI from "./OpenAI"; class TextGenWebUI extends OpenAI { static providerName: ModelProvider = "text-gen-webui"; static defaultOptions: Partial = { - apiBase: "http://localhost:5000/v1", + apiBase: "http://localhost:5000/v1/", }; } diff --git a/core/llm/llms/Together.ts b/core/llm/llms/Together.ts index 1b2aa29bd..62ce21e83 100644 --- a/core/llm/llms/Together.ts +++ b/core/llm/llms/Together.ts @@ -10,7 +10,7 @@ import OpenAI from "./OpenAI"; class Together extends OpenAI { static providerName: ModelProvider = "together"; static defaultOptions: Partial = { - apiBase: "https://api.together.xyz/v1", + apiBase: "https://api.together.xyz/v1/", }; private static MODEL_IDS: { [name: string]: string } = { diff --git a/docs/docs/walkthroughs/codellama.md b/docs/docs/walkthroughs/codellama.md index 961788cc6..9ee122234 100644 --- a/docs/docs/walkthroughs/codellama.md +++ b/docs/docs/walkthroughs/codellama.md @@ -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/" } ] } diff --git a/gui/src/util/modelData.ts b/gui/src/util/modelData.ts index ff782f86a..adab0715a 100644 --- a/gui/src/util/modelData.ts +++ b/gui/src/util/modelData.ts @@ -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, ],