LangChain(5)Conversational Agents

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

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

agent 简单使用

python 复制代码
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 拥有更多能力。

python 复制代码
# 可以在 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 无缓存的方式,聊天是单次的,无上下文缓存

python 复制代码
zero_shot_agent = initialize_agent(
agent="zero-shot-react-description",
tools=tools,
llm=llm,
verbose=True,
max_iterations=3,
)

conversational-react-description 带缓存

python 复制代码
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 可以检索知识库,无缓存

python 复制代码
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与搜索引擎结合起来

python 复制代码
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

相关推荐
杨智炜5 小时前
我没有凑到 100 道题:一个 Agent 评测集的取舍
agent
xyphf_和派孔明5 小时前
第六章: LangGraph的工具调用代理-Tool Calling Agent
langchain·llm
承渊政道6 小时前
【Python编程—从入门到实践】(设置样式并部署:给Django网站换新装,把学习笔记发布到互联网)
python·学习·pycharm·django
泯泷6 小时前
AI Agent 工具设计:能执行任务的 Agent,先要学会何时停手
llm·agent
用户298698530146 小时前
使用 Python 将 JSON 转换为 Word 文档的实践指南
python·json·api
卷无止境7 小时前
AI 加速开发之后,如何应对尚未发现的代码安全威胁
后端·python
cpolar技术支持7 小时前
容器数是 6 个、6 个、还是 6 个,可服务早被换掉了:用一个 5.5MB 的 Go 单文件把机器每周的变化记下来,再给它套一层别人看不懂的入口
python·docker·ai·cpolar
ONEYAC唯样7 小时前
YXC扬兴科技小型化有源晶振OT4JI-111-24M,提升红外热成像稳定性
人工智能·python·pygame
威哥爱编程7 小时前
模拟面试智能体开发实战 09:把面试官装进手机,鸿蒙端聊天界面用 SSE 流式输出让回复逐字滚出来
python·agent·harmonyos
威哥爱编程7 小时前
模拟面试智能体开发实战 10:配置写死在代码里还怎么换服务端,把设置和简历做成界面可配、可存
python·agent·harmonyos