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
G


using var api = new OllamaApiClient();

await apiClient.Models.PullModelAsync("llama3.2").EnsureSuccessAsync();


var messages = new List<Message>
{
    "You are a helpful weather assistant.".AsSystemMessage(),
    "What is the current temperature in Dubai, UAE in Celsius?".AsUserMessage(),
};
const string model = "llama3.2";

try
{
    var service = new WeatherService();
    var tools = service.AsTools().AsOllamaTools();
    var response = await api.Chat.GenerateChatCompletionAsync(
        model,
        messages,
        stream: false,
        tools: tools).WaitAsync();

    messages.Add(response.Message);


    foreach (var call in response.Message.ToolCalls!)
    {
        var json = await service.CallAsync(
            functionName: call.Function?.Name ?? string.Empty,
            argumentsAsJson: call.Function?.Arguments.AsJson() ?? string.Empty);
        messages.Add(json.AsToolMessage());
    }

    response = await api.Chat.GenerateChatCompletionAsync(
        model,
        messages,
        stream: false,
        tools: tools).WaitAsync();
    messages.Add(response.Message);
}
finally
{
    Console.WriteLine(Ollama.Chat.PrintMessages(messages));
}