开发应用

比如说,您可以使用 LangChain on Vertex AI 创建一个小型应用来返回指定日期两种货币之间的汇率。

您可以定义自己的 Python 类(请参阅自定义应用模板),也可以使用 Vertex AI SDK for Python 中适用于您的代理的 LangchainAgent 类。以下步骤演示了如何使用 LangchainAgent 预构建模板创建此应用:

  1. 定义和配置模型
  2. 定义和使用工具
  3. (可选)存储聊天记录
  4. (可选)自定义提示模板
  5. (可选)自定义编排

准备工作

在运行本教程之前,请确保按照设置环境中的步骤设置您的环境。

第 1 步:定义和配置模型

请按照以下步骤定义和配置模型:

  1. 您需要定义要使用的模型版本

    model = "gemini-1.5-flash-001"
    
  2. (可选)您可以配置模型的安全设置。如需详细了解可用于在 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,
    }
    
  3. (可选)您可以通过以下方式指定模型参数

    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

agent = reasoning_engines.LangchainAgent(
    model=model,                # Required.
    model_kwargs=model_kwargs,  # Optional.
)

response = agent.query(input="What is the exchange rate from US dollars to Swedish currency?")

响应是一个 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 获取的模型,您可以使用具有以下签名的 Python 函数指定 model_builder= 参数:

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 模板中使用,例如

agent = reasoning_engines.LangchainAgent(
    model=model,                # Required.
    model_kwargs=model_kwargs,  # Optional.
)

ChatAnthropic

首先,按照他们的文档设置账号并安装软件包。

接下来,定义一个返回 ChatAnthropicmodel_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 模板中使用它:

agent = reasoning_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 搭配使用。

首先,按照其文档安装软件包。

接下来,定义一个返回 ChatOpenAImodel_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 模板中使用它:

agent = reasoning_engines.LangchainAgent(
    model="google/gemini-1.5-pro-001",  # 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= 参数下的工具列表中:

agent = reasoning_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 模板中使用该工具:

agent = reasoning_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 模板中使用该工具:

agent = reasoning_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 Extensions

首先,导入扩展程序软件包并创建工具

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 模板中使用该工具:

agent = reasoning_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()
 ```
 """}

如需了解详情,请参阅 Vertex AI 扩展

您可以使用自己在 LangchainAgent 中创建的所有(或部分)工具:

agent = reasoning_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.preview.generative_models import ToolConfig

agent = reasoning_engines.LangchainAgent(
    model="gemini-1.5-pro",
    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 是这些输入消息所属会话的标识符。这样,您就可以同时进行多个对话。
  • BaseChatMessageHistory 是可加载和保存消息对象的类的接口。

设置数据库

如需查看 LangChain 中支持的 Google ChatMessageHistory 提供程序的列表,请参阅内存

Firestore(原生)

首先,按照 LangChain 的文档设置数据库并安装软件包。

接下来,按如下方式定义 get_session_history 函数:

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,
    )

创建代理并将其作为 chat_history 传入:

agent = reasoning_engines.LangchainAgent(
    model=model,
    chat_history=get_session_history,  # <- new
)

Bigtable

首先,按照 LangChain 的文档设置数据库并安装软件包。

接下来,按如下方式定义 get_session_history 函数:

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,
    )

创建代理并将其作为 chat_history 传入:

agent = reasoning_engines.LangchainAgent(
    model=model,
    chat_history=get_session_history,  # <- new
)

Spanner

首先,按照 LangChain 的文档设置数据库并安装软件包。

接下来,按如下方式定义 get_session_history 函数:

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 传入:

agent = reasoning_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"}},
)

第 4 步:自定义提示模板

提示模板有助于将用户输入转换为模型的说明,并用于指导模型的回答,帮助其理解上下文并生成相关且连贯的语言输出。如需了解详情,请参阅 ChatPromptTemplates

默认的提示模板会按顺序分为多个部分。

说明
(可选)系统说明 要应用于所有查询的代理说明。
(可选)Chat 记录 与过往会话的聊天记录对应的消息。
用户输入 用户向客服人员提出的询问。
客服助理记事板 智能体在使用其工具和执行推理来为用户制定回复时创建的消息(例如通过函数调用)。

如果您在创建代理时未指定自己的提示模板,系统会生成默认提示模板,其完整内容如下所示:

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}"),
])

您可以使用自己的提示模板替换默认提示模板,并在构建代理时使用该模板,例如:


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 = reasoning_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 时替换默认的可运行项,方法是使用具有以下签名的 Python 函数指定 runnable_builder= 实参:

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 返回的聊天模型(请参阅定义和配置模型),
  • toolsmodel_tool_kwargs 对应于要使用的工具和配置(请参阅定义和使用工具),
  • chat_history 对应于用于存储聊天消息的数据库(请参阅存储聊天记录),
  • system_instructionprompt 对应于提示配置(请参阅自定义提示模板),
  • agent_executor_kwargsrunnable_kwargs 是可用于自定义要构建的可运行程序的关键字参数。

这样,您就可以使用不同的选项来自定义编排逻辑。

ChatModel

在最简单的情况下,如需在不进行编排的情况下创建代理,您可以替换 LangchainAgentrunnable_builder 以直接返回 model

from langchain_core.language_models import BaseLanguageModel

def llm_builder(model: BaseLanguageModel, **kwargs):
    return model

agent = reasoning_engines.LangchainAgent(
    model=model,
    runnable_builder=llm_builder,
)

ReAct

如需使用基于您自己的 prompt 的自定义 ReAct 代理替换默认的调用工具行为(请参阅自定义提示模板),您需要替换 LangchainAgentrunnable_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

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 = reasoning_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

您需要替换 LangchainAgentrunnable_builder

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 = reasoning_engines.LangchainAgent(
    model=model,
    runnable_builder=lcel_builder,
)

LangGraph

如需使用 LangGraph 构建以下图表,请执行以下操作:

   Input
   /   \
 Pros  Cons
   \   /
  Summary

您需要替换 LangchainAgentrunnable_builder

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 = reasoning_engines.LangchainAgent(
    model=model,
    runnable_builder=langgraph_builder,
)

# Example query
agent.query(input={"role": "user", "content": "scrum methodology"})

后续步骤