Changesets · Branches · Tags · Compare
| Changeset | 89cedaf513f2509237f984265a45c9e7bce42a297e85de8ce53fb9953ca7979e |
|---|---|
| Author | Dallin Romney <dallinromney@gmail.com> · 2025-09-22 14:40 -07:00 |
| Committer | GitHub <noreply@github.com> · 2025-09-22 14:40 -07:00 |
| Parents | 7d65ae851267, 2f6079ba0e75 |
3 files changed
modified packages/openai-adapters/src/index.ts +5 −0
| @@ -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 | |
| @@ -141,6 +142,10 @@ | ||
| 141 | 142 | case "lmstudio": |
| 142 | 143 | return openAICompatible("http://localhost:1234/", config); |
| 143 | 144 | case "ollama": |
| 145 | + // for openai compaitability, we need to add /v1 to the end of the url | |
| 146 | + // this is required for cli (for core, endpoints are overriden by core/llm/llms/Ollama.ts) | |
| 147 | + if (config.apiBase) | |
| 148 | + config.apiBase = appendPathToUrlIfNotPresent(config.apiBase, "v1"); | |
| 144 | 149 | return openAICompatible("http://localhost:11434/v1/", config); |
| 145 | 150 | case "mock": |
| 146 | 151 | return new MockApi(); |
modified packages/openai-adapters/src/test/main.test.ts +48 −0
| @@ -311,4 +311,52 @@ | ||
| 311 | 311 | ); |
| 312 | 312 | expect((azure as OpenAIApi).openai.apiKey).toBe("sk-xxx"); |
| 313 | 313 | }); |
| 314 | + | |
| 315 | + describe("ollama api base", () => { | |
| 316 | + it('should have correct default API base for "ollama"', () => { | |
| 317 | + const ollama = constructLlmApi({ | |
| 318 | + provider: "ollama", | |
| 319 | + }); | |
| 320 | + | |
| 321 | + expect((ollama as OpenAIApi).openai.baseURL).toBe( | |
| 322 | + "http://localhost:11434/v1/", | |
| 323 | + ); | |
| 324 | + }); | |
| 325 | + it('should append /v1 to apiBase for "ollama"', () => { | |
| 326 | + const ollama = constructLlmApi({ | |
| 327 | + provider: "ollama", | |
| 328 | + apiBase: "http://localhost:123", | |
| 329 | + }); | |
| 330 | + | |
| 331 | + expect((ollama as OpenAIApi).openai.baseURL).toBe( | |
| 332 | + "http://localhost:123/v1/", | |
| 333 | + ); | |
| 334 | + }); | |
| 335 | + it("should not reappend /v1 to apibase for ollama if it is already present", () => { | |
| 336 | + const ollama = constructLlmApi({ | |
| 337 | + provider: "ollama", | |
| 338 | + apiBase: "http://localhost:123/v1/", | |
| 339 | + }); | |
| 340 | + | |
| 341 | + expect((ollama as OpenAIApi).openai.baseURL).toBe( | |
| 342 | + "http://localhost:123/v1/", | |
| 343 | + ); | |
| 344 | + }); | |
| 345 | + it("should append v1 if apiBase is like myhostv1/", () => { | |
| 346 | + const ollama = constructLlmApi({ | |
| 347 | + provider: "ollama", | |
| 348 | + apiBase: "https://myhostv1/", | |
| 349 | + }); | |
| 350 | + expect((ollama as OpenAIApi).openai.baseURL).toBe("https://myhostv1/v1/"); | |
| 351 | + }); | |
| 352 | + it("should preserve query params and append v1 in apibase", () => { | |
| 353 | + const ollama = constructLlmApi({ | |
| 354 | + provider: "ollama", | |
| 355 | + apiBase: "https://test.com:123/ollama-server?x=1", | |
| 356 | + }); | |
| 357 | + expect((ollama as OpenAIApi).openai.baseURL).toBe( | |
| 358 | + "https://test.com:123/ollama-server/v1/?x=1", | |
| 359 | + ); | |
| 360 | + }); | |
| 361 | + }); | |
| 314 | 362 | }); |
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 | +} |