diff --git a/core/llm/index.ts b/core/llm/index.ts index 5f7a59aa4..d04ea82f0 100644 --- a/core/llm/index.ts +++ b/core/llm/index.ts @@ -222,6 +222,9 @@ export abstract class LLM implements LLMOptions { this.llmRequestHook = options.llmRequestHook; this.apiKey = options.apiKey; this.apiBase = options.apiBase; + if (this.apiBase?.endsWith("/")) { + this.apiBase = this.apiBase.slice(0, -1); + } this.engine = options.engine; this.apiVersion = options.apiVersion; @@ -300,6 +303,20 @@ export abstract class LLM implements LLMOptions { // }); } + protected fetch(url: string, init?: RequestInit): Promise { + const headers = new Headers(init?.headers); + for (const [key, value] of Object.entries( + this.requestOptions?.headers || {} + )) { + headers.append(key, value as string); + } + + return fetch(url, { + ...init, + headers, + }); + } + private _parseCompletionOptions(options: LLMFullCompletionOptions) { const log = options.log ?? true; const raw = options.raw ?? false; diff --git a/core/llm/llms/Anthropic.ts b/core/llm/llms/Anthropic.ts index a93b50cba..6716e01d7 100644 --- a/core/llm/llms/Anthropic.ts +++ b/core/llm/llms/Anthropic.ts @@ -46,7 +46,7 @@ class Anthropic extends LLM { prompt: string, options: CompletionOptions ): Promise { - const response = await fetch(this.apiBase + "/complete", { + const response = await this.fetch(this.apiBase + "/complete", { method: "POST", headers: { "Content-Type": "application/json", @@ -67,7 +67,7 @@ class Anthropic extends LLM { prompt: string, options: CompletionOptions ): AsyncGenerator { - const response = await fetch(this.apiBase + "/complete", { + const response = await this.fetch(this.apiBase + "/complete", { method: "POST", headers: { "Content-Type": "application/json", diff --git a/core/llm/llms/FreeTrial.ts b/core/llm/llms/FreeTrial.ts index 5b32aa846..95669513e 100644 --- a/core/llm/llms/FreeTrial.ts +++ b/core/llm/llms/FreeTrial.ts @@ -21,7 +21,7 @@ class FreeTrial extends LLM { ): Promise { const args = this.collectArgs(options); - const response = await fetch(`${SERVER_URL}/complete`, { + const response = await this.fetch(`${SERVER_URL}/complete`, { method: "POST", headers: this._getHeaders(), body: JSON.stringify({ @@ -39,7 +39,7 @@ class FreeTrial extends LLM { ): AsyncGenerator { const args = this.collectArgs(options); - const response = await fetch(`${SERVER_URL}/stream_complete`, { + const response = await this.fetch(`${SERVER_URL}/stream_complete`, { method: "POST", headers: this._getHeaders(), body: JSON.stringify({ @@ -69,7 +69,7 @@ class FreeTrial extends LLM { ): AsyncGenerator { const args = this.collectArgs(options); - const response = await fetch(`${SERVER_URL}/stream_chat`, { + const response = await this.fetch(`${SERVER_URL}/stream_chat`, { method: "POST", headers: this._getHeaders(), body: JSON.stringify({ diff --git a/core/llm/llms/Gemini.ts b/core/llm/llms/Gemini.ts index c165d166a..0e8ad731c 100644 --- a/core/llm/llms/Gemini.ts +++ b/core/llm/llms/Gemini.ts @@ -43,7 +43,7 @@ class Gemini extends LLM { stopSequences: options.stop, }, }; - const response = await fetch(apiUrl, { + const response = await this.fetch(apiUrl, { method: "POST", body: JSON.stringify(body), headers: { diff --git a/core/llm/llms/GooglePalm.ts b/core/llm/llms/GooglePalm.ts index 39e4a773f..04fb0a40e 100644 --- a/core/llm/llms/GooglePalm.ts +++ b/core/llm/llms/GooglePalm.ts @@ -49,7 +49,7 @@ class GooglePalm extends LLM { }; }), }; - const response = await fetch(apiURL, { + const response = await this.fetch(apiURL, { method: "POST", body: JSON.stringify(body), }); @@ -73,7 +73,7 @@ class GooglePalm extends LLM { const apiURL = `https://generativelanguage.googleapis.com/v1beta2/models/${options.model}:generateMessage?key=${this.apiKey}`; const body = { prompt: { messages: msgList } }; - const response = await fetch(apiURL, { + const response = await this.fetch(apiURL, { method: "POST", body: JSON.stringify(body), }); diff --git a/core/llm/llms/HuggingFaceTGI.ts b/core/llm/llms/HuggingFaceTGI.ts index 0f1f852d4..49dd8034f 100644 --- a/core/llm/llms/HuggingFaceTGI.ts +++ b/core/llm/llms/HuggingFaceTGI.ts @@ -1,6 +1,6 @@ import { LLM, LLMOptions } from ".."; import { ModelProvider } from "../../config"; -import { ChatMessage, CompletionOptions } from "../types"; +import { CompletionOptions } from "../types"; class HuggingFaceTGI extends LLM { static providerName: ModelProvider = "huggingface-tgi"; @@ -29,7 +29,7 @@ class HuggingFaceTGI extends LLM { ): AsyncGenerator { const args = this._convertArgs(options, prompt); - const response = await fetch(`${this.apiBase}/generate_stream`, { + const response = await this.fetch(`${this.apiBase}/generate_stream`, { method: "POST", headers: { "Content-Type": "application/json", diff --git a/core/llm/llms/LlamaCpp.ts b/core/llm/llms/LlamaCpp.ts index 6674b3aee..f5090e6b2 100644 --- a/core/llm/llms/LlamaCpp.ts +++ b/core/llm/llms/LlamaCpp.ts @@ -27,7 +27,7 @@ class LlamaCpp extends LLM { ...this.requestOptions?.headers, }; - const resp = await fetch(`${this.apiBase}/completion`, { + const resp = await this.fetch(`${this.apiBase}/completion`, { method: "POST", headers, body: JSON.stringify({ @@ -49,7 +49,11 @@ class LlamaCpp extends LLM { buffer = lines.pop() ?? ""; for (const line of lines) { if (line.trim() === "") continue; - yield JSON.parse(line.substring(6))["content"]; + const data = JSON.parse(line.substring(6)); + if ("error" in data) { + throw new Error(data.error); + } + yield data.content; } } diff --git a/core/llm/llms/Llamafile.ts b/core/llm/llms/Llamafile.ts index 07c2029be..be5ad40c9 100644 --- a/core/llm/llms/Llamafile.ts +++ b/core/llm/llms/Llamafile.ts @@ -1,4 +1,3 @@ -import { LLM } from ".."; import { ModelProvider } from "../../config"; import LlamaCpp from "./LlamaCpp"; diff --git a/core/llm/llms/Ollama.ts b/core/llm/llms/Ollama.ts index 935bf1331..0b594f8bf 100644 --- a/core/llm/llms/Ollama.ts +++ b/core/llm/llms/Ollama.ts @@ -53,7 +53,7 @@ class Ollama extends LLM { prompt: string, options: CompletionOptions ): AsyncGenerator { - const response = await fetch(`${this.apiBase}/api/generate`, { + const response = await this.fetch(`${this.apiBase}/api/generate`, { method: "POST", headers: { "Content-Type": "application/json", diff --git a/core/llm/llms/OpenAI.ts b/core/llm/llms/OpenAI.ts index 0de5ba505..b218b7f37 100644 --- a/core/llm/llms/OpenAI.ts +++ b/core/llm/llms/OpenAI.ts @@ -65,7 +65,7 @@ class OpenAI extends LLM { messages: ChatMessage[], options: CompletionOptions ): AsyncGenerator { - const response = await fetch(this._getChatUrl(), { + const response = await this.fetch(this._getChatUrl(), { method: "POST", headers: { "Content-Type": "application/json", diff --git a/core/llm/llms/Together.ts b/core/llm/llms/Together.ts index 6bcd6ae68..04a100d80 100644 --- a/core/llm/llms/Together.ts +++ b/core/llm/llms/Together.ts @@ -42,7 +42,7 @@ class Together extends LLM { prompt: string, options: CompletionOptions ): AsyncGenerator { - const response = await fetch(`${this.apiBase}/inference`, { + const response = await this.fetch(`${this.apiBase}/inference`, { method: "POST", headers: { "Content-Type": "application/json",