本頁說明如何使用架構專屬的 LangChain 範本 (Vertex AI SDK for Python 中的 LangchainAgent
類別) 開發代理程式。代理程式會傳回指定日期的兩種貨幣匯率。步驟如下:
事前準備
請按照「設定環境」一文中的步驟,確認環境已設定完成。
步驟 1:定義及設定模型
定義要使用的模型版本。
model = "gemini-2.0-flash"
(選用) 設定模型的安全性設定。如要進一步瞭解 Gemini 安全設定的可用選項,請參閱設定安全屬性。 以下範例說明如何設定安全設定:
from langchain_google_vertexai import HarmBlockThreshold, HarmCategory
safety_settings = {
HarmCategory.HARM_CATEGORY_UNSPECIFIED: HarmBlockThreshold.BLOCK_NONE,
HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_ONLY_HIGH,
HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE,
}
(選用) 按照下列方式指定模型參數:
model_kwargs = {
# temperature (float): The sampling temperature controls the degree of
# randomness in token selection.
"temperature": 0.28,
# max_output_tokens (int): The token limit determines the maximum amount of
# text output from one prompt.
"max_output_tokens": 1000,
# top_p (float): Tokens are selected from most probable to least until
# the sum of their probabilities equals the top-p value.
"top_p": 0.95,
# top_k (int): The next token is selected from among the top-k most
# probable tokens. This is not supported by all model versions. See
# https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/image-understanding#valid_parameter_values
# for details.
"top_k": None,
# safety_settings (Dict[HarmCategory, HarmBlockThreshold]): The safety
# settings to use for generating content.
# (you must create your safety settings using the previous step first).
"safety_settings": safety_settings,
}
使用模型設定建立 LangchainAgent
:
from vertexai import agent_engines
agent = agent_engines.LangchainAgent(
model=model, # Required.
model_kwargs=model_kwargs, # Optional.
)
如果您在互動式環境 (例如終端機或 Colab 筆記本) 中執行作業,可以執行查詢做為中繼測試步驟:
response = agent.query(input="What is the exchange rate from US dollars to SEK today?")
print(response)
回應是類似下列範例的 Python 字典:
{"input": "What is the exchange rate from US dollars to Swedish currency?",
"output": """I cannot provide the live exchange rate from US dollars to Swedish currency (Swedish krona, SEK).
**Here's why:**
* **Exchange rates constantly fluctuate.** Factors like global economics, interest rates, and political events cause
these changes throughout the day.
* **Providing inaccurate information would be misleading.**
**How to find the current exchange rate:**
1. **Use a reliable online converter:** Many websites specialize in live exchange rates. Some popular options include:
* Google Finance (google.com/finance)
* XE.com
* Bank websites (like Bank of America, Chase, etc.)
2. **Contact your bank or financial institution:** They can give you the exact exchange rate they are using.
Remember to factor in any fees or commissions when exchanging currency.
"""}
(選用) 進階自訂
LangchainAgent
範本預設使用 ChatVertexAI
,因為這可存取 Google Cloud中所有可用的基礎模型。如要使用 ChatVertexAI
無法提供的模型,可以指定 model_builder=
引數,並使用下列簽章的 Python 函式:
from typing import Optional
def model_builder(
*,
model_name: str, # Required. The name of the model
model_kwargs: Optional[dict] = None, # Optional. The model keyword arguments.
**kwargs, # Optional. The remaining keyword arguments to be ignored.
):
如需 LangChain 支援的即時通訊模型清單及其功能,請參閱「即時通訊模型」。model=
和 model_kwargs=
的支援值組合會因各個即時通訊模型而異,因此您必須參閱對應的說明文件瞭解詳情。
ChatVertexAI
預設為已安裝。
省略 model_builder
引數時,會用於 LangchainAgent
,例如
from vertexai import agent_engines
agent = agent_engines.LangchainAgent(
model=model, # Required.
model_kwargs=model_kwargs, # Optional.
)
ChatAnthropic
首先,請按照他們的文件設定帳戶並安裝套件。
接著,定義會傳回 ChatAnthropic
的 model_builder
:
def model_builder(*, model_name: str, model_kwargs = None, **kwargs):
from langchain_anthropic import ChatAnthropic
return ChatAnthropic(model_name=model_name, **model_kwargs)
最後,請使用下列程式碼在 LangchainAgent
中使用:
from vertexai import agent_engines
agent = agent_engines.LangchainAgent(
model="claude-3-opus-20240229", # Required.
model_builder=model_builder, # Required.
model_kwargs={
"api_key": "ANTHROPIC_API_KEY", # Required.
"temperature": 0.28, # Optional.
"max_tokens": 1000, # Optional.
},
)
ChatOpenAI
您可以搭配使用 ChatOpenAI
和 Gemini 的 ChatCompletions API。
首先,請按照說明文件安裝套件。
接著,定義會傳回 ChatOpenAI
的 model_builder
:
def model_builder(
*,
model_name: str,
model_kwargs = None,
project: str, # Specified via vertexai.init
location: str, # Specified via vertexai.init
**kwargs,
):
import google.auth
from langchain_openai import ChatOpenAI
# Note: the credential lives for 1 hour by default.
# After expiration, it must be refreshed.
creds, _ = google.auth.default(scopes=["https://www.googleapis.com/auth/cloud-platform"])
auth_req = google.auth.transport.requests.Request()
creds.refresh(auth_req)
if model_kwargs is None:
model_kwargs = {}
endpoint = f"https://{location}-aiplatform.googleapis.com"
base_url = f'{endpoint}/v1beta1/projects/{project}/locations/{location}/endpoints/openapi'
return ChatOpenAI(
model=model_name,
base_url=base_url,
api_key=creds.token,
**model_kwargs,
)
最後,請使用下列程式碼在 LangchainAgent
中使用:
from vertexai import agent_engines
agent = agent_engines.LangchainAgent(
model="google/gemini-2.0-flash", # Or "meta/llama3-405b-instruct-maas"
model_builder=model_builder, # Required.
model_kwargs={
"temperature": 0, # Optional.
"max_retries": 2, # Optional.
},
)
步驟 2:定義及使用工具
定義模型後,下一步是定義模型用於推理的工具。工具可以是 LangChain 工具或 Python 函式。您也可以將定義的 Python 函式轉換為 LangChain 工具。
定義函式時,請務必加入註解,完整且清楚地說明函式的參數、函式的作用,以及函式傳回的內容。模型會根據這項資訊判斷要使用哪個函式。您也必須在本機測試函式,確認函式運作正常。
使用下列程式碼定義函式,傳回匯率:
def get_exchange_rate(
currency_from: str = "USD",
currency_to: str = "EUR",
currency_date: str = "latest",
):
"""Retrieves the exchange rate between two currencies on a specified date.
Uses the Frankfurter API (https://api.frankfurter.app/) to obtain
exchange rate data.
Args:
currency_from: The base currency (3-letter currency code).
Defaults to "USD" (US Dollar).
currency_to: The target currency (3-letter currency code).
Defaults to "EUR" (Euro).
currency_date: The date for which to retrieve the exchange rate.
Defaults to "latest" for the most recent exchange rate data.
Can be specified in YYYY-MM-DD format for historical rates.
Returns:
dict: A dictionary containing the exchange rate information.
Example: {"amount": 1.0, "base": "USD", "date": "2023-11-24",
"rates": {"EUR": 0.95534}}
"""
import requests
response = requests.get(
f"https://api.frankfurter.app/{currency_date}",
params={"from": currency_from, "to": currency_to},
)
return response.json()
如要在代理程式中使用函式前先測試,請執行下列指令:
get_exchange_rate(currency_from="USD", currency_to="SEK")
回覆內容應如下所示:
{'amount': 1.0, 'base': 'USD', 'date': '2024-02-22', 'rates': {'SEK': 10.3043}}
如要在 LangchainAgent
中使用這項工具,請將其新增至 tools=
引數下的工具清單:
from vertexai import agent_engines
agent = agent_engines.LangchainAgent(
model=model, # Required.
tools=[get_exchange_rate], # Optional.
model_kwargs=model_kwargs, # Optional.
)
您可以對代理執行測試查詢,在本機測試代理。執行下列指令,使用美元和瑞典克朗在本機測試代理程式:
response = agent.query(
input="What is the exchange rate from US dollars to Swedish currency?"
)
回應是類似下列內容的字典:
{"input": "What is the exchange rate from US dollars to Swedish currency?",
"output": "For 1 US dollar you will get 10.7345 Swedish Krona."}
(選用) 多種工具
工具 LangchainAgent
可以透過其他方式定義及例項化。
接地工具
首先,匯入 generate_models
套件並建立工具
from vertexai.generative_models import grounding, Tool
grounded_search_tool = Tool.from_google_search_retrieval(
grounding.GoogleSearchRetrieval()
)
接下來,請在 LangchainAgent
中使用這項工具:
from vertexai import agent_engines
agent = agent_engines.LangchainAgent(
model=model,
tools=[grounded_search_tool],
)
agent.query(input="When is the next total solar eclipse in US?")
回應是類似下列內容的字典:
{"input": "When is the next total solar eclipse in US?",
"output": """The next total solar eclipse in the U.S. will be on August 23, 2044.
This eclipse will be visible from three states: Montana, North Dakota, and
South Dakota. The path of totality will begin in Greenland, travel through
Canada, and end around sunset in the United States."""}
詳情請參閱「基礎」。
LangChain 工具
首先,請安裝定義工具的套件。
pip install langchain-google-community
接著,匯入套件並建立工具。
from langchain_google_community import VertexAISearchRetriever
from langchain.tools.retriever import create_retriever_tool
retriever = VertexAISearchRetriever(
project_id="PROJECT_ID",
data_store_id="DATA_STORE_ID",
location_id="DATA_STORE_LOCATION_ID",
engine_data_type=1,
max_documents=10,
)
movie_search_tool = create_retriever_tool(
retriever=retriever,
name="search_movies",
description="Searches information about movies.",
)
最後,使用 LangchainAgent
內的工具:
from vertexai import agent_engines
agent = agent_engines.LangchainAgent(
model=model,
tools=[movie_search_tool],
)
response = agent.query(
input="List some sci-fi movies from the 1990s",
)
系統應會傳回類似以下的回覆:
{"input": "List some sci-fi movies from the 1990s",
"output": """Here are some sci-fi movies from the 1990s:
* The Matrix (1999): A computer hacker learns from mysterious rebels about the true nature of his reality and his role in the war against its controllers.
* Star Wars: Episode I - The Phantom Menace (1999): Two Jedi Knights escape a hostile blockade to find a queen and her protector, and come across a young boy [...]
* Men in Black (1997): A police officer joins a secret organization that monitors extraterrestrial interactions on Earth.
[...]
"""}
如要查看完整範例,請參閱筆記本。
如要查看 LangChain 中可用的工具,請參閱 Google 工具。
Vertex AI 擴充功能
首先,匯入擴充功能套件並建立工具
from typing import Optional
def generate_and_execute_code(
query: str,
files: Optional[list[str]] = None,
file_gcs_uris: Optional[list[str]] = None,
) -> str:
"""Get the results of a natural language query by generating and executing
a code snippet.
Example queries: "Find the max in [1, 2, 5]" or "Plot average sales by
year (from data.csv)". Only one of `file_gcs_uris` and `files` field
should be provided.
Args:
query:
The natural language query to generate and execute.
file_gcs_uris:
Optional. URIs of input files to use when executing the code
snippet. For example, ["gs://input-bucket/data.csv"].
files:
Optional. Input files to use when executing the generated code.
If specified, the file contents are expected be base64-encoded.
For example: [{"name": "data.csv", "contents": "aXRlbTEsaXRlbTI="}].
Returns:
The results of the query.
"""
operation_params = {"query": query}
if files:
operation_params["files"] = files
if file_gcs_uris:
operation_params["file_gcs_uris"] = file_gcs_uris
from vertexai.preview import extensions
# If you have an existing extension instance, you can get it here
# i.e. code_interpreter = extensions.Extension(resource_name).
code_interpreter = extensions.Extension.from_hub("code_interpreter")
return extensions.Extension.from_hub("code_interpreter").execute(
operation_id="generate_and_execute",
operation_params=operation_params,
)
接下來,請在 LangchainAgent
中使用這項工具:
from vertexai import agent_engines
agent = agent_engines.LangchainAgent(
model=model,
tools=[generate_and_execute_code],
)
agent.query(
input="""Using the data below, construct a bar chart that includes only the height values with different colors for the bars:
tree_heights_prices = {
\"Pine\": {\"height\": 100, \"price\": 100},
\"Oak\": {\"height\": 65, \"price\": 135},
\"Birch\": {\"height\": 45, \"price\": 80},
\"Redwood\": {\"height\": 200, \"price\": 200},
\"Fir\": {\"height\": 180, \"price\": 162},
}
"""
)
系統應會傳回類似以下的回覆:
{"input": """Using the data below, construct a bar chart that includes only the height values with different colors for the bars:
tree_heights_prices = {
\"Pine\": {\"height\": 100, \"price\": 100},
\"Oak\": {\"height\": 65, \"price\": 135},
\"Birch\": {\"height\": 45, \"price\": 80},
\"Redwood\": {\"height\": 200, \"price\": 200},
\"Fir\": {\"height\": 180, \"price\": 162},
}
""",
"output": """Here's the generated bar chart:
```python
import matplotlib.pyplot as plt
tree_heights_prices = {
"Pine": {"height": 100, "price": 100},
"Oak": {"height": 65, "price": 135},
"Birch": {"height": 45, "price": 80},
"Redwood": {"height": 200, "price": 200},
"Fir": {"height": 180, "price": 162},
}
heights = [tree["height"] for tree in tree_heights_prices.values()]
names = list(tree_heights_prices.keys())
plt.bar(names, heights, color=['red', 'green', 'blue', 'purple', 'orange'])
plt.xlabel('Tree Species')
plt.ylabel('Height')
plt.title('Tree Heights')
plt.show()
```
"""}
如要讓已部署的代理程式存取 Code Interpreter 擴充功能,請將 Vertex AI 使用者角色 (roles/aiplatform.user
) 新增至 AI Platform Reasoning Engine Service Agent 服務帳戶。詳情請參閱「管理存取權」。
詳情請參閱「Vertex AI 擴充功能」。
您可以使用在 LangchainAgent
中建立的所有工具 (或部分工具):
from vertexai import agent_engines
agent = agent_engines.LangchainAgent(
model=model,
tools=[
get_exchange_rate, # Optional (Python function)
grounded_search_tool, # Optional (Grounding Tool)
movie_search_tool, # Optional (Langchain Tool)
generate_and_execute_code, # Optional (Vertex Extension)
],
)
agent.query(input="When is the next total solar eclipse in US?")
(選用) 工具設定
使用 Gemini 時,你可以限制工具的使用方式。舉例來說,您可以強制模型只生成函式呼叫 (「強制函式呼叫」),而非允許模型生成自然語言回應。
from vertexai import agent_engines
from vertexai.preview.generative_models import ToolConfig
agent = agent_engines.LangchainAgent(
model="gemini-2.0-flash",
tools=[search_arxiv, get_exchange_rate],
model_tool_kwargs={
"tool_config": { # Specify the tool configuration here.
"function_calling_config": {
"mode": ToolConfig.FunctionCallingConfig.Mode.ANY,
"allowed_function_names": ["search_arxiv", "get_exchange_rate"],
},
},
},
)
agent.query(
input="Explain the Schrodinger equation in a few sentences",
)
詳情請參閱「工具設定」。
步驟 3:儲存即時通訊記錄
如要追蹤即時通訊訊息並附加至資料庫,請定義 get_session_history
函式,並在建立代理程式時傳遞該函式。這個函式應採用 session_id
,並傳回 BaseChatMessageHistory
物件。
session_id
是這些輸入訊息所屬工作階段的 ID。這樣一來,你就能同時進行多個對話。BaseChatMessageHistory
是可載入及儲存訊息物件的類別介面。
設定資料庫
如要查看 LangChain 支援的 Google ChatMessageHistory
提供者清單,請參閱「記憶體」。
首先,請按照 LangChain 的文件安裝及使用相關套件,設定所選資料庫 (例如 Firestore、Bigtable 或 Spanner):
接著,請按照下列方式定義 get_session_history
函式:
Firestore (原生模式)
def get_session_history(session_id: str):
from langchain_google_firestore import FirestoreChatMessageHistory
from google.cloud import firestore
client = firestore.Client(project="PROJECT_ID")
return FirestoreChatMessageHistory(
client=client,
session_id=session_id,
collection="TABLE_NAME",
encode_message=False,
)
Bigtable
def get_session_history(session_id: str):
from langchain_google_bigtable import BigtableChatMessageHistory
return BigtableChatMessageHistory(
instance_id="INSTANCE_ID",
table_id="TABLE_NAME",
session_id=session_id,
)
Spanner
def get_session_history(session_id: str):
from langchain_google_spanner import SpannerChatMessageHistory
return SpannerChatMessageHistory(
instance_id="INSTANCE_ID",
database_id="DATABASE_ID",
table_name="TABLE_NAME",
session_id=session_id,
)
最後,建立代理程式並以 chat_history
形式傳遞:
from vertexai import agent_engines
agent = agent_engines.LangchainAgent(
model=model,
chat_history=get_session_history, # <- new
)
查詢代理程式時,請務必傳遞 session_id
,讓代理程式「記住」先前的問題和答案:
agent.query(
input="What is the exchange rate from US dollars to Swedish currency?",
config={"configurable": {"session_id": "SESSION_ID"}},
)
您可以檢查後續查詢是否會保留工作階段的記憶體:
response = agent.query(
input="How much is 100 USD?",
config={"configurable": {"session_id": "SESSION_ID"}},
)
print(response)
步驟 4:自訂提示範本
提示範本有助於將使用者輸入內容轉換為模型指令,並引導模型回覆,協助模型瞭解脈絡,生成相關且連貫的語言輸出內容。詳情請參閱「ChatPromptTemplates」。
預設提示範本會依序分成多個區段。
區段 | 說明 |
---|---|
(選填) 系統指令 | 適用於所有查詢的代理程式指令。 |
(選用) 即時通訊記錄 | 與上個工作階段的即時通訊記錄相符的訊息。 |
使用者輸入內容 | 使用者提出的查詢,代理程式會根據這項查詢做出回應。 |
服務專員便條簿 | 代理程式建立的訊息 (例如透過函式呼叫),因為代理程式會使用工具並執行推理,以擬定給使用者的回覆。 |
如果您建立代理程式時未指定自己的提示範本,系統就會產生預設提示範本,完整內容如下:
from langchain_core.prompts import ChatPromptTemplate
from langchain.agents.format_scratchpad.tools import format_to_tool_messages
prompt_template = {
"user_input": lambda x: x["input"],
"history": lambda x: x["history"],
"agent_scratchpad": lambda x: format_to_tool_messages(x["intermediate_steps"]),
} | ChatPromptTemplate.from_messages([
("system", "{system_instruction}"),
("placeholder", "{history}"),
("user", "{user_input}"),
("placeholder", "{agent_scratchpad}"),
])
在下列範例中,您在例項化代理程式時,會隱含使用完整提示範本:
from vertexai import agent_engines
system_instruction = "I help look up the rate between currencies"
agent = agent_engines.LangchainAgent(
model=model,
system_instruction=system_instruction,
chat_history=get_session_history,
tools=[get_exchange_rate],
)
您可以覆寫預設提示範本,改用自己的提示範本,並在建構代理程式時使用,例如:
from vertexai import agent_engines
custom_prompt_template = {
"user_input": lambda x: x["input"],
"history": lambda x: x["history"],
"agent_scratchpad": lambda x: format_to_tool_messages(x["intermediate_steps"]),
} | ChatPromptTemplate.from_messages([
("placeholder", "{history}"),
("user", "{user_input}"),
("placeholder", "{agent_scratchpad}"),
])
agent = agent_engines.LangchainAgent(
model=model,
prompt=custom_prompt_template,
chat_history=get_session_history,
tools=[get_exchange_rate],
)
agent.query(
input="What is the exchange rate from US dollars to Swedish currency?",
config={"configurable": {"session_id": "SESSION_ID"}},
)
步驟 5:自訂協調流程
所有 LangChain 元件都會實作 Runnable 介面,提供用於協調流程的輸入和輸出結構定義。LangchainAgent
類別需要建構可執行的項目,才能回應查詢。根據預設,LangchainAgent
會將模型與工具繫結,藉此建構這類可執行的項目,並使用封裝在 RunnableWithMessageHistory
中的 AgentExecutor
(如果已啟用即時通訊記錄)。
如果您打算 (i) 實作執行確定步驟組合的代理程式 (而非執行開放式推理),或 (ii) 以類似 ReAct 的方式提示代理程式,為每個步驟加上執行該步驟原因的附註,則可能需要自訂協調程序。如要執行這項操作,您必須在建立 LangchainAgent
時覆寫預設可執行檔,方法是使用下列簽章指定 runnable_builder=
引數和 Python 函式:
from typing import Optional
from langchain_core.language_models import BaseLanguageModel
def runnable_builder(
model: BaseLanguageModel,
*,
system_instruction: Optional[str] = None,
prompt: Optional["RunnableSerializable"] = None,
tools: Optional[Sequence["_ToolLike"]] = None,
chat_history: Optional["GetSessionHistoryCallable"] = None,
model_tool_kwargs: Optional[Mapping[str, Any]] = None,
agent_executor_kwargs: Optional[Mapping[str, Any]] = None,
runnable_kwargs: Optional[Mapping[str, Any]] = None,
**kwargs,
):
其中
model
對應於從model_builder
傳回的即時通訊模型 (請參閱「定義及設定模型」),tools
和model_tool_kwargs
對應要使用的工具和設定 (請參閱「定義及使用工具」),chat_history
對應於儲存即時通訊訊息的資料庫 (請參閱「儲存即時通訊記錄」),system_instruction
和prompt
對應提示設定 (請參閱「自訂提示範本」),agent_executor_kwargs
和runnable_kwargs
是關鍵字引數,可用於自訂要建構的可執行檔。
這項功能提供多種自訂協調邏輯的選項。
ChatModel
在最簡單的情況下,如要建立不含協調機制的代理程式,您可以覆寫 runnable_builder
的 LangchainAgent
,直接傳回 model
。
from vertexai import agent_engines
from langchain_core.language_models import BaseLanguageModel
def llm_builder(model: BaseLanguageModel, **kwargs):
return model
agent = agent_engines.LangchainAgent(
model=model,
runnable_builder=llm_builder,
)
ReAct
如要使用自己的 ReAct 代理程式 (以自己的 prompt
為準,請參閱「自訂提示範本」),覆寫預設的工具呼叫行為,您需要覆寫 LangchainAgent
的 runnable_builder
。
from typing import Sequence
from langchain_core.language_models import BaseLanguageModel
from langchain_core.prompts import BasePromptTemplate
from langchain_core.tools import BaseTool
from langchain import hub
from vertexai import agent_engines
def react_builder(
model: BaseLanguageModel,
*,
tools: Sequence[BaseTool],
prompt: BasePromptTemplate,
agent_executor_kwargs = None,
**kwargs,
):
from langchain.agents.react.agent import create_react_agent
from langchain.agents import AgentExecutor
agent = create_react_agent(model, tools, prompt)
return AgentExecutor(agent=agent, tools=tools, **agent_executor_kwargs)
agent = agent_engines.LangchainAgent(
model=model,
tools=[get_exchange_rate],
prompt=hub.pull("hwchase17/react"),
agent_executor_kwargs={"verbose": True}, # Optional. For illustration.
runnable_builder=react_builder,
)
LCEL 語法
如要使用 LangChain 運算式語言 (LCEL) 建構下圖,
Input
/ \
Pros Cons
\ /
Summary
您需要覆寫 LangchainAgent
的 runnable_builder
:
from vertexai import agent_engines
def lcel_builder(*, model, **kwargs):
from operator import itemgetter
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnablePassthrough
from langchain_core.output_parsers import StrOutputParser
output_parser = StrOutputParser()
planner = ChatPromptTemplate.from_template(
"Generate an argument about: {input}"
) | model | output_parser | {"argument": RunnablePassthrough()}
pros = ChatPromptTemplate.from_template(
"List the positive aspects of {argument}"
) | model | output_parser
cons = ChatPromptTemplate.from_template(
"List the negative aspects of {argument}"
) | model | output_parser
final_responder = ChatPromptTemplate.from_template(
"Argument:{argument}\nPros:\n{pros}\n\nCons:\n{cons}\n"
"Generate a final response given the critique",
) | model | output_parser
return planner | {
"pros": pros,
"cons": cons,
"argument": itemgetter("argument"),
} | final_responder
agent = agent_engines.LangchainAgent(
model=model,
runnable_builder=lcel_builder,
)
LangGraph
如要使用 LangGraph 建構下圖,
Input
/ \
Pros Cons
\ /
Summary
您需要覆寫 LangchainAgent
的 runnable_builder
:
from vertexai import agent_engines
def langgraph_builder(*, model, **kwargs):
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langgraph.graph import END, MessageGraph
output_parser = StrOutputParser()
planner = ChatPromptTemplate.from_template(
"Generate an argument about: {input}"
) | model | output_parser
pros = ChatPromptTemplate.from_template(
"List the positive aspects of {input}"
) | model | output_parser
cons = ChatPromptTemplate.from_template(
"List the negative aspects of {input}"
) | model | output_parser
summary = ChatPromptTemplate.from_template(
"Input:{input}\nGenerate a final response given the critique",
) | model | output_parser
builder = MessageGraph()
builder.add_node("planner", planner)
builder.add_node("pros", pros)
builder.add_node("cons", cons)
builder.add_node("summary", summary)
builder.add_edge("planner", "pros")
builder.add_edge("planner", "cons")
builder.add_edge("pros", "summary")
builder.add_edge("cons", "summary")
builder.add_edge("summary", END)
builder.set_entry_point("planner")
return builder.compile()
agent = agent_engines.LangchainAgent(
model=model,
runnable_builder=langgraph_builder,
)
# Example query
agent.query(input={"role": "user", "content": "scrum methodology"})