除了使用代理程式的一般操作說明外,本頁還會說明 LangchainAgent
專屬的功能。
事前準備
本教學課程假設您已閱讀並遵循以下說明:
- 開發 LangChain 代理程式:將
agent
開發為LangchainAgent
的例項。 - 使用者驗證:以使用者身分驗證,以便查詢服務專員。
支援的作業
LangchainAgent
支援下列作業:
query
:用於同步取得查詢的回應。stream_query
:用於將查詢的回應串流傳輸。
query
和 stream_query
方法都支援相同類型的引數:
查詢代理程式
指令:
agent.query(input="What is the exchange rate from US dollars to SEK today?")
等同於以下 (完整形式):
agent.query(input={
"input": [ # The input is represented as a list of messages (each message as a dict)
{
# The role (e.g. "system", "user", "assistant", "tool")
"role": "user",
# The type (e.g. "text", "tool_use", "image_url", "media")
"type": "text",
# The rest of the message (this varies based on the type)
"text": "What is the exchange rate from US dollars to Swedish currency?",
},
]
})
角色可協助模型在回應時,區分不同類型的訊息。如果輸入內容中省略 role
,系統會預設為 "user"
。
角色 | 說明 |
---|---|
system |
用於告訴聊天模型如何運作,並提供額外背景資訊。部分即時通訊模型供應商不支援此功能。 |
user |
代表使用者與模型互動時的輸入內容,通常為文字或其他互動輸入內容。 |
assistant |
代表模型的回應,可包含文字或叫用工具的要求。 |
tool |
在擷取外部資料或處理作業後,用於將工具叫用結果傳回模型的訊息。 |
訊息的 type
也會決定如何解讀訊息的其餘部分 (請參閱「處理多模態內容」)。
使用多模態內容查詢服務機器人
我們會使用以下代理程式 (會將輸入內容轉送至模型,且不使用任何工具),說明如何將多模態輸入內容傳入代理程式:
agent = agent_engines.LangchainAgent(
model="gemini-2.0-flash",
runnable_builder=lambda model, **kwargs: model,
)
多模態訊息會透過指定 type
和對應資料的內容區塊呈現。一般來說,如果是多模態內容,您可以將 type
指定為 "media"
,將 file_uri
指向 Cloud Storage URI,並將 mime_type
用於解讀檔案。
映像檔
agent.query(input={"input": [
{"type": "text", "text": "Describe the attached media in 5 words!"},
{"type": "media", "mime_type": "image/jpeg", "file_uri": "gs://cloud-samples-data/generative-ai/image/cricket.jpeg"},
]})
影片
agent.query(input={"input": [
{"type": "text", "text": "Describe the attached media in 5 words!"},
{"type": "media", "mime_type": "video/mp4", "file_uri": "gs://cloud-samples-data/generative-ai/video/pixel8.mp4"},
]})
音訊
agent.query(input={"input": [
{"type": "text", "text": "Describe the attached media in 5 words!"},
{"type": "media", "mime_type": "audio/mp3", "file_uri": "gs://cloud-samples-data/generative-ai/audio/pixel.mp3"},
]})
如要查看 Gemini 支援的 MIME 類型清單,請參閱以下說明文件:
使用可執行設定查詢代理程式
查詢代理程式時,您也可以為代理程式指定 config
(遵循 RunnableConfig
的結構定義)。以下是兩種常見情況:
- 預設設定參數:
run_id
/run_name
:執行作業的 ID。tags
/metadata
:使用 OpenTelemetry 進行追蹤時的執行作業分類器。
- 自訂設定參數 (透過
configurable
):
舉例來說:
import uuid
run_id = uuid.uuid4() # Generate an ID for tracking the run later.
response = agent.query(
input="What is the exchange rate from US dollars to Swedish currency?",
config={ # Specify the RunnableConfig here.
"run_id": run_id # Optional.
"tags": ["config-tag"], # Optional.
"metadata": {"config-key": "config-value"}, # Optional.
"configurable": {"session_id": "SESSION_ID"} # Optional.
},
)
print(response)
後續步驟
- 使用代理程式。
- 評估服務專員。
- 管理已部署的服務專員。
- 取得支援。