mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
.NET: Update Conversation Sample to use Conversation Id instead (#3180)
* Update Conversation Sample to use conversation Id instead * Remove Run infix * Remove the sync GetAIAgent from sample
This commit is contained in:
committed by
GitHub
Unverified
parent
41cc34421f
commit
aa6579f38c
+14
-7
@@ -19,19 +19,23 @@ AIProjectClient aiProjectClient = new(new Uri(endpoint), new AzureCliCredential(
|
||||
// Define the agent you want to create. (Prompt Agent in this case)
|
||||
AgentVersionCreationOptions options = new(new PromptAgentDefinition(model: deploymentName) { Instructions = JokerInstructions });
|
||||
|
||||
// Create a server side agent version with the Azure.AI.Agents SDK client.
|
||||
AgentVersion agentVersion = aiProjectClient.Agents.CreateAgentVersion(agentName: JokerName, options);
|
||||
|
||||
// Use an AIAgent with an already created server side agent version.
|
||||
AIAgent jokerAgent = aiProjectClient.AsAIAgent(agentVersion);
|
||||
// Retrieve an AIAgent for the created server side agent version.
|
||||
ChatClientAgent jokerAgent = await aiProjectClient.CreateAIAgentAsync(name: JokerName, options);
|
||||
|
||||
// Invoke the agent with a multi-turn conversation, where the context is preserved in the thread object.
|
||||
AgentThread thread = await jokerAgent.GetNewThreadAsync();
|
||||
// Create a conversation in the server
|
||||
ProjectConversationsClient conversationsClient = aiProjectClient.GetProjectOpenAIClient().GetProjectConversationsClient();
|
||||
ProjectConversation conversation = await conversationsClient.CreateProjectConversationAsync();
|
||||
|
||||
// Providing the conversation Id is not strictly necessary, but by not providing it no information will show up in the Foundry Project UI as conversations.
|
||||
// Threads that doesn't have a conversation Id will work based on the `PreviousResponseId`.
|
||||
AgentThread thread = await jokerAgent.GetNewThreadAsync(conversation.Id);
|
||||
|
||||
Console.WriteLine(await jokerAgent.RunAsync("Tell me a joke about a pirate.", thread));
|
||||
Console.WriteLine(await jokerAgent.RunAsync("Now add some emojis to the joke and tell it in the voice of a pirate's parrot.", thread));
|
||||
|
||||
// Invoke the agent with a multi-turn conversation and streaming, where the context is preserved in the thread object.
|
||||
thread = await jokerAgent.GetNewThreadAsync();
|
||||
thread = await jokerAgent.GetNewThreadAsync(conversation.Id);
|
||||
await foreach (AgentResponseUpdate update in jokerAgent.RunStreamingAsync("Tell me a joke about a pirate.", thread))
|
||||
{
|
||||
Console.WriteLine(update);
|
||||
@@ -43,3 +47,6 @@ await foreach (AgentResponseUpdate update in jokerAgent.RunStreamingAsync("Now a
|
||||
|
||||
// Cleanup by agent name removes the agent version created.
|
||||
await aiProjectClient.Agents.DeleteAgentAsync(jokerAgent.Name);
|
||||
|
||||
// Cleanup the conversation created.
|
||||
await conversationsClient.DeleteConversationAsync(conversation.Id);
|
||||
|
||||
+18
-9
@@ -1,14 +1,15 @@
|
||||
# Multi-turn Conversation with AI Agents
|
||||
|
||||
This sample demonstrates how to implement multi-turn conversations with AI agents, where context is preserved across multiple agent runs using threads.
|
||||
This sample demonstrates how to implement multi-turn conversations with AI agents, where context is preserved across multiple agent runs using threads and conversation IDs.
|
||||
|
||||
## What this sample demonstrates
|
||||
|
||||
- Creating an AI agent with instructions
|
||||
- Using threads to maintain conversation context
|
||||
- Creating a project conversation to track conversations in the Foundry UI
|
||||
- Using threads with conversation IDs to maintain conversation context
|
||||
- Running multi-turn conversations with text output
|
||||
- Running multi-turn conversations with streaming output
|
||||
- Managing agent lifecycle (creation and deletion)
|
||||
- Managing agent and conversation lifecycle (creation and deletion)
|
||||
|
||||
## Prerequisites
|
||||
|
||||
@@ -41,10 +42,18 @@ dotnet run --project .\FoundryAgents_Step02_MultiturnConversation
|
||||
The sample will:
|
||||
|
||||
1. Create an agent named "JokerAgent" with instructions to tell jokes
|
||||
2. Create a thread for conversation context
|
||||
3. Run the agent with a text prompt and display the response
|
||||
4. Send a follow-up message to the same thread, demonstrating context preservation
|
||||
5. Create a new thread and run the agent with streaming
|
||||
6. Send a follow-up streaming message to demonstrate multi-turn streaming
|
||||
7. Clean up resources by deleting the agent
|
||||
2. Create a project conversation to enable visibility in the Azure Foundry UI
|
||||
3. Create a thread linked to the conversation ID for context tracking
|
||||
4. Run the agent with a text prompt and display the response
|
||||
5. Send a follow-up message to the same thread, demonstrating context preservation
|
||||
6. Create a new thread sharing the same conversation ID and run the agent with streaming
|
||||
7. Send a follow-up streaming message to demonstrate multi-turn streaming
|
||||
8. Clean up resources by deleting the agent and conversation
|
||||
|
||||
## Conversation ID vs PreviousResponseId
|
||||
|
||||
When working with multi-turn conversations, there are two approaches:
|
||||
|
||||
- **With Conversation ID**: By passing a `conversation.Id` to `GetNewThreadAsync()`, the conversation will be visible in the Azure Foundry Project UI. This is useful for tracking and debugging conversations.
|
||||
- **Without Conversation ID**: Threads created without a conversation ID still work correctly, maintaining context via `PreviousResponseId`. However, these conversations may not appear in the Foundry UI.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user