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?",},]})
多模態訊息會透過指定 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"},]})
importuuidrun_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)
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-04 (世界標準時間)。"],[],[],null,["# Use a LangChain agent\n\nIn addition to the general instructions for [using an agent](/vertex-ai/generative-ai/docs/agent-engine/use),\nthis page describes features that are specific to `LangchainAgent`.\n\nBefore you begin\n----------------\n\nThis tutorial assumes that you have read and followed the instructions in:\n\n- [Develop a LangChain agent](/vertex-ai/generative-ai/docs/agent-engine/develop/langchain): to develop `agent` as an instance of `LangchainAgent`.\n- [User authentication](/vertex-ai/generative-ai/docs/agent-engine/set-up#authentication) to authenticate as a user for querying the agent.\n\nSupported operations\n--------------------\n\nThe following operations are supported for `LangchainAgent`:\n\n- [`query`](/vertex-ai/generative-ai/docs/agent-engine/use#query-agent): for getting a response to a query synchronously.\n- [`stream_query`](/vertex-ai/generative-ai/docs/agent-engine/use#stream-responses): for streaming a response to a query.\n\nBoth `query` and `stream_query` methods support the same type of arguments:\n\n- [`input`](#input-messages): the messages to be sent to the agent.\n- [`config`](#runnable-configuration): the configuration (if applicable) for the context of the query.\n\nQuery the agent\n---------------\n\nThe command: \n\n agent.query(input=\"What is the exchange rate from US dollars to SEK today?\")\n\nis equivalent to the following (in full form): \n\n agent.query(input={\n \"input\": [ # The input is represented as a list of messages (each message as a dict)\n {\n # The role (e.g. \"system\", \"user\", \"assistant\", \"tool\")\n \"role\": \"user\",\n # The type (e.g. \"text\", \"tool_use\", \"image_url\", \"media\")\n \"type\": \"text\",\n # The rest of the message (this varies based on the type)\n \"text\": \"What is the exchange rate from US dollars to Swedish currency?\",\n },\n ]\n })\n\nRoles are used to help the model distinguish between different types of [messages](https://python.langchain.com/docs/concepts/messages)\nwhen responding. When the `role` is omitted in the input, it defaults to `\"user\"`.\n\nThe `type` of the message will also determine how the rest of the message is\ninterpreted (see [Handle multi-modal content](#multimodal-content)).\n\nQuery the agent with multi-modal content\n----------------------------------------\n\nWe will use the following agent (which forwards the input to the model and does\nnot use any tools) to illustrate how to pass in multimodal inputs to an agent:\n**Note:** there isn't any known support for multi-modal outputs. \n\n agent = agent_engines.LangchainAgent(\n model=\"gemini-2.0-flash\",\n runnable_builder=lambda model, **kwargs: model,\n )\n\nMultimodal messages are represented through content blocks that specify a `type`\nand corresponding data. In general, for multimodal content, you would specify\nthe `type` to be `\"media\"`, the `file_uri` to point to a Cloud Storage URI,\nand the `mime_type` for interpreting the file. \n\n### Image\n\n agent.query(input={\"input\": [\n {\"type\": \"text\", \"text\": \"Describe the attached media in 5 words!\"},\n {\"type\": \"media\", \"mime_type\": \"image/jpeg\", \"file_uri\": \"gs://cloud-samples-data/generative-ai/image/cricket.jpeg\"},\n ]})\n\n### Video\n\n agent.query(input={\"input\": [\n {\"type\": \"text\", \"text\": \"Describe the attached media in 5 words!\"},\n {\"type\": \"media\", \"mime_type\": \"video/mp4\", \"file_uri\": \"gs://cloud-samples-data/generative-ai/video/pixel8.mp4\"},\n ]})\n\n### Audio\n\n agent.query(input={\"input\": [\n {\"type\": \"text\", \"text\": \"Describe the attached media in 5 words!\"},\n {\"type\": \"media\", \"mime_type\": \"audio/mp3\", \"file_uri\": \"gs://cloud-samples-data/generative-ai/audio/pixel.mp3\"},\n ]})\n\nFor the list of MIME types supported by Gemini, visit the documentation on:\n\n- [Image](/vertex-ai/generative-ai/docs/multimodal/image-understanding#image-requirements)\n- [Video](/vertex-ai/generative-ai/docs/multimodal/video-understanding#video-requirements)\n- [Audio](/vertex-ai/generative-ai/docs/multimodal/audio-understanding#audio-requirements)\n\nQuery the agent with a runnable configuration\n---------------------------------------------\n\nWhen querying the agent, you can also specify a `config` for the agent (which\nfollows the schema of a [`RunnableConfig`](https://python.langchain.com/docs/concepts/runnables/#runnableconfig)).\nTwo common scenarios are:\n\n- Default configuration parameters:\n - `run_id` / `run_name`: identifier for the run.\n - `tags` / `metadata`: classifier for the run when [tracing with OpenTelemetry](/vertex-ai/generative-ai/docs/agent-engine/develop/custom#tracing).\n- Custom configuration parameters (via `configurable`):\n - `session_id`: the session under which the run is happening (see [Store chat history](/vertex-ai/generative-ai/docs/agent-engine/develop/langchain#chat-history)).\n - `thread_id`: the thread under which the run is happening (see [Store Checkpoints](/vertex-ai/generative-ai/docs/agent-engine/develop/langgraph#store-checkpoints)).\n\nAs an example: \n\n import uuid\n\n run_id = uuid.uuid4() # Generate an ID for tracking the run later.\n\n response = agent.query(\n input=\"What is the exchange rate from US dollars to Swedish currency?\",\n config={ # Specify the RunnableConfig here.\n \"run_id\": run_id # Optional.\n \"tags\": [\"config-tag\"], # Optional.\n \"metadata\": {\"config-key\": \"config-value\"}, # Optional.\n \"configurable\": {\"session_id\": \"SESSION_ID\"} # Optional.\n },\n )\n\n print(response)\n\nWhat's next\n-----------\n\n- [Use an agent](/vertex-ai/generative-ai/docs/agent-engine/use).\n- [Evaluate an agent](/vertex-ai/generative-ai/docs/agent-engine/evaluate).\n- [Manage deployed agents](/vertex-ai/generative-ai/docs/agent-engine/manage).\n- [Get support](/vertex-ai/generative-ai/docs/agent-engine/support)."]]