为什么我们需要 Agent?
我们都知道,即使是 GPT-4 这样的大型语言模型在自然语言理解和生成方面的成绩已经取得巨大飞跃,但它们也有一些缺点。以下是大型语言模型的一些关键限制以及需要 Agent 的原因:
知识有限:大型语言模型需要外部系统来访问最新信息,这需要与其他应用程序/工具和系统集成。 不一致和缺乏控制:即使我们告诉大型语言模型始终以 JSON 进行响应,大型语言模型也可能会出错。 无法执行复杂任务:大型语言模型不擅长推理和计算。
举一个例子,假如我们的目标是基于 LLM 创建一个系统。该系统将为提供来自 Google 的最新信息并执行复杂的计算。但是,这些功能被认为是 LLM 的局限性。
我们在系统中建立了两种不同的功能:一种是利用 Google API,另一种是计算器功能。这个概念围绕着在必要时根据用户的输入调用适当的功能并确保正确的操作顺序。
Agent 的基本概念是在以下情况下选择要执行的一系列操作:
步骤不是预先定义的。 有必要利用外部工具或功能,例如计算器或谷歌搜索。
React Agent 是如何工作的?
“Reason Only”(向内求索):COT 类型的 Prompt,希望模型一步一步思考去解决问题。这类型 Prompt 非常擅长逻辑推理,能将问题拆解成一个一个小问题,逐一解决。但是没有灵活运用外部工具和外部知识的能力。向内求索型人格:像一个思维严谨,思考问题很深入,但是不够开放,不喜欢接触新知识的人。
“Act Only”(向外探索):工具使用 Prompt,非常擅长借助外部工具和外部知识去解决问题,但是逻辑推理能力不强。向外探索人格:像一个思维开放,非常喜欢探索新知识,尝试新工具,但是思想深度不够的人。
“ReAct”(内外兼修) :就是将“Reason Only”(向内求索)和“Act Only”(向外探索)这两者相结合,
将严谨思维和探索的心态结合起来,优势互补,成为一个内外兼修的人。
直观的理解就是,ReAct 就是结合了的 推理能力 + 工具使用能力/外部知识能力,利用大模型去解决难问题。
在 ReAct Prompting 中,使用 LLMs 以交错的方式生成推理轨迹和特定于任务的操作,从而实现两者之间更大的协同作用:
推理跟踪帮助模型诱导、跟踪和更新行动计划以及处理异常。 执行操作与外部源(例如知识库或环境)进行交互,以获取相关信息。
ReAct Agent 高级用法参考以下两篇文章:
1. ReAct Agent 回答 RAG 系统中的复杂问题
2. 更智能的 Agent,Plan-and-Execute Agents 计划和执行
为了深入研究 ReAct 框架的内部工作原理,我们来看看下面的例子。在这个场景中,用户输入如下:“C罗的年龄是多少,并计算它的平方根?”如前所述,这涉及两个步骤:搜索(获取罗纳尔多的年龄)和计算(确定该年龄的平方根)。
我们通过向模型提供提示来启动该过程,指导其解决此类问题的方法,强调思想、行动、行动输入和观察的要素。在每个阶段,我们都会在上一步取得的成果的基础上不断迭代。
第 1 步:向 Agent 提问 — 使用 ReAct 的内部流程
收到提问后,模型根据 ReAct 规范进行推理,
Thought: Do I need to use a tool ? Yes
Action: Search
Action Input: I need to find out Rolando's Age
Function : 调用函数,返回结果
第 2 步:向 Agent 提问 — 使用 ReAct 的内部流程
收到一步函数调用后的结果,模型进行思考推理,
Thought: Do I need to use a tool? Yes
Action: Calculator
Action Input: I need to calculate square root of 38
Function: 调用函数,返回结果
最后一步:向 Agent 提问 — 使用 ReAct 的内部流程
根据上一步的结果,继续进行推理,
Thought: Do I need to use a tool? No
AI: Ronaldo's age is 38 and its square root is 6.06
以下是 ReAct Prompt Demo:
PREFIX = """Assistant is a large language model trained by OpenAI.
Assistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.
Assistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.
Overall, Assistant is a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist.
TOOLS:
------
Assistant has access to the following tools:"""
FORMAT_INSTRUCTIONS = """To use a tool, please use the following format:
\```
Thought: Do I need to use a tool? Yes
Action: the action to take, should be one of [{tool_names}]
Action Input: the input to the action
Observation: the result of the action
\```
When you have a response to say to the Human, or if you do not need to use a tool, you MUST use the format:
\```
Thought: Do I need to use a tool? No
{ai_prefix}: [your response here]
\```"""
SUFFIX = """Begin!
Previous conversation history:
{chat_history}
New input: {input}
{agent_scratchpad}"""
OpenAI Functions
OpenAI 拥有针对特定任务的微调模型,例如 gpt-3.5-turbo-0613 和 gpt-4-0613。 这些模型经过了微调,可以检测何时需要调用函数(取决于用户的输入),并使用遵循函数签名的 JSON 进行响应。函数调用使开发人员能够更可靠地从模型中获取结构化数据。 进行 API 调用时,我们可以定义函数并请求模型提供一个 JSON 对象,其中包含执行这些函数所需的参数。 与传统的文本完成或聊天 API 相比,OpenAI Function API 的主要目标是提高函数调用响应的准确性和实用性。
OpenAI 函数示例
假设我们想要构建一个可以提供查询当前股票价格的聊天机器人。
运行过程
OpenAI 根据用户的问题,返回 function 类型的消息,并选择合适的 function 和 对应的参数结构,如上图。
调用函数并返回结果
OpenAI function 功能与LangChain Agent 的比较
特点 | LangChain Agent | OpenAI functios |
---|---|---|
控制 Function Calls | 简化函数调用,内部处理细节 | 提供对函数调用和参数的完全控制 |
功能描述 | 使用 structured prompts,ReAct 思维 | OpenAI 微调 Function calls |
准确性 | 低-中(依赖于使用的模型能力) | 高 |
处理复杂函数调用 | 适用于描述较短的简单函数 | 适用于复杂的、长时间运行的工作流或复杂的功能 |
Token 效率 | No | Yes |
使用场景 | 当需要一种简化的方法来处理更简单的函数和更短的描述时,或者当喜欢结构化的提示格式进行交互时 | 当我们需要完全控制函数调用时,有复杂的函数要实现,或者当函数描述很广泛并且可能超过消息长度限制时 |
推荐阅读
ReAct Agent 回答 RAG 系统中的复杂问题 更智能的 Agent,Plan-and-Execute Agents 计划和执行 如何使用大模型调用函数 图解 RAG 高级算法和技术 高级 RAG(Self-Reflective RAG)
参考资料:
Qwen 7B大模型ReAct Prompt详解以及LLM 智能体Agent实战 (https://zhuanlan.zhihu.com/p/664477178?utm_id=0)
ReAct Prompt Demo(https://github.com/langchain-ai/langchain/blob/9731ce5a406d5a7bb1878a54b265a6f7c728effc/libs/langchain/langchain/agents/conversational/prompt.py)
OpenAI functions vs LangChain Agent — Which one is better? (https://blog.gopenai.com/openai-functions-langchain-agent-db9a7977e597)
?分享、点赞、在看,给个3连击呗!?