-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1_basic.ts
More file actions
63 lines (56 loc) · 1.51 KB
/
Copy path1_basic.ts
File metadata and controls
63 lines (56 loc) · 1.51 KB
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
import Anthropic from "@anthropic-ai/sdk";
import dotenv from "dotenv";
import { type WeatherInput, getWeather } from "../functions/get_weather";
dotenv.config();
const anthropic = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY,
});
// 基本パターン
async function basicToolUse() {
const tools = [
{
name: "get_weather",
description: "指定された場所の天気情報を取得します",
input_schema: {
type: "object" as const,
properties: {
location: {
type: "string" as const,
description: "場所(例:東京)",
},
},
required: ["location"],
},
},
];
console.log("モデルに問い合わせ...");
const message = await anthropic.messages.create({
model: "claude-3-5-sonnet-20241022",
max_tokens: 1000,
messages: [
{
role: "user",
content: "東京の天気は?",
},
],
tools,
});
console.log("Response:", JSON.stringify(message, null, 2));
// ツールの実行
if (message.stop_reason === "tool_use") {
const toolContent = message.content[message.content.length - 1];
if (toolContent.type === "tool_use") {
try {
const toolArgs = toolContent.input as WeatherInput;
console.log(
`ツール実行..天気を取得...引数: ${JSON.stringify(toolArgs)}`,
);
const weatherResult = await getWeather(toolArgs);
console.log("Weather Result:", JSON.stringify(weatherResult, null, 2));
} catch (error) {
console.error("Weather API Error:", error);
}
}
}
}
basicToolUse().catch(console.error);