0

0

在 langchain 中 initialize_agent 被禁用后,应该如何进行替代?

碧海醫心

碧海醫心

发布时间:2025-03-17 08:24:15

|

1054人浏览过

|

来源于php中文网

原创

在 langchain 中 initialize_agent 被禁用后,应该如何进行替代?

Langchain initialize_agent 函数已被弃用后的替代方案

Langchain 的 initialize_agent 函数已被弃用,这是为了提升框架的灵活性及模块化程度。本文将介绍如何使用更细粒度的 API 调用来替代它。

主要替代方法有两种:

1. 使用 AgentExecutor:

AgentExecutorinitialize_agent 的直接替代品,它允许更灵活地构建和管理 Agent。以下是一个使用 ZeroShotAgentAgentExecutor 的示例:

牛面
牛面

牛面AI面试,大厂级面试特训平台

下载
from langchain.agents import AgentExecutor, ZeroShotAgent
from langchain import LLMChain, PromptTemplate
from langchain.llms import OpenAI  # 或者其他LLM

llm = OpenAI(temperature=0) # 使用OpenAI模型,可替换为其他LLM

prompt = PromptTemplate(
    input_variables=["input", "agent_scratchpad"],
    template="Answer the following question: {input}\n\n{agent_scratchpad}"
)

llm_chain = LLMChain(llm=llm, prompt=prompt)
tools = [...]  # 你的工具列表,例如搜索工具、计算工具等

agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools)
agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True)

# 使用 agent_executor 执行任务
result = agent_executor.run("你的问题或任务")
print(result)

2. 自定义 Agent:

对于更复杂的场景,你可以自定义 Agent。这需要你手动组合不同的工具和链,以满足特定需求。

from langchain.agents import Tool, AgentExecutor
from langchain.chains import LLMChain
from langchain.llms import OpenAI # 或者其他LLM
from langchain.prompts import PromptTemplate

llm = OpenAI(temperature=0) # 使用OpenAI模型,可替换为其他LLM

# 定义工具
tool1 = Tool(name="tool1", func=lambda x: "tool1 的结果", description="tool1 的描述")
tool2 = Tool(name="tool2", func=lambda x: "tool2 的结果", description="tool2 的描述")
tools = [tool1, tool2]

# 定义提示模板 (根据你的Agent类型调整)
prompt_template = """Use the following tools to answer the question.
{tools}

Question: {input}
{agent_scratchpad}"""
prompt = PromptTemplate(
    input_variables=["input", "agent_scratchpad", "tools"],
    template=prompt_template,
)

# 创建 LLM 链
llm_chain = LLMChain(llm=llm, prompt=prompt)

# 创建自定义 Agent (根据你的需求选择合适的Agent类型,例如ZeroShotAgent,  ConversationAgent等)
#  agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools)  # 例如ZeroShotAgent
#  agent = ... # 其他Agent类型


# 创建 AgentExecutor
agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True)

# 运行 agent
result = agent_executor.run("你的问题或任务")
print(result)

记住替换代码中的 ...你的问题或任务 为你实际的工具和问题。 选择哪种方法取决于你的应用场景和复杂度。 AgentExecutor 提供了更简便的途径,而自定义 Agent 则提供了更大的灵活性。 确保安装必要的 Langchain 包以及选择的LLM。

相关专题

更多
php远程文件教程合集
php远程文件教程合集

本专题整合了php远程文件相关教程,阅读专题下面的文章了解更多详细内容。

21

2026.01.22

PHP后端开发相关内容汇总
PHP后端开发相关内容汇总

本专题整合了PHP后端开发相关内容,阅读专题下面的文章了解更多详细内容。

14

2026.01.22

php会话教程合集
php会话教程合集

本专题整合了php会话教程相关合集,阅读专题下面的文章了解更多详细内容。

8

2026.01.22

宝塔PHP8.4相关教程汇总
宝塔PHP8.4相关教程汇总

本专题整合了宝塔PHP8.4相关教程,阅读专题下面的文章了解更多详细内容。

7

2026.01.22

PHP特殊符号教程合集
PHP特殊符号教程合集

本专题整合了PHP特殊符号相关处理方法,阅读专题下面的文章了解更多详细内容。

6

2026.01.22

PHP探针相关教程合集
PHP探针相关教程合集

本专题整合了PHP探针相关教程,阅读专题下面的文章了解更多详细内容。

6

2026.01.22

菜鸟裹裹入口以及教程汇总
菜鸟裹裹入口以及教程汇总

本专题整合了菜鸟裹裹入口地址及教程分享,阅读专题下面的文章了解更多详细内容。

20

2026.01.22

Golang 性能分析与pprof调优实战
Golang 性能分析与pprof调优实战

本专题系统讲解 Golang 应用的性能分析与调优方法,重点覆盖 pprof 的使用方式,包括 CPU、内存、阻塞与 goroutine 分析,火焰图解读,常见性能瓶颈定位思路,以及在真实项目中进行针对性优化的实践技巧。通过案例讲解,帮助开发者掌握 用数据驱动的方式持续提升 Go 程序性能与稳定性。

9

2026.01.22

html编辑相关教程合集
html编辑相关教程合集

本专题整合了html编辑相关教程合集,阅读专题下面的文章了解更多详细内容。

106

2026.01.21

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
React 教程
React 教程

共58课时 | 4万人学习

Pandas 教程
Pandas 教程

共15课时 | 0.9万人学习

ASP 教程
ASP 教程

共34课时 | 3.9万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号