@ai-sdk/cohere 4.0.52 → 4.0.54
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 +18 -0
- package/dist/index.d.ts +101 -96
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +894 -1147
- package/dist/index.js.map +1 -1
- package/package.json +8 -8
package/dist/index.js
CHANGED
|
@@ -1,1171 +1,918 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
NoSuchModelError
|
|
4
|
-
} from "@ai-sdk/provider";
|
|
5
|
-
import {
|
|
6
|
-
generateId,
|
|
7
|
-
loadApiKey,
|
|
8
|
-
withoutTrailingSlash,
|
|
9
|
-
withUserAgentSuffix
|
|
10
|
-
} from "@ai-sdk/provider-utils";
|
|
11
|
-
|
|
12
|
-
// src/cohere-chat-language-model.ts
|
|
13
|
-
import {
|
|
14
|
-
combineHeaders,
|
|
15
|
-
createEventSourceResponseHandler,
|
|
16
|
-
createJsonResponseHandler,
|
|
17
|
-
isCustomReasoning,
|
|
18
|
-
mapReasoningToProviderBudget,
|
|
19
|
-
parseProviderOptions as parseProviderOptions2,
|
|
20
|
-
parseJSON,
|
|
21
|
-
postJsonToApi,
|
|
22
|
-
serializeModelOptions,
|
|
23
|
-
WORKFLOW_SERIALIZE,
|
|
24
|
-
WORKFLOW_DESERIALIZE
|
|
25
|
-
} from "@ai-sdk/provider-utils";
|
|
26
|
-
import { z as z3 } from "zod/v4";
|
|
27
|
-
|
|
28
|
-
// src/cohere-chat-language-model-options.ts
|
|
1
|
+
import { NoSuchModelError, TooManyEmbeddingValuesForCallError, UnsupportedFunctionalityError } from "@ai-sdk/provider";
|
|
2
|
+
import { WORKFLOW_DESERIALIZE, WORKFLOW_SERIALIZE, combineHeaders, convertToBase64, createEventSourceResponseHandler, createJsonErrorResponseHandler, createJsonResponseHandler, createNullLanguageModelUsage, generateId, getTopLevelMediaType, isCustomReasoning, lazySchema, loadApiKey, mapReasoningToProviderBudget, parseJSON, parseProviderOptions, postJsonToApi, resolveFullMediaType, serializeModelOptions, withUserAgentSuffix, withoutTrailingSlash, zodSchema } from "@ai-sdk/provider-utils";
|
|
29
3
|
import { z } from "zod/v4";
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
errorToMessage: (data) => data.message
|
|
4
|
+
//#region src/cohere-chat-language-model-options.ts
|
|
5
|
+
const cohereLanguageModelChatOptions = z.object({
|
|
6
|
+
/**
|
|
7
|
+
* Configuration for reasoning features (optional)
|
|
8
|
+
*
|
|
9
|
+
* Can be set to an object with the two properties `type` and `tokenBudget`. `type` can be set to `'enabled'` or `'disabled'` (defaults to `'enabled'`).
|
|
10
|
+
* `tokenBudget` is the maximum number of tokens the model can use for thinking, which must be set to a positive integer. The model will stop thinking if it reaches the thinking token budget and will proceed with the response
|
|
11
|
+
*
|
|
12
|
+
* @see https://docs.cohere.com/reference/chat#request.body.thinking
|
|
13
|
+
*/
|
|
14
|
+
thinking: z.object({
|
|
15
|
+
type: z.enum(["enabled", "disabled"]).optional(),
|
|
16
|
+
tokenBudget: z.number().optional()
|
|
17
|
+
}).optional() });
|
|
18
|
+
const cohereImagePartProviderOptions = z.object({
|
|
19
|
+
/**
|
|
20
|
+
* Image fidelity level passed through as `image_url.detail` on the Cohere chat API.
|
|
21
|
+
*
|
|
22
|
+
* @see https://docs.cohere.com/docs/image-inputs
|
|
23
|
+
*/
|
|
24
|
+
detail: z.enum([
|
|
25
|
+
"auto",
|
|
26
|
+
"low",
|
|
27
|
+
"high"
|
|
28
|
+
]).optional() });
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/cohere-error.ts
|
|
31
|
+
const cohereErrorDataSchema = z.object({ message: z.string() });
|
|
32
|
+
const cohereFailedResponseHandler = createJsonErrorResponseHandler({
|
|
33
|
+
errorSchema: cohereErrorDataSchema,
|
|
34
|
+
errorToMessage: (data) => data.message
|
|
62
35
|
});
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
const _exhaustiveCheck = type;
|
|
116
|
-
throw new UnsupportedFunctionalityError({
|
|
117
|
-
functionality: `tool choice type: ${_exhaustiveCheck}`
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
}
|
|
36
|
+
//#endregion
|
|
37
|
+
//#region src/cohere-prepare-tools.ts
|
|
38
|
+
function prepareTools({ tools, toolChoice }) {
|
|
39
|
+
tools = tools?.length ? tools : void 0;
|
|
40
|
+
const toolWarnings = [];
|
|
41
|
+
if (tools == null) return {
|
|
42
|
+
tools: void 0,
|
|
43
|
+
toolChoice: void 0,
|
|
44
|
+
toolWarnings
|
|
45
|
+
};
|
|
46
|
+
const cohereTools = [];
|
|
47
|
+
for (const tool of tools) if (tool.type === "provider") toolWarnings.push({
|
|
48
|
+
type: "unsupported",
|
|
49
|
+
feature: `provider-defined tool ${tool.id}`
|
|
50
|
+
});
|
|
51
|
+
else cohereTools.push({
|
|
52
|
+
type: "function",
|
|
53
|
+
function: {
|
|
54
|
+
name: tool.name,
|
|
55
|
+
description: tool.description,
|
|
56
|
+
parameters: tool.inputSchema
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
if (toolChoice == null) return {
|
|
60
|
+
tools: cohereTools,
|
|
61
|
+
toolChoice: void 0,
|
|
62
|
+
toolWarnings
|
|
63
|
+
};
|
|
64
|
+
const type = toolChoice.type;
|
|
65
|
+
switch (type) {
|
|
66
|
+
case "auto": return {
|
|
67
|
+
tools: cohereTools,
|
|
68
|
+
toolChoice: void 0,
|
|
69
|
+
toolWarnings
|
|
70
|
+
};
|
|
71
|
+
case "none": return {
|
|
72
|
+
tools: cohereTools,
|
|
73
|
+
toolChoice: "NONE",
|
|
74
|
+
toolWarnings
|
|
75
|
+
};
|
|
76
|
+
case "required": return {
|
|
77
|
+
tools: cohereTools,
|
|
78
|
+
toolChoice: "REQUIRED",
|
|
79
|
+
toolWarnings
|
|
80
|
+
};
|
|
81
|
+
case "tool": return {
|
|
82
|
+
tools: cohereTools.filter((tool) => tool.function.name === toolChoice.toolName),
|
|
83
|
+
toolChoice: "REQUIRED",
|
|
84
|
+
toolWarnings
|
|
85
|
+
};
|
|
86
|
+
default: throw new UnsupportedFunctionalityError({ functionality: `tool choice type: ${type}` });
|
|
87
|
+
}
|
|
121
88
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
import { createNullLanguageModelUsage } from "@ai-sdk/provider-utils";
|
|
89
|
+
//#endregion
|
|
90
|
+
//#region src/convert-cohere-usage.ts
|
|
125
91
|
function convertCohereUsage(usage) {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
raw: usage
|
|
145
|
-
};
|
|
92
|
+
if (usage == null) return createNullLanguageModelUsage();
|
|
93
|
+
const tokens = usage.tokens;
|
|
94
|
+
const inputTokens = tokens.input_tokens;
|
|
95
|
+
const outputTokens = tokens.output_tokens;
|
|
96
|
+
return {
|
|
97
|
+
inputTokens: {
|
|
98
|
+
total: inputTokens,
|
|
99
|
+
noCache: inputTokens,
|
|
100
|
+
cacheRead: void 0,
|
|
101
|
+
cacheWrite: void 0
|
|
102
|
+
},
|
|
103
|
+
outputTokens: {
|
|
104
|
+
total: outputTokens,
|
|
105
|
+
text: outputTokens,
|
|
106
|
+
reasoning: void 0
|
|
107
|
+
},
|
|
108
|
+
raw: usage
|
|
109
|
+
};
|
|
146
110
|
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
import {
|
|
150
|
-
UnsupportedFunctionalityError as UnsupportedFunctionalityError2
|
|
151
|
-
} from "@ai-sdk/provider";
|
|
152
|
-
import {
|
|
153
|
-
convertToBase64,
|
|
154
|
-
getTopLevelMediaType,
|
|
155
|
-
parseProviderOptions,
|
|
156
|
-
resolveFullMediaType
|
|
157
|
-
} from "@ai-sdk/provider-utils";
|
|
111
|
+
//#endregion
|
|
112
|
+
//#region src/convert-to-cohere-chat-prompt.ts
|
|
158
113
|
async function convertToCohereChatPrompt(prompt) {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
contentValue = output.reason ?? "Tool call execution denied.";
|
|
281
|
-
break;
|
|
282
|
-
case "content":
|
|
283
|
-
case "json":
|
|
284
|
-
case "error-json":
|
|
285
|
-
contentValue = JSON.stringify(output.value);
|
|
286
|
-
break;
|
|
287
|
-
}
|
|
288
|
-
return {
|
|
289
|
-
role: "tool",
|
|
290
|
-
content: contentValue,
|
|
291
|
-
tool_call_id: toolResult.toolCallId
|
|
292
|
-
};
|
|
293
|
-
})
|
|
294
|
-
);
|
|
295
|
-
break;
|
|
296
|
-
}
|
|
297
|
-
default: {
|
|
298
|
-
const _exhaustiveCheck = role;
|
|
299
|
-
throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
return { messages, documents, warnings };
|
|
114
|
+
const messages = [];
|
|
115
|
+
const documents = [];
|
|
116
|
+
const warnings = [];
|
|
117
|
+
for (const { role, content } of prompt) switch (role) {
|
|
118
|
+
case "system":
|
|
119
|
+
messages.push({
|
|
120
|
+
role: "system",
|
|
121
|
+
content
|
|
122
|
+
});
|
|
123
|
+
break;
|
|
124
|
+
case "user": {
|
|
125
|
+
const userContentParts = [];
|
|
126
|
+
let hasImage = false;
|
|
127
|
+
for (const part of content) switch (part.type) {
|
|
128
|
+
case "text":
|
|
129
|
+
if (part.text.length > 0) userContentParts.push({
|
|
130
|
+
type: "text",
|
|
131
|
+
text: part.text
|
|
132
|
+
});
|
|
133
|
+
break;
|
|
134
|
+
case "file": {
|
|
135
|
+
if (getTopLevelMediaType(part.mediaType) === "image") {
|
|
136
|
+
hasImage = true;
|
|
137
|
+
const url = buildImageUrl({ part });
|
|
138
|
+
const cohereOptions = await parseProviderOptions({
|
|
139
|
+
provider: "cohere",
|
|
140
|
+
providerOptions: part.providerOptions,
|
|
141
|
+
schema: cohereImagePartProviderOptions
|
|
142
|
+
}) ?? {};
|
|
143
|
+
userContentParts.push({
|
|
144
|
+
type: "image_url",
|
|
145
|
+
image_url: {
|
|
146
|
+
url,
|
|
147
|
+
...cohereOptions.detail ? { detail: cohereOptions.detail } : {}
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
break;
|
|
151
|
+
}
|
|
152
|
+
let textContent;
|
|
153
|
+
switch (part.data.type) {
|
|
154
|
+
case "reference": throw new UnsupportedFunctionalityError({ functionality: "file parts with provider references" });
|
|
155
|
+
case "url": throw new UnsupportedFunctionalityError({
|
|
156
|
+
functionality: "File URL data",
|
|
157
|
+
message: "URLs should be downloaded by the AI SDK and not reach this point. This indicates a configuration issue."
|
|
158
|
+
});
|
|
159
|
+
case "text":
|
|
160
|
+
textContent = part.data.text;
|
|
161
|
+
break;
|
|
162
|
+
case "data": textContent = typeof part.data.data === "string" ? part.data.data : new TextDecoder().decode(part.data.data);
|
|
163
|
+
}
|
|
164
|
+
documents.push({ data: {
|
|
165
|
+
text: textContent,
|
|
166
|
+
title: part.filename
|
|
167
|
+
} });
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
if (hasImage) messages.push({
|
|
172
|
+
role: "user",
|
|
173
|
+
content: userContentParts
|
|
174
|
+
});
|
|
175
|
+
else messages.push({
|
|
176
|
+
role: "user",
|
|
177
|
+
content: userContentParts.map((p) => p.type === "text" ? p.text : "").join("")
|
|
178
|
+
});
|
|
179
|
+
break;
|
|
180
|
+
}
|
|
181
|
+
case "assistant": {
|
|
182
|
+
let text = "";
|
|
183
|
+
const toolCalls = [];
|
|
184
|
+
for (const part of content) switch (part.type) {
|
|
185
|
+
case "text":
|
|
186
|
+
text += part.text;
|
|
187
|
+
break;
|
|
188
|
+
case "tool-call": toolCalls.push({
|
|
189
|
+
id: part.toolCallId,
|
|
190
|
+
type: "function",
|
|
191
|
+
function: {
|
|
192
|
+
name: part.toolName,
|
|
193
|
+
arguments: JSON.stringify(part.input)
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
messages.push({
|
|
198
|
+
role: "assistant",
|
|
199
|
+
content: toolCalls.length > 0 ? void 0 : text,
|
|
200
|
+
tool_calls: toolCalls.length > 0 ? toolCalls : void 0,
|
|
201
|
+
tool_plan: void 0
|
|
202
|
+
});
|
|
203
|
+
break;
|
|
204
|
+
}
|
|
205
|
+
case "tool":
|
|
206
|
+
messages.push(...content.filter((toolResult) => toolResult.type !== "tool-approval-response").map((toolResult) => {
|
|
207
|
+
const output = toolResult.output;
|
|
208
|
+
let contentValue;
|
|
209
|
+
switch (output.type) {
|
|
210
|
+
case "text":
|
|
211
|
+
case "error-text":
|
|
212
|
+
contentValue = output.value;
|
|
213
|
+
break;
|
|
214
|
+
case "execution-denied":
|
|
215
|
+
contentValue = output.reason ?? "Tool call execution denied.";
|
|
216
|
+
break;
|
|
217
|
+
case "content":
|
|
218
|
+
case "json":
|
|
219
|
+
case "error-json": contentValue = JSON.stringify(output.value);
|
|
220
|
+
}
|
|
221
|
+
return {
|
|
222
|
+
role: "tool",
|
|
223
|
+
content: contentValue,
|
|
224
|
+
tool_call_id: toolResult.toolCallId
|
|
225
|
+
};
|
|
226
|
+
}));
|
|
227
|
+
break;
|
|
228
|
+
default: throw new Error(`Unsupported role: ${role}`);
|
|
229
|
+
}
|
|
230
|
+
return {
|
|
231
|
+
messages,
|
|
232
|
+
documents,
|
|
233
|
+
warnings
|
|
234
|
+
};
|
|
304
235
|
}
|
|
305
236
|
function buildImageUrl({ part }) {
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
}
|
|
313
|
-
case "reference": {
|
|
314
|
-
throw new UnsupportedFunctionalityError2({
|
|
315
|
-
functionality: "image file parts with provider references"
|
|
316
|
-
});
|
|
317
|
-
}
|
|
318
|
-
case "text": {
|
|
319
|
-
throw new UnsupportedFunctionalityError2({
|
|
320
|
-
functionality: "image file parts with text data"
|
|
321
|
-
});
|
|
322
|
-
}
|
|
323
|
-
}
|
|
237
|
+
switch (part.data.type) {
|
|
238
|
+
case "url": return part.data.url.toString();
|
|
239
|
+
case "data": return `data:${resolveFullMediaType({ part })};base64,${convertToBase64(part.data.data)}`;
|
|
240
|
+
case "reference": throw new UnsupportedFunctionalityError({ functionality: "image file parts with provider references" });
|
|
241
|
+
case "text": throw new UnsupportedFunctionalityError({ functionality: "image file parts with text data" });
|
|
242
|
+
}
|
|
324
243
|
}
|
|
325
|
-
|
|
326
|
-
|
|
244
|
+
//#endregion
|
|
245
|
+
//#region src/map-cohere-finish-reason.ts
|
|
327
246
|
function mapCohereFinishReason(finishReason) {
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
case "TOOL_CALL":
|
|
337
|
-
return "tool-calls";
|
|
338
|
-
default:
|
|
339
|
-
return "other";
|
|
340
|
-
}
|
|
247
|
+
switch (finishReason) {
|
|
248
|
+
case "COMPLETE":
|
|
249
|
+
case "STOP_SEQUENCE": return "stop";
|
|
250
|
+
case "MAX_TOKENS": return "length";
|
|
251
|
+
case "ERROR": return "error";
|
|
252
|
+
case "TOOL_CALL": return "tool-calls";
|
|
253
|
+
default: return "other";
|
|
254
|
+
}
|
|
341
255
|
}
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
var CohereChatLanguageModel = class
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
)
|
|
638
|
-
});
|
|
639
|
-
pendingToolCall.hasFinished = true;
|
|
640
|
-
pendingToolCall = null;
|
|
641
|
-
}
|
|
642
|
-
return;
|
|
643
|
-
}
|
|
644
|
-
case "message-start": {
|
|
645
|
-
controller.enqueue({
|
|
646
|
-
type: "response-metadata",
|
|
647
|
-
id: value.id ?? void 0
|
|
648
|
-
});
|
|
649
|
-
return;
|
|
650
|
-
}
|
|
651
|
-
case "message-end": {
|
|
652
|
-
finishReason = {
|
|
653
|
-
unified: mapCohereFinishReason(value.delta.finish_reason),
|
|
654
|
-
raw: value.delta.finish_reason
|
|
655
|
-
};
|
|
656
|
-
usage = value.delta.usage;
|
|
657
|
-
return;
|
|
658
|
-
}
|
|
659
|
-
default: {
|
|
660
|
-
}
|
|
661
|
-
}
|
|
662
|
-
},
|
|
663
|
-
flush(controller) {
|
|
664
|
-
controller.enqueue({
|
|
665
|
-
type: "finish",
|
|
666
|
-
finishReason,
|
|
667
|
-
usage: convertCohereUsage(usage)
|
|
668
|
-
});
|
|
669
|
-
}
|
|
670
|
-
})
|
|
671
|
-
),
|
|
672
|
-
request: { body: { ...args, stream: true } },
|
|
673
|
-
response: { headers: responseHeaders }
|
|
674
|
-
};
|
|
675
|
-
}
|
|
256
|
+
//#endregion
|
|
257
|
+
//#region src/cohere-chat-language-model.ts
|
|
258
|
+
var CohereChatLanguageModel = class CohereChatLanguageModel {
|
|
259
|
+
static [WORKFLOW_SERIALIZE](model) {
|
|
260
|
+
return serializeModelOptions({
|
|
261
|
+
modelId: model.modelId,
|
|
262
|
+
config: model.config
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
static [WORKFLOW_DESERIALIZE](options) {
|
|
266
|
+
return new CohereChatLanguageModel(options.modelId, options.config);
|
|
267
|
+
}
|
|
268
|
+
constructor(modelId, config) {
|
|
269
|
+
this.specificationVersion = "v4";
|
|
270
|
+
this.supportedUrls = { "image/*": [/^https?:\/\/.*$/] };
|
|
271
|
+
this.modelId = modelId;
|
|
272
|
+
this.config = config;
|
|
273
|
+
}
|
|
274
|
+
get provider() {
|
|
275
|
+
return this.config.provider;
|
|
276
|
+
}
|
|
277
|
+
async getArgs({ prompt, maxOutputTokens, temperature, topP, topK, frequencyPenalty, presencePenalty, stopSequences, responseFormat, seed, reasoning, tools, toolChoice, providerOptions }) {
|
|
278
|
+
const warnings = [];
|
|
279
|
+
const cohereOptions = await parseProviderOptions({
|
|
280
|
+
provider: "cohere",
|
|
281
|
+
providerOptions,
|
|
282
|
+
schema: cohereLanguageModelChatOptions
|
|
283
|
+
}) ?? {};
|
|
284
|
+
const { messages: chatPrompt, documents: cohereDocuments, warnings: promptWarnings } = await convertToCohereChatPrompt(prompt);
|
|
285
|
+
const { tools: cohereTools, toolChoice: cohereToolChoice, toolWarnings } = prepareTools({
|
|
286
|
+
tools,
|
|
287
|
+
toolChoice
|
|
288
|
+
});
|
|
289
|
+
warnings.push(...toolWarnings, ...promptWarnings);
|
|
290
|
+
return {
|
|
291
|
+
args: {
|
|
292
|
+
model: this.modelId,
|
|
293
|
+
frequency_penalty: frequencyPenalty,
|
|
294
|
+
presence_penalty: presencePenalty,
|
|
295
|
+
max_tokens: maxOutputTokens,
|
|
296
|
+
temperature,
|
|
297
|
+
p: topP,
|
|
298
|
+
k: topK,
|
|
299
|
+
seed,
|
|
300
|
+
stop_sequences: stopSequences,
|
|
301
|
+
response_format: responseFormat?.type === "json" ? {
|
|
302
|
+
type: "json_object",
|
|
303
|
+
json_schema: responseFormat.schema
|
|
304
|
+
} : void 0,
|
|
305
|
+
messages: chatPrompt,
|
|
306
|
+
tools: cohereTools,
|
|
307
|
+
tool_choice: cohereToolChoice,
|
|
308
|
+
...cohereDocuments.length > 0 && { documents: cohereDocuments },
|
|
309
|
+
...resolveCohereThinking({
|
|
310
|
+
reasoning,
|
|
311
|
+
cohereOptions,
|
|
312
|
+
warnings
|
|
313
|
+
})
|
|
314
|
+
},
|
|
315
|
+
warnings
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
async doGenerate(options) {
|
|
319
|
+
const { args, warnings } = await this.getArgs(options);
|
|
320
|
+
const { responseHeaders, value: response, rawValue: rawResponse } = await postJsonToApi({
|
|
321
|
+
url: `${this.config.baseURL}/chat`,
|
|
322
|
+
headers: combineHeaders(this.config.headers?.(), options.headers),
|
|
323
|
+
body: args,
|
|
324
|
+
failedResponseHandler: cohereFailedResponseHandler,
|
|
325
|
+
successfulResponseHandler: createJsonResponseHandler(cohereChatResponseSchema),
|
|
326
|
+
abortSignal: options.abortSignal,
|
|
327
|
+
fetch: this.config.fetch
|
|
328
|
+
});
|
|
329
|
+
const content = [];
|
|
330
|
+
for (const item of response.message.content ?? []) {
|
|
331
|
+
if (item.type === "text" && item.text.length > 0) {
|
|
332
|
+
content.push({
|
|
333
|
+
type: "text",
|
|
334
|
+
text: item.text
|
|
335
|
+
});
|
|
336
|
+
continue;
|
|
337
|
+
}
|
|
338
|
+
if (item.type === "thinking" && item.thinking.length > 0) {
|
|
339
|
+
content.push({
|
|
340
|
+
type: "reasoning",
|
|
341
|
+
text: item.thinking
|
|
342
|
+
});
|
|
343
|
+
continue;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
for (const citation of response.message.citations ?? []) content.push({
|
|
347
|
+
type: "source",
|
|
348
|
+
sourceType: "document",
|
|
349
|
+
id: this.config.generateId(),
|
|
350
|
+
mediaType: "text/plain",
|
|
351
|
+
title: citation.sources[0]?.document?.title || "Document",
|
|
352
|
+
providerMetadata: { cohere: {
|
|
353
|
+
start: citation.start,
|
|
354
|
+
end: citation.end,
|
|
355
|
+
text: citation.text,
|
|
356
|
+
sources: citation.sources,
|
|
357
|
+
...citation.type && { citationType: citation.type }
|
|
358
|
+
} }
|
|
359
|
+
});
|
|
360
|
+
for (const toolCall of response.message.tool_calls ?? []) content.push({
|
|
361
|
+
type: "tool-call",
|
|
362
|
+
toolCallId: toolCall.id,
|
|
363
|
+
toolName: toolCall.function.name,
|
|
364
|
+
input: toolCall.function.arguments.replace(/^null$/, "{}")
|
|
365
|
+
});
|
|
366
|
+
return {
|
|
367
|
+
content,
|
|
368
|
+
finishReason: {
|
|
369
|
+
unified: mapCohereFinishReason(response.finish_reason),
|
|
370
|
+
raw: response.finish_reason ?? void 0
|
|
371
|
+
},
|
|
372
|
+
usage: convertCohereUsage(response.usage),
|
|
373
|
+
request: { body: args },
|
|
374
|
+
response: {
|
|
375
|
+
id: response.generation_id ?? void 0,
|
|
376
|
+
headers: responseHeaders,
|
|
377
|
+
body: rawResponse
|
|
378
|
+
},
|
|
379
|
+
warnings
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
async doStream(options) {
|
|
383
|
+
const { args, warnings } = await this.getArgs(options);
|
|
384
|
+
const { responseHeaders, value: response } = await postJsonToApi({
|
|
385
|
+
url: `${this.config.baseURL}/chat`,
|
|
386
|
+
headers: combineHeaders(this.config.headers?.(), options.headers),
|
|
387
|
+
body: {
|
|
388
|
+
...args,
|
|
389
|
+
stream: true
|
|
390
|
+
},
|
|
391
|
+
failedResponseHandler: cohereFailedResponseHandler,
|
|
392
|
+
successfulResponseHandler: createEventSourceResponseHandler(cohereChatChunkSchema),
|
|
393
|
+
abortSignal: options.abortSignal,
|
|
394
|
+
fetch: this.config.fetch
|
|
395
|
+
});
|
|
396
|
+
let finishReason = {
|
|
397
|
+
unified: "other",
|
|
398
|
+
raw: void 0
|
|
399
|
+
};
|
|
400
|
+
let usage = void 0;
|
|
401
|
+
let pendingToolCall = null;
|
|
402
|
+
let isActiveReasoning = false;
|
|
403
|
+
return {
|
|
404
|
+
stream: response.pipeThrough(new TransformStream({
|
|
405
|
+
start(controller) {
|
|
406
|
+
controller.enqueue({
|
|
407
|
+
type: "stream-start",
|
|
408
|
+
warnings
|
|
409
|
+
});
|
|
410
|
+
},
|
|
411
|
+
async transform(chunk, controller) {
|
|
412
|
+
if (options.includeRawChunks) controller.enqueue({
|
|
413
|
+
type: "raw",
|
|
414
|
+
rawValue: chunk.rawValue
|
|
415
|
+
});
|
|
416
|
+
if (!chunk.success) {
|
|
417
|
+
finishReason = {
|
|
418
|
+
unified: "error",
|
|
419
|
+
raw: void 0
|
|
420
|
+
};
|
|
421
|
+
controller.enqueue({
|
|
422
|
+
type: "error",
|
|
423
|
+
error: chunk.error
|
|
424
|
+
});
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
427
|
+
const value = chunk.value;
|
|
428
|
+
switch (value.type) {
|
|
429
|
+
case "content-start":
|
|
430
|
+
if (value.delta.message.content.type === "thinking") {
|
|
431
|
+
controller.enqueue({
|
|
432
|
+
type: "reasoning-start",
|
|
433
|
+
id: String(value.index)
|
|
434
|
+
});
|
|
435
|
+
isActiveReasoning = true;
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
controller.enqueue({
|
|
439
|
+
type: "text-start",
|
|
440
|
+
id: String(value.index)
|
|
441
|
+
});
|
|
442
|
+
return;
|
|
443
|
+
case "content-delta":
|
|
444
|
+
if ("thinking" in value.delta.message.content) {
|
|
445
|
+
controller.enqueue({
|
|
446
|
+
type: "reasoning-delta",
|
|
447
|
+
id: String(value.index),
|
|
448
|
+
delta: value.delta.message.content.thinking
|
|
449
|
+
});
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
controller.enqueue({
|
|
453
|
+
type: "text-delta",
|
|
454
|
+
id: String(value.index),
|
|
455
|
+
delta: value.delta.message.content.text
|
|
456
|
+
});
|
|
457
|
+
return;
|
|
458
|
+
case "content-end":
|
|
459
|
+
if (isActiveReasoning) {
|
|
460
|
+
controller.enqueue({
|
|
461
|
+
type: "reasoning-end",
|
|
462
|
+
id: String(value.index)
|
|
463
|
+
});
|
|
464
|
+
isActiveReasoning = false;
|
|
465
|
+
return;
|
|
466
|
+
}
|
|
467
|
+
controller.enqueue({
|
|
468
|
+
type: "text-end",
|
|
469
|
+
id: String(value.index)
|
|
470
|
+
});
|
|
471
|
+
return;
|
|
472
|
+
case "tool-call-start": {
|
|
473
|
+
const toolId = value.delta.message.tool_calls.id;
|
|
474
|
+
const toolName = value.delta.message.tool_calls.function.name;
|
|
475
|
+
const initialArgs = value.delta.message.tool_calls.function.arguments;
|
|
476
|
+
pendingToolCall = {
|
|
477
|
+
id: toolId,
|
|
478
|
+
name: toolName,
|
|
479
|
+
arguments: initialArgs,
|
|
480
|
+
hasFinished: false
|
|
481
|
+
};
|
|
482
|
+
controller.enqueue({
|
|
483
|
+
type: "tool-input-start",
|
|
484
|
+
id: toolId,
|
|
485
|
+
toolName
|
|
486
|
+
});
|
|
487
|
+
if (initialArgs.length > 0) controller.enqueue({
|
|
488
|
+
type: "tool-input-delta",
|
|
489
|
+
id: toolId,
|
|
490
|
+
delta: initialArgs
|
|
491
|
+
});
|
|
492
|
+
return;
|
|
493
|
+
}
|
|
494
|
+
case "tool-call-delta":
|
|
495
|
+
if (pendingToolCall && !pendingToolCall.hasFinished) {
|
|
496
|
+
const argsDelta = value.delta.message.tool_calls.function.arguments;
|
|
497
|
+
pendingToolCall.arguments += argsDelta;
|
|
498
|
+
controller.enqueue({
|
|
499
|
+
type: "tool-input-delta",
|
|
500
|
+
id: pendingToolCall.id,
|
|
501
|
+
delta: argsDelta
|
|
502
|
+
});
|
|
503
|
+
}
|
|
504
|
+
return;
|
|
505
|
+
case "tool-call-end":
|
|
506
|
+
if (pendingToolCall && !pendingToolCall.hasFinished) {
|
|
507
|
+
controller.enqueue({
|
|
508
|
+
type: "tool-input-end",
|
|
509
|
+
id: pendingToolCall.id
|
|
510
|
+
});
|
|
511
|
+
controller.enqueue({
|
|
512
|
+
type: "tool-call",
|
|
513
|
+
toolCallId: pendingToolCall.id,
|
|
514
|
+
toolName: pendingToolCall.name,
|
|
515
|
+
input: JSON.stringify(await parseJSON({ text: pendingToolCall.arguments?.trim() || "{}" }))
|
|
516
|
+
});
|
|
517
|
+
pendingToolCall.hasFinished = true;
|
|
518
|
+
pendingToolCall = null;
|
|
519
|
+
}
|
|
520
|
+
return;
|
|
521
|
+
case "message-start":
|
|
522
|
+
controller.enqueue({
|
|
523
|
+
type: "response-metadata",
|
|
524
|
+
id: value.id ?? void 0
|
|
525
|
+
});
|
|
526
|
+
return;
|
|
527
|
+
case "message-end":
|
|
528
|
+
finishReason = {
|
|
529
|
+
unified: mapCohereFinishReason(value.delta.finish_reason),
|
|
530
|
+
raw: value.delta.finish_reason
|
|
531
|
+
};
|
|
532
|
+
usage = value.delta.usage;
|
|
533
|
+
return;
|
|
534
|
+
}
|
|
535
|
+
},
|
|
536
|
+
flush(controller) {
|
|
537
|
+
controller.enqueue({
|
|
538
|
+
type: "finish",
|
|
539
|
+
finishReason,
|
|
540
|
+
usage: convertCohereUsage(usage)
|
|
541
|
+
});
|
|
542
|
+
}
|
|
543
|
+
})),
|
|
544
|
+
request: { body: {
|
|
545
|
+
...args,
|
|
546
|
+
stream: true
|
|
547
|
+
} },
|
|
548
|
+
response: { headers: responseHeaders }
|
|
549
|
+
};
|
|
550
|
+
}
|
|
676
551
|
};
|
|
677
|
-
function resolveCohereThinking({
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
}
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
}
|
|
696
|
-
const tokenBudget = mapReasoningToProviderBudget({
|
|
697
|
-
reasoning,
|
|
698
|
-
maxOutputTokens: 32768,
|
|
699
|
-
maxReasoningBudget: 32768,
|
|
700
|
-
warnings
|
|
701
|
-
});
|
|
702
|
-
if (tokenBudget == null) {
|
|
703
|
-
return {};
|
|
704
|
-
}
|
|
705
|
-
return { thinking: { type: "enabled", token_budget: tokenBudget } };
|
|
552
|
+
function resolveCohereThinking({ reasoning, cohereOptions, warnings }) {
|
|
553
|
+
if (cohereOptions.thinking) return { thinking: {
|
|
554
|
+
type: cohereOptions.thinking.type ?? "enabled",
|
|
555
|
+
token_budget: cohereOptions.thinking.tokenBudget
|
|
556
|
+
} };
|
|
557
|
+
if (!isCustomReasoning(reasoning)) return {};
|
|
558
|
+
if (reasoning === "none") return { thinking: { type: "disabled" } };
|
|
559
|
+
const tokenBudget = mapReasoningToProviderBudget({
|
|
560
|
+
reasoning,
|
|
561
|
+
maxOutputTokens: 32768,
|
|
562
|
+
maxReasoningBudget: 32768,
|
|
563
|
+
warnings
|
|
564
|
+
});
|
|
565
|
+
if (tokenBudget == null) return {};
|
|
566
|
+
return { thinking: {
|
|
567
|
+
type: "enabled",
|
|
568
|
+
token_budget: tokenBudget
|
|
569
|
+
} };
|
|
706
570
|
}
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
571
|
+
const cohereUsageSchema = z.looseObject({
|
|
572
|
+
billed_units: z.looseObject({
|
|
573
|
+
input_tokens: z.number(),
|
|
574
|
+
output_tokens: z.number()
|
|
575
|
+
}).nullish(),
|
|
576
|
+
tokens: z.looseObject({
|
|
577
|
+
input_tokens: z.number(),
|
|
578
|
+
output_tokens: z.number()
|
|
579
|
+
}),
|
|
580
|
+
cached_tokens: z.number().nullish()
|
|
717
581
|
});
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
text: z3.string(),
|
|
757
|
-
title: z3.string()
|
|
758
|
-
})
|
|
759
|
-
})
|
|
760
|
-
),
|
|
761
|
-
type: z3.string().optional()
|
|
762
|
-
})
|
|
763
|
-
).nullish()
|
|
764
|
-
}),
|
|
765
|
-
finish_reason: z3.string(),
|
|
766
|
-
usage: cohereUsageSchema
|
|
582
|
+
const cohereChatResponseSchema = z.object({
|
|
583
|
+
generation_id: z.string().nullish(),
|
|
584
|
+
message: z.object({
|
|
585
|
+
role: z.string(),
|
|
586
|
+
content: z.array(z.union([z.object({
|
|
587
|
+
type: z.literal("text"),
|
|
588
|
+
text: z.string()
|
|
589
|
+
}), z.object({
|
|
590
|
+
type: z.literal("thinking"),
|
|
591
|
+
thinking: z.string()
|
|
592
|
+
})])).nullish(),
|
|
593
|
+
tool_plan: z.string().nullish(),
|
|
594
|
+
tool_calls: z.array(z.object({
|
|
595
|
+
id: z.string(),
|
|
596
|
+
type: z.literal("function"),
|
|
597
|
+
function: z.object({
|
|
598
|
+
name: z.string(),
|
|
599
|
+
arguments: z.string()
|
|
600
|
+
})
|
|
601
|
+
})).nullish(),
|
|
602
|
+
citations: z.array(z.object({
|
|
603
|
+
start: z.number(),
|
|
604
|
+
end: z.number(),
|
|
605
|
+
text: z.string(),
|
|
606
|
+
sources: z.array(z.object({
|
|
607
|
+
type: z.string().optional(),
|
|
608
|
+
id: z.string().optional(),
|
|
609
|
+
document: z.object({
|
|
610
|
+
id: z.string().optional(),
|
|
611
|
+
text: z.string(),
|
|
612
|
+
title: z.string()
|
|
613
|
+
})
|
|
614
|
+
})),
|
|
615
|
+
type: z.string().optional()
|
|
616
|
+
})).nullish()
|
|
617
|
+
}),
|
|
618
|
+
finish_reason: z.string(),
|
|
619
|
+
usage: cohereUsageSchema
|
|
767
620
|
});
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
})
|
|
823
|
-
}),
|
|
824
|
-
// https://docs.cohere.com/v2/docs/streaming#tool-use-stream-events-for-tool-calling
|
|
825
|
-
z3.object({
|
|
826
|
-
type: z3.literal("tool-plan-delta"),
|
|
827
|
-
delta: z3.object({
|
|
828
|
-
message: z3.object({
|
|
829
|
-
tool_plan: z3.string()
|
|
830
|
-
})
|
|
831
|
-
})
|
|
832
|
-
}),
|
|
833
|
-
z3.object({
|
|
834
|
-
type: z3.literal("tool-call-start"),
|
|
835
|
-
delta: z3.object({
|
|
836
|
-
message: z3.object({
|
|
837
|
-
tool_calls: z3.object({
|
|
838
|
-
id: z3.string(),
|
|
839
|
-
type: z3.literal("function"),
|
|
840
|
-
function: z3.object({
|
|
841
|
-
name: z3.string(),
|
|
842
|
-
arguments: z3.string()
|
|
843
|
-
})
|
|
844
|
-
})
|
|
845
|
-
})
|
|
846
|
-
})
|
|
847
|
-
}),
|
|
848
|
-
// A single tool call's `arguments` stream in chunks and must be accumulated
|
|
849
|
-
// in a string and so the full tool object info can only be parsed once we see
|
|
850
|
-
// `tool-call-end`.
|
|
851
|
-
z3.object({
|
|
852
|
-
type: z3.literal("tool-call-delta"),
|
|
853
|
-
delta: z3.object({
|
|
854
|
-
message: z3.object({
|
|
855
|
-
tool_calls: z3.object({
|
|
856
|
-
function: z3.object({
|
|
857
|
-
arguments: z3.string()
|
|
858
|
-
})
|
|
859
|
-
})
|
|
860
|
-
})
|
|
861
|
-
})
|
|
862
|
-
}),
|
|
863
|
-
z3.object({
|
|
864
|
-
type: z3.literal("tool-call-end")
|
|
865
|
-
})
|
|
621
|
+
const cohereChatChunkSchema = z.discriminatedUnion("type", [
|
|
622
|
+
z.object({ type: z.literal("citation-start") }),
|
|
623
|
+
z.object({ type: z.literal("citation-end") }),
|
|
624
|
+
z.object({
|
|
625
|
+
type: z.literal("content-start"),
|
|
626
|
+
index: z.number(),
|
|
627
|
+
delta: z.object({ message: z.object({ content: z.union([z.object({
|
|
628
|
+
type: z.literal("text"),
|
|
629
|
+
text: z.string()
|
|
630
|
+
}), z.object({
|
|
631
|
+
type: z.literal("thinking"),
|
|
632
|
+
thinking: z.string()
|
|
633
|
+
})]) }) })
|
|
634
|
+
}),
|
|
635
|
+
z.object({
|
|
636
|
+
type: z.literal("content-delta"),
|
|
637
|
+
index: z.number(),
|
|
638
|
+
delta: z.object({ message: z.object({ content: z.union([z.object({ text: z.string() }), z.object({ thinking: z.string() })]) }) })
|
|
639
|
+
}),
|
|
640
|
+
z.object({
|
|
641
|
+
type: z.literal("content-end"),
|
|
642
|
+
index: z.number()
|
|
643
|
+
}),
|
|
644
|
+
z.object({
|
|
645
|
+
type: z.literal("message-start"),
|
|
646
|
+
id: z.string().nullish()
|
|
647
|
+
}),
|
|
648
|
+
z.object({
|
|
649
|
+
type: z.literal("message-end"),
|
|
650
|
+
delta: z.object({
|
|
651
|
+
finish_reason: z.string(),
|
|
652
|
+
usage: cohereUsageSchema
|
|
653
|
+
})
|
|
654
|
+
}),
|
|
655
|
+
z.object({
|
|
656
|
+
type: z.literal("tool-plan-delta"),
|
|
657
|
+
delta: z.object({ message: z.object({ tool_plan: z.string() }) })
|
|
658
|
+
}),
|
|
659
|
+
z.object({
|
|
660
|
+
type: z.literal("tool-call-start"),
|
|
661
|
+
delta: z.object({ message: z.object({ tool_calls: z.object({
|
|
662
|
+
id: z.string(),
|
|
663
|
+
type: z.literal("function"),
|
|
664
|
+
function: z.object({
|
|
665
|
+
name: z.string(),
|
|
666
|
+
arguments: z.string()
|
|
667
|
+
})
|
|
668
|
+
}) }) })
|
|
669
|
+
}),
|
|
670
|
+
z.object({
|
|
671
|
+
type: z.literal("tool-call-delta"),
|
|
672
|
+
delta: z.object({ message: z.object({ tool_calls: z.object({ function: z.object({ arguments: z.string() }) }) }) })
|
|
673
|
+
}),
|
|
674
|
+
z.object({ type: z.literal("tool-call-end") })
|
|
866
675
|
]);
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
*/
|
|
911
|
-
outputDimension: z4.union([z4.literal(256), z4.literal(512), z4.literal(1024), z4.literal(1536)]).optional()
|
|
676
|
+
//#endregion
|
|
677
|
+
//#region src/cohere-embedding-model-options.ts
|
|
678
|
+
const cohereEmbeddingModelOptions = z.object({
|
|
679
|
+
/**
|
|
680
|
+
* Specifies the type of input passed to the model. Default is `search_query`.
|
|
681
|
+
*
|
|
682
|
+
* - "search_document": Used for embeddings stored in a vector database for search use-cases.
|
|
683
|
+
* - "search_query": Used for embeddings of search queries run against a vector DB to find relevant documents.
|
|
684
|
+
* - "classification": Used for embeddings passed through a text classifier.
|
|
685
|
+
* - "clustering": Used for embeddings run through a clustering algorithm.
|
|
686
|
+
*/
|
|
687
|
+
inputType: z.enum([
|
|
688
|
+
"search_document",
|
|
689
|
+
"search_query",
|
|
690
|
+
"classification",
|
|
691
|
+
"clustering"
|
|
692
|
+
]).optional(),
|
|
693
|
+
/**
|
|
694
|
+
* Specifies how the API will handle inputs longer than the maximum token length.
|
|
695
|
+
* Default is `END`.
|
|
696
|
+
*
|
|
697
|
+
* - "NONE": If selected, when the input exceeds the maximum input token length will return an error.
|
|
698
|
+
* - "START": Will discard the start of the input until the remaining input is exactly the maximum input token length for the model.
|
|
699
|
+
* - "END": Will discard the end of the input until the remaining input is exactly the maximum input token length for the model.
|
|
700
|
+
*/
|
|
701
|
+
truncate: z.enum([
|
|
702
|
+
"NONE",
|
|
703
|
+
"START",
|
|
704
|
+
"END"
|
|
705
|
+
]).optional(),
|
|
706
|
+
/**
|
|
707
|
+
* The number of dimensions of the output embedding.
|
|
708
|
+
* Only available for `embed-v4.0` and newer models.
|
|
709
|
+
*
|
|
710
|
+
* Possible values are `256`, `512`, `1024`, and `1536`.
|
|
711
|
+
* The default is `1536`.
|
|
712
|
+
*/
|
|
713
|
+
outputDimension: z.union([
|
|
714
|
+
z.literal(256),
|
|
715
|
+
z.literal(512),
|
|
716
|
+
z.literal(1024),
|
|
717
|
+
z.literal(1536)
|
|
718
|
+
]).optional()
|
|
912
719
|
});
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
var CohereEmbeddingModel = class
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
successfulResponseHandler: createJsonResponseHandler2(
|
|
974
|
-
cohereTextEmbeddingResponseSchema
|
|
975
|
-
),
|
|
976
|
-
abortSignal,
|
|
977
|
-
fetch: this.config.fetch
|
|
978
|
-
});
|
|
979
|
-
return {
|
|
980
|
-
warnings: [],
|
|
981
|
-
embeddings: response.embeddings.float,
|
|
982
|
-
usage: { tokens: response.meta.billed_units.input_tokens },
|
|
983
|
-
response: { headers: responseHeaders, body: rawValue }
|
|
984
|
-
};
|
|
985
|
-
}
|
|
720
|
+
//#endregion
|
|
721
|
+
//#region src/cohere-embedding-model.ts
|
|
722
|
+
var CohereEmbeddingModel = class CohereEmbeddingModel {
|
|
723
|
+
static [WORKFLOW_SERIALIZE](model) {
|
|
724
|
+
return serializeModelOptions({
|
|
725
|
+
modelId: model.modelId,
|
|
726
|
+
config: model.config
|
|
727
|
+
});
|
|
728
|
+
}
|
|
729
|
+
static [WORKFLOW_DESERIALIZE](options) {
|
|
730
|
+
return new CohereEmbeddingModel(options.modelId, options.config);
|
|
731
|
+
}
|
|
732
|
+
constructor(modelId, config) {
|
|
733
|
+
this.specificationVersion = "v4";
|
|
734
|
+
this.maxEmbeddingsPerCall = 96;
|
|
735
|
+
this.supportsParallelCalls = true;
|
|
736
|
+
this.modelId = modelId;
|
|
737
|
+
this.config = config;
|
|
738
|
+
}
|
|
739
|
+
get provider() {
|
|
740
|
+
return this.config.provider;
|
|
741
|
+
}
|
|
742
|
+
async doEmbed({ values, headers, abortSignal, providerOptions }) {
|
|
743
|
+
const embeddingOptions = await parseProviderOptions({
|
|
744
|
+
provider: "cohere",
|
|
745
|
+
providerOptions,
|
|
746
|
+
schema: cohereEmbeddingModelOptions
|
|
747
|
+
});
|
|
748
|
+
if (values.length > this.maxEmbeddingsPerCall) throw new TooManyEmbeddingValuesForCallError({
|
|
749
|
+
provider: this.provider,
|
|
750
|
+
modelId: this.modelId,
|
|
751
|
+
maxEmbeddingsPerCall: this.maxEmbeddingsPerCall,
|
|
752
|
+
values
|
|
753
|
+
});
|
|
754
|
+
const { responseHeaders, value: response, rawValue } = await postJsonToApi({
|
|
755
|
+
url: `${this.config.baseURL}/embed`,
|
|
756
|
+
headers: combineHeaders(this.config.headers?.(), headers),
|
|
757
|
+
body: {
|
|
758
|
+
model: this.modelId,
|
|
759
|
+
embedding_types: ["float"],
|
|
760
|
+
texts: values,
|
|
761
|
+
input_type: embeddingOptions?.inputType ?? "search_query",
|
|
762
|
+
truncate: embeddingOptions?.truncate,
|
|
763
|
+
output_dimension: embeddingOptions?.outputDimension
|
|
764
|
+
},
|
|
765
|
+
failedResponseHandler: cohereFailedResponseHandler,
|
|
766
|
+
successfulResponseHandler: createJsonResponseHandler(cohereTextEmbeddingResponseSchema),
|
|
767
|
+
abortSignal,
|
|
768
|
+
fetch: this.config.fetch
|
|
769
|
+
});
|
|
770
|
+
return {
|
|
771
|
+
warnings: [],
|
|
772
|
+
embeddings: response.embeddings.float,
|
|
773
|
+
usage: { tokens: response.meta.billed_units.input_tokens },
|
|
774
|
+
response: {
|
|
775
|
+
headers: responseHeaders,
|
|
776
|
+
body: rawValue
|
|
777
|
+
}
|
|
778
|
+
};
|
|
779
|
+
}
|
|
986
780
|
};
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
}),
|
|
991
|
-
meta: z5.object({
|
|
992
|
-
billed_units: z5.object({
|
|
993
|
-
input_tokens: z5.number()
|
|
994
|
-
})
|
|
995
|
-
})
|
|
781
|
+
const cohereTextEmbeddingResponseSchema = z.object({
|
|
782
|
+
embeddings: z.object({ float: z.array(z.array(z.number())) }),
|
|
783
|
+
meta: z.object({ billed_units: z.object({ input_tokens: z.number() }) })
|
|
996
784
|
});
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
}
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
index: z6.number(),
|
|
1016
|
-
relevance_score: z6.number()
|
|
1017
|
-
})
|
|
1018
|
-
),
|
|
1019
|
-
meta: z6.any()
|
|
1020
|
-
})
|
|
1021
|
-
)
|
|
1022
|
-
);
|
|
1023
|
-
|
|
1024
|
-
// src/reranking/cohere-reranking-model-options.ts
|
|
1025
|
-
import {
|
|
1026
|
-
lazySchema as lazySchema2,
|
|
1027
|
-
zodSchema as zodSchema2
|
|
1028
|
-
} from "@ai-sdk/provider-utils";
|
|
1029
|
-
import { z as z7 } from "zod/v4";
|
|
1030
|
-
var cohereRerankingModelOptionsSchema = lazySchema2(
|
|
1031
|
-
() => zodSchema2(
|
|
1032
|
-
z7.object({
|
|
1033
|
-
maxTokensPerDoc: z7.number().optional(),
|
|
1034
|
-
priority: z7.number().optional()
|
|
1035
|
-
})
|
|
1036
|
-
)
|
|
1037
|
-
);
|
|
1038
|
-
|
|
1039
|
-
// src/reranking/cohere-reranking-model.ts
|
|
785
|
+
//#endregion
|
|
786
|
+
//#region src/reranking/cohere-reranking-api.ts
|
|
787
|
+
const cohereRerankingResponseSchema = lazySchema(() => zodSchema(z.object({
|
|
788
|
+
id: z.string().nullish(),
|
|
789
|
+
results: z.array(z.object({
|
|
790
|
+
index: z.number(),
|
|
791
|
+
relevance_score: z.number()
|
|
792
|
+
})),
|
|
793
|
+
meta: z.any()
|
|
794
|
+
})));
|
|
795
|
+
//#endregion
|
|
796
|
+
//#region src/reranking/cohere-reranking-model-options.ts
|
|
797
|
+
const cohereRerankingModelOptionsSchema = lazySchema(() => zodSchema(z.object({
|
|
798
|
+
maxTokensPerDoc: z.number().optional(),
|
|
799
|
+
priority: z.number().optional()
|
|
800
|
+
})));
|
|
801
|
+
//#endregion
|
|
802
|
+
//#region src/reranking/cohere-reranking-model.ts
|
|
1040
803
|
var CohereRerankingModel = class {
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
abortSignal,
|
|
1091
|
-
fetch: this.config.fetch
|
|
1092
|
-
});
|
|
1093
|
-
return {
|
|
1094
|
-
ranking: response.results.map((result) => ({
|
|
1095
|
-
index: result.index,
|
|
1096
|
-
relevanceScore: result.relevance_score
|
|
1097
|
-
})),
|
|
1098
|
-
warnings,
|
|
1099
|
-
response: {
|
|
1100
|
-
id: response.id ?? void 0,
|
|
1101
|
-
headers: responseHeaders,
|
|
1102
|
-
body: rawValue
|
|
1103
|
-
}
|
|
1104
|
-
};
|
|
1105
|
-
}
|
|
804
|
+
constructor(modelId, config) {
|
|
805
|
+
this.specificationVersion = "v4";
|
|
806
|
+
this.modelId = modelId;
|
|
807
|
+
this.config = config;
|
|
808
|
+
}
|
|
809
|
+
get provider() {
|
|
810
|
+
return this.config.provider;
|
|
811
|
+
}
|
|
812
|
+
async doRerank({ documents, headers, query, topN, abortSignal, providerOptions }) {
|
|
813
|
+
const rerankingOptions = await parseProviderOptions({
|
|
814
|
+
provider: "cohere",
|
|
815
|
+
providerOptions,
|
|
816
|
+
schema: cohereRerankingModelOptionsSchema
|
|
817
|
+
});
|
|
818
|
+
const warnings = [];
|
|
819
|
+
if (documents.type === "object") warnings.push({
|
|
820
|
+
type: "compatibility",
|
|
821
|
+
feature: "object documents",
|
|
822
|
+
details: "Object documents are converted to strings."
|
|
823
|
+
});
|
|
824
|
+
const { responseHeaders, value: response, rawValue } = await postJsonToApi({
|
|
825
|
+
url: `${this.config.baseURL}/rerank`,
|
|
826
|
+
headers: combineHeaders(this.config.headers(), headers),
|
|
827
|
+
body: {
|
|
828
|
+
model: this.modelId,
|
|
829
|
+
query,
|
|
830
|
+
documents: documents.type === "text" ? documents.values : documents.values.map((value) => JSON.stringify(value)),
|
|
831
|
+
top_n: topN,
|
|
832
|
+
max_tokens_per_doc: rerankingOptions?.maxTokensPerDoc,
|
|
833
|
+
priority: rerankingOptions?.priority
|
|
834
|
+
},
|
|
835
|
+
failedResponseHandler: cohereFailedResponseHandler,
|
|
836
|
+
successfulResponseHandler: createJsonResponseHandler(cohereRerankingResponseSchema),
|
|
837
|
+
abortSignal,
|
|
838
|
+
fetch: this.config.fetch
|
|
839
|
+
});
|
|
840
|
+
return {
|
|
841
|
+
ranking: response.results.map((result) => ({
|
|
842
|
+
index: result.index,
|
|
843
|
+
relevanceScore: result.relevance_score
|
|
844
|
+
})),
|
|
845
|
+
warnings,
|
|
846
|
+
response: {
|
|
847
|
+
id: response.id ?? void 0,
|
|
848
|
+
headers: responseHeaders,
|
|
849
|
+
body: rawValue
|
|
850
|
+
}
|
|
851
|
+
};
|
|
852
|
+
}
|
|
1106
853
|
};
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
854
|
+
//#endregion
|
|
855
|
+
//#region src/version.ts
|
|
856
|
+
const VERSION = "4.0.54";
|
|
857
|
+
//#endregion
|
|
858
|
+
//#region src/cohere-provider.ts
|
|
859
|
+
/**
|
|
860
|
+
* Create a Cohere AI provider instance.
|
|
861
|
+
*/
|
|
1112
862
|
function createCohere(options = {}) {
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
provider.imageModel = (modelId) => {
|
|
1161
|
-
throw new NoSuchModelError({ modelId, modelType: "imageModel" });
|
|
1162
|
-
};
|
|
1163
|
-
return provider;
|
|
863
|
+
const baseURL = withoutTrailingSlash(options.baseURL) ?? "https://api.cohere.com/v2";
|
|
864
|
+
const getHeaders = () => withUserAgentSuffix({
|
|
865
|
+
Authorization: `Bearer ${loadApiKey({
|
|
866
|
+
apiKey: options.apiKey,
|
|
867
|
+
environmentVariableName: "COHERE_API_KEY",
|
|
868
|
+
description: "Cohere"
|
|
869
|
+
})}`,
|
|
870
|
+
...options.headers
|
|
871
|
+
}, `ai-sdk-cohere/${VERSION}`);
|
|
872
|
+
const createChatModel = (modelId) => new CohereChatLanguageModel(modelId, {
|
|
873
|
+
provider: "cohere.chat",
|
|
874
|
+
baseURL,
|
|
875
|
+
headers: getHeaders,
|
|
876
|
+
fetch: options.fetch,
|
|
877
|
+
generateId: options.generateId ?? generateId
|
|
878
|
+
});
|
|
879
|
+
const createEmbeddingModel = (modelId) => new CohereEmbeddingModel(modelId, {
|
|
880
|
+
provider: "cohere.textEmbedding",
|
|
881
|
+
baseURL,
|
|
882
|
+
headers: getHeaders,
|
|
883
|
+
fetch: options.fetch
|
|
884
|
+
});
|
|
885
|
+
const createRerankingModel = (modelId) => new CohereRerankingModel(modelId, {
|
|
886
|
+
provider: "cohere.reranking",
|
|
887
|
+
baseURL,
|
|
888
|
+
headers: getHeaders,
|
|
889
|
+
fetch: options.fetch
|
|
890
|
+
});
|
|
891
|
+
const provider = function(modelId) {
|
|
892
|
+
if (new.target) throw new Error("The Cohere model function cannot be called with the new keyword.");
|
|
893
|
+
return createChatModel(modelId);
|
|
894
|
+
};
|
|
895
|
+
provider.specificationVersion = "v4";
|
|
896
|
+
provider.languageModel = createChatModel;
|
|
897
|
+
provider.embedding = createEmbeddingModel;
|
|
898
|
+
provider.embeddingModel = createEmbeddingModel;
|
|
899
|
+
provider.textEmbedding = createEmbeddingModel;
|
|
900
|
+
provider.textEmbeddingModel = createEmbeddingModel;
|
|
901
|
+
provider.reranking = createRerankingModel;
|
|
902
|
+
provider.rerankingModel = createRerankingModel;
|
|
903
|
+
provider.imageModel = (modelId) => {
|
|
904
|
+
throw new NoSuchModelError({
|
|
905
|
+
modelId,
|
|
906
|
+
modelType: "imageModel"
|
|
907
|
+
});
|
|
908
|
+
};
|
|
909
|
+
return provider;
|
|
1164
910
|
}
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
};
|
|
911
|
+
/**
|
|
912
|
+
* Default Cohere provider instance.
|
|
913
|
+
*/
|
|
914
|
+
const cohere = createCohere();
|
|
915
|
+
//#endregion
|
|
916
|
+
export { VERSION, cohere, createCohere };
|
|
917
|
+
|
|
1171
918
|
//# sourceMappingURL=index.js.map
|