OpenAI Functions Agent持久化(Add Memory to OpenAI Functions Agent)

28 篇文章 12 订阅

准备工作

  1. 注册获取openAIkey,这个自己弄。
  2. 注册并获取Google Search APIkey,为了可以在工具中使用google查询,https://serpapi.com/search-api

概述

本笔记本介绍了如何对 OpenAI Functions 代理进行持久化。

内容

import os
# 自己的openai 的Key
os.environ["OPENAI_API_KEY"] = "xxx"
# https://serpapi.com/search-api
os.environ["SERPAPI_API_KEY"] = "xxx"
from langchain import (
    LLMMathChain,
    OpenAI,
    SerpAPIWrapper,
    SQLDatabase,
    SQLDatabaseChain,
)
from langchain.agents import initialize_agent, Tool
from langchain.agents import AgentType
from langchain.chat_models import ChatOpenAI

#可能会有的警告:/Users/harrisonchase/.pyenv/versions/3.9.1/envs/langchain/lib/python3.9/site-packages/deeplake/util/check_latest_version.py:32: UserWarning: A newer version of deeplake (3.6.4) is available. 
#需要安装:It's recommended that you update to the latest version using `pip install -U deeplake`.

# 得到大模型
llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-0613")
# Google搜索工具
search = SerpAPIWrapper()
# 得到llmMathchain
llm_math_chain = LLMMathChain.from_llm(llm=llm, verbose=True)
# 持久化的DB
db = SQLDatabase.from_uri("sqlite:///../../../../../notebooks/Chinook.db")
# 得到db chain
db_chain = SQLDatabaseChain.from_llm(llm, db, verbose=True)
# 工具列表
tools = [
    Tool(
        name="Search",
        func=search.run,
        description="useful for when you need to answer questions about current events. You should ask targeted questions",
    ),
    Tool(
        name="Calculator",
        func=llm_math_chain.run,
        description="useful for when you need to answer questions about math",
    ),
    Tool(
        name="FooBar-DB",
        func=db_chain.run,
        description="useful for when you need to answer questions about FooBar. Input should be in the form of a question containing full context",
    ),
]

from langchain.prompts import MessagesPlaceholder
from langchain.memory import ConversationBufferMemory
agent_kwargs = {
    "extra_prompt_messages": [MessagesPlaceholder(variable_name="memory")],
}
memory = ConversationBufferMemory(memory_key="memory", return_messages=True)

agent = initialize_agent(
    tools, 
    llm, 
    agent=AgentType.OPENAI_FUNCTIONS, 
    verbose=True, 
    agent_kwargs=agent_kwargs, 
    memory=memory
)

agent.run("hi")


"""     
> Entering new  chain...
Hello! How can I assist you today?

> Finished chain.

'Hello! How can I assist you today?'
"""

总结

  1. 创建大模型
  2. Google搜索组件、数学计算组件、DB组件
  3. 将2中的组件,整合成工具列表
  4. 初始化代理,并得到代理对象
  5. 通过代理对象,开始启动

参考地址:

https://python.langchain.com/docs/modules/agents/how_to/add_memory_openai_functions

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

山鬼谣me

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值