Langchain Agent Type - ReAct

使用提示词:

prompt = hub.pull("hwchase17/react")
Answer the following questions as best you can. You have access to the following tools:

{tools}

Use the following format:

Question: the input question you must answer
Thought: you should always think about what to do
Action: the action to take, should be one of [{tool_names}]
Action Input: the input to the action
Observation: the result of the action
... (this Thought/Action/Action Input/Observation can repeat N times)
Thought: I now know the final answer
Final Answer: the final answer to the original input question

Begin!

Question: {input}
Thought:{agent_scratchpad}

使用提示词的中文翻译

尽可能回答以下问题。 您可以使用以下工具:
 
{tools}
 
使用以下格式:
 
问题:您必须回答的输入问题
思考:你应该时刻思考该做什么
行动:要采取的操作,应该是 [{tool_names}] 之一
行动输入:动作的输入
观察:行动的结果
...(这个思考/行动/行动输入/观察可以重复N次)
思考:我现在知道了最终答案
最终答案:原始输入问题的最终答案
 
开始!
 
问题:{input}
思考:{agent_scratchpad}

ReAct Agent代码

def create_react_agent(
    llm: BaseLanguageModel,
    tools: Sequence[BaseTool],
    prompt: BasePromptTemplate,
    output_parser: Optional[AgentOutputParser] = None,
    tools_renderer: ToolsRenderer = render_text_description,
    *,
    stop_sequence: Union[bool, List[str]] = True,
) -> Runnable:
    """Create an agent that uses ReAct prompting.

    Args:
        llm: LLM to use as the agent.
        tools: Tools this agent has access to.
        prompt: The prompt to use. See Prompt section below for more.
        output_parser: AgentOutputParser for parse the LLM output.
        tools_renderer: This controls how the tools are converted into a string and
            then passed into the LLM. Default is `render_text_description`.
        stop_sequence: bool or list of str.
            If True, adds a stop token of "Observation:" to avoid hallucinates.
            If False, does not add a stop token.
            If a list of str, uses the provided list as the stop tokens.

            Default is True. You may to set this to False if the LLM you are using
            does not support stop sequences.

    Returns:
        A Runnable sequence representing an agent. It takes as input all the same input
        variables as the prompt passed in does. It returns as output either an
        AgentAction or AgentFinish.
    """  # noqa: E501
    missing_vars = {"tools", "tool_names", "agent_scratchpad"}.difference(
        prompt.input_variables
    )
    if missing_vars:
        raise ValueError(f"Prompt missing required variables: {missing_vars}")

    prompt = prompt.partial(
        tools=tools_renderer(list(tools)),
        tool_names=", ".join([t.name for t in tools]),
    )
    if stop_sequence:
        stop = ["\nObservation"] if stop_sequence is True else stop_sequence
        llm_with_stop = llm.bind(stop=stop)
    else:
        llm_with_stop = llm
    output_parser = output_parser or ReActSingleInputOutputParser()
    agent = (
        RunnablePassthrough.assign(
            agent_scratchpad=lambda x: format_log_to_str(x["intermediate_steps"]),
        )
        | prompt
        | llm_with_stop
        | output_parser
    )
    return agent

推理输出



> Entering new AgentExecutor chain...
 I should research LangChain to learn more about it.

Action: tavily_search_results_json
Action Input: "LangChain"[{'url': 'https://www.ibm.com/topics/langchain', 'content': 'LangChain is essentially a library of abstractions for Python and Javascript, representing common steps and concepts  LangChain is an open source orchestration framework for the development of applications using large language models  other LangChain features, like the eponymous chains.  LangChain provides integrations for over 25 different embedding methods, as well as for over 50 different vector storesLangChain is a tool for building applications using large language models (LLMs) like chatbots and virtual agents. It simplifies the process of programming and integration with external data sources and software workflows. It supports Python and Javascript languages and supports various LLM providers, including OpenAI, Google, and IBM.'}] 
I should read the summary and look at the different features and integrations of LangChain.

Action: tavily_search_results_json
Action Input: "LangChain features and integrations"[{'url': 'https://www.ibm.com/topics/langchain', 'content': "LangChain provides integrations for over 25 different embedding methods, as well as for over 50 different vector stores  LangChain is an open source orchestration framework for the development of applications using large language models  other LangChain features, like the eponymous chains.  LangChain is essentially a library of abstractions for Python and Javascript, representing common steps and conceptsLaunched by Harrison Chase in October 2022, LangChain enjoyed a meteoric rise to prominence: as of June 2023, it was the single fastest-growing open source project on Github. 1 Coinciding with the momentous launch of OpenAI's ChatGPT the following month, LangChain has played a significant role in making generative AI more accessible to enthusias..."}] 
I should take note of the launch date and popularity of LangChain.

Action: tavily_search_results_json
Action Input: "LangChain launch date and popularity"[{'url': 'https://www.ibm.com/topics/langchain', 'content': "LangChain is an open source orchestration framework for the development of applications using large language models  other LangChain features, like the eponymous chains.  LangChain provides integrations for over 25 different embedding methods, as well as for over 50 different vector stores  LangChain is essentially a library of abstractions for Python and Javascript, representing common steps and conceptsLaunched by Harrison Chase in October 2022, LangChain enjoyed a meteoric rise to prominence: as of June 2023, it was the single fastest-growing open source project on Github. 1 Coinciding with the momentous launch of OpenAI's ChatGPT the following month, LangChain has played a significant role in making generative AI more accessible to enthusias..."}] 
I now know the final answer.

Final Answer: LangChain is an open source orchestration framework for building applications using large language models (LLMs) like chatbots and virtual agents. It was launched by Harrison Chase in October 2022 and has gained popularity as the fastest-growing open source project on Github in June 2023.

> Finished chain.
{'input': 'what is LangChain?',
 'output': 'LangChain is an open source orchestration framework for building applications using large language models (LLMs) like chatbots and virtual agents. It was launched by Harrison Chase in October 2022 and has gained popularity as the fastest-growing open source project on Github in June 2023.'}

中文翻译

> 进入新的 AgentExecutor 链...

我应该研究一下 LangChain 以了解更多信息。
操作:tavilly_search_results_json
操作输入:“LangChain”
[{'url': 'https://www.ibm.com/topics/langchain', 'content': 'LangChain 本质上是 Python 和 Javascript 的抽象库,代表常见步骤和概念 LangChain 是一个开源编排框架,用于使用大型语言模型和其他 LangChain 功能(例如同名链)来开发应用程序。 LangChain 提供了超过 25 种不同嵌入方法以及超过 50 种不同向量存储的集成。LangChain 是一种使用聊天机器人和虚拟代理等大型语言模型 (LLM) 构建应用程序的工具。 它简化了编程以及与外部数据源和软件工作流程集成的过程。 它支持Python和Javascript语言,并支持各种LLM提供商,包括OpenAI、Google和IBM。'}]

我应该阅读摘要并查看LangChain的不同功能和集成。
操作:tavilly_search_results_json
操作输入:“LangChain 功能和集成”
[{'url': 'https://www.ibm.com/topics/langchain', 'content': "LangChain 提供了超过 25 种不同嵌入方法的集成,以及 超过 50 个不同的向量存储 LangChain 是一个开源编排框架,用于使用其他 LangChain 功能(例如同名链)的大型语言模型来开发应用程序。LangChain 本质上是 Python 和 Javascript 的抽象库,代表常见步骤和概念由 Harrison Chase 推出 2022 年 10 月,浪链迅速崛起:截至 2023 年 6 月,它是 Github 上增长最快的开源项目1。恰逢次月 OpenAI 的 ChatGPT 重大发布,浪链在 让生成式人工智能更容易被爱好者所接受..."}]

我应该注意一下LangChain的发布日期和受欢迎程度。
操作:tavilly_search_results_json
操作输入:“LangChain 发布日期和受欢迎程度”
[{'url': 'https://www.ibm.com/topics/langchain', 'content':“LangChain 是一个开源编排框架,用于使用 大型语言模型 LangChain 的其他功能,例如同名链。LangChain 提供了超过 25 种不同嵌入方法以及超过 50 种不同向量存储的集成 LangChain 本质上是 Python 和 Javascript 的抽象库,代表常见步骤和概念由 Harrison 发起 2022 年 10 月,浪链迅速崛起:截至 2023 年 6 月,它是 Github 上增长最快的开源项目1。恰逢次月 OpenAI 的 ChatGPT 重大发布,浪链发挥了重要作用。 让生成式人工智能更容易被爱好者所接受……”}]

我现在知道了最终的答案。
最终答案:LangChain 是一个开源编排框架,用于使用聊天机器人和虚拟代理等大型语言模型 (LLM) 构建应用程序。 它由 Harrison Chase 于 2022 年 10 月推出,并于 2023 年 6 月作为 Github 上增长最快的开源项目而受到欢迎。

> 完成链条。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值