@aigne/anthropic 0.1.0
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 +17 -0
- package/LICENSE.md +93 -0
- package/README.md +114 -0
- package/README.zh.md +114 -0
- package/lib/cjs/anthropic-chat-model.d.ts +113 -0
- package/lib/cjs/anthropic-chat-model.js +317 -0
- package/lib/cjs/index.d.ts +1 -0
- package/lib/cjs/index.js +17 -0
- package/lib/cjs/package.json +1 -0
- package/lib/dts/anthropic-chat-model.d.ts +113 -0
- package/lib/dts/index.d.ts +1 -0
- package/lib/esm/anthropic-chat-model.d.ts +113 -0
- package/lib/esm/anthropic-chat-model.js +310 -0
- package/lib/esm/index.d.ts +1 -0
- package/lib/esm/index.js +1 -0
- package/lib/esm/package.json +1 -0
- package/package.json +55 -0
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.AnthropicChatModel = exports.claudeChatModelOptionsSchema = void 0;
|
|
7
|
+
const core_1 = require("@aigne/core");
|
|
8
|
+
const json_schema_js_1 = require("@aigne/core/utils/json-schema.js");
|
|
9
|
+
const model_utils_js_1 = require("@aigne/core/utils/model-utils.js");
|
|
10
|
+
const stream_utils_js_1 = require("@aigne/core/utils/stream-utils.js");
|
|
11
|
+
const type_utils_js_1 = require("@aigne/core/utils/type-utils.js");
|
|
12
|
+
const sdk_1 = __importDefault(require("@anthropic-ai/sdk"));
|
|
13
|
+
const zod_1 = require("zod");
|
|
14
|
+
const CHAT_MODEL_CLAUDE_DEFAULT_MODEL = "claude-3-7-sonnet-latest";
|
|
15
|
+
/**
|
|
16
|
+
* @hidden
|
|
17
|
+
*/
|
|
18
|
+
exports.claudeChatModelOptionsSchema = zod_1.z.object({
|
|
19
|
+
apiKey: zod_1.z.string().optional(),
|
|
20
|
+
model: zod_1.z.string().optional(),
|
|
21
|
+
modelOptions: zod_1.z
|
|
22
|
+
.object({
|
|
23
|
+
model: zod_1.z.string().optional(),
|
|
24
|
+
temperature: zod_1.z.number().optional(),
|
|
25
|
+
topP: zod_1.z.number().optional(),
|
|
26
|
+
frequencyPenalty: zod_1.z.number().optional(),
|
|
27
|
+
presencePenalty: zod_1.z.number().optional(),
|
|
28
|
+
parallelToolCalls: zod_1.z.boolean().optional().default(true),
|
|
29
|
+
})
|
|
30
|
+
.optional(),
|
|
31
|
+
});
|
|
32
|
+
/**
|
|
33
|
+
* Implementation of the ChatModel interface for Anthropic's Claude API
|
|
34
|
+
*
|
|
35
|
+
* This model provides access to Claude's capabilities including:
|
|
36
|
+
* - Text generation
|
|
37
|
+
* - Tool use
|
|
38
|
+
* - JSON structured output
|
|
39
|
+
*
|
|
40
|
+
* Default model: 'claude-3-7-sonnet-latest'
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* Here's how to create and use a Claude chat model:
|
|
44
|
+
* {@includeCode ../test/anthropic-chat-model.test.ts#example-anthropic-chat-model}
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* Here's an example with streaming response:
|
|
48
|
+
* {@includeCode ../test/anthropic-chat-model.test.ts#example-anthropic-chat-model-streaming-async-generator}
|
|
49
|
+
*/
|
|
50
|
+
class AnthropicChatModel extends core_1.ChatModel {
|
|
51
|
+
options;
|
|
52
|
+
constructor(options) {
|
|
53
|
+
if (options)
|
|
54
|
+
(0, type_utils_js_1.checkArguments)("AnthropicChatModel", exports.claudeChatModelOptionsSchema, options);
|
|
55
|
+
super();
|
|
56
|
+
this.options = options;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* @hidden
|
|
60
|
+
*/
|
|
61
|
+
_client;
|
|
62
|
+
get client() {
|
|
63
|
+
const apiKey = this.options?.apiKey || process.env.ANTHROPIC_API_KEY || process.env.CLAUDE_API_KEY;
|
|
64
|
+
if (!apiKey)
|
|
65
|
+
throw new Error("Api Key is required for AnthropicChatModel");
|
|
66
|
+
this._client ??= new sdk_1.default({ apiKey });
|
|
67
|
+
return this._client;
|
|
68
|
+
}
|
|
69
|
+
get modelOptions() {
|
|
70
|
+
return this.options?.modelOptions;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Process the input using Claude's chat model
|
|
74
|
+
* @param input - The input to process
|
|
75
|
+
* @returns The processed output from the model
|
|
76
|
+
*/
|
|
77
|
+
process(input) {
|
|
78
|
+
return this._process(input);
|
|
79
|
+
}
|
|
80
|
+
async _process(input) {
|
|
81
|
+
const model = this.options?.model || CHAT_MODEL_CLAUDE_DEFAULT_MODEL;
|
|
82
|
+
const disableParallelToolUse = input.modelOptions?.parallelToolCalls === false ||
|
|
83
|
+
this.modelOptions?.parallelToolCalls === false;
|
|
84
|
+
const body = {
|
|
85
|
+
model,
|
|
86
|
+
temperature: input.modelOptions?.temperature ?? this.modelOptions?.temperature,
|
|
87
|
+
top_p: input.modelOptions?.topP ?? this.modelOptions?.topP,
|
|
88
|
+
// TODO: make dynamic based on model https://docs.anthropic.com/en/docs/about-claude/models/all-models
|
|
89
|
+
max_tokens: /claude-3-[5|7]/.test(model) ? 8192 : 4096,
|
|
90
|
+
...convertMessages(input),
|
|
91
|
+
...convertTools({ ...input, disableParallelToolUse }),
|
|
92
|
+
};
|
|
93
|
+
const stream = this.client.messages.stream({
|
|
94
|
+
...body,
|
|
95
|
+
stream: true,
|
|
96
|
+
});
|
|
97
|
+
if (input.responseFormat?.type !== "json_schema") {
|
|
98
|
+
return this.extractResultFromAnthropicStream(stream, true);
|
|
99
|
+
}
|
|
100
|
+
const result = await this.extractResultFromAnthropicStream(stream);
|
|
101
|
+
// Claude doesn't support json_schema response and tool calls in the same request,
|
|
102
|
+
// so we need to make a separate request for json_schema response when the tool calls is empty
|
|
103
|
+
if (!result.toolCalls?.length && input.responseFormat?.type === "json_schema") {
|
|
104
|
+
const output = await this.requestStructuredOutput(body, input.responseFormat);
|
|
105
|
+
return {
|
|
106
|
+
...output,
|
|
107
|
+
// merge usage from both requests
|
|
108
|
+
usage: (0, model_utils_js_1.mergeUsage)(result.usage, output.usage),
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
return result;
|
|
112
|
+
}
|
|
113
|
+
async extractResultFromAnthropicStream(stream, streaming) {
|
|
114
|
+
const logs = [];
|
|
115
|
+
const result = new ReadableStream({
|
|
116
|
+
async start(controller) {
|
|
117
|
+
try {
|
|
118
|
+
const toolCalls = [];
|
|
119
|
+
let usage;
|
|
120
|
+
let model;
|
|
121
|
+
for await (const chunk of stream) {
|
|
122
|
+
if (chunk.type === "message_start") {
|
|
123
|
+
if (!model) {
|
|
124
|
+
model = chunk.message.model;
|
|
125
|
+
controller.enqueue({ delta: { json: { model } } });
|
|
126
|
+
}
|
|
127
|
+
const { input_tokens, output_tokens } = chunk.message.usage;
|
|
128
|
+
usage = {
|
|
129
|
+
inputTokens: input_tokens,
|
|
130
|
+
outputTokens: output_tokens,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
if (chunk.type === "message_delta" && usage) {
|
|
134
|
+
usage.outputTokens = chunk.usage.output_tokens;
|
|
135
|
+
}
|
|
136
|
+
logs.push(JSON.stringify(chunk));
|
|
137
|
+
// handle streaming text
|
|
138
|
+
if (chunk.type === "content_block_delta" && chunk.delta.type === "text_delta") {
|
|
139
|
+
controller.enqueue({ delta: { text: { text: chunk.delta.text } } });
|
|
140
|
+
}
|
|
141
|
+
if (chunk.type === "content_block_start" && chunk.content_block.type === "tool_use") {
|
|
142
|
+
toolCalls[chunk.index] = {
|
|
143
|
+
type: "function",
|
|
144
|
+
id: chunk.content_block.id,
|
|
145
|
+
function: {
|
|
146
|
+
name: chunk.content_block.name,
|
|
147
|
+
arguments: {},
|
|
148
|
+
},
|
|
149
|
+
args: "",
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
if (chunk.type === "content_block_delta" && chunk.delta.type === "input_json_delta") {
|
|
153
|
+
const call = toolCalls[chunk.index];
|
|
154
|
+
if (!call)
|
|
155
|
+
throw new Error("Tool call not found");
|
|
156
|
+
call.args += chunk.delta.partial_json;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
controller.enqueue({ delta: { json: { usage } } });
|
|
160
|
+
if (toolCalls.length) {
|
|
161
|
+
controller.enqueue({
|
|
162
|
+
delta: {
|
|
163
|
+
json: {
|
|
164
|
+
toolCalls: toolCalls
|
|
165
|
+
.map(({ args, ...c }) => ({
|
|
166
|
+
...c,
|
|
167
|
+
function: {
|
|
168
|
+
...c.function,
|
|
169
|
+
// NOTE: claude may return a blank string for empty object (the tool's input schema is a empty object)
|
|
170
|
+
arguments: args.trim() ? (0, json_schema_js_1.parseJSON)(args) : {},
|
|
171
|
+
},
|
|
172
|
+
}))
|
|
173
|
+
.filter(type_utils_js_1.isNonNullable),
|
|
174
|
+
},
|
|
175
|
+
},
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
controller.close();
|
|
179
|
+
}
|
|
180
|
+
catch (error) {
|
|
181
|
+
controller.error(error);
|
|
182
|
+
}
|
|
183
|
+
},
|
|
184
|
+
});
|
|
185
|
+
return streaming ? result : await (0, stream_utils_js_1.agentResponseStreamToObject)(result);
|
|
186
|
+
}
|
|
187
|
+
async requestStructuredOutput(body, responseFormat) {
|
|
188
|
+
if (responseFormat?.type !== "json_schema") {
|
|
189
|
+
throw new Error("Expected json_schema response format");
|
|
190
|
+
}
|
|
191
|
+
const result = await this.client.messages.create({
|
|
192
|
+
...body,
|
|
193
|
+
tools: [
|
|
194
|
+
{
|
|
195
|
+
name: "generate_json",
|
|
196
|
+
description: "Generate a json result by given context",
|
|
197
|
+
input_schema: responseFormat.jsonSchema.schema,
|
|
198
|
+
},
|
|
199
|
+
],
|
|
200
|
+
tool_choice: {
|
|
201
|
+
type: "tool",
|
|
202
|
+
name: "generate_json",
|
|
203
|
+
disable_parallel_tool_use: true,
|
|
204
|
+
},
|
|
205
|
+
stream: false,
|
|
206
|
+
});
|
|
207
|
+
const jsonTool = result.content.find((i) => i.type === "tool_use" && i.name === "generate_json");
|
|
208
|
+
if (!jsonTool)
|
|
209
|
+
throw new Error("Json tool not found");
|
|
210
|
+
return {
|
|
211
|
+
json: jsonTool.input,
|
|
212
|
+
model: result.model,
|
|
213
|
+
usage: {
|
|
214
|
+
inputTokens: result.usage.input_tokens,
|
|
215
|
+
outputTokens: result.usage.output_tokens,
|
|
216
|
+
},
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
exports.AnthropicChatModel = AnthropicChatModel;
|
|
221
|
+
function convertMessages({ messages, responseFormat }) {
|
|
222
|
+
const systemMessages = [];
|
|
223
|
+
const msgs = [];
|
|
224
|
+
for (const msg of messages) {
|
|
225
|
+
if (msg.role === "system") {
|
|
226
|
+
if (typeof msg.content !== "string")
|
|
227
|
+
throw new Error("System message must have content");
|
|
228
|
+
systemMessages.push(msg.content);
|
|
229
|
+
}
|
|
230
|
+
else if (msg.role === "tool") {
|
|
231
|
+
if (!msg.toolCallId)
|
|
232
|
+
throw new Error("Tool message must have toolCallId");
|
|
233
|
+
if (typeof msg.content !== "string")
|
|
234
|
+
throw new Error("Tool message must have string content");
|
|
235
|
+
msgs.push({
|
|
236
|
+
role: "user",
|
|
237
|
+
content: [{ type: "tool_result", tool_use_id: msg.toolCallId, content: msg.content }],
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
else if (msg.role === "user") {
|
|
241
|
+
if (!msg.content)
|
|
242
|
+
throw new Error("User message must have content");
|
|
243
|
+
msgs.push({ role: "user", content: convertContent(msg.content) });
|
|
244
|
+
}
|
|
245
|
+
else if (msg.role === "agent") {
|
|
246
|
+
if (msg.toolCalls?.length) {
|
|
247
|
+
msgs.push({
|
|
248
|
+
role: "assistant",
|
|
249
|
+
content: msg.toolCalls.map((i) => ({
|
|
250
|
+
type: "tool_use",
|
|
251
|
+
id: i.id,
|
|
252
|
+
name: i.function.name,
|
|
253
|
+
input: i.function.arguments,
|
|
254
|
+
})),
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
else if (msg.content) {
|
|
258
|
+
msgs.push({ role: "assistant", content: convertContent(msg.content) });
|
|
259
|
+
}
|
|
260
|
+
else {
|
|
261
|
+
throw new Error("Agent message must have content or toolCalls");
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
if (responseFormat?.type === "json_schema") {
|
|
266
|
+
systemMessages.push(`You should provide a json response with schema: ${JSON.stringify(responseFormat.jsonSchema.schema)}`);
|
|
267
|
+
}
|
|
268
|
+
const system = systemMessages.join("\n").trim() || undefined;
|
|
269
|
+
// Claude requires at least one message, so we add a system message if there are no messages
|
|
270
|
+
if (msgs.length === 0) {
|
|
271
|
+
if (!system)
|
|
272
|
+
throw new Error("No messages provided");
|
|
273
|
+
return { messages: [{ role: "user", content: system }] };
|
|
274
|
+
}
|
|
275
|
+
return { messages: msgs, system };
|
|
276
|
+
}
|
|
277
|
+
function convertContent(content) {
|
|
278
|
+
if (typeof content === "string")
|
|
279
|
+
return content;
|
|
280
|
+
if (Array.isArray(content)) {
|
|
281
|
+
return content.map((item) => item.type === "image_url"
|
|
282
|
+
? { type: "image", source: { type: "url", url: item.url } }
|
|
283
|
+
: { type: "text", text: item.text });
|
|
284
|
+
}
|
|
285
|
+
throw new Error("Invalid chat message content");
|
|
286
|
+
}
|
|
287
|
+
function convertTools({ tools, toolChoice, disableParallelToolUse, }) {
|
|
288
|
+
let choice;
|
|
289
|
+
if (typeof toolChoice === "object" && "type" in toolChoice && toolChoice.type === "function") {
|
|
290
|
+
choice = {
|
|
291
|
+
type: "tool",
|
|
292
|
+
name: toolChoice.function.name,
|
|
293
|
+
disable_parallel_tool_use: disableParallelToolUse,
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
else if (toolChoice === "required") {
|
|
297
|
+
choice = { type: "any", disable_parallel_tool_use: disableParallelToolUse };
|
|
298
|
+
}
|
|
299
|
+
else if (toolChoice === "auto") {
|
|
300
|
+
choice = { type: "auto", disable_parallel_tool_use: disableParallelToolUse };
|
|
301
|
+
}
|
|
302
|
+
else if (toolChoice === "none") {
|
|
303
|
+
choice = { type: "none" };
|
|
304
|
+
}
|
|
305
|
+
return {
|
|
306
|
+
tools: tools?.length
|
|
307
|
+
? tools.map((i) => ({
|
|
308
|
+
name: i.function.name,
|
|
309
|
+
description: i.function.description,
|
|
310
|
+
input_schema: (0, type_utils_js_1.isEmpty)(i.function.parameters)
|
|
311
|
+
? { type: "object" }
|
|
312
|
+
: i.function.parameters,
|
|
313
|
+
}))
|
|
314
|
+
: undefined,
|
|
315
|
+
tool_choice: choice,
|
|
316
|
+
};
|
|
317
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./anthropic-chat-model.js";
|
package/lib/cjs/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./anthropic-chat-model.js"), exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type": "commonjs"}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { type AgentProcessResult, ChatModel, type ChatModelInput, type ChatModelOptions, type ChatModelOutput } from "@aigne/core";
|
|
2
|
+
import { type PromiseOrValue } from "@aigne/core/utils/type-utils.js";
|
|
3
|
+
import Anthropic from "@anthropic-ai/sdk";
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
/**
|
|
6
|
+
* Configuration options for Claude Chat Model
|
|
7
|
+
*/
|
|
8
|
+
export interface AnthropicChatModelOptions {
|
|
9
|
+
/**
|
|
10
|
+
* API key for Anthropic's Claude API
|
|
11
|
+
*
|
|
12
|
+
* If not provided, will look for ANTHROPIC_API_KEY or CLAUDE_API_KEY in environment variables
|
|
13
|
+
*/
|
|
14
|
+
apiKey?: string;
|
|
15
|
+
/**
|
|
16
|
+
* Claude model to use
|
|
17
|
+
*
|
|
18
|
+
* Defaults to 'claude-3-7-sonnet-latest'
|
|
19
|
+
*/
|
|
20
|
+
model?: string;
|
|
21
|
+
/**
|
|
22
|
+
* Additional model options to control behavior
|
|
23
|
+
*/
|
|
24
|
+
modelOptions?: ChatModelOptions;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* @hidden
|
|
28
|
+
*/
|
|
29
|
+
export declare const claudeChatModelOptionsSchema: z.ZodObject<{
|
|
30
|
+
apiKey: z.ZodOptional<z.ZodString>;
|
|
31
|
+
model: z.ZodOptional<z.ZodString>;
|
|
32
|
+
modelOptions: z.ZodOptional<z.ZodObject<{
|
|
33
|
+
model: z.ZodOptional<z.ZodString>;
|
|
34
|
+
temperature: z.ZodOptional<z.ZodNumber>;
|
|
35
|
+
topP: z.ZodOptional<z.ZodNumber>;
|
|
36
|
+
frequencyPenalty: z.ZodOptional<z.ZodNumber>;
|
|
37
|
+
presencePenalty: z.ZodOptional<z.ZodNumber>;
|
|
38
|
+
parallelToolCalls: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
|
|
39
|
+
}, "strip", z.ZodTypeAny, {
|
|
40
|
+
parallelToolCalls: boolean;
|
|
41
|
+
model?: string | undefined;
|
|
42
|
+
temperature?: number | undefined;
|
|
43
|
+
topP?: number | undefined;
|
|
44
|
+
frequencyPenalty?: number | undefined;
|
|
45
|
+
presencePenalty?: number | undefined;
|
|
46
|
+
}, {
|
|
47
|
+
model?: string | undefined;
|
|
48
|
+
temperature?: number | undefined;
|
|
49
|
+
topP?: number | undefined;
|
|
50
|
+
frequencyPenalty?: number | undefined;
|
|
51
|
+
presencePenalty?: number | undefined;
|
|
52
|
+
parallelToolCalls?: boolean | undefined;
|
|
53
|
+
}>>;
|
|
54
|
+
}, "strip", z.ZodTypeAny, {
|
|
55
|
+
apiKey?: string | undefined;
|
|
56
|
+
model?: string | undefined;
|
|
57
|
+
modelOptions?: {
|
|
58
|
+
parallelToolCalls: boolean;
|
|
59
|
+
model?: string | undefined;
|
|
60
|
+
temperature?: number | undefined;
|
|
61
|
+
topP?: number | undefined;
|
|
62
|
+
frequencyPenalty?: number | undefined;
|
|
63
|
+
presencePenalty?: number | undefined;
|
|
64
|
+
} | undefined;
|
|
65
|
+
}, {
|
|
66
|
+
apiKey?: string | undefined;
|
|
67
|
+
model?: string | undefined;
|
|
68
|
+
modelOptions?: {
|
|
69
|
+
model?: string | undefined;
|
|
70
|
+
temperature?: number | undefined;
|
|
71
|
+
topP?: number | undefined;
|
|
72
|
+
frequencyPenalty?: number | undefined;
|
|
73
|
+
presencePenalty?: number | undefined;
|
|
74
|
+
parallelToolCalls?: boolean | undefined;
|
|
75
|
+
} | undefined;
|
|
76
|
+
}>;
|
|
77
|
+
/**
|
|
78
|
+
* Implementation of the ChatModel interface for Anthropic's Claude API
|
|
79
|
+
*
|
|
80
|
+
* This model provides access to Claude's capabilities including:
|
|
81
|
+
* - Text generation
|
|
82
|
+
* - Tool use
|
|
83
|
+
* - JSON structured output
|
|
84
|
+
*
|
|
85
|
+
* Default model: 'claude-3-7-sonnet-latest'
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* Here's how to create and use a Claude chat model:
|
|
89
|
+
* {@includeCode ../test/anthropic-chat-model.test.ts#example-anthropic-chat-model}
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* Here's an example with streaming response:
|
|
93
|
+
* {@includeCode ../test/anthropic-chat-model.test.ts#example-anthropic-chat-model-streaming-async-generator}
|
|
94
|
+
*/
|
|
95
|
+
export declare class AnthropicChatModel extends ChatModel {
|
|
96
|
+
options?: AnthropicChatModelOptions | undefined;
|
|
97
|
+
constructor(options?: AnthropicChatModelOptions | undefined);
|
|
98
|
+
/**
|
|
99
|
+
* @hidden
|
|
100
|
+
*/
|
|
101
|
+
protected _client?: Anthropic;
|
|
102
|
+
get client(): Anthropic;
|
|
103
|
+
get modelOptions(): ChatModelOptions | undefined;
|
|
104
|
+
/**
|
|
105
|
+
* Process the input using Claude's chat model
|
|
106
|
+
* @param input - The input to process
|
|
107
|
+
* @returns The processed output from the model
|
|
108
|
+
*/
|
|
109
|
+
process(input: ChatModelInput): PromiseOrValue<AgentProcessResult<ChatModelOutput>>;
|
|
110
|
+
private _process;
|
|
111
|
+
private extractResultFromAnthropicStream;
|
|
112
|
+
private requestStructuredOutput;
|
|
113
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./anthropic-chat-model.js";
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { type AgentProcessResult, ChatModel, type ChatModelInput, type ChatModelOptions, type ChatModelOutput } from "@aigne/core";
|
|
2
|
+
import { type PromiseOrValue } from "@aigne/core/utils/type-utils.js";
|
|
3
|
+
import Anthropic from "@anthropic-ai/sdk";
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
/**
|
|
6
|
+
* Configuration options for Claude Chat Model
|
|
7
|
+
*/
|
|
8
|
+
export interface AnthropicChatModelOptions {
|
|
9
|
+
/**
|
|
10
|
+
* API key for Anthropic's Claude API
|
|
11
|
+
*
|
|
12
|
+
* If not provided, will look for ANTHROPIC_API_KEY or CLAUDE_API_KEY in environment variables
|
|
13
|
+
*/
|
|
14
|
+
apiKey?: string;
|
|
15
|
+
/**
|
|
16
|
+
* Claude model to use
|
|
17
|
+
*
|
|
18
|
+
* Defaults to 'claude-3-7-sonnet-latest'
|
|
19
|
+
*/
|
|
20
|
+
model?: string;
|
|
21
|
+
/**
|
|
22
|
+
* Additional model options to control behavior
|
|
23
|
+
*/
|
|
24
|
+
modelOptions?: ChatModelOptions;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* @hidden
|
|
28
|
+
*/
|
|
29
|
+
export declare const claudeChatModelOptionsSchema: z.ZodObject<{
|
|
30
|
+
apiKey: z.ZodOptional<z.ZodString>;
|
|
31
|
+
model: z.ZodOptional<z.ZodString>;
|
|
32
|
+
modelOptions: z.ZodOptional<z.ZodObject<{
|
|
33
|
+
model: z.ZodOptional<z.ZodString>;
|
|
34
|
+
temperature: z.ZodOptional<z.ZodNumber>;
|
|
35
|
+
topP: z.ZodOptional<z.ZodNumber>;
|
|
36
|
+
frequencyPenalty: z.ZodOptional<z.ZodNumber>;
|
|
37
|
+
presencePenalty: z.ZodOptional<z.ZodNumber>;
|
|
38
|
+
parallelToolCalls: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
|
|
39
|
+
}, "strip", z.ZodTypeAny, {
|
|
40
|
+
parallelToolCalls: boolean;
|
|
41
|
+
model?: string | undefined;
|
|
42
|
+
temperature?: number | undefined;
|
|
43
|
+
topP?: number | undefined;
|
|
44
|
+
frequencyPenalty?: number | undefined;
|
|
45
|
+
presencePenalty?: number | undefined;
|
|
46
|
+
}, {
|
|
47
|
+
model?: string | undefined;
|
|
48
|
+
temperature?: number | undefined;
|
|
49
|
+
topP?: number | undefined;
|
|
50
|
+
frequencyPenalty?: number | undefined;
|
|
51
|
+
presencePenalty?: number | undefined;
|
|
52
|
+
parallelToolCalls?: boolean | undefined;
|
|
53
|
+
}>>;
|
|
54
|
+
}, "strip", z.ZodTypeAny, {
|
|
55
|
+
apiKey?: string | undefined;
|
|
56
|
+
model?: string | undefined;
|
|
57
|
+
modelOptions?: {
|
|
58
|
+
parallelToolCalls: boolean;
|
|
59
|
+
model?: string | undefined;
|
|
60
|
+
temperature?: number | undefined;
|
|
61
|
+
topP?: number | undefined;
|
|
62
|
+
frequencyPenalty?: number | undefined;
|
|
63
|
+
presencePenalty?: number | undefined;
|
|
64
|
+
} | undefined;
|
|
65
|
+
}, {
|
|
66
|
+
apiKey?: string | undefined;
|
|
67
|
+
model?: string | undefined;
|
|
68
|
+
modelOptions?: {
|
|
69
|
+
model?: string | undefined;
|
|
70
|
+
temperature?: number | undefined;
|
|
71
|
+
topP?: number | undefined;
|
|
72
|
+
frequencyPenalty?: number | undefined;
|
|
73
|
+
presencePenalty?: number | undefined;
|
|
74
|
+
parallelToolCalls?: boolean | undefined;
|
|
75
|
+
} | undefined;
|
|
76
|
+
}>;
|
|
77
|
+
/**
|
|
78
|
+
* Implementation of the ChatModel interface for Anthropic's Claude API
|
|
79
|
+
*
|
|
80
|
+
* This model provides access to Claude's capabilities including:
|
|
81
|
+
* - Text generation
|
|
82
|
+
* - Tool use
|
|
83
|
+
* - JSON structured output
|
|
84
|
+
*
|
|
85
|
+
* Default model: 'claude-3-7-sonnet-latest'
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* Here's how to create and use a Claude chat model:
|
|
89
|
+
* {@includeCode ../test/anthropic-chat-model.test.ts#example-anthropic-chat-model}
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* Here's an example with streaming response:
|
|
93
|
+
* {@includeCode ../test/anthropic-chat-model.test.ts#example-anthropic-chat-model-streaming-async-generator}
|
|
94
|
+
*/
|
|
95
|
+
export declare class AnthropicChatModel extends ChatModel {
|
|
96
|
+
options?: AnthropicChatModelOptions | undefined;
|
|
97
|
+
constructor(options?: AnthropicChatModelOptions | undefined);
|
|
98
|
+
/**
|
|
99
|
+
* @hidden
|
|
100
|
+
*/
|
|
101
|
+
protected _client?: Anthropic;
|
|
102
|
+
get client(): Anthropic;
|
|
103
|
+
get modelOptions(): ChatModelOptions | undefined;
|
|
104
|
+
/**
|
|
105
|
+
* Process the input using Claude's chat model
|
|
106
|
+
* @param input - The input to process
|
|
107
|
+
* @returns The processed output from the model
|
|
108
|
+
*/
|
|
109
|
+
process(input: ChatModelInput): PromiseOrValue<AgentProcessResult<ChatModelOutput>>;
|
|
110
|
+
private _process;
|
|
111
|
+
private extractResultFromAnthropicStream;
|
|
112
|
+
private requestStructuredOutput;
|
|
113
|
+
}
|