Microsoft Agent Framework——智能体教程——与智能体进行多轮对话

This tutorial step shows you how to have a multi-turn conversation with an agent, where the agent is built on the Azure OpenAI Chat Completion service.

本教程步骤将向您展示如何基于 Azure OpenAI 聊天补全服务构建的智能体,与其进行多轮对话。

Important    重要提示

The agent framework supports many different types of agents. This tutorial uses an agent based on a Chat Completion service, but all other agent types are run in the same way. See the Agent Framework user guide for more information on other agent types and how to construct them.

Agent Framework 支持多种不同类型的智能体。本教程使用基于聊天补全服务的智能体,但所有其他类型的智能体运行方式相同。有关其他智能体类型及其构建方法的更多信息,请参见 Agent Framework 用户指南。

Prerequisites    前置条件

For prerequisites and creating the agent, see the Create and run a simple agent step in this tutorial.

有关先决条件和创建智能体的步骤,请参阅本教程中的“创建并运行一个简单智能体”部分。

Running the agent with a multi-turn conversation    运行智能体并进行多轮对话

Agents are stateless and do not maintain any state internally between calls. To have a multi-turn conversation with an agent, you need to create an object to hold the conversation state and pass this object to the agent when running it.

智能体本身是无状态的,在多次调用之间不会在内部维持任何状态。若要实现与智能体的多轮对话,您需要创建一个用于保存对话状态的对象,并在运行智能体时将该对象传递给智能体。

To create the conversation state object, call the GetNewThread method on the agent instance.

要创建此对话状态对象,请调用智能体实例的 GetNewThread 方法。

AgentThread thread = agent.GetNewThread();

You can then pass this thread object to the RunAsync and RunStreamingAsync methods on the agent instance, along with the user input.

然后,您可以将此thread对象连同用户输入一起传递给智能体实例上的 RunAsyncRunStreamingAsync 方法。

Console.WriteLine(await agent.RunAsync("Tell me a joke about a pirate.", thread));
Console.WriteLine(await agent.RunAsync("Now add some emojis to the joke and tell it in the voice of a pirate's parrot.", thread));

This will maintain the conversation state between the calls, and the agent will be able to refer to previous input and response messages in the conversation when responding to new input.

这样就能在多次调用间保持对话状态,使智能体在响应新输入时能够参照对话中先前的输入和回复消息。

Important    重要提示

The type of service that is used by the AIAgent will determine how conversation history is stored. E.g. when using a ChatCompletion service, like in this example, the conversation history is stored in the AgentThread object and sent to the service on each call. When using the Azure AI Agent service on the other hand, the conversation history is stored in the Azure AI Agent service and only a reference to the conversation is sent to the service on each call.

AIAgent 所使用的服务类型将决定对话历史的存储方式。例如,在本示例中使用的 ChatCompletion 服务,其对话历史会存储在 AgentThread 对象中,并在每次调用时发送给服务。而当使用 Azure AI Agent 服务时,对话历史将存储在 Azure AI Agent 服务端,每次调用时仅向服务发送对话的引用信息。

Single agent with multiple conversations    单个智能体支持多场对话

It is possible to have multiple, independent conversations with the same agent instance, by creating multiple AgentThread objects. These threads can then be used to maintain separate conversation states for each conversation. The conversations will be fully independent of each other, since the agent does not maintain any state internally.

通过创建多个 AgentThread 对象,可以与同一个智能体实例进行多场独立的对话。这些线程可用于为每场对话维护各自的对话状态。由于智能体内部不保存任何状态,因此各场对话将完全相互独立。(类似于deepseek里的开启新对话,独立的上下文)

AgentThread thread1 = agent.GetNewThread();
AgentThread thread2 = agent.GetNewThread();
Console.WriteLine(await agent.RunAsync("Tell me a joke about a pirate.", thread1));
Console.WriteLine(await agent.RunAsync("Tell me a joke about a robot.", thread2));
Console.WriteLine(await agent.RunAsync("Now add some emojis to the joke and tell it in the voice of a pirate's parrot.", thread1));
Console.WriteLine(await agent.RunAsync("Now add some emojis to the joke and tell it in the voice of a robot.", thread2));

自己实际使用的代码如下:

using Azure;
using Azure.AI.OpenAI;
using Microsoft.Agents.AI;
using OpenAI;

var token = Environment.GetEnvironmentVariable("GITHUB_TOKEN");
AIAgent agent = new AzureOpenAIClient(new Uri("https://models.inference.ai.azure.com"), new AzureKeyCredential(token))
    .GetChatClient("gpt-4o-mini")
    .CreateAIAgent(instructions: "You are good at telling jokes.", name: "Joker");

AgentThread thread1 = agent.GetNewThread();
AgentThread thread2 = agent.GetNewThread();
Console.WriteLine(await agent.RunAsync("Tell me a joke about a pirate.", thread1));
Console.WriteLine(await agent.RunAsync("Tell me a joke about a robot.", thread2));
Console.WriteLine(await agent.RunAsync("Now add some emojis to the joke and tell it in the voice of a pirate's parrot.", thread1));
Console.WriteLine(await agent.RunAsync("Now add some emojis to the joke and tell it in the voice of a robot.", thread2));

Console.Read();

 

posted @ 2025-10-04 15:54  菜鸟吊思  阅读(14)  评论(0)    收藏  举报