Open History to see the last commit
1/**
2 * Jina AI — public OpenAPI spec at api.jina.ai/openapi.json.
3 * Models endpoint requires JINA_API_KEY.
4 */
5import type { Activity } from '#/db/schema.ts'
6import { perTokenListingCard } from '../catalog-prices.ts'
7import { jinaChatModel, parseJinaChatRequest } from '../jina-chat.ts'
8import { listOpenAiCompatibleModels } from '../openai-compat.ts'
9import { compatGenerationEndpointId, jinaModelActivity } from '../model-meta.ts'
10import { fetchOpenApi } from '../types.ts'
11import type {
12 ProviderConfig,
13 ProviderSecrets,
14 SpecFetchResult,
15} from '../types.ts'
16
17const JINA_OPENAPI_URL = 'https://api.jina.ai/openapi.json'
18const JINA_MODELS_URL = 'https://api.jina.ai/v1/models'
19
20/**
21 * Embeddings and chat completions. Rerank, classifier, train, and the
22 * rest of the Search Foundation API classify to null.
23 */
24function classify(path: string): Activity | null {
25 const bare = (path.split('?')[0] ?? path).replace(/\/+$/, '')
26 if (bare.endsWith('/embeddings')) return 'embeddings'
27 if (bare.endsWith('/chat/completions')) return 'chat'
28 return null
29}
30
31async function fetchSpec(_env: ProviderSecrets): Promise<SpecFetchResult> {
32 const { spec, hash } = await fetchOpenApi(JINA_OPENAPI_URL)
33 return {
34 specs: [spec],
35 sources: [{ url: JINA_OPENAPI_URL, hash }],
36 outputStrategy: 'post-200',
37 }
38}
39
40export const provider: ProviderConfig = {
41 id: 'jina',
42 displayName: 'Jina AI',
43 authEnvVar: 'JINA_API_KEY',
44 specSourceUrl: JINA_OPENAPI_URL,
45 modelsEndpoint: JINA_MODELS_URL,
46 defaultDerivation: 'upstream-spec',
47 fetchSpec,
48 listModels: async (env) => {
49 if (!env.JINA_API_KEY) {
50 return listOpenAiCompatibleModels({
51 providerId: 'jina',
52 url: JINA_MODELS_URL,
53 env,
54 envVar: 'JINA_API_KEY',
55 })
56 }
57 // Chat ids and the request map come from the same document fetchSpec
58 // stores. A spec that names no chat model throws, so a poll does not
59 // unclassify every text row.
60 const { spec } = await fetchOpenApi(JINA_OPENAPI_URL)
61 const chat = parseJinaChatRequest(spec)
62 return listOpenAiCompatibleModels({
63 providerId: 'jina',
64 url: JINA_MODELS_URL,
65 env,
66 envVar: 'JINA_API_KEY',
67 activity: (row) => jinaModelActivity(row, chat.modelIds),
68 extend: async (row) => {
69 const pricing = await perTokenListingCard(row.pricing, JINA_MODELS_URL)
70 const requestMap = jinaChatModel(row.id, chat.modelIds)
71 ? chat.requestMap
72 : null
73 return {
74 ...(pricing ? { pricing } : {}),
75 ...(requestMap ? { requestMap } : {}),
76 }
77 },
78 })
79 },
80 classify,
81 generationEndpointId: ({ activity }) =>
82 compatGenerationEndpointId(activity, 'v1/'),
83}