@ai-sdk/cohere 4.0.75 → 4.0.77
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +57 -0
- package/dist/index.js +281 -51
- package/dist/index.js.map +1 -1
- package/docs/25-cohere.mdx +100 -19
- package/package.json +3 -3
- package/src/cohere-chat-language-model.ts +83 -50
- package/src/cohere-citations.ts +77 -0
- package/src/cohere-embedding-model.ts +16 -4
- package/src/cohere-model-catalog.ts +344 -0
- package/src/convert-cohere-usage.ts +11 -7
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ModelCapabilities,
|
|
3
|
+
ModelCapability,
|
|
4
|
+
ModelEntry,
|
|
5
|
+
ProviderOptionDefinition,
|
|
6
|
+
ProviderOptionDefinitions,
|
|
7
|
+
} from '@ai-sdk/provider';
|
|
8
|
+
import type { CohereLanguageModelChatOptions } from './cohere-chat-language-model-options';
|
|
9
|
+
import type { CohereEmbeddingModelOptions } from './cohere-embedding-model-options';
|
|
10
|
+
import type { CohereRerankingModelOptions } from './reranking/cohere-reranking-model-options';
|
|
11
|
+
|
|
12
|
+
export const cohereProviderOptions = {
|
|
13
|
+
language: {
|
|
14
|
+
thinking: {
|
|
15
|
+
description: 'Enable or disable reasoning and set its token budget.',
|
|
16
|
+
},
|
|
17
|
+
} satisfies Record<
|
|
18
|
+
keyof CohereLanguageModelChatOptions,
|
|
19
|
+
ProviderOptionDefinition
|
|
20
|
+
>,
|
|
21
|
+
embedding: {
|
|
22
|
+
embeddingType: { description: 'Embedding format returned by the API.' },
|
|
23
|
+
inputType: { description: 'Intended use of the input embeddings.' },
|
|
24
|
+
truncate: {
|
|
25
|
+
description: 'How to truncate inputs exceeding the token limit.',
|
|
26
|
+
},
|
|
27
|
+
outputDimension: { description: 'Output embedding dimensions.' },
|
|
28
|
+
} satisfies Record<
|
|
29
|
+
keyof CohereEmbeddingModelOptions,
|
|
30
|
+
ProviderOptionDefinition
|
|
31
|
+
>,
|
|
32
|
+
reranking: {
|
|
33
|
+
maxTokensPerDoc: { description: 'Token limit for each document.' },
|
|
34
|
+
priority: { description: 'Request processing priority.' },
|
|
35
|
+
} satisfies Record<
|
|
36
|
+
keyof CohereRerankingModelOptions,
|
|
37
|
+
ProviderOptionDefinition
|
|
38
|
+
>,
|
|
39
|
+
} as const satisfies ProviderOptionDefinitions;
|
|
40
|
+
|
|
41
|
+
type LanguageCapabilities = NonNullable<ModelCapabilities['language']> & {
|
|
42
|
+
reasoning: NonNullable<ModelCapabilities['language']>['reasoning'] & {
|
|
43
|
+
maxBudget: number;
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
type LanguageRequestConfig = {
|
|
48
|
+
thinkingType: 'enabled';
|
|
49
|
+
reasoningMaxOutputTokens: number;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
type EmbeddingType = NonNullable<CohereEmbeddingModelOptions['embeddingType']>;
|
|
53
|
+
|
|
54
|
+
type EmbeddingCapabilities = NonNullable<ModelCapabilities['embedding']> & {
|
|
55
|
+
dimensions: readonly number[];
|
|
56
|
+
embeddingTypes: readonly EmbeddingType[];
|
|
57
|
+
maxEmbeddingsPerCall: number;
|
|
58
|
+
supportsParallelCalls: boolean;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
type EmbeddingRequestConfig = {
|
|
62
|
+
embeddingType: EmbeddingType;
|
|
63
|
+
inputType: NonNullable<CohereEmbeddingModelOptions['inputType']>;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export type CohereLanguageModelEntry = {
|
|
67
|
+
capabilities: { language: LanguageCapabilities };
|
|
68
|
+
providerOptions: {
|
|
69
|
+
language: Record<keyof CohereLanguageModelChatOptions, ModelCapability>;
|
|
70
|
+
};
|
|
71
|
+
requestConfig: { language: LanguageRequestConfig };
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
type CohereEmbeddingModelEntry = {
|
|
75
|
+
// Unknown model IDs do not invent a default dimension.
|
|
76
|
+
capabilities: { embedding?: EmbeddingCapabilities };
|
|
77
|
+
providerOptions: {
|
|
78
|
+
embedding: Record<keyof CohereEmbeddingModelOptions, ModelCapability>;
|
|
79
|
+
};
|
|
80
|
+
requestConfig: { embedding: EmbeddingRequestConfig };
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
type CohereRerankingModelEntry = {
|
|
84
|
+
capabilities: { reranking: NonNullable<ModelCapabilities['reranking']> };
|
|
85
|
+
providerOptions: {
|
|
86
|
+
reranking: Record<keyof CohereRerankingModelOptions, ModelCapability>;
|
|
87
|
+
};
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
type CohereModelEntry = ModelEntry<{
|
|
91
|
+
language?: LanguageRequestConfig;
|
|
92
|
+
embedding?: EmbeddingRequestConfig;
|
|
93
|
+
}> & {
|
|
94
|
+
capabilities: ModelCapabilities & {
|
|
95
|
+
language?: LanguageCapabilities;
|
|
96
|
+
embedding?: EmbeddingCapabilities;
|
|
97
|
+
};
|
|
98
|
+
providerOptions?: {
|
|
99
|
+
[Section in keyof typeof cohereProviderOptions]?: Record<
|
|
100
|
+
keyof (typeof cohereProviderOptions)[Section],
|
|
101
|
+
ModelCapability
|
|
102
|
+
>;
|
|
103
|
+
};
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
type LanguageDefinition = {
|
|
107
|
+
image: boolean;
|
|
108
|
+
objectGeneration: boolean;
|
|
109
|
+
tools: boolean;
|
|
110
|
+
reasoning?: boolean;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
const standard = { image: false, objectGeneration: true, tools: true };
|
|
114
|
+
const legacy = { image: false, objectGeneration: false, tools: false };
|
|
115
|
+
|
|
116
|
+
// Preserve the provider page's model set and per-model exceptions.
|
|
117
|
+
const languageDefinitions: Record<string, LanguageDefinition> = {
|
|
118
|
+
'command-a-03-2025': standard,
|
|
119
|
+
'command-a-reasoning-08-2025': { ...standard, reasoning: true },
|
|
120
|
+
'command-a-vision-07-2025': { ...standard, image: true },
|
|
121
|
+
'command-r7b-12-2024': standard,
|
|
122
|
+
'command-r-plus-04-2024': standard,
|
|
123
|
+
'command-r-plus': standard,
|
|
124
|
+
'command-r-08-2024': standard,
|
|
125
|
+
'command-r-03-2024': standard,
|
|
126
|
+
'command-r': standard,
|
|
127
|
+
command: legacy,
|
|
128
|
+
'command-nightly': legacy,
|
|
129
|
+
'command-light': legacy,
|
|
130
|
+
'command-light-nightly': legacy,
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
function createLanguageModelEntry(
|
|
134
|
+
definition?: LanguageDefinition,
|
|
135
|
+
): CohereLanguageModelEntry {
|
|
136
|
+
const reasoningSupport = definition?.reasoning ?? 'unknown';
|
|
137
|
+
return {
|
|
138
|
+
capabilities: {
|
|
139
|
+
language: {
|
|
140
|
+
inputs: { image: definition?.image ?? 'unknown', audio: false },
|
|
141
|
+
objectGeneration: definition?.objectGeneration ?? 'unknown',
|
|
142
|
+
tools: {
|
|
143
|
+
usage: definition?.tools ?? 'unknown',
|
|
144
|
+
streaming: definition?.tools ?? 'unknown',
|
|
145
|
+
},
|
|
146
|
+
reasoning: { support: reasoningSupport, maxBudget: 32768 },
|
|
147
|
+
sampling: { temperature: 'unknown', topP: 'unknown', logprobs: false },
|
|
148
|
+
},
|
|
149
|
+
},
|
|
150
|
+
providerOptions: {
|
|
151
|
+
language: declareProviderOptions(
|
|
152
|
+
cohereProviderOptions.language,
|
|
153
|
+
definition != null ? true : 'unknown',
|
|
154
|
+
{ thinking: reasoningSupport },
|
|
155
|
+
),
|
|
156
|
+
},
|
|
157
|
+
requestConfig: {
|
|
158
|
+
language: { thinkingType: 'enabled', reasoningMaxOutputTokens: 32768 },
|
|
159
|
+
},
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// These SDK limits apply to custom model IDs as well as catalog models.
|
|
164
|
+
export const cohereEmbeddingModelLimits = {
|
|
165
|
+
maxEmbeddingsPerCall: 96,
|
|
166
|
+
supportsParallelCalls: true,
|
|
167
|
+
} as const;
|
|
168
|
+
|
|
169
|
+
const embeddingRequestConfig: EmbeddingRequestConfig = {
|
|
170
|
+
embeddingType: 'float',
|
|
171
|
+
inputType: 'search_query',
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
const embeddingTypes: readonly EmbeddingType[] = [
|
|
175
|
+
'float',
|
|
176
|
+
'int8',
|
|
177
|
+
'uint8',
|
|
178
|
+
'binary',
|
|
179
|
+
'ubinary',
|
|
180
|
+
];
|
|
181
|
+
|
|
182
|
+
function createEmbeddingModelEntry(
|
|
183
|
+
defaultDimensions: number,
|
|
184
|
+
dimensions: readonly number[],
|
|
185
|
+
formats: readonly EmbeddingType[] = embeddingTypes,
|
|
186
|
+
) {
|
|
187
|
+
const customDimensions = dimensions.length > 1;
|
|
188
|
+
return {
|
|
189
|
+
capabilities: {
|
|
190
|
+
embedding: {
|
|
191
|
+
defaultDimensions,
|
|
192
|
+
customDimensions,
|
|
193
|
+
dimensions,
|
|
194
|
+
embeddingTypes: formats,
|
|
195
|
+
...cohereEmbeddingModelLimits,
|
|
196
|
+
},
|
|
197
|
+
},
|
|
198
|
+
providerOptions: {
|
|
199
|
+
embedding: declareProviderOptions(cohereProviderOptions.embedding, true, {
|
|
200
|
+
outputDimension: customDimensions,
|
|
201
|
+
embeddingType:
|
|
202
|
+
formats.length === 1
|
|
203
|
+
? { condition: "Only embeddingType: 'float' is supported." }
|
|
204
|
+
: true,
|
|
205
|
+
}),
|
|
206
|
+
},
|
|
207
|
+
requestConfig: { embedding: embeddingRequestConfig },
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function createRerankingModelEntry(
|
|
212
|
+
declared: boolean,
|
|
213
|
+
): CohereRerankingModelEntry {
|
|
214
|
+
const support = declared ? true : 'unknown';
|
|
215
|
+
return {
|
|
216
|
+
capabilities: {
|
|
217
|
+
reranking: {
|
|
218
|
+
documents: {
|
|
219
|
+
text: support,
|
|
220
|
+
object: declared
|
|
221
|
+
? {
|
|
222
|
+
condition:
|
|
223
|
+
'Objects are converted to JSON strings with a compatibility warning.',
|
|
224
|
+
}
|
|
225
|
+
: 'unknown',
|
|
226
|
+
},
|
|
227
|
+
topN: support,
|
|
228
|
+
},
|
|
229
|
+
},
|
|
230
|
+
providerOptions: {
|
|
231
|
+
reranking: declareProviderOptions(
|
|
232
|
+
cohereProviderOptions.reranking,
|
|
233
|
+
support,
|
|
234
|
+
{},
|
|
235
|
+
),
|
|
236
|
+
},
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
export const cohereModelCatalog: Readonly<Record<string, CohereModelEntry>> = {
|
|
241
|
+
...Object.fromEntries(
|
|
242
|
+
Object.entries(languageDefinitions).map(([modelId, definition]) => [
|
|
243
|
+
modelId,
|
|
244
|
+
createLanguageModelEntry(definition),
|
|
245
|
+
]),
|
|
246
|
+
),
|
|
247
|
+
'embed-v5.0-pro': createEmbeddingModelEntry(
|
|
248
|
+
2048,
|
|
249
|
+
[256, 512, 768, 1024, 1536, 2048],
|
|
250
|
+
),
|
|
251
|
+
'embed-v5.0-fast': createEmbeddingModelEntry(
|
|
252
|
+
2048,
|
|
253
|
+
[256, 512, 768, 1024, 1536, 2048],
|
|
254
|
+
),
|
|
255
|
+
'embed-v4.0': createEmbeddingModelEntry(1536, [256, 512, 1024, 1536]),
|
|
256
|
+
'embed-english-v3.0': createEmbeddingModelEntry(1024, [1024]),
|
|
257
|
+
'embed-multilingual-v3.0': createEmbeddingModelEntry(1024, [1024]),
|
|
258
|
+
'embed-english-light-v3.0': createEmbeddingModelEntry(384, [384]),
|
|
259
|
+
'embed-multilingual-light-v3.0': createEmbeddingModelEntry(384, [384]),
|
|
260
|
+
'embed-english-v2.0': createEmbeddingModelEntry(4096, [4096], ['float']),
|
|
261
|
+
'embed-english-light-v2.0': createEmbeddingModelEntry(
|
|
262
|
+
1024,
|
|
263
|
+
[1024],
|
|
264
|
+
['float'],
|
|
265
|
+
),
|
|
266
|
+
'embed-multilingual-v2.0': createEmbeddingModelEntry(768, [768], ['float']),
|
|
267
|
+
'rerank-v4.0-pro': createRerankingModelEntry(true),
|
|
268
|
+
'rerank-v4.0-fast': createRerankingModelEntry(true),
|
|
269
|
+
'rerank-v3.5': createRerankingModelEntry(true),
|
|
270
|
+
'rerank-english-v3.0': createRerankingModelEntry(true),
|
|
271
|
+
'rerank-multilingual-v3.0': createRerankingModelEntry(true),
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
function findModelEntry(modelId: string) {
|
|
275
|
+
return Object.prototype.hasOwnProperty.call(cohereModelCatalog, modelId)
|
|
276
|
+
? cohereModelCatalog[modelId]
|
|
277
|
+
: undefined;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
export function getCohereLanguageModelEntry(
|
|
281
|
+
modelId: string,
|
|
282
|
+
): CohereLanguageModelEntry {
|
|
283
|
+
const entry = findModelEntry(modelId);
|
|
284
|
+
return entry?.capabilities.language != null &&
|
|
285
|
+
entry.providerOptions?.language != null &&
|
|
286
|
+
entry.requestConfig?.language != null
|
|
287
|
+
? {
|
|
288
|
+
capabilities: { language: entry.capabilities.language },
|
|
289
|
+
providerOptions: { language: entry.providerOptions.language },
|
|
290
|
+
requestConfig: { language: entry.requestConfig.language },
|
|
291
|
+
}
|
|
292
|
+
: createLanguageModelEntry();
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
export function getCohereEmbeddingModelEntry(
|
|
296
|
+
modelId: string,
|
|
297
|
+
): CohereEmbeddingModelEntry {
|
|
298
|
+
const entry = findModelEntry(modelId);
|
|
299
|
+
return entry?.capabilities.embedding != null &&
|
|
300
|
+
entry.providerOptions?.embedding != null &&
|
|
301
|
+
entry.requestConfig?.embedding != null
|
|
302
|
+
? {
|
|
303
|
+
capabilities: { embedding: entry.capabilities.embedding },
|
|
304
|
+
providerOptions: { embedding: entry.providerOptions.embedding },
|
|
305
|
+
requestConfig: { embedding: entry.requestConfig.embedding },
|
|
306
|
+
}
|
|
307
|
+
: {
|
|
308
|
+
capabilities: {},
|
|
309
|
+
providerOptions: {
|
|
310
|
+
embedding: declareProviderOptions(
|
|
311
|
+
cohereProviderOptions.embedding,
|
|
312
|
+
'unknown',
|
|
313
|
+
{},
|
|
314
|
+
),
|
|
315
|
+
},
|
|
316
|
+
requestConfig: { embedding: embeddingRequestConfig },
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
export function getCohereRerankingModelEntry(
|
|
321
|
+
modelId: string,
|
|
322
|
+
): CohereRerankingModelEntry {
|
|
323
|
+
const entry = findModelEntry(modelId);
|
|
324
|
+
return entry?.capabilities.reranking != null &&
|
|
325
|
+
entry.providerOptions?.reranking != null
|
|
326
|
+
? {
|
|
327
|
+
capabilities: { reranking: entry.capabilities.reranking },
|
|
328
|
+
providerOptions: { reranking: entry.providerOptions.reranking },
|
|
329
|
+
}
|
|
330
|
+
: createRerankingModelEntry(false);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function declareProviderOptions<Option extends string>(
|
|
334
|
+
definitions: Readonly<Record<Option, ProviderOptionDefinition>>,
|
|
335
|
+
support: ModelCapability,
|
|
336
|
+
exceptions: Partial<Record<Option, ModelCapability>>,
|
|
337
|
+
): Record<Option, ModelCapability> {
|
|
338
|
+
return Object.fromEntries(
|
|
339
|
+
(Object.keys(definitions) as Option[]).map(option => [
|
|
340
|
+
option,
|
|
341
|
+
exceptions[option] ?? support,
|
|
342
|
+
]),
|
|
343
|
+
) as Record<Option, ModelCapability>;
|
|
344
|
+
}
|
|
@@ -2,13 +2,13 @@ import type { LanguageModelV4Usage } from '@ai-sdk/provider';
|
|
|
2
2
|
import { createNullLanguageModelUsage } from '@ai-sdk/provider-utils';
|
|
3
3
|
|
|
4
4
|
export type CohereUsageTokens = {
|
|
5
|
-
input_tokens
|
|
6
|
-
output_tokens
|
|
5
|
+
input_tokens?: number | null;
|
|
6
|
+
output_tokens?: number | null;
|
|
7
7
|
};
|
|
8
8
|
|
|
9
9
|
export type CohereUsage = {
|
|
10
10
|
billed_units?: CohereUsageTokens | null;
|
|
11
|
-
tokens
|
|
11
|
+
tokens?: CohereUsageTokens | null;
|
|
12
12
|
cached_tokens?: number | null;
|
|
13
13
|
};
|
|
14
14
|
|
|
@@ -19,16 +19,20 @@ export function convertCohereUsage(
|
|
|
19
19
|
return createNullLanguageModelUsage();
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
// `tokens` is the only measure that matches the unified token counts.
|
|
23
|
+
// `billed_units` count billed units rather than tokens, so they are kept in
|
|
24
|
+
// `raw` only and never used as token counts or reduced by cached tokens.
|
|
25
|
+
const inputTokens = usage.tokens?.input_tokens ?? undefined;
|
|
26
|
+
const outputTokens = usage.tokens?.output_tokens ?? undefined;
|
|
25
27
|
const cacheReadTokens = usage.cached_tokens ?? undefined;
|
|
26
28
|
|
|
27
29
|
return {
|
|
28
30
|
inputTokens: {
|
|
29
31
|
total: inputTokens,
|
|
30
32
|
noCache:
|
|
31
|
-
|
|
33
|
+
inputTokens != null && cacheReadTokens != null
|
|
34
|
+
? inputTokens - cacheReadTokens
|
|
35
|
+
: inputTokens,
|
|
32
36
|
cacheRead: cacheReadTokens,
|
|
33
37
|
cacheWrite: undefined,
|
|
34
38
|
},
|