Tools

 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
using var client = new AnthropicClient(apiKey);
var service = new WeatherService();
var tools = service.AsTools().AsAnthropicTools();

List<Message> messages = ["What is the current temperature in Dubai, UAE in Celsius?"];

var response = await client.CreateMessageAsync(
    model: CreateMessageRequestModel.Claude35Sonnet20240620,
    messages: messages,
    maxTokens: 300,
    metadata: null,
    stopSequences: null,
    system: "You are a helpful weather assistant.",
    temperature: 0,
    toolChoice: new ToolChoice
    {
        Type = ToolChoiceType.Auto,
        Name = null,
    },
    tools: tools,
    topK: 0,
    topP: 0,
    stream: false);

messages.Add(response.AsRequestMessage());

foreach (var toolUse in response.Content.Value2!
             .Where(x => x.IsToolUse)
             .Select(x => x.ToolUse))
{
    var json = await service.CallAsync(
        functionName: toolUse!.Name,
        argumentsAsJson: toolUse.Input.AsJson());
    messages.Add(json.AsToolCall(toolUse));
}

response = await client.CreateMessageAsync(
    model: CreateMessageRequestModel.Claude35Sonnet20240620,
    messages: messages,
    maxTokens: 300,
    metadata: null,
    stopSequences: null,
    system: "You are a helpful weather assistant.",
    temperature: 0,
    toolChoice: new ToolChoice
    {
        Type = ToolChoiceType.Auto,
        Name = null,
    },
    tools: tools,
    topK: 0,
    topP: 0,
    stream: false);

Console.WriteLine(response.AsSimpleText());