@ai-sdk/cohere 3.0.75 → 3.0.76
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 +39 -0
- package/dist/index.js +223 -172
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +223 -172
- package/dist/index.mjs.map +1 -1
- package/docs/25-cohere.mdx +69 -0
- package/package.json +1 -1
- package/src/cohere-chat-language-model.ts +67 -45
- package/src/cohere-citations.ts +77 -0
- package/src/convert-cohere-usage.ts +8 -6
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { JSONArray, LanguageModelV3Source } from '@ai-sdk/provider';
|
|
2
|
+
import { z } from 'zod/v4';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Citation payload Cohere returns for text content, in non-streaming responses
|
|
6
|
+
* and in `citation-start` stream events.
|
|
7
|
+
*
|
|
8
|
+
* All fields are optional in the API specification, and a source can reference
|
|
9
|
+
* either a document or tool output, so partial citations are validated instead
|
|
10
|
+
* of discarding the whole response.
|
|
11
|
+
*/
|
|
12
|
+
export const cohereCitationSchema = z.object({
|
|
13
|
+
start: z.number().nullish(),
|
|
14
|
+
end: z.number().nullish(),
|
|
15
|
+
text: z.string().nullish(),
|
|
16
|
+
sources: z
|
|
17
|
+
.array(
|
|
18
|
+
z.object({
|
|
19
|
+
type: z.string().nullish(),
|
|
20
|
+
id: z.string().nullish(),
|
|
21
|
+
// Documents are arbitrary objects; only the fields the provider maps
|
|
22
|
+
// are typed, everything else is passed through as provider metadata.
|
|
23
|
+
document: z
|
|
24
|
+
.looseObject({
|
|
25
|
+
id: z.string().nullish(),
|
|
26
|
+
text: z.string().nullish(),
|
|
27
|
+
title: z.string().nullish(),
|
|
28
|
+
})
|
|
29
|
+
.nullish(),
|
|
30
|
+
tool_output: z.looseObject({}).nullish(),
|
|
31
|
+
}),
|
|
32
|
+
)
|
|
33
|
+
.nullish(),
|
|
34
|
+
type: z.string().nullish(),
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
export type CohereCitation = z.infer<typeof cohereCitationSchema>;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Maps a Cohere citation to a document source.
|
|
41
|
+
*
|
|
42
|
+
* The first source with a document provides the title; all sources are kept in
|
|
43
|
+
* `providerMetadata`. Citations that only reference tool output have no
|
|
44
|
+
* document to point at and return `undefined` (their data stays in the response
|
|
45
|
+
* body).
|
|
46
|
+
*/
|
|
47
|
+
export function mapCohereCitationToSource(
|
|
48
|
+
citation: CohereCitation,
|
|
49
|
+
generateId: () => string,
|
|
50
|
+
): LanguageModelV3Source | undefined {
|
|
51
|
+
const document = citation.sources?.find(
|
|
52
|
+
source => source.document != null,
|
|
53
|
+
)?.document;
|
|
54
|
+
|
|
55
|
+
if (document == null) {
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
type: 'source',
|
|
61
|
+
sourceType: 'document',
|
|
62
|
+
id: generateId(),
|
|
63
|
+
mediaType: 'text/plain',
|
|
64
|
+
title: document.title || 'Document',
|
|
65
|
+
providerMetadata: {
|
|
66
|
+
cohere: {
|
|
67
|
+
...(citation.start != null && { start: citation.start }),
|
|
68
|
+
...(citation.end != null && { end: citation.end }),
|
|
69
|
+
...(citation.text != null && { text: citation.text }),
|
|
70
|
+
...(citation.sources != null && {
|
|
71
|
+
sources: citation.sources as JSONArray,
|
|
72
|
+
}),
|
|
73
|
+
...(citation.type != null && { citationType: citation.type }),
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import type { LanguageModelV3Usage } from '@ai-sdk/provider';
|
|
2
2
|
|
|
3
3
|
export type CohereUsageTokens = {
|
|
4
|
-
input_tokens
|
|
5
|
-
output_tokens
|
|
4
|
+
input_tokens?: number | null;
|
|
5
|
+
output_tokens?: number | null;
|
|
6
6
|
};
|
|
7
7
|
|
|
8
8
|
export type CohereUsage = {
|
|
9
9
|
billed_units?: CohereUsageTokens | null;
|
|
10
|
-
tokens
|
|
10
|
+
tokens?: CohereUsageTokens | null;
|
|
11
11
|
cached_tokens?: number | null;
|
|
12
12
|
};
|
|
13
13
|
|
|
@@ -31,9 +31,11 @@ export function convertCohereUsage(
|
|
|
31
31
|
};
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
// `tokens` is the only measure that matches the unified token counts.
|
|
35
|
+
// `billed_units` count billed units rather than tokens, so they are kept in
|
|
36
|
+
// `raw` only and never used as token counts.
|
|
37
|
+
const inputTokens = usage.tokens?.input_tokens ?? undefined;
|
|
38
|
+
const outputTokens = usage.tokens?.output_tokens ?? undefined;
|
|
37
39
|
|
|
38
40
|
return {
|
|
39
41
|
inputTokens: {
|