From f0cae280e481c493a8098039368e8c405d427675 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Mon, 10 Mar 2025 15:50:56 -0700 Subject: [PATCH 1/2] this.fetch additions --- core/llm/llms/HuggingFaceTEI.ts | 30 +++++++++------------ core/llm/llms/Voyage.ts | 2 +- core/llm/llms/stubs/ContinueProxy.ts | 2 +- packages/openai-adapters/src/apis/OpenAI.ts | 8 +++--- 4 files changed, 19 insertions(+), 23 deletions(-) diff --git a/core/llm/llms/HuggingFaceTEI.ts b/core/llm/llms/HuggingFaceTEI.ts index 206bbd5e2..52580c187 100644 --- a/core/llm/llms/HuggingFaceTEI.ts +++ b/core/llm/llms/HuggingFaceTEI.ts @@ -1,5 +1,4 @@ import { Chunk, LLMOptions } from "../../index.js"; -import { withExponentialBackoff } from "../../util/withExponentialBackoff.js"; import { BaseLLM } from "../index.js"; class HuggingFaceTEIEmbeddingsProvider extends BaseLLM { @@ -36,17 +35,15 @@ class HuggingFaceTEIEmbeddingsProvider extends BaseLLM { } async doEmbedRequest(batch: string[]): Promise { - const resp = await withExponentialBackoff(() => - this.fetch(new URL("embed", this.apiBase), { - method: "POST", - body: JSON.stringify({ - inputs: batch, - }), - headers: { - "Content-Type": "application/json", - }, + const resp = await this.fetch(new URL("embed", this.apiBase), { + method: "POST", + body: JSON.stringify({ + inputs: batch, }), - ); + headers: { + "Content-Type": "application/json", + }, + }); if (!resp.ok) { const text = await resp.text(); const embedError = JSON.parse(text) as TEIEmbedErrorResponse; @@ -59,11 +56,10 @@ class HuggingFaceTEIEmbeddingsProvider extends BaseLLM { } async doInfoRequest(): Promise { - const resp = await withExponentialBackoff(() => - this.fetch(new URL("info", this.apiBase), { - method: "GET", - }), - ); + // TODO - need to use custom fetch for this request? + const resp = await this.fetch(new URL("info", this.apiBase), { + method: "GET", + }); if (!resp.ok) { throw new Error(await resp.text()); } @@ -79,7 +75,7 @@ class HuggingFaceTEIEmbeddingsProvider extends BaseLLM { headers["Authorization"] = `Bearer ${this.apiKey}`; } - const resp = await fetch(new URL("rerank", this.apiBase), { + const resp = await this.fetch(new URL("rerank", this.apiBase), { method: "POST", headers, body: JSON.stringify({ diff --git a/core/llm/llms/Voyage.ts b/core/llm/llms/Voyage.ts index 27db04e17..fe08d8b1d 100644 --- a/core/llm/llms/Voyage.ts +++ b/core/llm/llms/Voyage.ts @@ -14,7 +14,7 @@ class Voyage extends OpenAI { return []; } const url = new URL("rerank", this.apiBase); - const resp = await fetch(url, { + const resp = await this.fetch(url, { method: "POST", headers: { "Content-Type": "application/json", diff --git a/core/llm/llms/stubs/ContinueProxy.ts b/core/llm/llms/stubs/ContinueProxy.ts index 00ec93dbc..94221ec1c 100644 --- a/core/llm/llms/stubs/ContinueProxy.ts +++ b/core/llm/llms/stubs/ContinueProxy.ts @@ -65,7 +65,7 @@ class ContinueProxy extends OpenAI { async rerank(query: string, chunks: Chunk[]): Promise { const url = new URL("rerank", this.apiBase); - const resp = await fetch(url, { + const resp = await this.fetch(url, { method: "POST", headers: { "Content-Type": "application/json", diff --git a/packages/openai-adapters/src/apis/OpenAI.ts b/packages/openai-adapters/src/apis/OpenAI.ts index aa059f7db..358f52dca 100644 --- a/packages/openai-adapters/src/apis/OpenAI.ts +++ b/packages/openai-adapters/src/apis/OpenAI.ts @@ -14,7 +14,7 @@ import { } from "openai/resources/index"; import { z } from "zod"; import { OpenAIConfigSchema } from "../types.js"; -import { maybeCustomFetch } from "../util.js"; +import { customFetch, maybeCustomFetch } from "../util.js"; import { BaseLlmApi, CreateRerankResponse, @@ -31,7 +31,7 @@ export class OpenAIApi implements BaseLlmApi { this.openai = new OpenAI({ apiKey: config.apiKey, baseURL: this.apiBase, - fetch: maybeCustomFetch(config.requestOptions), + fetch: customFetch(config.requestOptions), }); } @@ -100,7 +100,7 @@ export class OpenAIApi implements BaseLlmApi { signal: AbortSignal, ): AsyncGenerator { const endpoint = new URL("fim/completions", this.apiBase); - const resp = await fetch(endpoint, { + const resp = await customFetch(this.config.requestOptions)(endpoint, { method: "POST", body: JSON.stringify({ model: body.model, @@ -139,7 +139,7 @@ export class OpenAIApi implements BaseLlmApi { async rerank(body: RerankCreateParams): Promise { const endpoint = new URL("rerank", this.apiBase); - const response = await fetch(endpoint, { + const response = await customFetch(this.config.requestOptions)(endpoint, { method: "POST", body: JSON.stringify(body), headers: { From 9d2a873abce95d4fa9b11bd567c245ee3429e093 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Mon, 10 Mar 2025 16:03:03 -0700 Subject: [PATCH 2/2] cohere --- core/llm/llms/Cohere.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/llm/llms/Cohere.ts b/core/llm/llms/Cohere.ts index 1c895c2fe..86834cb74 100644 --- a/core/llm/llms/Cohere.ts +++ b/core/llm/llms/Cohere.ts @@ -117,7 +117,7 @@ class Cohere extends BaseLLM { } async rerank(query: string, chunks: Chunk[]): Promise { - const resp = await fetch(new URL("rerank", this.apiBase), { + const resp = await this.fetch(new URL("rerank", this.apiBase), { method: "POST", headers: { Authorization: `Bearer ${this.apiKey}`,