本教學課程步驟示範如何與代理程式進行多回合交談,其中代理程式是以 Azure OpenAI 聊天完成服務為基礎。
這很重要
代理程式架構支援許多不同類型的代理程式。 本教學課程使用以聊天完成服務為基礎的代理程式,但所有其他代理程式類型都會以相同的方式執行。 如需其他代理程式類型以及如何建構它們的詳細資訊,請參閱代理 程式架構使用者指南。
先決條件
如需必要條件和建立代理程式,請參閱本教學課程中的 建立並執行簡式代理程式 步驟。
使用多回合交談執行代理程式
代理是無狀態的,並且在呼叫之間不會在內部維護任何狀態。 若要與客服專員進行多輪對話,您需要建立一個物件來保存對話狀態,並在執行時將此物件傳遞給客服專員。
若要建立交談狀態物件,請在代理程式執行個體上呼叫 GetNewThread 方法。
AgentThread thread = agent.GetNewThread();
然後,您可以將此執行緒物件與使用者輸入一起傳遞至代理程式執行個體上的 RunAsync 和 RunStreamingAsync 方法。
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));
這將保持通話之間的對話狀態,並且客服專員在回應新輸入時將能夠參考對話中先前的輸入和回應訊息。
這很重要
所 AIAgent 使用的服務類型會決定交談歷程記錄的儲存方式。 例如,使用 ChatCompletion 服務時,如本例所示,交談歷程記錄會儲存在 AgentThread 物件中,並在每次呼叫時傳送至服務。 另一方面,使用 Azure AI 代理程式服務時,交談歷程記錄會儲存在 Azure AI 代理程式服務中,而且每次呼叫時只會將交談的參考傳送至服務。
具有多個對話的單一客服專員
透過建立多個 AgentThread 物件,可以與相同的代理程式執行個體進行多個獨立的交談。
然後,這些線程可用來維護每個對話的個別交談狀態。
對話將彼此完全獨立,因為代理程式在內部不維護任何狀態。
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));
使用多回合交談執行代理程式
代理是無狀態的,並且在呼叫之間不會在內部維護任何狀態。 若要與客服專員進行多輪對話,您需要建立一個物件來保存對話狀態,並在執行時將此物件傳遞給客服專員。
若要建立交談狀態物件,請在代理程式執行個體上呼叫 get_new_thread() 方法。
thread = agent.get_new_thread()
然後,您可以將此執行緒物件與使用者輸入一起傳遞至代理程式執行個體上的 run 和 run_stream 方法。
async def main():
result1 = await agent.run("Tell me a joke about a pirate.", thread=thread)
print(result1.text)
result2 = await agent.run("Now add some emojis to the joke and tell it in the voice of a pirate's parrot.", thread=thread)
print(result2.text)
asyncio.run(main())
這將保持通話之間的對話狀態,並且客服專員在回應新輸入時將能夠參考對話中先前的輸入和回應訊息。
這很重要
代理程式使用的服務類型將決定交談歷程記錄的儲存方式。 例如,使用聊天完成服務時,如本例所示,交談歷程記錄會儲存在 AgentThread 物件中,並在每次呼叫時傳送至服務。 另一方面,使用 Azure AI 代理程式服務時,交談歷程記錄會儲存在 Azure AI 代理程式服務中,而且每次呼叫時只會將交談的參考傳送至服務。
具有多個對話的單一客服專員
透過建立多個 AgentThread 物件,可以與相同的代理程式執行個體進行多個獨立的交談。
然後,這些線程可用來維護每個對話的個別交談狀態。
對話將彼此完全獨立,因為代理程式在內部不維護任何狀態。
async def main():
thread1 = agent.get_new_thread()
thread2 = agent.get_new_thread()
result1 = await agent.run("Tell me a joke about a pirate.", thread=thread1)
print(result1.text)
result2 = await agent.run("Tell me a joke about a robot.", thread=thread2)
print(result2.text)
result3 = await agent.run("Now add some emojis to the joke and tell it in the voice of a pirate's parrot.", thread=thread1)
print(result3.text)
result4 = await agent.run("Now add some emojis to the joke and tell it in the voice of a robot.", thread=thread2)
print(result4.text)
asyncio.run(main())