除了使用代理程式的一般操作說明外,本頁面也會說明 LangchainAgent
的專屬功能。
事前準備
本教學課程假設您已詳閱並按照下列教學課程的指示操作:
- 開發 LangChain 代理程式:將
agent
開發為LangchainAgent
的執行個體。 - 使用者驗證:以使用者身分進行驗證,以便查詢代理程式。
- 匯入並初始化 SDK,以便初始化用戶端來取得已部署的執行個體 (如有需要)。
支援的作業
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)
後續步驟
- 使用代理程式。
- 評估代理商。
- 管理已部署的代理程式。
- 取得支援。