Skip to content

Tools from Streaming Chat Completion not triggering #313

Closed
@RichOwenMercury

Description

@RichOwenMercury

Service

Azure OpenAI

Describe the bug

When using the CompleteChatStreamingAsync method on the ChatClient. I have tools configured with the ToolChoice set to Auto. However, whenever I send a user message that should trigger the tool, the TooCallUpdates against the StreamingChatCompletionUpdate is always empty.

To test this I have a simple get_today tool that when executed the function will return the formatted datetime. I have provide the code below.

Not sure if this is something worng with

Steps to reproduce

  1. Send a User Message "What is today's date?"

Expected:
Will raise the ToolCallUpdate that will then allow me to call the required method.

Actual:
No ToolCallUpdate are raised so the chat returns the text '{get_today}'

Code snippets

// Main Code

var endpoint = "":
var apiKey = "";
var deployment = "";

var creds = new AzureKeyCredential(apiKey);
var client = new AzureOpenAIClient(endpoint, creds);
var chatClient = client.GetChatClient(deployment);

var tools = GetTools(); // Returns Dictionary<string, ITool>

var options = new ChatCompletionOptions();
if (tools.Any())
{
    options.ToolChoice = ChatToolChoice.CreateAutoChoice();
    
    foreach (var toolData in tools)
    {
        var tool = toolData.Value;

        options.Tools.Add(ChatTool.CreateFunctionTool(tool.Name, tool.Description, tool.Parameters));
    }
}

var messages = new List<ChatMessage>
{
    new SystemChatMessage("You are a helpful assistant. Always lookup the current date and/or time using the tool 'get_today' when required."),
    new UserChatMessage("What is today's date?")
};

var completionUpdates = chatClient.CompleteChatStreamingAsync(messages, options);

await foreach (var update in completionUpdates)
{
    Console.WriteLine("Finish Reason: {0}", update.FinishReason);
    if (update.FinishReason == ChatFinishReason.FunctionCall)
    {
        // Would expect here to be hit (1/2)
    }

    foreach (var toolCall in update.ToolCallUpdates)
    {
        // Would expect here to be hit (2/2)
    }
    
    foreach (var contentPart in update.ContentUpdate)
    {
        Console.WriteLine(contentPart.Text);
    }
}

// ITool
public interface ITool
{
    public string Name { get; }
    public string Description { get; }
    public BinaryData Parameters { get; }

    Task<string> RunAsync(string args);
}

// GetTodayTool
public class GetTodayTool : ITool
{
    public string Name => "get_today";
    public string Description => "Know the date and time of today. Will return in format dd/MM/yyyy HH:mm:ss";
    public BinaryData Parameters => BinaryData.FromString("{}");

    public Task<string> RunAsync(string args)
    {
        return Task.FromResult(DateTime.Now.ToString("dd//MM/yyyy HH:mm:ss"));
    }
}

OS

winOS

.NET version

8

Library version

2.1.0

Metadata

Metadata

Assignees

Labels

bugCategory: Something isn't working and appears to be a defect in the client library.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions