LangChain(5)Conversational Agents

这篇具有很好参考价值的文章主要介绍了LangChain(5)Conversational Agents。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Large Language Models (LLMs) 在语义知识方面表现不错,但也有一些不足,如:不能正确计算数学公式、无法获取最新知识新闻

通过 Agents 可以赋予 LLMs 更多能力,让LLM能够计算、上网查询

agent 简单使用

from langchain import OpenAI
# 语言模型
llm = OpenAI(
openai_api_key="OPENAI_API_KEY",
temperature=0,
model_name="text-davinci-003"
)

from langchain.chains import LLMMathChain
from langchain.agents import Tool
# 能计算数学公式的一个chain
llm_math = LLMMathChain(llm=llm)

# initialize the math tool
math_tool = Tool(
name='Calculator',
func=llm_math.run,
description='Useful for when you need to answer questions about math.' # 描述工具能做什么
)
# when giving tools to LLM, we must pass as list of tools
tools = [math_tool]

# 如果 langchain.agents 中有相关工具,则可以直接使用
#from langchain.agents import load_tools
#tools = load_tools(
#['llm-math'],
#llm=llm
)

# 初始化 agent
from langchain.agents import initialize_agent
zero_shot_agent = initialize_agent(
				agent="zero-shot-react-description", # 无记忆的agent
				tools=tools, # tools 中只有math_tool,所以只能做计算
				llm=llm,
				verbose=True, # 显示执行过程
				max_iterations=3
		)
zero_shot_agent("what is (4.5*2.1)^2.2?")

上面的 tools 中只有math_tool,所以 zero_shot_agent 只能做计算,不能回答其它常识问题,可以在 tools 中添加更多工具,使得 zero_shot_agent 拥有更多能力。

# 可以在 tools 中新增聊天工具
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
prompt = PromptTemplate(
input_variables=["query"],
template="{query}"
)
llm_chain = LLMChain(llm=llm, prompt=prompt)

# initialize the LLM tool
llm_tool = Tool(
name='Language Model',
func=llm_chain.run,
description='use this tool for general purpose queries and logic'
)
tools.append(llm_tool)
# reinitialize the agent
zero_shot_agent = initialize_agent(
agent="zero-shot-react-description",
tools=tools,
llm=llm,
verbose=True,
max_iterations=3
)

agent 类型

zero-shot-react-description 无缓存的方式,聊天是单次的,无上下文缓存

zero_shot_agent = initialize_agent(
agent="zero-shot-react-description",
tools=tools,
llm=llm,
verbose=True,
max_iterations=3,
)

conversational-react-description 带缓存

from langchain.memory import ConversationBufferMemory

memory = ConversationBufferMemory(memory_key="chat_history")

conversational_agent = initialize_agent(
agent='conversational-react-description',
tools=tools,
llm=llm,
verbose=True,
max_iterations=3,
memory=memory,
)

react-docstore 可以检索知识库,无缓存

from langchain import Wikipedia
from langchain.agents.react.base import DocstoreExplorer

docstore=DocstoreExplorer(Wikipedia())
tools = [
			Tool(
				name="Search", # 信息检索
				func=docstore.search, 
				description='search wikipedia'
			),
			Tool(
				name="Lookup", # 匹配相近结果
				func=docstore.lookup, 
				description='lookup a term in wikipedia'
			)
]

docstore_agent = initialize_agent(
								tools,
								llm,
								agent="react-docstore",
								verbose=True,
								max_iterations=3
								)

self-ask-with-search 将LLM与搜索引擎结合起来

from langchain import SerpAPIWrapper

# initialize the search chain
search = SerpAPIWrapper(serpapi_api_key='serp_api_key')

# create a search tool
tools = [
			Tool(
			name="Intermediate Answer",
			func=search.run,
			description='google search'
			)
		]

# initialize the search enabled agent
self_ask_with_search = initialize_agent(
						tools,
						llm,
						agent="self-ask-with-search",
						verbose=True
						)

参考:
Superpower LLMs with Conversational Agents文章来源地址https://www.toymoban.com/news/detail-578896.html

到了这里,关于LangChain(5)Conversational Agents的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • LangChain Agents深入剖析及源码解密上(一)

    LangChain Agents深入剖析及源码解密上(一) LangChain Agents深入剖析及源码解密上 Agent工作原理详解 本节会结合AutoGPT的案例,讲解LangChain代理(Agent)为核心的内容。我们前面已经谈了代理本身的很多内容,也看了绝大部分的源代码,例如:ReAct的源代码,还有mrkl的源代码,如图

    2024年02月15日
    浏览(25)
  • 使用 LangChain 构建 LLM 应用详细教程(附python代码演练)

    介绍 欢迎来到语言处理的未来!在一个语言是连接人与技术的桥梁的世界中,自然语言处理(NLP)的进步为我们带来了令人难以置信的机会。其中一个重要的进步是革命性的语言模型,即大型语言模型(LLM),它彻底改变了我们与基于文本的数据进行交互的方式。我们将探索

    2024年01月24日
    浏览(29)
  • LangChain-10 Agents langchainhub 共享的提示词Prompt

    LangChainHub 的思路真的很好,通过Hub的方式将 Prompt 共享起来,大家可以通过很方便的手段,短短的几行代码就可以使用共享的 Prompt 。 我个人非常看好这个项目。 官方推荐使用LangChainHub,但是它在GitHub已经一年没有更新了, 倒是数据还在更新。 为了防止大家不能访问,我这

    2024年04月10日
    浏览(37)
  • 自然语言处理从入门到应用——LangChain:代理(Agents)-[代理类型]

    分类目录:《大模型从入门到应用》总目录 LangChain系列文章: 基础知识 快速入门 安装与环境配置 链(Chains)、代理(Agent:)和记忆(Memory) 快速开发聊天模型 模型(Models) 基础知识 大型语言模型(LLMs) 基础知识 LLM的异步API、自定义LLM包装器、虚假LLM和人类输入LLM(

    2024年02月15日
    浏览(38)
  • 自然语言处理从入门到应用——LangChain:代理(Agents)-[基础知识]

    分类目录:《大模型从入门到应用》总目录 LangChain系列文章: 基础知识 快速入门 安装与环境配置 链(Chains)、代理(Agent:)和记忆(Memory) 快速开发聊天模型 模型(Models) 基础知识 大型语言模型(LLMs) 基础知识 LLM的异步API、自定义LLM包装器、虚假LLM和人类输入LLM(

    2024年02月13日
    浏览(56)
  • 【LangChain】结合代理和向量存储(Combine agents and vector stores)

    本笔记本介绍了如何组合代理和向量存储。其用例是,您已将数据提取到向量存储中,并希望以 代理 方式与其进行交互。 下文讲述的方法是创建 RetrievalQA ,然后将其用作整体代理中的工具。 #请注意,在上面的示例中,代理在查询 RetrievalQAChain 后做了一些额外的工作。您可

    2024年02月13日
    浏览(26)
  • LangChain 27 AI Agents角色扮演多轮对话解决问题CAMEL

    LangChain系列文章 LangChain 实现给动物取名字, LangChain 2模块化prompt template并用streamlit生成网站 实现给动物取名字 LangChain 3使用Agent访问Wikipedia和llm-math计算狗的平均年龄 LangChain 4用向量数据库Faiss存储,读取YouTube的视频文本搜索Indexes for information retrieve LangChain 5易速鲜花内部问

    2024年02月03日
    浏览(31)
  • 自然语言处理从入门到应用——LangChain:代理(Agents)-[自定义MRKL代理]

    分类目录:《大模型从入门到应用》总目录 LangChain系列文章: 基础知识 快速入门 安装与环境配置 链(Chains)、代理(Agent:)和记忆(Memory) 快速开发聊天模型 模型(Models) 基础知识 大型语言模型(LLMs) 基础知识 LLM的异步API、自定义LLM包装器、虚假LLM和人类输入LLM(

    2024年02月16日
    浏览(22)
  • 【LLM】LangChain基础使用(构建LLM应用)

    LangChain应用开发框架,支持python和typescript语言;可以帮助生成prompt模板,并通过代理充当其他组件(如提示模板、其他大语言模型、外部数据和其他工具)的中央接口。 LangChain可以直接与 OpenAI 的 text-davinci-003、gpt-3.5-turbo 模型以及 Hugging Face 的各种开源语言模如 Google 的 fl

    2024年02月04日
    浏览(34)
  • 【LangChain】LLM

    基础 【LangChain】LLM LLMChain 是一个简单的链,它围绕语言模型添加了一些功能。它在整个 LangChain 中广泛使用,包括在其他链和代理中。 LLMChain 由 PromptTemplate 和 语言模型(LLM 或聊天模型) 组成。它使用提供的输入键值对(以及内存键值对,如果可用的话)作用于 格式化提示

    2024年02月13日
    浏览(24)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包