From c84f2a1af9b5e683b68ea20351db6f1ab8a5a2b0 Mon Sep 17 00:00:00 2001 From: uinstinct <61635505+uinstinct@users.noreply.github.com> Date: Fri, 19 Sep 2025 12:01:29 +0530 Subject: [PATCH] append path to url if not present --- packages/openai-adapters/src/index.ts | 10 +-- .../openai-adapters/src/test/main.test.ts | 66 ++++++++++++------- .../src/util/appendPathToUrl.ts | 17 +++++ 3 files changed, 61 insertions(+), 32 deletions(-) create mode 100644 packages/openai-adapters/src/util/appendPathToUrl.ts diff --git a/packages/openai-adapters/src/index.ts b/packages/openai-adapters/src/index.ts index ac2ccc048..a8b85a4aa 100644 --- a/packages/openai-adapters/src/index.ts +++ b/packages/openai-adapters/src/index.ts @@ -18,6 +18,7 @@ import { VertexAIApi } from "./apis/VertexAI.js"; import { WatsonXApi } from "./apis/WatsonX.js"; import { BaseLlmApi } from "./apis/base.js"; import { LLMConfig, OpenAIConfigSchema } from "./types.js"; +import { appendPathToUrlIfNotPresent } from "./util/appendPathToUrl.js"; dotenv.config(); @@ -143,13 +144,8 @@ export function constructLlmApi(config: LLMConfig): BaseLlmApi | undefined { case "ollama": // for openai compaitability, we need to add /v1 to the end of the url // this is required for cli (for core, endpoints are overriden by core/llm/llms/Ollama.ts) - if (config.apiBase && !new RegExp(/v1\/?$/).test(config.apiBase)) { - if (config.apiBase.endsWith("/")) { - config.apiBase += "v1/"; - } else { - config.apiBase += "/v1/"; - } - } + if (config.apiBase) + config.apiBase = appendPathToUrlIfNotPresent(config.apiBase, "v1"); return openAICompatible("http://localhost:11434/v1/", config); case "mock": return new MockApi(); diff --git a/packages/openai-adapters/src/test/main.test.ts b/packages/openai-adapters/src/test/main.test.ts index 4e93100c2..6acf04f9e 100644 --- a/packages/openai-adapters/src/test/main.test.ts +++ b/packages/openai-adapters/src/test/main.test.ts @@ -310,35 +310,51 @@ describe("Configuration", () => { expect((azure as OpenAIApi).openai.apiKey).toBe("sk-xxx"); }); - it('should have correct default API base for "ollama"', () => { - const ollama = constructLlmApi({ - provider: "ollama", + describe("ollama api base", () => { + it('should have correct default API base for "ollama"', () => { + const ollama = constructLlmApi({ + provider: "ollama", + }); + + expect((ollama as OpenAIApi).openai.baseURL).toBe( + "http://localhost:11434/v1/", + ); }); + it('should append /v1 to apiBase for "ollama"', () => { + const ollama = constructLlmApi({ + provider: "ollama", + apiBase: "http://localhost:123", + }); - expect((ollama as OpenAIApi).openai.baseURL).toBe( - "http://localhost:11434/v1/", - ); - }); - - it('should append /v1 to apiBase for "ollama"', () => { - const ollama = constructLlmApi({ - provider: "ollama", - apiBase: "http://localhost:123", + expect((ollama as OpenAIApi).openai.baseURL).toBe( + "http://localhost:123/v1/", + ); }); + it("should not reappend /v1 to apibase for ollama if it is already present", () => { + const ollama = constructLlmApi({ + provider: "ollama", + apiBase: "http://localhost:123/v1/", + }); - expect((ollama as OpenAIApi).openai.baseURL).toBe( - "http://localhost:123/v1/", - ); - }); - - it("should not reappend /v1 to apibase for ollama if it is already present", () => { - const ollama = constructLlmApi({ - provider: "ollama", - apiBase: "http://localhost:123/v1/", + expect((ollama as OpenAIApi).openai.baseURL).toBe( + "http://localhost:123/v1/", + ); + }); + it("should append v1 if apiBase is like myhostv1/", () => { + const ollama = constructLlmApi({ + provider: "ollama", + apiBase: "https://myhostv1/", + }); + expect((ollama as OpenAIApi).openai.baseURL).toBe("https://myhostv1/v1/"); + }); + it("should preserve query params and append v1 in apibase", () => { + const ollama = constructLlmApi({ + provider: "ollama", + apiBase: "https://test.com:123/ollama-server?x=1", + }); + expect((ollama as OpenAIApi).openai.baseURL).toBe( + "https://test.com:123/ollama-server/v1/?x=1", + ); }); - - expect((ollama as OpenAIApi).openai.baseURL).toBe( - "http://localhost:123/v1/", - ); }); }); diff --git a/packages/openai-adapters/src/util/appendPathToUrl.ts b/packages/openai-adapters/src/util/appendPathToUrl.ts new file mode 100644 index 000000000..7c687e407 --- /dev/null +++ b/packages/openai-adapters/src/util/appendPathToUrl.ts @@ -0,0 +1,17 @@ +export function appendPathToUrlIfNotPresent( + urlString: string, + pathWithoutSlash: string, +) { + const url = new URL(urlString); + if (!url.pathname.endsWith("/")) { + url.pathname += "/"; + } + if (!url.pathname.endsWith(pathWithoutSlash + "/")) { + url.pathname += pathWithoutSlash + "/"; + } + if (url.search) { + return url.toString(); + } + // append slash at the end + return url.toString(); +}