Changesets · Branches · Tags · Compare
| Changeset | 2f6079ba0e75b405e0aabb09a5307f890ed7e200aa020cd49b7be9e6699cfe62 |
|---|---|
| Author | uinstinct <61635505+uinstinct@users.noreply.github.com> · 2025-09-19 12:01 +05:30 |
| Committer | uinstinct <61635505+uinstinct@users.noreply.github.com> · 2025-09-19 12:01 +05:30 |
| Parents | 1509c9fc0952 |
3 files changed
modified packages/openai-adapters/src/index.ts +3 −7
| @@ -18,6 +18,7 @@ | ||
| 18 | 18 | import { WatsonXApi } from "./apis/WatsonX.js"; |
| 19 | 19 | import { BaseLlmApi } from "./apis/base.js"; |
| 20 | 20 | import { LLMConfig, OpenAIConfigSchema } from "./types.js"; |
| 21 | +import { appendPathToUrlIfNotPresent } from "./util/appendPathToUrl.js"; | |
| 21 | 22 | |
| 22 | 23 | dotenv.config(); |
| 23 | 24 | |
| @@ -143,13 +144,8 @@ | ||
| 143 | 144 | case "ollama": |
| 144 | 145 | // for openai compaitability, we need to add /v1 to the end of the url |
| 145 | 146 | // this is required for cli (for core, endpoints are overriden by core/llm/llms/Ollama.ts) |
| 146 | - if (config.apiBase && !new RegExp(/v1\/?$/).test(config.apiBase)) { | |
| 147 | - if (config.apiBase.endsWith("/")) { | |
| 148 | - config.apiBase += "v1/"; | |
| 149 | - } else { | |
| 150 | - config.apiBase += "/v1/"; | |
| 151 | - } | |
| 152 | - } | |
| 147 | + if (config.apiBase) | |
| 148 | + config.apiBase = appendPathToUrlIfNotPresent(config.apiBase, "v1"); | |
| 153 | 149 | return openAICompatible("http://localhost:11434/v1/", config); |
| 154 | 150 | case "mock": |
| 155 | 151 | return new MockApi(); |
modified packages/openai-adapters/src/test/main.test.ts +41 −25
| @@ -310,35 +310,51 @@ | ||
| 310 | 310 | expect((azure as OpenAIApi).openai.apiKey).toBe("sk-xxx"); |
| 311 | 311 | }); |
| 312 | 312 | |
| 313 | - it('should have correct default API base for "ollama"', () => { | |
| 314 | - const ollama = constructLlmApi({ | |
| 315 | - provider: "ollama", | |
| 316 | - }); | |
| 313 | + describe("ollama api base", () => { | |
| 314 | + it('should have correct default API base for "ollama"', () => { | |
| 315 | + const ollama = constructLlmApi({ | |
| 316 | + provider: "ollama", | |
| 317 | + }); | |
| 317 | 318 | |
| 318 | - expect((ollama as OpenAIApi).openai.baseURL).toBe( | |
| 319 | - "http://localhost:11434/v1/", | |
| 320 | - ); | |
| 321 | - }); | |
| 322 | - | |
| 323 | - it('should append /v1 to apiBase for "ollama"', () => { | |
| 324 | - const ollama = constructLlmApi({ | |
| 325 | - provider: "ollama", | |
| 326 | - apiBase: "http://localhost:123", | |
| 319 | + expect((ollama as OpenAIApi).openai.baseURL).toBe( | |
| 320 | + "http://localhost:11434/v1/", | |
| 321 | + ); | |
| 327 | 322 | }); |
| 323 | + it('should append /v1 to apiBase for "ollama"', () => { | |
| 324 | + const ollama = constructLlmApi({ | |
| 325 | + provider: "ollama", | |
| 326 | + apiBase: "http://localhost:123", | |
| 327 | + }); | |
| 328 | 328 | |
| 329 | - expect((ollama as OpenAIApi).openai.baseURL).toBe( | |
| 330 | - "http://localhost:123/v1/", | |
| 331 | - ); | |
| 332 | - }); | |
| 333 | - | |
| 334 | - it("should not reappend /v1 to apibase for ollama if it is already present", () => { | |
| 335 | - const ollama = constructLlmApi({ | |
| 336 | - provider: "ollama", | |
| 337 | - apiBase: "http://localhost:123/v1/", | |
| 329 | + expect((ollama as OpenAIApi).openai.baseURL).toBe( | |
| 330 | + "http://localhost:123/v1/", | |
| 331 | + ); | |
| 338 | 332 | }); |
| 333 | + it("should not reappend /v1 to apibase for ollama if it is already present", () => { | |
| 334 | + const ollama = constructLlmApi({ | |
| 335 | + provider: "ollama", | |
| 336 | + apiBase: "http://localhost:123/v1/", | |
| 337 | + }); | |
| 339 | 338 | |
| 340 | - expect((ollama as OpenAIApi).openai.baseURL).toBe( | |
| 341 | - "http://localhost:123/v1/", | |
| 342 | - ); | |
| 339 | + expect((ollama as OpenAIApi).openai.baseURL).toBe( | |
| 340 | + "http://localhost:123/v1/", | |
| 341 | + ); | |
| 342 | + }); | |
| 343 | + it("should append v1 if apiBase is like myhostv1/", () => { | |
| 344 | + const ollama = constructLlmApi({ | |
| 345 | + provider: "ollama", | |
| 346 | + apiBase: "https://myhostv1/", | |
| 347 | + }); | |
| 348 | + expect((ollama as OpenAIApi).openai.baseURL).toBe("https://myhostv1/v1/"); | |
| 349 | + }); | |
| 350 | + it("should preserve query params and append v1 in apibase", () => { | |
| 351 | + const ollama = constructLlmApi({ | |
| 352 | + provider: "ollama", | |
| 353 | + apiBase: "https://test.com:123/ollama-server?x=1", | |
| 354 | + }); | |
| 355 | + expect((ollama as OpenAIApi).openai.baseURL).toBe( | |
| 356 | + "https://test.com:123/ollama-server/v1/?x=1", | |
| 357 | + ); | |
| 358 | + }); | |
| 343 | 359 | }); |
| 344 | 360 | }); |
added packages/openai-adapters/src/util/appendPathToUrl.ts +17 −0
| @@ -0,0 +1,17 @@ | ||
| 1 | +export function appendPathToUrlIfNotPresent( | |
| 2 | + urlString: string, | |
| 3 | + pathWithoutSlash: string, | |
| 4 | +) { | |
| 5 | + const url = new URL(urlString); | |
| 6 | + if (!url.pathname.endsWith("/")) { | |
| 7 | + url.pathname += "/"; | |
| 8 | + } | |
| 9 | + if (!url.pathname.endsWith(pathWithoutSlash + "/")) { | |
| 10 | + url.pathname += pathWithoutSlash + "/"; | |
| 11 | + } | |
| 12 | + if (url.search) { | |
| 13 | + return url.toString(); | |
| 14 | + } | |
| 15 | + // append slash at the end | |
| 16 | + return url.toString(); | |
| 17 | +} |