mirror of
https://github.com/DayuanJiang/next-ai-draw-io.git
synced 2026-10-09 19:19:50 +08:00
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:
@@ -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",
|
||||
)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
@@ -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",
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user