-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathtools.ts
63 lines (55 loc) · 1.51 KB
/
tools.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
62
63
#!/usr/bin/env -S npm run tsn -T
import Anthropic from '@anthropic-ai/sdk';
import assert from 'node:assert';
const client = new Anthropic(); // gets API Key from environment variable ANTHROPIC_API_KEY
async function main() {
const userMessage: Anthropic.MessageParam = {
role: 'user',
content: 'What is the weather in SF?',
};
const tools: Anthropic.Tool[] = [
{
name: 'get_weather',
description: 'Get the weather for a specific location',
input_schema: {
type: 'object',
properties: { location: { type: 'string' } },
},
},
];
const message = await client.messages.create({
model: 'claude-3-5-sonnet-latest',
max_tokens: 1024,
messages: [userMessage],
tools,
});
console.log('Initial response:');
console.dir(message, { depth: 4 });
assert(message.stop_reason === 'tool_use');
const tool = message.content.find(
(content): content is Anthropic.ToolUseBlock => content.type === 'tool_use',
);
assert(tool);
const result = await client.messages.create({
model: 'claude-3-5-sonnet-latest',
max_tokens: 1024,
messages: [
userMessage,
{ role: message.role, content: message.content },
{
role: 'user',
content: [
{
type: 'tool_result',
tool_use_id: tool.id,
content: [{ type: 'text', text: 'The weather is 73f' }],
},
],
},
],
tools,
});
console.log('\nFinal response');
console.dir(result, { depth: 4 });
}
main();