Open History to see the last commit
1/**
2 * Ollama — the REST API a local (or self-hosted) Ollama server serves.
3 * The spec is Ollama's own OpenAPI document (`docs/openapi.yaml` in
4 * ollama/ollama, the source of docs.ollama.com/api). `/api/chat` and
5 * `/api/generate` stream NDJSON (`application/x-ndjson`), kept beside the
6 * JSON body.
7 *
8 * No model catalog: a server runs whatever was pulled into it, so the
9 * listing is empty and `model` stays a free string in the assembled
10 * document. The server URL is a variable, default `http://localhost:11434`.
11 * Local servers take no key; ollama.com takes a bearer key.
12 */
13import type { Activity } from '#/db/schema.ts'
14
15import { fetchOpenApi } from '../types.ts'
16import type {
17 ListModelsResult,
18 ProviderConfig,
19 ProviderConnect,
20 SpecFetchResult,
21} from '../types.ts'
22
23export const OLLAMA_OPENAPI_URL =
24 'https://raw.githubusercontent.com/ollama/ollama/main/docs/openapi.yaml'
25const OLLAMA_DOCS_URL = 'https://docs.ollama.com/api'
26
27const ROUTES: Record<string, Activity> = {
28 '/api/chat': 'chat',
29 '/api/generate': 'chat',
30 '/api/embed': 'embeddings',
31}
32
33export const OLLAMA_CONNECT: ProviderConnect = {
34 servers: [
35 {
36 url: '{baseUrl}',
37 description: 'Your Ollama server.',
38 variables: {
39 baseUrl: {
40 default: 'http://localhost:11434',
41 description:
42 'Ollama server URL: `http://localhost:11434` locally, `https://ollama.com` for Ollama cloud.',
43 },
44 },
45 },
46 ],
47 securitySchemes: {
48 bearerAuth: {
49 type: 'http',
50 scheme: 'bearer',
51 description:
52 'Ollama API key as `Authorization: Bearer <key>`; only ollama.com needs it.',
53 },
54 },
55 // `{}`: a local server takes no auth.
56 security: [{}, { bearerAuth: [] }],
57}
58
59async function fetchSpec(): Promise<SpecFetchResult> {
60 const { spec, hash } = await fetchOpenApi(OLLAMA_OPENAPI_URL)
61 return {
62 specs: [spec],
63 sources: [{ url: OLLAMA_OPENAPI_URL, hash }],
64 outputStrategy: 'post-200',
65 }
66}
67
68function listModels(): Promise<ListModelsResult> {
69 return Promise.resolve({ models: [] })
70}
71
72export const provider: ProviderConfig = {
73 id: 'ollama',
74 displayName: 'Ollama',
75 specSourceUrl: OLLAMA_DOCS_URL,
76 defaultDerivation: 'upstream-spec',
77 connect: OLLAMA_CONNECT,
78 fetchSpec,
79 listModels,
80 classify: (path) => ROUTES[path] ?? null,
81}