跳过导航
跳过mega-menu

使用ChatGPT函数将REST接口转换为Chatbots

在这个故事中, 我们将探索如何使用plain将旧的REST接口转换为Chatbot ChatGPT函数调用. 有很多是以数据为中心的 REST API’s out there and many of them are used in the context of websites to list or to perform searches on all types of datasets. 这里的主要思想是基于现有REST接口创建基于聊天的体验. So we are going to add a conversational layer powered by ChatGPT on top of an existing REST interface which performs searches on an event database.

我们还将探索如何从头创建聊天机器人UI Python 建立服务器. 因此,我们不会使用任何预构建的开源库来构建UI. The server will be written in Python and the web based user interface will be written using 打印稿 and 反应.

事件聊天-一个REST转换项目

为了理解如何将REST接口转换为聊天机器人, 我们已经构建了一个小型事件聊天应用程序. This application receives a user question about upcoming events and answers the questions with a list of details about the events including images and links.

事件聊天截图

This application supports streaming and replies with external links and images that are stored in a database.

应用程序体系结构

下图是我们构建的应用程序的粗略架构:

高级组件

我们在这个应用程序中有以下组件:

  • 类编写的用户界面 反应 框架的使用 打印稿. 用户界面使用web套接字与聊天服务器通信. 输入只是十大网博靠谱平台一些事件的问题.

  • A chat server written in Python containing an http server that also uses web套接字s to communicate with the client. The chat server orchestrates the interaction with ChatGPT and old school REST interfaces to answer the user questions.

  • ChatGPT which analyses the user question and routes it to the correct function and then receives the results from the REST interface to produce the final output.

  • REST接口,用于搜索事件,并用一些额外的数据丰富检索到的事件

  • 为REST接口提供数据的PostgresSQL数据库. 实际上我们用a来搜索 Lucene索引,但其中的数据来自PostgresSQL数据库.

应用程序工作流程

这是处理单个聊天请求的工作流:

单事件请求工作流单事件请求工作流

请注意,我们在图中省略了错误边界.

工作流有4个参与者(池):

  • 只提出问题并接收对问题的响应的用户.

  • 在所有其他参与者之间协调大部分操作的聊天服务器.

  • ChatGPT接收初始问题, figures out the function calls and receives the results of function calls to format these into natural language.

  • REST Interfaces — there are only two of them: one is a simple search interface and then there is a second which enriches the initial response with URLs of web pages and images.

聊天服务器工作流程的细节

主要操作发生在聊天服务器层. 以下是十大网博靠谱平台这个池的工作流程的细节(仅限快乐路径):

  • Initially it receives the question from the client and that should typically be a question about events.

  • 问题与函数调用请求一起发送到ChatGPT 3.5 (gpt-3.5 -涡轮- 16 k - 0613)

  • 如果没有错误发生, ChatGPT接收函数名和从用户问题中提取的参数.

  • 在此阶段调用ChatGPT指定的函数, 这只是一个事件搜索REST接口).

  • 结果以JSON格式从REST接口返回.

  • 对REST接口的第二次调用恰好丰富了当前数据. 这个调用的目的是用URL和图像来丰富初始搜索结果.

  • ChatGPT is then called a second time with the reference to the previously called function and the enriched data

  • ChatGPT使用自然语言处理JSON数据后进行回复. 结果实际上被流式传输到客户端. 一旦ChatGPT生成令牌,它就会通过web套接字s流传输到客户端.

函数调用是如何工作的?

函数调用 has been introduced in June 2023 to help developers to receive structured JSON with descriptions of locally callable functions. 事实证明,函数调用非常适合集成 ChatGPT 使用REST接口,因为它有助于提高ChatGPT的输出可靠性.

当您使用ChatGPT模型(如e.g. gpt-3.5-turbo-16k-0613或gpt-4-0613您最初发送消息, 函数和提示符的说明. 在理想的场景中,ChatGPT使用要调用的函数进行响应.

In a second step you call the function specified by ChatGPT with the extracted parameters and get the result in some text format (like i.e. JSON).

在最后一步中,将被调用函数的输出发送给ChatGPT, 与初始提示一起. ChatGPT使用自然语言生成最终答案.

总共有3个步骤:

  1. 查询ChatGPT以获取函数调用及其参数. 以基于JSON的格式发送用户提示和功能规范.

  2. 使用ChatGPT提取的参数调用实际的Python函数, 哪些是从用户提示符中提取的.

  3. Query ChatGPT with the function output and then initial user prompt to get the final natural language based output.

例子

在我们的例子中,我们有一个签名如下的函数:

def event_search (

   搜索:str,

   locality:可选[str] = "",

   country:可选[str] = "",

   tags: List[str] = []

   repeat: bool = True;

) -> str:

...

当用户询问例如:

你能在英国找到十大网博靠谱平台冥想的活动吗?

ChatGPT将在第一次调用(步骤1)中提取对应的两个函数参数:

  • 搜索:“冥想”

  • 国家:“联合王国”

The function will then be called with these two parameters (step 2) and it will then call the REST interface that returns JSON based response. 然后,这个JSON响应将与原始问题一起发送回ChatGPT(步骤3)。.

在步骤3中 聊天完成API 将用3条消息调用:

  • 系统消息(ChatGPT请求的后台信息)

  • 原始用户提示

  • 使用提取的JSON进行函数调用

Note that the last message has the role “function” and contains the JSON content in the variable “content”. The full code of the function which extracts the parameters for this last call to the ChatGPT completion API can be found here in function extract_event_search_parameters in http://github.com/onepointconsulting/event_management_agent/blob/main/event_management_agent/service/event_search_service.py

事件聊天实现

Event Chat原型包含两个应用程序. The first application is the back-end application written in Python and can be found in this repository:

http://github.com/onepointconsulting/event_management_agent 

第二个应用程序是用Javascript编写的前端应用程序 反应:

http://github.com/onepointconsulting/chatbot-ui 

后端代码

后端代码由一个基于web套接字的服务器组成 python-套接字io (implements a transport protocol that enables real-time bidirectional event-based communication between clients) and aiohttp 异步HTTP客户端/服务器 asyncio 和Python).

主要的web套接字服务器实现可以在这个文件中找到:

http://github.com/onepointconsulting/event_management_agent/blob/main/event_management_agent/server/websocket_server.py 

处理用户问题的函数如下:

http://github.com/onepointconsulting/event_management_agent/blob/main/event_management_agent/server/websocket_server.py#L37 

这是在聊天服务器上触发整个工作流的功能. 它接收来自客户端的问题, 触发事件搜索并将数据流发送到客户端. 它还确保在发生错误时正确关闭流.

这个方法调用另外两个方法:

  • process_search — this method processes the incoming question up to the point in which the search via the REST API is executed.

  • aprocess_stream — this method receives the search result and returns an iterable that allows streaming to the client.

这些方法可以在这个文件中找到: http://github.com/onepointconsulting/event_management_agent/blob/main/event_management_agent/service/event_search_service.py 

你可以在这里找到process_search方法: http://github.com/onepointconsulting/event_management_agent/blob/main/event_management_agent/service/event_search_service.py#L115 

This method contains first a call to event_search_openai the main function which performs all calls to the OpenAI function calls: http://github.com/onepointconsulting/event_management_agent/blob/main/event_management_agent/service/event_search_service.py#L53 

The process_search method contains also a call to execute_chat_function which takes the completion_message produced by the OpenAI function and finds the function using the Python’s evalmethod and finally executes the event_search function: http://github.com/onepointconsulting/event_management_agent/blob/main/event_management_agent/service/event_search_service.py#L89 

This is the method which handles the result of the second call to event_search_openai in process_search. 它大部分时间通过OpenAI API生成的令牌流循环: http://github.com/onepointconsulting/event_management_agent/blob/main/event_management_agent/service/event_search_service.py#L176 

前端代码

聊天界面的主要组成部分是:

http://github.com/onepointconsulting/chatbot-ui/blob/main/src/components/MainChat.tsx 

除了渲染UI,它还使用webhook useWeb套接字, 其中包含处理基于web套接字的事件处理程序的代码 套接字.io-client 图书馆. useWeb套接字hook的实现可以在这个文件中找到: http://github.com/onepointconsulting/chatbot-ui/blob/main/src/hooks/useWebsocket.ts 

观察

This project is just meant as a small prototype and a learning experience and could be improved in many ways — especially the user interface would need a better and more professional design and lacks features like e.g. 聊天记录.

外卖

如果您有允许您搜索数据的REST接口, 然后,您应该能够使用它们构建聊天界面. ChatGPT的函数调用是在这些搜索界面之上堆叠聊天层的好方法. 你不需要一个非常强大的模型,你可以使用像gpt-3这样的旧模型.5-turbo-16k-0613,仍然提供良好的用户体验.

Writing your own chat user interface is also an interesting experience and allows you to create much more customized implementations — when compared to using projects like e.g Chainlit or Streamlit. 等项目 Chainlit 为您提供了大量的功能,并帮助您快速启动和运行, 但在某个阶段限制你的设计选择,以防你做了很多UI定制. Writing your own chat user interface though requires some proficiency with 打印稿 or Javascript and some UI framework, 像反应Js, VueJs或苗条.

如果你想有聊天流媒体, then you should definitely use the web 套接字 protocol which also allows you to keep a chat memory in a rather convenient way, 由于其内置的web套接字会话.

My last takeaway is also that you do not always need to use LangChain whenever you want to interface with LLM models. LangChain 作为许多LLM api之上的一层非常方便,但代价是一个很大的依赖树. 如果你真的知道,你只会使用一个特定的LLM的一个非常具体的功能, 那你就别用了 LangChain.


吉尔·费尔南德斯,Onepoint咨询公司

澳门十大正规赌博娱乐平台

在这里注册