从0手搓4种Agentic模式,主打去框架化!

Agentic Patterns项目的4种AI模式解析

Agentic Patterns项目:包含Andrew Ng在其 DeepLearning.AI博客文章系列中定义的4种Agentic模式的从头实现,主打去框架化:没有 LangChain,没有 LangGraph,没有 LlamaIndex,没有 CrewAI。对LLM API 调用纯粹而简单。

图片

反思模式:Reflection Pattern

这是一个非常基本的模式,但尽管它很简单,它却为LLM响应带来了令人惊讶的性能提升。

它允许大模型反思其结果,建议修改、添加、改进写作风格等。

图片

BASE_GENERATION_SYSTEM_PROMPT = """Your task is to Generate the best content possible for the user's request.If the user provides critique, respond with a revised version of your previous attempt.You must always output the revised content."""
BASE_REFLECTION_SYSTEM_PROMPT = """You are tasked with generating critique and recommendations to the user's generated content.If the user content has something wrong or something to be improved, output a list of recommendationsand critiques. If the user content is ok and there's nothing to change, output this: <OK>"""

工具模式(Tool Pattern)

LLM 权重中存储的信息(通常)不足以对我们的问题提供准确而有见地的答案

这就是为什么我们需要为大模型提供接触外部世界的途径

实际上,可以构建任何你想要的工具(归根结底,它们只是 LLM 可以使用的功能),一个工具可以让你访问维基百科,另一个工具可以分析 YouTube 视频的内容或计算 Wolfram Alpha 中的困难积分。

图片

TOOL_SYSTEM_PROMPT = """You are a function calling AI model. You are provided with function signatures within <tools></tools> XML tags.You may call one or more functions to assist with the user query. Don't make assumptions about what values to pluginto functions. Pay special attention to the properties 'types'. You should use those types as in a Python dict.For each function call return a json object with function name and arguments within <tool_call></tool_call>XML tags as follows:
<tool_call>{"name": <function-name>,"arguments": <args-dict>,  "id": <monotonically-increasing-id>}</tool_call>
Here are the available tools:
<tools>%s</tools>"""

规划模式(Planning Pattern)

经过上述两步,已经看到了能够反思和使用工具来访问外部世界的智能体。但是......规划呢,即决定按照什么步骤顺序来完成一项大任务?

这正是规划模式所提供的:让 LLM 将任务分解为更小、更容易实现的子目标,而不会忘记最终目标。

规划模式最典型的例子是ReAct技术:

图片

REACT_SYSTEM_PROMPT = """You are a function calling AI model. You operate by running a loop with the following steps: Thought, Action, Observation.You are provided with function signatures within <tools></tools> XML tags.You may call one or more functions to assist with the user query. Don' make assumptions about what values to pluginto functions. Pay special attention to the properties 'types'. You should use those types as in a Python dict.
For each function call return a json object with function name and arguments within <tool_call></tool_call> XML tags as follows:
<tool_call>{"name": <function-name>,"arguments": <args-dict>, "id": <monotonically-increasing-id>}</tool_call>
Here are the available tools / actions:
<tools>%s</tools>
Example session:
<question>What's the current temperature in Madrid?</question><thought>I need to get the current weather in Madrid</thought><tool_call>{"name": "get_current_weather","arguments": {"location": "Madrid", "unit": "celsius"}, "id": 0}</tool_call>
You will be called again with this:
<observation>{0: {"temperature": 25, "unit": "celsius"}}</observation>
You then output:
<response>The current temperature in Madrid is 25 degrees Celsius</response>
Additional constraints:
- If the user asks you something unrelated to any of the tools above, answer freely enclosing your answer with <response></response> tags."""

多智能体模式(Multiagent Pattern)

相信不少小伙伴都听过crewAI 或 AutoGen 等框架,它们允许您创建多智能体应用程序。

这些框架实现了多智能体模式的不同变体,其中任务被划分为由不同角色执行的较小子任务(例如,一个Agent可以是软件工程师,另一个Agent可以是项目经理等)。

这一模式的实际效果开发ing......

图片

文末讨论:真正的AGI?

真正的AGI的秘密不在于一个万能的模型......而在于一系列针对特定问题空间的明确定义的本体,其中包含许多 AI 模型 + 代码,以在直觉+逻辑护栏之间取得适当的平衡,从而实现Agent推理、规划和决策???

图片

https://www.deeplearning.ai/the-batch/how-agents-can-improve-llm-performance/?ref=dl-staging-website.ghost.iohttps://github.com/neural-maze/agentic_patterns

来源 | PaperAgent

### Agentic RAG Framework 的概述 Agentic RAG 是一种基于 LangGraph 实现的高级检索增强生成(Retrieval-Augmented Generation, RAG)方法[^1]。它旨在通过结合语义图结构和自然语言处理技术来提升文档检索与问答系统的性能。 #### 核心功能 LangGraph 提供了一种灵活的方式来构建和操作语义图,从而支持复杂的查询模式匹配以及高效的上下文理解能力。以下是其主要特点: - **语义建模**:利用预训练的语言模型对输入数据进行编码并映射到向量空间中,以便后续相似度计算。 - **高效索引**:采用 FAISS 或 HNSW 等近似最近邻算法加速大规模数据库中的文本片段查找过程。 - **动态更新机制**:允许实时修改存储的知识库内容而不影响整体架构稳定性。 #### 技术栈需求 为了顺利部署此解决方案,开发者需具备以下条件之一或者多个方面的经验: - Python 编程技能; - 对 Transformer 架构及其变体有基本认识; - 掌握 Elasticsearch / Milvus 这类专用搜索引擎工具链的应用场景; 下面给出一段简单的代码示例展示如何初始化一个基础版本的 agentic rag 流水线: ```python from langchain import LangChain from langgraph.agents.rag import AgentRAG # 初始化 LangChain 和 RAG agent lang_chain = LangChain() agent_rag = AgentRAG(lang_chain) # 加载已有知识库文件夹路径下的所有资料作为背景材料 agent_rag.load_documents("/path/to/knowledge_base") def query_handler(user_input): """ 处理来自用户的请求 """ result = agent_rag.run_query(user_input) return result['answer'] if __name__ == "__main__": while True: user_question = input("请输入您的问题:") answer = query_handler(user_question) print(f"答案:{answer}") ``` 上述脚本定义了一个循环交互界面,在其中不断接收新提问并通过调用 `AgentRAG` 类完成相应解答逻辑。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值