fix(ollama): send chat requests to the /api path

The Ollama SDK appends /chat to the base URL, and Ollama serves chat at
/api/chat. The model list already added the missing /api, so with
"http://localhost:11434" (the address our docs showed) models were listed
but every chat request went to /chat and got a 404. An OpenAI-style
".../v1" address went to /v1/chat.

ollamaApiUrl() turns the server address, ".../v1" and ".../api" into
".../api". Chat, the model list and the settings dialog's request URL
hint all use it. The docs now show http://localhost:11434/api, which also
works on released versions.
This commit is contained in:
dayuan.jiang
2026-10-06 09:31:15 +09:00
parent 38b72e89b6
commit 98e91d33dc
9 changed files with 68 additions and 12 deletions
+20 -3
View File
@@ -434,7 +434,7 @@ describe("Ollama API key security", () => {
expect(createOllamaMock).toHaveBeenCalledWith(
expect.objectContaining({
baseURL: "https://my-ollama.com",
baseURL: "https://my-ollama.com/api",
headers: { Authorization: "Bearer client-key" },
}),
)
@@ -448,7 +448,7 @@ describe("Ollama API key security", () => {
expect(createOllamaMock).toHaveBeenCalledWith(
expect.objectContaining({
baseURL: "https://cloud.ollama.com",
baseURL: "https://cloud.ollama.com/api",
headers: { Authorization: "Bearer server-key" },
}),
)
@@ -483,7 +483,24 @@ describe("Ollama API key security", () => {
expect(createOllamaMock).toHaveBeenCalledTimes(1)
const callArgs = createOllamaMock.mock.calls[0][0]
expect(callArgs.baseURL).toBe("https://my-ollama.com")
expect(callArgs.baseURL).toBe("https://my-ollama.com/api")
expect(callArgs).not.toHaveProperty("headers")
})
it("sends chat to Ollama's /api for a server address or a /v1 URL", () => {
delete process.env.OLLAMA_API_KEY
for (const baseUrl of [
"http://localhost:11434",
"http://localhost:11434/",
"http://localhost:11434/v1",
"http://localhost:11434/api",
]) {
createOllamaMock.mockClear()
getAIModel({ provider: "ollama", baseUrl, modelId: "llama3.2" })
expect(createOllamaMock.mock.calls[0][0].baseURL).toBe(
"http://localhost:11434/api",
)
}
})
})
+26 -1
View File
@@ -1,5 +1,9 @@
import { describe, expect, it } from "vitest"
import { chatRequestUrl, normalizeBaseUrl } from "@/lib/types/model-config"
import {
chatRequestUrl,
normalizeBaseUrl,
ollamaApiUrl,
} from "@/lib/types/model-config"
describe("normalizeBaseUrl", () => {
it("drops spaces, trailing slashes and a pasted endpoint path", () => {
@@ -32,6 +36,9 @@ describe("chatRequestUrl", () => {
expect(
chatRequestUrl("anthropic", "https://proxy.example.com/v1"),
).toBe("https://proxy.example.com/v1/messages")
expect(chatRequestUrl("ollama", "http://localhost:11434")).toBe(
"http://localhost:11434/api/chat",
)
})
it("stays out of the way for SDKs that build their own paths", () => {
@@ -40,3 +47,21 @@ describe("chatRequestUrl", () => {
expect(chatRequestUrl("glm", " ")).toBeNull()
})
})
describe("ollamaApiUrl", () => {
it("points at Ollama's /api whatever form the address takes", () => {
for (const url of [
"http://localhost:11434",
"http://localhost:11434/",
"http://localhost:11434/api",
"http://localhost:11434/api/",
"http://localhost:11434/v1",
"http://localhost:11434/v1/chat/completions",
]) {
expect(ollamaApiUrl(url)).toBe("http://localhost:11434/api")
}
expect(ollamaApiUrl("https://ollama.com/api")).toBe(
"https://ollama.com/api",
)
})
})