Skip to content

Chat Client Streaming Tool Calling

This example assumes using Writer; is in scope and apiKey contains your Writer API key.

 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
using var client = new WriterClient(apiKey);
Meai.IChatClient chatClient = client;

// Stream chat responses with tool calling using the MEAI IChatClient interface.
// Define a simple tool that returns the current weather.
var tool = Meai.AIFunctionFactory.Create(
    (string location) => $"The weather in {location} is sunny, 72°F.",
    "get_weather",
    "Gets the current weather for a location.");

var enumerable = chatClient.GetStreamingResponseAsync(
    messages: [new Meai.ChatMessage(Meai.ChatRole.User, "What's the weather in San Francisco?")],
    new Meai.ChatOptions
    {
        ModelId = "palmyra-x-004",
        Tools = [tool],
    });

var hasContent = false;
Meai.ChatFinishReason? finishReason = null;
await foreach (var update in enumerable)
{
    if (update.Text is { Length: > 0 })
    {
        Console.Write(update.Text);
        hasContent = true;
    }

    if (update.FinishReason is not null)
    {
        finishReason = update.FinishReason;
    }
}

// The model should either respond with tool calls or text content.