@langchain/google-common 0.1.0 → 0.1.2
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/dist/chat_models.cjs +29 -49
- package/dist/chat_models.d.ts +6 -7
- package/dist/chat_models.js +29 -50
- package/dist/connection.cjs +178 -98
- package/dist/connection.d.ts +47 -16
- package/dist/connection.js +174 -97
- package/dist/llms.cjs +2 -2
- package/dist/llms.js +2 -2
- package/dist/types-anthropic.cjs +2 -0
- package/dist/types-anthropic.d.ts +159 -0
- package/dist/types-anthropic.js +1 -0
- package/dist/types.cjs +54 -0
- package/dist/types.d.ts +68 -6
- package/dist/types.js +39 -1
- package/dist/utils/anthropic.cjs +541 -0
- package/dist/utils/anthropic.d.ts +4 -0
- package/dist/utils/anthropic.js +535 -0
- package/dist/utils/common.cjs +20 -1
- package/dist/utils/common.d.ts +3 -2
- package/dist/utils/common.js +18 -0
- package/dist/utils/gemini.cjs +306 -127
- package/dist/utils/gemini.d.ts +4 -14
- package/dist/utils/gemini.js +303 -124
- package/dist/utils/stream.cjs +184 -4
- package/dist/utils/stream.d.ts +73 -3
- package/dist/utils/stream.js +178 -3
- package/package.json +1 -1
|
@@ -0,0 +1,541 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isModelClaude = exports.validateClaudeParams = exports.getAnthropicAPI = void 0;
|
|
4
|
+
const outputs_1 = require("@langchain/core/outputs");
|
|
5
|
+
const messages_1 = require("@langchain/core/messages");
|
|
6
|
+
function getAnthropicAPI(config) {
|
|
7
|
+
function partToString(part) {
|
|
8
|
+
return "text" in part ? part.text : "";
|
|
9
|
+
}
|
|
10
|
+
function messageToString(message) {
|
|
11
|
+
const content = message?.content ?? [];
|
|
12
|
+
const ret = content.reduce((acc, part) => {
|
|
13
|
+
const str = partToString(part);
|
|
14
|
+
return acc + str;
|
|
15
|
+
}, "");
|
|
16
|
+
return ret;
|
|
17
|
+
}
|
|
18
|
+
function responseToString(response) {
|
|
19
|
+
const data = response.data;
|
|
20
|
+
switch (data?.type) {
|
|
21
|
+
case "message":
|
|
22
|
+
return messageToString(data);
|
|
23
|
+
default:
|
|
24
|
+
throw Error(`Unknown type: ${data?.type}`);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Normalize the AIMessageChunk.
|
|
29
|
+
* If the fields are just a string - use that as content.
|
|
30
|
+
* If the content is an array of just text fields, turn them into a string.
|
|
31
|
+
* @param fields
|
|
32
|
+
*/
|
|
33
|
+
function newAIMessageChunk(fields) {
|
|
34
|
+
if (typeof fields === "string") {
|
|
35
|
+
return new messages_1.AIMessageChunk(fields);
|
|
36
|
+
}
|
|
37
|
+
const ret = {
|
|
38
|
+
...fields,
|
|
39
|
+
};
|
|
40
|
+
if (Array.isArray(fields?.content)) {
|
|
41
|
+
let str = "";
|
|
42
|
+
fields.content.forEach((val) => {
|
|
43
|
+
if (str !== undefined && val.type === "text") {
|
|
44
|
+
str = `${str}${val.text}`;
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
str = undefined;
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
if (str) {
|
|
51
|
+
ret.content = str;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return new messages_1.AIMessageChunk(ret);
|
|
55
|
+
}
|
|
56
|
+
function textContentToMessageFields(textContent) {
|
|
57
|
+
return {
|
|
58
|
+
content: [textContent],
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function toolUseContentToMessageFields(toolUseContent) {
|
|
62
|
+
const tool = {
|
|
63
|
+
id: toolUseContent.id,
|
|
64
|
+
name: toolUseContent.name,
|
|
65
|
+
type: "tool_call",
|
|
66
|
+
args: toolUseContent.input,
|
|
67
|
+
};
|
|
68
|
+
return {
|
|
69
|
+
content: [],
|
|
70
|
+
tool_calls: [tool],
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
function anthropicContentToMessageFields(anthropicContent) {
|
|
74
|
+
const type = anthropicContent?.type;
|
|
75
|
+
switch (type) {
|
|
76
|
+
case "text":
|
|
77
|
+
return textContentToMessageFields(anthropicContent);
|
|
78
|
+
case "tool_use":
|
|
79
|
+
return toolUseContentToMessageFields(anthropicContent);
|
|
80
|
+
default:
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
function contentToMessage(anthropicContent) {
|
|
85
|
+
const complexContent = [];
|
|
86
|
+
const toolCalls = [];
|
|
87
|
+
anthropicContent.forEach((ac) => {
|
|
88
|
+
const messageFields = anthropicContentToMessageFields(ac);
|
|
89
|
+
if (messageFields?.content) {
|
|
90
|
+
complexContent.push(...messageFields.content);
|
|
91
|
+
}
|
|
92
|
+
if (messageFields?.tool_calls) {
|
|
93
|
+
toolCalls.push(...messageFields.tool_calls);
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
const ret = {
|
|
97
|
+
content: complexContent,
|
|
98
|
+
tool_calls: toolCalls,
|
|
99
|
+
};
|
|
100
|
+
return newAIMessageChunk(ret);
|
|
101
|
+
}
|
|
102
|
+
function messageToGenerationInfo(message) {
|
|
103
|
+
const usage = message?.usage;
|
|
104
|
+
const usageMetadata = {
|
|
105
|
+
input_tokens: usage?.input_tokens ?? 0,
|
|
106
|
+
output_tokens: usage?.output_tokens ?? 0,
|
|
107
|
+
total_tokens: (usage?.input_tokens ?? 0) + (usage?.output_tokens ?? 0),
|
|
108
|
+
};
|
|
109
|
+
return {
|
|
110
|
+
usage_metadata: usageMetadata,
|
|
111
|
+
finish_reason: message.stop_reason,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
function messageToChatGeneration(responseMessage) {
|
|
115
|
+
const content = responseMessage?.content ?? [];
|
|
116
|
+
const text = messageToString(responseMessage);
|
|
117
|
+
const message = contentToMessage(content);
|
|
118
|
+
const generationInfo = messageToGenerationInfo(responseMessage);
|
|
119
|
+
return new outputs_1.ChatGenerationChunk({
|
|
120
|
+
text,
|
|
121
|
+
message,
|
|
122
|
+
generationInfo,
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
function messageStartToChatGeneration(event) {
|
|
126
|
+
const responseMessage = event.message;
|
|
127
|
+
return messageToChatGeneration(responseMessage);
|
|
128
|
+
}
|
|
129
|
+
function messageDeltaToChatGeneration(event) {
|
|
130
|
+
const responseMessage = event.delta;
|
|
131
|
+
return messageToChatGeneration(responseMessage);
|
|
132
|
+
}
|
|
133
|
+
function contentBlockStartTextToChatGeneration(event) {
|
|
134
|
+
const content = event.content_block;
|
|
135
|
+
const message = contentToMessage([content]);
|
|
136
|
+
if (!message) {
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
const text = "text" in content ? content.text : "";
|
|
140
|
+
return new outputs_1.ChatGenerationChunk({
|
|
141
|
+
message,
|
|
142
|
+
text,
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
function contentBlockStartToolUseToChatGeneration(event) {
|
|
146
|
+
const contentBlock = event.content_block;
|
|
147
|
+
const text = "";
|
|
148
|
+
const toolChunk = {
|
|
149
|
+
type: "tool_call_chunk",
|
|
150
|
+
index: event.index,
|
|
151
|
+
name: contentBlock.name,
|
|
152
|
+
id: contentBlock.id,
|
|
153
|
+
};
|
|
154
|
+
if (typeof contentBlock.input === "object" &&
|
|
155
|
+
Object.keys(contentBlock.input).length > 0) {
|
|
156
|
+
toolChunk.args = JSON.stringify(contentBlock.input);
|
|
157
|
+
}
|
|
158
|
+
const toolChunks = [toolChunk];
|
|
159
|
+
const content = [
|
|
160
|
+
{
|
|
161
|
+
index: event.index,
|
|
162
|
+
...contentBlock,
|
|
163
|
+
},
|
|
164
|
+
];
|
|
165
|
+
const messageFields = {
|
|
166
|
+
content,
|
|
167
|
+
tool_call_chunks: toolChunks,
|
|
168
|
+
};
|
|
169
|
+
const message = newAIMessageChunk(messageFields);
|
|
170
|
+
return new outputs_1.ChatGenerationChunk({
|
|
171
|
+
message,
|
|
172
|
+
text,
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
function contentBlockStartToChatGeneration(event) {
|
|
176
|
+
switch (event.content_block.type) {
|
|
177
|
+
case "text":
|
|
178
|
+
return contentBlockStartTextToChatGeneration(event);
|
|
179
|
+
case "tool_use":
|
|
180
|
+
return contentBlockStartToolUseToChatGeneration(event);
|
|
181
|
+
default:
|
|
182
|
+
console.warn(`Unexpected start content_block type: ${JSON.stringify(event)}`);
|
|
183
|
+
return null;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
function contentBlockDeltaTextToChatGeneration(event) {
|
|
187
|
+
const delta = event.delta;
|
|
188
|
+
const text = delta?.text;
|
|
189
|
+
const message = newAIMessageChunk(text);
|
|
190
|
+
return new outputs_1.ChatGenerationChunk({
|
|
191
|
+
message,
|
|
192
|
+
text,
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
function contentBlockDeltaInputJsonDeltaToChatGeneration(event) {
|
|
196
|
+
const delta = event.delta;
|
|
197
|
+
const text = "";
|
|
198
|
+
const toolChunks = [
|
|
199
|
+
{
|
|
200
|
+
index: event.index,
|
|
201
|
+
args: delta.partial_json,
|
|
202
|
+
},
|
|
203
|
+
];
|
|
204
|
+
const content = [
|
|
205
|
+
{
|
|
206
|
+
index: event.index,
|
|
207
|
+
...delta,
|
|
208
|
+
},
|
|
209
|
+
];
|
|
210
|
+
const messageFields = {
|
|
211
|
+
content,
|
|
212
|
+
tool_call_chunks: toolChunks,
|
|
213
|
+
};
|
|
214
|
+
const message = newAIMessageChunk(messageFields);
|
|
215
|
+
return new outputs_1.ChatGenerationChunk({
|
|
216
|
+
message,
|
|
217
|
+
text,
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
function contentBlockDeltaToChatGeneration(event) {
|
|
221
|
+
switch (event.delta.type) {
|
|
222
|
+
case "text_delta":
|
|
223
|
+
return contentBlockDeltaTextToChatGeneration(event);
|
|
224
|
+
case "input_json_delta":
|
|
225
|
+
return contentBlockDeltaInputJsonDeltaToChatGeneration(event);
|
|
226
|
+
default:
|
|
227
|
+
console.warn(`Unexpected delta content_block type: ${JSON.stringify(event)}`);
|
|
228
|
+
return null;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
function responseToChatGeneration(response) {
|
|
232
|
+
const data = response.data;
|
|
233
|
+
switch (data.type) {
|
|
234
|
+
case "message":
|
|
235
|
+
return messageToChatGeneration(data);
|
|
236
|
+
case "message_start":
|
|
237
|
+
return messageStartToChatGeneration(data);
|
|
238
|
+
case "message_delta":
|
|
239
|
+
return messageDeltaToChatGeneration(data);
|
|
240
|
+
case "content_block_start":
|
|
241
|
+
return contentBlockStartToChatGeneration(data);
|
|
242
|
+
case "content_block_delta":
|
|
243
|
+
return contentBlockDeltaToChatGeneration(data);
|
|
244
|
+
case "ping":
|
|
245
|
+
case "message_stop":
|
|
246
|
+
case "content_block_stop":
|
|
247
|
+
// These are ignorable
|
|
248
|
+
return null;
|
|
249
|
+
case "error":
|
|
250
|
+
throw new Error(`Error while streaming results: ${JSON.stringify(data)}`);
|
|
251
|
+
default:
|
|
252
|
+
// We don't know what type this is, but Anthropic may have added
|
|
253
|
+
// new ones without telling us. Don't error, but don't use them.
|
|
254
|
+
console.warn("Unknown data for responseToChatGeneration", data);
|
|
255
|
+
// throw new Error(`Unknown response type: ${data.type}`);
|
|
256
|
+
return null;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
function chunkToString(chunk) {
|
|
260
|
+
if (chunk === null) {
|
|
261
|
+
return "";
|
|
262
|
+
}
|
|
263
|
+
else if (typeof chunk.content === "string") {
|
|
264
|
+
return chunk.content;
|
|
265
|
+
}
|
|
266
|
+
else if (chunk.content.length === 0) {
|
|
267
|
+
return "";
|
|
268
|
+
}
|
|
269
|
+
else if (chunk.content[0].type === "text") {
|
|
270
|
+
return chunk.content[0].text;
|
|
271
|
+
}
|
|
272
|
+
else {
|
|
273
|
+
throw new Error(`Unexpected chunk: ${chunk}`);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
function responseToBaseMessage(response) {
|
|
277
|
+
const data = response.data;
|
|
278
|
+
const content = data?.content ?? [];
|
|
279
|
+
return contentToMessage(content);
|
|
280
|
+
}
|
|
281
|
+
function responseToChatResult(response) {
|
|
282
|
+
const message = response.data;
|
|
283
|
+
const generations = [];
|
|
284
|
+
const gen = responseToChatGeneration(response);
|
|
285
|
+
if (gen) {
|
|
286
|
+
generations.push(gen);
|
|
287
|
+
}
|
|
288
|
+
const llmOutput = messageToGenerationInfo(message);
|
|
289
|
+
return {
|
|
290
|
+
generations,
|
|
291
|
+
llmOutput,
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
function formatAnthropicVersion() {
|
|
295
|
+
return config?.version ?? "vertex-2023-10-16";
|
|
296
|
+
}
|
|
297
|
+
function textContentToAnthropicContent(content) {
|
|
298
|
+
return content;
|
|
299
|
+
}
|
|
300
|
+
function extractMimeType(str) {
|
|
301
|
+
if (str.startsWith("data:")) {
|
|
302
|
+
return {
|
|
303
|
+
media_type: str.split(":")[1].split(";")[0],
|
|
304
|
+
data: str.split(",")[1],
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
return null;
|
|
308
|
+
}
|
|
309
|
+
function imageContentToAnthropicContent(content) {
|
|
310
|
+
const dataUrl = content.image_url;
|
|
311
|
+
const url = typeof dataUrl === "string" ? dataUrl : dataUrl?.url;
|
|
312
|
+
const urlInfo = extractMimeType(url);
|
|
313
|
+
if (!urlInfo) {
|
|
314
|
+
return undefined;
|
|
315
|
+
}
|
|
316
|
+
return {
|
|
317
|
+
type: "image",
|
|
318
|
+
source: {
|
|
319
|
+
type: "base64",
|
|
320
|
+
...urlInfo,
|
|
321
|
+
},
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
function contentComplexToAnthropicContent(content) {
|
|
325
|
+
const type = content?.type;
|
|
326
|
+
switch (type) {
|
|
327
|
+
case "text":
|
|
328
|
+
return textContentToAnthropicContent(content);
|
|
329
|
+
case "image_url":
|
|
330
|
+
return imageContentToAnthropicContent(content);
|
|
331
|
+
default:
|
|
332
|
+
console.warn(`Unexpected content type: ${type}`);
|
|
333
|
+
return undefined;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
function contentToAnthropicContent(content) {
|
|
337
|
+
const ret = [];
|
|
338
|
+
const ca = typeof content === "string" ? [{ type: "text", text: content }] : content;
|
|
339
|
+
ca.forEach((complex) => {
|
|
340
|
+
const ac = contentComplexToAnthropicContent(complex);
|
|
341
|
+
if (ac) {
|
|
342
|
+
ret.push(ac);
|
|
343
|
+
}
|
|
344
|
+
});
|
|
345
|
+
return ret;
|
|
346
|
+
}
|
|
347
|
+
function baseRoleToAnthropicMessage(base, role) {
|
|
348
|
+
const content = contentToAnthropicContent(base.content);
|
|
349
|
+
return {
|
|
350
|
+
role,
|
|
351
|
+
content,
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
function toolMessageToAnthropicMessage(base) {
|
|
355
|
+
const role = "user";
|
|
356
|
+
const toolUseId = base.tool_call_id;
|
|
357
|
+
const toolContent = contentToAnthropicContent(base.content);
|
|
358
|
+
const content = [
|
|
359
|
+
{
|
|
360
|
+
type: "tool_result",
|
|
361
|
+
tool_use_id: toolUseId,
|
|
362
|
+
content: toolContent,
|
|
363
|
+
},
|
|
364
|
+
];
|
|
365
|
+
return {
|
|
366
|
+
role,
|
|
367
|
+
content,
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
function baseToAnthropicMessage(base) {
|
|
371
|
+
const type = base.getType();
|
|
372
|
+
switch (type) {
|
|
373
|
+
case "human":
|
|
374
|
+
return baseRoleToAnthropicMessage(base, "user");
|
|
375
|
+
case "ai":
|
|
376
|
+
return baseRoleToAnthropicMessage(base, "assistant");
|
|
377
|
+
case "tool":
|
|
378
|
+
return toolMessageToAnthropicMessage(base);
|
|
379
|
+
default:
|
|
380
|
+
return undefined;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
function formatMessages(input) {
|
|
384
|
+
const ret = [];
|
|
385
|
+
input.forEach((baseMessage) => {
|
|
386
|
+
const anthropicMessage = baseToAnthropicMessage(baseMessage);
|
|
387
|
+
if (anthropicMessage) {
|
|
388
|
+
ret.push(anthropicMessage);
|
|
389
|
+
}
|
|
390
|
+
});
|
|
391
|
+
return ret;
|
|
392
|
+
}
|
|
393
|
+
function formatSettings(parameters) {
|
|
394
|
+
const ret = {
|
|
395
|
+
stream: parameters?.streaming ?? false,
|
|
396
|
+
max_tokens: parameters?.maxOutputTokens ?? 8192,
|
|
397
|
+
};
|
|
398
|
+
if (parameters.topP) {
|
|
399
|
+
ret.top_p = parameters.topP;
|
|
400
|
+
}
|
|
401
|
+
if (parameters.topK) {
|
|
402
|
+
ret.top_k = parameters.topK;
|
|
403
|
+
}
|
|
404
|
+
if (parameters.temperature) {
|
|
405
|
+
ret.temperature = parameters.temperature;
|
|
406
|
+
}
|
|
407
|
+
if (parameters.stopSequences) {
|
|
408
|
+
ret.stop_sequences = parameters.stopSequences;
|
|
409
|
+
}
|
|
410
|
+
return ret;
|
|
411
|
+
}
|
|
412
|
+
function contentComplexArrayToText(contentArray) {
|
|
413
|
+
let ret = "";
|
|
414
|
+
contentArray.forEach((content) => {
|
|
415
|
+
const contentType = content?.type;
|
|
416
|
+
if (contentType === "text") {
|
|
417
|
+
const textContent = content;
|
|
418
|
+
ret = `${ret}\n${textContent.text}`;
|
|
419
|
+
}
|
|
420
|
+
});
|
|
421
|
+
return ret;
|
|
422
|
+
}
|
|
423
|
+
function formatSystem(input) {
|
|
424
|
+
let ret = "";
|
|
425
|
+
input.forEach((message) => {
|
|
426
|
+
if (message._getType() === "system") {
|
|
427
|
+
const content = message?.content;
|
|
428
|
+
const contentString = typeof content === "string"
|
|
429
|
+
? content
|
|
430
|
+
: contentComplexArrayToText(content);
|
|
431
|
+
ret = `${ret}\n${contentString}`;
|
|
432
|
+
}
|
|
433
|
+
});
|
|
434
|
+
return ret;
|
|
435
|
+
}
|
|
436
|
+
function formatGeminiTool(tool) {
|
|
437
|
+
if (Object.hasOwn(tool, "functionDeclarations")) {
|
|
438
|
+
const funcs = tool?.functionDeclarations ?? [];
|
|
439
|
+
return funcs.map((func) => {
|
|
440
|
+
const inputSchema = func.parameters;
|
|
441
|
+
return {
|
|
442
|
+
// type: "tool", // This may only be valid for models 20241022+
|
|
443
|
+
name: func.name,
|
|
444
|
+
description: func.description,
|
|
445
|
+
input_schema: inputSchema,
|
|
446
|
+
};
|
|
447
|
+
});
|
|
448
|
+
}
|
|
449
|
+
else {
|
|
450
|
+
console.warn(`Unable to format GeminiTool: ${JSON.stringify(tool, null, 1)}`);
|
|
451
|
+
return [];
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
function formatTool(tool) {
|
|
455
|
+
if (Object.hasOwn(tool, "name")) {
|
|
456
|
+
return [tool];
|
|
457
|
+
}
|
|
458
|
+
else {
|
|
459
|
+
return formatGeminiTool(tool);
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
function formatTools(parameters) {
|
|
463
|
+
const tools = parameters?.tools ?? [];
|
|
464
|
+
const ret = [];
|
|
465
|
+
tools.forEach((tool) => {
|
|
466
|
+
const anthropicTools = formatTool(tool);
|
|
467
|
+
anthropicTools.forEach((anthropicTool) => {
|
|
468
|
+
if (anthropicTool) {
|
|
469
|
+
ret.push(anthropicTool);
|
|
470
|
+
}
|
|
471
|
+
});
|
|
472
|
+
});
|
|
473
|
+
return ret;
|
|
474
|
+
}
|
|
475
|
+
function formatToolChoice(parameters) {
|
|
476
|
+
const choice = parameters?.tool_choice;
|
|
477
|
+
if (!choice) {
|
|
478
|
+
return undefined;
|
|
479
|
+
}
|
|
480
|
+
else if (typeof choice === "object") {
|
|
481
|
+
return choice;
|
|
482
|
+
}
|
|
483
|
+
else {
|
|
484
|
+
switch (choice) {
|
|
485
|
+
case "any":
|
|
486
|
+
case "auto":
|
|
487
|
+
return {
|
|
488
|
+
type: choice,
|
|
489
|
+
};
|
|
490
|
+
case "none":
|
|
491
|
+
return undefined;
|
|
492
|
+
default:
|
|
493
|
+
return {
|
|
494
|
+
type: "tool",
|
|
495
|
+
name: choice,
|
|
496
|
+
};
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
async function formatData(input, parameters) {
|
|
501
|
+
const typedInput = input;
|
|
502
|
+
const anthropicVersion = formatAnthropicVersion();
|
|
503
|
+
const messages = formatMessages(typedInput);
|
|
504
|
+
const settings = formatSettings(parameters);
|
|
505
|
+
const system = formatSystem(typedInput);
|
|
506
|
+
const tools = formatTools(parameters);
|
|
507
|
+
const toolChoice = formatToolChoice(parameters);
|
|
508
|
+
const ret = {
|
|
509
|
+
anthropic_version: anthropicVersion,
|
|
510
|
+
messages,
|
|
511
|
+
...settings,
|
|
512
|
+
};
|
|
513
|
+
if (tools && tools.length && parameters?.tool_choice !== "none") {
|
|
514
|
+
ret.tools = tools;
|
|
515
|
+
}
|
|
516
|
+
if (toolChoice) {
|
|
517
|
+
ret.tool_choice = toolChoice;
|
|
518
|
+
}
|
|
519
|
+
if (system?.length) {
|
|
520
|
+
ret.system = system;
|
|
521
|
+
}
|
|
522
|
+
return ret;
|
|
523
|
+
}
|
|
524
|
+
return {
|
|
525
|
+
responseToString,
|
|
526
|
+
responseToChatGeneration,
|
|
527
|
+
chunkToString,
|
|
528
|
+
responseToBaseMessage,
|
|
529
|
+
responseToChatResult,
|
|
530
|
+
formatData,
|
|
531
|
+
};
|
|
532
|
+
}
|
|
533
|
+
exports.getAnthropicAPI = getAnthropicAPI;
|
|
534
|
+
function validateClaudeParams(_params) {
|
|
535
|
+
// FIXME - validate the parameters
|
|
536
|
+
}
|
|
537
|
+
exports.validateClaudeParams = validateClaudeParams;
|
|
538
|
+
function isModelClaude(modelName) {
|
|
539
|
+
return modelName.toLowerCase().startsWith("claude");
|
|
540
|
+
}
|
|
541
|
+
exports.isModelClaude = isModelClaude;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { AnthropicAPIConfig, GoogleAIAPI, GoogleAIModelParams } from "../types.js";
|
|
2
|
+
export declare function getAnthropicAPI(config?: AnthropicAPIConfig): GoogleAIAPI;
|
|
3
|
+
export declare function validateClaudeParams(_params: GoogleAIModelParams): void;
|
|
4
|
+
export declare function isModelClaude(modelName: string): boolean;
|