在本文中,我们将探讨如何创建一个简单的基于web的聊天应用程序,该应用程序与私有REST API进行通信, uses OpenAI functions and conversational memory.
We will be using again the LangChain framework 哪个提供了一个很好的交互基础设施 Large Language Models (LLM).
我们将在这篇文章中描述的Agent将使用这些工具:
Wikipedia with LangChain’s WikipediaAPIWrapper
DuckDuckGo Search with LangChain’s DuckDuckGoSearchAPIWrapper
Pubmed with PubMedAPIWrapper
LLM Math Chain with LLMMathChain
带有自定义实现的事件API,我们将在后面描述.
The agent will have two user interfaces:
A Streamlit based tweb client
A command line interface
OpenAI functions
处理法学硕士回复的主要问题之一是 ChatGPT 人们的反应是不能完全预测的吗. 当您尝试解析响应时,输出中可能会有细微的变化,这使得编程工具的解析容易出错. 因为LangChain代理将用户输入发送到LLM,并期望它将输出路由到特定的工具(或功能)。, 代理需要能够解析可预测的输出.
为了解决这个问题,OpenAI在2023年6月13日引入了“函数调用”,它允许开发人员描述JSON输出,描述基于特定输入调用哪些函数(在LangChain中称为:工具).
最初由LangChain代理(如e.g. ZERO_SHOT_REACT_DESCRIPTION代理—使用提示工程将消息路由到正确的工具. 这种方法可能比使用OpenAI函数的方法更不准确和更慢.
在撰写本文时,支持此特性的模型如下:
gpt-4–0613
gpt-3.5-turbo-0613 (this includes gpt-3.5-turbo-16k-0613,我们用它来做你的游乐场聊天代理)
以下是OpenAI十大网博靠谱平台函数调用如何工作的一些例子:
将查询转换为send_email(to: string)这样的函数调用,例如“给Anya发邮件,看看她下周五是否想喝咖啡”, body: string), or “What’s the weather like in Boston?get_current_weather(位置:string,单位:'摄氏度' | '华氏度')
在内部,十大网博靠谱平台函数及其参数的指令被注入到系统消息中.
The API endpoint is:
POST http://api.openai.com/v1/chat/completions
你可以在这里找到底层API的细节:
OpenAI Platform
Explore developer resources, tutorials, API docs, 和动态示例,以充分利用OpenAI的平台.
The Agent Loop
据博客作者所知,代理循环与大多数代理相同. The basic agents code 类似于ZERO_SHOT_REACT_DESCRIPTION代理. 所以代理循环仍然可以用下图来描述:
The agent loop
Custom and LangChain Tools
LangChain代理使用工具(对应于OpenAPI函数). LangChain (v0.0.220)开箱即用,提供了大量的工具,可以让你连接到各种付费和免费服务或互动, like e.g:
arxiv (free)
azure_cognitive_services
bing_search
brave_search
ddg_search
file_management
gmail
google_places
google_search
google_serper
graphql
human interaction
jira
json
metaphor_search
office365
openapi
openweathermap
playwright
powerbi
pubmed
python
requests
scenexplain
searx_search
shell
sleep
spark_sql
sql_database
steamship_image_generation
vectorstore
wikipedia (free)
wolfram_alpha
youtube
zapier
我们将在这篇博客中展示如何创建一个自定义工具来访问自定义REST API.
Conversational Memory
当你想记住以前输入的东西时,这种类型的记忆会派上用场. 例如:如果你问“谁是阿尔伯特·爱因斯坦??然后是“谁是他的导师。?,那么会话记忆将帮助代理记住“他的”指的是“阿尔伯特·爱因斯坦”。.
Here is LangChain’s documentation on Memory.
具有函数、自定义工具和内存的代理
我们的代理可以在Git仓库中找到:
GitHub - gilfernandes/chat_functions:简单的操场聊天应用程序,与OpenAI的…
简单的操场聊天应用程序,与OpenAI的功能与内存和自定义工具进行交互. - GitHub …
In order to get it to run, please install Conda first.
然后创建如下环境并安装以下库:
conda activate langchain_streamlit
pip install langchain
pip install prompt_toolkit
pip install wikipedia
pip install arxiv
pip install python-dotenv
pip install streamlit
pip install openai
pip install duckduckgo-search
Then create an .env file with this content:
OPENAI_API_KEY=
然后,您可以使用以下命令运行代理的命令行版本:
python .\agent_cli.py
Streamlit版本可以在端口8080上使用以下命令运行:
streamlit run ./agent_streamlit.py --server.port 8080
Here are some screenshots of the tool:
Using a custom agent
UI显示正在使用哪个工具(OpenAI函数)以及向它发送了哪些输入.
Using Arxiv Tool
Using the calculator
Using Wikipedia
Code Details
http://github.com/gilfernandes/chat_functions?= post_page——e34daa331aa7来源 --------------------------------
Summary
OpenAI functions can now be easily used with LangChain. 与基于提示的方法相比,这似乎是一种更好(更快、更准确)的创建代理的方法.
OpenAI functions 也可以很容易地集成内存和自定义工具吗. There is no excuse to not use OpenAI functions 获取本博客中描述的代理类型.