Skip to content

.Net: Return result of the function executed before termination for streaming API #6428

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

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 @@ -397,6 +397,47 @@ public async Task ValidateGetChatMessageContentsWithAutoFunctionInvocationFilter
Assert.Contains("GetWeather", invokedFunctions);
}

[Fact]
public async Task ValidateGetStreamingChatMessageContentWithAutoFunctionInvocationFilterTerminateAsync()
{
// Arrange
var client = this.CreateMistralClientStreaming("mistral-tiny", "https://api.mistral.ai/v1/chat/completions", "chat_completions_streaming_function_call_response.txt");

var kernel = new Kernel();
kernel.Plugins.AddFromType<WeatherPlugin>();

var filter = new FakeAutoFunctionFilter(async (context, next) =>
{
await next(context);
context.Terminate = true;
});
kernel.AutoFunctionInvocationFilters.Add(filter);

var executionSettings = new MistralAIPromptExecutionSettings { ToolCallBehavior = MistralAIToolCallBehavior.AutoInvokeKernelFunctions };
var chatHistory = new ChatHistory
{
new ChatMessageContent(AuthorRole.User, "What is the weather like in Paris?")
};

List<StreamingKernelContent> streamingContent = [];

// Act
await foreach (var item in client.GetStreamingChatMessageContentsAsync(chatHistory, default, executionSettings, kernel))
{
streamingContent.Add(item);
}

// Assert
// Results of function invoked before termination should be returned
Assert.Equal(3, streamingContent.Count);

var lastMessageContent = streamingContent[^1] as StreamingChatMessageContent;
Assert.NotNull(lastMessageContent);

Assert.Equal("12°C\nWind: 11 KMPH\nHumidity: 48%\nMostly cloudy", lastMessageContent.Content);
Assert.Equal(AuthorRole.Tool, lastMessageContent.Role);
}

[Theory]
[InlineData("system", "System Content")]
[InlineData("user", "User Content")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,14 +447,17 @@ internal async IAsyncEnumerable<StreamingChatMessageContent> GetStreamingChatMes

this.AddResponseMessage(chatRequest, chatHistory, toolCall, result: stringResult, errorMessage: null);

// If filter requested termination, breaking request iteration loop.
// If filter requested termination, returning latest function result and breaking request iteration loop.
if (invocationContext.Terminate)
{
if (this._logger.IsEnabled(LogLevel.Debug))
{
this._logger.LogDebug("Filter requested termination of automatic function invocation.");
}

var lastChatMessage = chatHistory.Last();

yield return new StreamingChatMessageContent(lastChatMessage.Role, lastChatMessage.Content);
yield break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -859,14 +859,17 @@ internal async IAsyncEnumerable<OpenAIStreamingChatMessageContent> GetStreamingC

AddResponseMessage(chatOptions, chat, streamedRole, toolCall, metadata, stringResult, errorMessage: null, this.Logger);

// If filter requested termination, breaking request iteration loop.
// If filter requested termination, returning latest function result and breaking request iteration loop.
if (invocationContext.Terminate)
{
if (this.Logger.IsEnabled(LogLevel.Debug))
{
this.Logger.LogDebug("Filter requested termination of automatic function invocation.");
}

var lastChatMessage = chat.Last();

yield return new OpenAIStreamingChatMessageContent(lastChatMessage.Role, lastChatMessage.Content);
yield break;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ public async Task PostFilterCanTerminateOperationAsync()
this._messageHandlerStub.ResponsesToReturn = GetFunctionCallingResponses();

// Act
await kernel.InvokePromptAsync("Test prompt", new(new OpenAIPromptExecutionSettings
var result = await kernel.InvokePromptAsync("Test prompt", new(new OpenAIPromptExecutionSettings
{
ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions
}));
Expand All @@ -507,6 +507,13 @@ public async Task PostFilterCanTerminateOperationAsync()
Assert.Equal(0, secondFunctionInvocations);
Assert.Equal([0], requestSequenceNumbers);
Assert.Equal([0], functionSequenceNumbers);

// Results of function invoked before termination should be returned
var lastMessageContent = result.GetValue<ChatMessageContent>();
Assert.NotNull(lastMessageContent);

Assert.Equal("function1-value", lastMessageContent.Content);
Assert.Equal(AuthorRole.Tool, lastMessageContent.Role);
}

[Fact]
Expand Down Expand Up @@ -538,15 +545,28 @@ public async Task PostFilterCanTerminateOperationOnStreamingAsync()

var executionSettings = new OpenAIPromptExecutionSettings { ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions };

List<StreamingKernelContent> streamingContent = [];

// Act
await foreach (var item in kernel.InvokePromptStreamingAsync("Test prompt", new(executionSettings)))
{ }
{
streamingContent.Add(item);
}

// Assert
Assert.Equal(1, firstFunctionInvocations);
Assert.Equal(0, secondFunctionInvocations);
Assert.Equal([0], requestSequenceNumbers);
Assert.Equal([0], functionSequenceNumbers);

// Results of function invoked before termination should be returned
Assert.Equal(3, streamingContent.Count);

var lastMessageContent = streamingContent[^1] as StreamingChatMessageContent;
Assert.NotNull(lastMessageContent);

Assert.Equal("function1-value", lastMessageContent.Content);
Assert.Equal(AuthorRole.Tool, lastMessageContent.Role);
}

public void Dispose()
Expand Down
Loading