Open
Description
Describe the bug
Following the example from here the ResponseCallback and the response is randomly empty.
To Reproduce
Steps to reproduce the behavior:
- Following the example from here
Expected behavior
The agent should always respond
Screenshots
If applicable, add screenshots to help explain your problem.
Platform
- Language: .NET
- Source: Microsoft.SemanticKernel 1.58.0
- AI model: llama3.2
- IDE: Visual Studio
- OS: Windows
Additional context
Sample code
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents;
using Microsoft.SemanticKernel.Agents.Orchestration;
using Microsoft.SemanticKernel.Agents.Orchestration.Sequential;
using Microsoft.SemanticKernel.Agents.Runtime.InProcess;
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;
#pragma warning disable SKEXP0110 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
#pragma warning disable SKEXP0070 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
#pragma warning disable SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
namespace SequentialTest
{
internal class SQ
{
public static async Task<string> RunSequentialOrchestrationAsync(string input, string model = "llama3.2")
{
ChatCompletionAgent analystAgent =
CreateAgent(
name: "Analyst",
instructions:
"""
You are a marketing analyst. Given a product description, identify:
- Key features
- Target audience
- Unique selling points
- Keep your response short (around 50 words)
""",
description: "A agent that extracts key concepts from a product description.",
model);
ChatCompletionAgent writerAgent =
CreateAgent(
name: "copywriter",
instructions:
"""
You are a marketing copywriter. Given a block of text describing features, audience, and USPs,
compose a compelling marketing copy (like a newsletter section) that highlights these points.
Output should be short (around 150 words), output just the copy as a single text block.
""",
description: "An agent that writes a marketing copy based on the extracted concepts.",
model);
ChatCompletionAgent editorAgent =
CreateAgent(
name: "editor",
instructions:
"""
You are an editor. Given the draft copy, correct grammar, improve clarity, ensure consistent tone,
give format and make it polished. Output the final improved copy as a single text block. Output should be short (around 150 words).
""",
description: "An agent that formats and proofreads the marketing copy.",
model);
SequentialOrchestration orchestration =
new(analystAgent, writerAgent, editorAgent)
{
ResponseCallback = ResponseCallback,
};
// Start the runtime
InProcessRuntime runtime = new();
await runtime.StartAsync();
// Run the orchestration
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine($"\n# INPUT: {input}\n");
Console.WriteLine("**************************************************************************************");
Console.ResetColor();
OrchestrationResult<string> result = await orchestration.InvokeAsync(input, runtime);
string text = await result.GetValueAsync(TimeSpan.FromSeconds(25000));
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("**************************************************************************************");
Console.WriteLine($"\n# RESULT: {text}");
Console.ResetColor();
await runtime.RunUntilIdleAsync();
return text;
}
public static async Task<string> RunKernelDirectlyAsync(string prompt, string model = "llama3.2")
{
Kernel kernel = CreateKernel(model);
var results = await kernel.InvokePromptAsync(
promptTemplate: prompt);
Console.WriteLine($"\n# RESULT: {results}");
return results.ToString();
}
private static ValueTask ResponseCallback(ChatMessageContent response)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"\n# RESPONSE CALLBACK:{response?.AuthorName}: {response?.Role}, {response?.Content}");
Console.ResetColor();
return default;
}
private static ChatCompletionAgent CreateAgent(string name, string instructions, string description, string model)
{
return new ChatCompletionAgent()
{
Name = name,
Description = description,
Instructions = instructions,
Kernel = CreateKernel(model),
};
}
private static Kernel CreateKernel(string model)
{
IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.AddOllamaChatCompletion(
modelId: model,
httpClient: CreateHttpClient(new Uri("http://localhost:7869/v1"), 5000),
serviceId: "OllamaChatCompletion"
);
return kernelBuilder.Build();
}
private static HttpClient CreateHttpClient(Uri baseAddress, long minutes = 10)
{
return new HttpClient
{
Timeout = TimeSpan.FromMinutes(minutes),
BaseAddress = baseAddress,
};
}
}
}
#pragma warning restore SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
#pragma warning restore SKEXP0110 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
#pragma warning restore SKEXP0070 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
Main method
await SequentialTest.SQ.RunSequentialOrchestrationAsync("An eco-friendly stainless steel water bottle that keeps drinks cold for 24 hours");
Console.WriteLine("**************************** DONE ****************************");
Console.Read();
Example output (notice the result and the response callback to the editor is empty)
# INPUT: An eco-friendly stainless steel water bottle that keeps drinks cold for 24 hours
**************************************************************************************
# RESPONSE CALLBACK:Analyst: assistant, Here's the analysis:
**Key Features:**
* Eco-friendly materials
* Insulated design
* 24-hour temperature retention
**Target Audience:**
* Health-conscious individuals
* Environmentally aware consumers
* Outdoor enthusiasts and commuters
**Unique Selling Points:**
* Sustainable product option
* Long-lasting temperature control
# RESPONSE CALLBACK:copywriter: assistant,
* Innovative insulation technology
Here's the marketing copy:
"Introducing our EcoSmart Insulated Bag - designed with you and the planet in mind. Made from eco-friendly materials, this bag not only keeps your essentials at the perfect temperature for 24 hours, but also reduces waste and minimizes environmental impact. Perfect for health-conscious individuals, environmentally aware consumers, and outdoor enthusiasts who care about their carbon footprint. Say goodbye to lukewarm coffee on your morning commute or chilly snacks during hikes. Our innovative insulation technology ensures long-lasting temperature retention, keeping your food fresh and hot. Upgrade to an eco-friendly lifestyle with our EcoSmart Insulated Bag - order now!"
# RESPONSE CALLBACK:editor: assistant,
**************************************************************************************
# RESULT:
**************************** DONE ****************************