Skip to content
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 @@ -19,19 +19,23 @@
// 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);
Expand All @@ -43,3 +47,6 @@

// Cleanup by agent name removes the agent version created.
await aiProjectClient.Agents.DeleteAgentAsync(jokerAgent.Name);

// Cleanup the conversation created.
await conversationsClient.DeleteConversationAsync(conversation.Id);
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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.

Loading