Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure non-streaming usage data from function calling is in history #5676

Merged
merged 1 commit into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,13 @@ public override async Task<ChatCompletion> CompleteAsync(IList<ChatMessage> chat
}
}

// If the original chat completion included usage data,
// add that into the message so it's available in the history.
if (KeepFunctionCallingMessages && response.Usage is { } usage)
{
response.Message.Contents = [.. response.Message.Contents, new UsageContent(usage)];
}

// Add the responses from the function calls into the history.
var modeAndMessages = await ProcessFunctionCallsAsync(chatMessages, options, functionCallContents, iteration, cancellationToken).ConfigureAwait(false);
if (modeAndMessages.MessagesAdded is not null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,25 @@ public virtual async Task FunctionInvocation_AutomaticallyInvokeFunction_Paramet

int secretNumber = 42;

var response = await chatClient.CompleteAsync("What is the current secret number?", new()
List<ChatMessage> messages =
[
new(ChatRole.User, "What is the current secret number?")
];

var response = await chatClient.CompleteAsync(messages, new()
{
Tools = [AIFunctionFactory.Create(() => secretNumber, "GetSecretNumber")]
});

Assert.Single(response.Choices);
Assert.Contains(secretNumber.ToString(), response.Message.Text);

if (response.Usage is { } finalUsage)
{
UsageContent? intermediate = messages.SelectMany(m => m.Contents).OfType<UsageContent>().FirstOrDefault();
Assert.NotNull(intermediate);
Assert.True(finalUsage.TotalTokenCount > intermediate.Details.TotalTokenCount);
SteveSandersonMS marked this conversation as resolved.
Show resolved Hide resolved
}
}

[ConditionalFact]
Expand Down
Loading