除了使用代理程式的一般操作說明外,本頁還會說明 AG2Agent
專屬的功能。
事前準備
本教學課程假設您已閱讀並遵循以下說明:
- 開發 AG2 代理程式:將
agent
開發為AG2Agent
的例項。 - 使用者驗證:以使用者身分驗證,以便查詢服務機器人。
支援的作業
AG2Agent
支援下列作業:
query
:用於同步取得查詢的回應。
query
方法支援下列引數:
input
:要傳送給服務專員的訊息。max_turns
:允許的對話回合數量上限。使用工具時,至少需要max_turns=2
:一個回合用於產生工具引數,另一個回合用於執行工具。
查詢代理程式
query()
方法提供簡易的方式,讓您與服務專員互動。一般呼叫如下所示:
response = agent.query(input="What is the exchange rate from US dollars to Swedish currency?", max_turns=2)
這個方法會處理與代理程式之間的基礎通訊,並以字典的形式傳回代理程式的最終回應。等同於以下 (完整形式):
from autogen import ConversableAgent
import dataclasses
import json
input_message: str = "What is the exchange rate from US dollars to Swedish currency?"
max_turns: int = 2
with agent._runnable._create_or_get_executor(
tools=agent._ag2_tool_objects, # Use the agent's existing tools
agent_name="user", # Default
agent_human_input_mode="NEVER", # query() enforces this
) as executor:
chat_result = executor.initiate_chat(
agent._runnable,
message=input_message,
max_turns=max_turns,
clear_history=False, # Default
summary_method="last_msg" # Default
)
response = json.loads(
json.dumps(dataclasses.asdict(chat_result)) # query() does this conversion
)
您可以將其他關鍵字引數傳遞至 query()
,藉此自訂代理程式的行為 (除了 input
和 max_turns
以外)。
response = agent.query(
input="What is the exchange rate from US dollars to Swedish currency?",
max_turns=2,
msg_to="user" # Start the conversation with the "user" agent
)
print(response)
如需可用參數的完整清單,請參閱 ConversableAgent.run
說明文件。不過請注意,user_input
一律會由 AG2Agent 範本覆寫為 False
。
後續步驟
- 使用代理程式。
- 評估服務專員。
- 管理已部署的服務專員。
- 取得支援。