Open History to see the last commit
1/**
2 * Voyage AI — official OpenAPI spec from github.com/voyage-ai/openapi
3 * (`voyage-openapi.yml`; the hinted `openapi.yaml` 404s). Embeddings only;
4 * rerank is not in our activity enum. There is no `/v1/models` list
5 * (that URL 404s); the catalog is the model names the spec documents.
6 */
7import type { Activity } from '#/db/schema.ts'
8import { voyageModelPricing } from '../voyage-pricing.ts'
9import { normalizeMediaModalities } from '../media-modalities.ts'
10import { fetchOpenApi } from '../types.ts'
11import type {
12 ListModelsResult,
13 ModelInfo,
14 OpenApiDocument,
15 ProviderConfig,
16 ProviderSecrets,
17 SpecFetchResult,
18} from '../types.ts'
19
20const VOYAGE_OPENAPI_URL =
21 'https://raw.githubusercontent.com/voyage-ai/openapi/main/voyage-openapi.yml'
22
23const DOCUMENTED_MODEL = /`((?:voyage|rerank)-[a-z0-9.-]+)`/g
24
25/**
26 * Path rules — the published spec (and docs) sit under server
27 * `https://api.voyageai.com/v1`, so classify sees `/embeddings` etc. Also
28 * accept a `/v1/` prefix. Text, multimodal, and contextualized embedding
29 * POSTs are generation; rerank, models, and batch/admin are platform.
30 */
31function classify(path: string): Activity | null {
32 const bare = path.startsWith('/v1/') ? path.slice(3) : path
33 if (
34 bare === '/embeddings' ||
35 bare === '/multimodalembeddings' ||
36 bare === '/contextualizedembeddings'
37 ) {
38 return 'embeddings'
39 }
40 return null
41}
42
43async function fetchSpec(_env: ProviderSecrets): Promise<SpecFetchResult> {
44 const { spec, hash } = await fetchOpenApi(VOYAGE_OPENAPI_URL)
45 return {
46 specs: [spec],
47 sources: [{ url: VOYAGE_OPENAPI_URL, hash }],
48 outputStrategy: 'post-200',
49 }
50}
51
52/** Model ids Voyage documents in descriptions (and any real enums). */
53export function voyageModelsFromSpec(spec: OpenApiDocument): Array<ModelInfo> {
54 const ids = new Set<string>()
55 const visit = (node: unknown): void => {
56 if (node === null || typeof node !== 'object') return
57 if (Array.isArray(node)) {
58 for (const item of node) visit(item)
59 return
60 }
61 const rec = node as Record<string, unknown>
62 if (typeof rec.description === 'string') {
63 for (const match of rec.description.matchAll(DOCUMENTED_MODEL)) {
64 const id = match[1]
65 if (id !== undefined) ids.add(id)
66 }
67 }
68 if (Array.isArray(rec.enum)) {
69 for (const value of rec.enum) {
70 if (typeof value === 'string' && /^(?:voyage|rerank)-/.test(value)) {
71 ids.add(value)
72 }
73 }
74 }
75 for (const value of Object.values(rec)) visit(value)
76 }
77 visit(spec)
78 return [...ids]
79 .filter((id) => id.startsWith('voyage-'))
80 .sort()
81 .map((rawId) => ({
82 rawId,
83 activity: 'embeddings' as const,
84 modalities: normalizeMediaModalities({
85 activity: 'embeddings',
86 listingInput: rawId.includes('multimodal')
87 ? ['text', 'image']
88 : ['text'],
89 listingOutput: [],
90 }),
91 }))
92}
93
94async function listModels(
95 _env: ProviderSecrets,
96 kv?: KVNamespace,
97): Promise<ListModelsResult> {
98 const { spec } = await fetchOpenApi(VOYAGE_OPENAPI_URL)
99 const pricing = await voyageModelPricing(kv)
100 return {
101 models: voyageModelsFromSpec(spec).map((model) => ({
102 ...model,
103 ...pricing(model.rawId),
104 })),
105 }
106}
107
108export const provider: ProviderConfig = {
109 id: 'voyage',
110 displayName: 'Voyage AI',
111 authEnvVar: 'VOYAGE_API_KEY',
112 specSourceUrl: VOYAGE_OPENAPI_URL,
113 modelsEndpoint: VOYAGE_OPENAPI_URL,
114 defaultDerivation: 'upstream-spec',
115 fetchSpec,
116 listModels,
117 classify,
118 generationEndpointId: ({ rawId }) =>
119 rawId.includes('multimodal')
120 ? 'multimodalembeddings'
121 : rawId.includes('context')
122 ? 'contextualizedembeddings'
123 : 'embeddings',
124}