@ai-sdk/cohere 3.0.32 → 3.0.34
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 +13 -0
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +110 -64
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +87 -41
- package/dist/index.mjs.map +1 -1
- package/docs/25-cohere.mdx +57 -0
- package/package.json +2 -2
- package/src/cohere-chat-language-model.ts +3 -3
- package/src/cohere-chat-options.ts +14 -0
- package/src/cohere-chat-prompt.ts +8 -1
- package/src/convert-to-cohere-chat-prompt.ts +103 -52
|
@@ -1,20 +1,26 @@
|
|
|
1
1
|
import {
|
|
2
2
|
UnsupportedFunctionalityError,
|
|
3
|
-
type
|
|
3
|
+
type LanguageModelV3FilePart,
|
|
4
4
|
type LanguageModelV3Prompt,
|
|
5
|
+
type SharedV3Warning,
|
|
5
6
|
} from '@ai-sdk/provider';
|
|
7
|
+
import { convertToBase64, parseProviderOptions } from '@ai-sdk/provider-utils';
|
|
8
|
+
import { cohereImagePartProviderOptions } from './cohere-chat-options';
|
|
6
9
|
import type {
|
|
7
10
|
CohereAssistantMessage,
|
|
8
11
|
CohereChatPrompt,
|
|
12
|
+
CohereUserMessageContent,
|
|
9
13
|
} from './cohere-chat-prompt';
|
|
10
14
|
|
|
11
|
-
export function convertToCohereChatPrompt(
|
|
15
|
+
export async function convertToCohereChatPrompt(
|
|
16
|
+
prompt: LanguageModelV3Prompt,
|
|
17
|
+
): Promise<{
|
|
12
18
|
messages: CohereChatPrompt;
|
|
13
19
|
documents: Array<{
|
|
14
20
|
data: { text: string; title?: string };
|
|
15
21
|
}>;
|
|
16
22
|
warnings: SharedV3Warning[];
|
|
17
|
-
} {
|
|
23
|
+
}> {
|
|
18
24
|
const messages: CohereChatPrompt = [];
|
|
19
25
|
const documents: Array<{ data: { text: string; title?: string } }> = [];
|
|
20
26
|
const warnings: SharedV3Warning[] = [];
|
|
@@ -27,58 +33,86 @@ export function convertToCohereChatPrompt(prompt: LanguageModelV3Prompt): {
|
|
|
27
33
|
}
|
|
28
34
|
|
|
29
35
|
case 'user': {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
content: content
|
|
33
|
-
.map(part => {
|
|
34
|
-
switch (part.type) {
|
|
35
|
-
case 'text': {
|
|
36
|
-
return part.text;
|
|
37
|
-
}
|
|
38
|
-
case 'file': {
|
|
39
|
-
// Extract documents for RAG
|
|
40
|
-
let textContent: string;
|
|
41
|
-
|
|
42
|
-
if (typeof part.data === 'string') {
|
|
43
|
-
// Base64 or text data
|
|
44
|
-
textContent = part.data;
|
|
45
|
-
} else if (part.data instanceof Uint8Array) {
|
|
46
|
-
// Check if the media type is supported for text extraction
|
|
47
|
-
if (
|
|
48
|
-
!(
|
|
49
|
-
part.mediaType?.startsWith('text/') ||
|
|
50
|
-
part.mediaType === 'application/json'
|
|
51
|
-
)
|
|
52
|
-
) {
|
|
53
|
-
throw new UnsupportedFunctionalityError({
|
|
54
|
-
functionality: `document media type: ${part.mediaType}`,
|
|
55
|
-
message: `Media type '${part.mediaType}' is not supported. Supported media types are: text/* and application/json.`,
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
textContent = new TextDecoder().decode(part.data);
|
|
59
|
-
} else {
|
|
60
|
-
throw new UnsupportedFunctionalityError({
|
|
61
|
-
functionality: 'File URL data',
|
|
62
|
-
message:
|
|
63
|
-
'URLs should be downloaded by the AI SDK and not reach this point. This indicates a configuration issue.',
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
documents.push({
|
|
68
|
-
data: {
|
|
69
|
-
text: textContent,
|
|
70
|
-
title: part.filename,
|
|
71
|
-
},
|
|
72
|
-
});
|
|
36
|
+
const userContentParts: Array<CohereUserMessageContent> = [];
|
|
37
|
+
let hasImage = false;
|
|
73
38
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
39
|
+
for (const part of content) {
|
|
40
|
+
switch (part.type) {
|
|
41
|
+
case 'text': {
|
|
42
|
+
if (part.text.length > 0) {
|
|
43
|
+
userContentParts.push({ type: 'text', text: part.text });
|
|
44
|
+
}
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
case 'file': {
|
|
48
|
+
if (isImageMediaType(part.mediaType)) {
|
|
49
|
+
hasImage = true;
|
|
50
|
+
const url = buildImageUrl({ part });
|
|
51
|
+
const cohereOptions =
|
|
52
|
+
(await parseProviderOptions({
|
|
53
|
+
provider: 'cohere',
|
|
54
|
+
providerOptions: part.providerOptions,
|
|
55
|
+
schema: cohereImagePartProviderOptions,
|
|
56
|
+
})) ?? {};
|
|
57
|
+
|
|
58
|
+
userContentParts.push({
|
|
59
|
+
type: 'image_url',
|
|
60
|
+
image_url: {
|
|
61
|
+
url,
|
|
62
|
+
...(cohereOptions.detail
|
|
63
|
+
? { detail: cohereOptions.detail }
|
|
64
|
+
: {}),
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
let textContent: string;
|
|
71
|
+
|
|
72
|
+
if (typeof part.data === 'string') {
|
|
73
|
+
textContent = part.data;
|
|
74
|
+
} else if (part.data instanceof Uint8Array) {
|
|
75
|
+
if (
|
|
76
|
+
!(
|
|
77
|
+
part.mediaType?.startsWith('text/') ||
|
|
78
|
+
part.mediaType === 'application/json'
|
|
79
|
+
)
|
|
80
|
+
) {
|
|
81
|
+
throw new UnsupportedFunctionalityError({
|
|
82
|
+
functionality: `document media type: ${part.mediaType}`,
|
|
83
|
+
message: `Media type '${part.mediaType}' is not supported. Supported media types are: text/* and application/json.`,
|
|
84
|
+
});
|
|
77
85
|
}
|
|
86
|
+
textContent = new TextDecoder().decode(part.data);
|
|
87
|
+
} else {
|
|
88
|
+
throw new UnsupportedFunctionalityError({
|
|
89
|
+
functionality: 'File URL data',
|
|
90
|
+
message:
|
|
91
|
+
'URLs should be downloaded by the AI SDK and not reach this point. This indicates a configuration issue.',
|
|
92
|
+
});
|
|
78
93
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
94
|
+
|
|
95
|
+
documents.push({
|
|
96
|
+
data: {
|
|
97
|
+
text: textContent,
|
|
98
|
+
title: part.filename,
|
|
99
|
+
},
|
|
100
|
+
});
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (hasImage) {
|
|
107
|
+
messages.push({ role: 'user', content: userContentParts });
|
|
108
|
+
} else {
|
|
109
|
+
messages.push({
|
|
110
|
+
role: 'user',
|
|
111
|
+
content: userContentParts
|
|
112
|
+
.map(p => (p.type === 'text' ? p.text : ''))
|
|
113
|
+
.join(''),
|
|
114
|
+
});
|
|
115
|
+
}
|
|
82
116
|
break;
|
|
83
117
|
}
|
|
84
118
|
|
|
@@ -157,3 +191,20 @@ export function convertToCohereChatPrompt(prompt: LanguageModelV3Prompt): {
|
|
|
157
191
|
|
|
158
192
|
return { messages, documents, warnings };
|
|
159
193
|
}
|
|
194
|
+
|
|
195
|
+
function isImageMediaType(mediaType: string | undefined): boolean {
|
|
196
|
+
return mediaType === 'image' || mediaType?.startsWith('image/') === true;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function buildImageUrl({ part }: { part: LanguageModelV3FilePart }): string {
|
|
200
|
+
if (part.data instanceof URL) {
|
|
201
|
+
return part.data.toString();
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const mediaType =
|
|
205
|
+
part.mediaType === 'image' || part.mediaType === 'image/*'
|
|
206
|
+
? 'image/jpeg'
|
|
207
|
+
: part.mediaType;
|
|
208
|
+
|
|
209
|
+
return `data:${mediaType};base64,${convertToBase64(part.data)}`;
|
|
210
|
+
}
|