Replies: 4 comments
-
I was able to retrieve tool call messages like the ones below. using Azure.Identity;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents;
using Microsoft.SemanticKernel.ChatCompletion;
using System.ComponentModel;
using System.Text.Json;
var builder = Kernel.CreateBuilder();
builder.AddAzureOpenAIChatClient(
"gpt-4.1",
"https://<<your AOAI resource name>>.openai.azure.com/",
new AzureCliCredential());
builder.Plugins.AddFromType<WeatherPlugin>();
var kernel = builder.Build();
var catAgent = new ChatCompletionAgent
{
Name = "CatAgent",
Instructions = """
You are a cat.
Respond to all messages as if you are a cat, and end each sentence with "meow".
Your tone should be playful and curious, just like a real cat.
""",
Kernel = kernel,
Arguments = new(new PromptExecutionSettings
{
FunctionChoiceBehavior = FunctionChoiceBehavior.Auto(),
}),
};
ChatHistory messages = [];
AgentThread thread = new ChatHistoryAgentThread(messages);
await foreach (var result in catAgent.InvokeAsync("What is the weather at Seattle?", thread))
{
thread = result.Thread;
if (!string.IsNullOrWhiteSpace(result.Message.Content))
{
Console.WriteLine($"Cat: {result.Message.Content}");
}
}
Console.WriteLine(JsonSerializer.Serialize(messages,
new JsonSerializerOptions
{
WriteIndented = true,
}));
class WeatherPlugin
{
[KernelFunction, Description("Get the current weather for a specified city.")]
public string GetWeather(
[Description("The name of the city to get the weather for.")]
string city)
{
return $"The weather in {city} is sunny with a high of 25°C.";
}
} The output is:
|
Beta Was this translation helpful? Give feedback.
-
@ploenvora does this help unblock you? |
Beta Was this translation helpful? Give feedback.
-
@ploenvora I am facing a similar issue. Whenever FunctionChoiceBehavior is set for the Agent's Arguments, no response is generated. I am also using ChatCompletionAgent with a registered OpenAIChatCompletion service to the Kernel. Once FunctionChoiceBehavior is removed, responses are generated (however, not from registered plugins) I'm running in a C# .NET Function App Environment |
Beta Was this translation helpful? Give feedback.
-
Tagging @SergeyMenshykh |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm currently using the Semantic Kernel ChatCompletionAgent in C#. When I invoke the agent using Agent.InvokeAsync(), the assistant's tool call (i.e., function call) executes correctly, but it does not get recorded in the ChatHistory.
I attempted to manually retrieve messages using ChatHistoryAgentThread.GetMessagesAsync(), but it looks like that method is either deprecated / marked as experimental, and I’m hesitant to rely on it.
Is there a supported way to ensure that tool calls and their results are reflected in the chat history when using Agent.InvokeAsync()? Alternatively, is there a recommended workaround to manually record tool calls in the history that works well with ChatCompletionAgent?
Any guidance or examples would be appreciated! :)
Beta Was this translation helpful? Give feedback.
All reactions