一、什么是ChatGPT插件
由于ChatGPT的数据是停留在2021年,插件的目的是为ChatGPT提供获取最新数据、计算任务和第三方服务功能的能力,进而提升ChatGPT的体验。开发者也可以为ChatGPT开发不同功能的插件给用户使用,就像大家在AppStore开发不同的App一样。
二、官方ChatGPT插件实现的原理
在体验官方插件的使用大致推断其工作原理:
- 用户想ChatGPT进行提问
- ChatGPT会理解用户的提问选择相应的插件,并且把问题传递给相应的插件
- 插件会返回对应问题的结果
- ChatGPT会对插件返回的问题进行总结,看是否需要继续选择其他可用插件,如果是跳到步骤2
- ChatGPT将处理好的结果返回给用户
三、自定义的插件功能原理
通过上面我们大概知道了官方的插件实现的一个流程,其中最重要的部分是就是ChatGPT到底是怎么自动选择相应的插件然后执行的。只要解决这个问题就实现插件功能就能迎刃而解了。
比如我想知道今天北京的天气,当我们直接问ChatGPT时,它是无法回答的。
而我们想要的效果是当我们问的问题ChatGPT无法回答时,它能够返回一个JSON用于描述我们怎么调用第三方接口,比如使用Google的API。那到底要怎么实现呢?
带着好奇,我在github搜索GPT相关项目,发现了一个实现插件机制的开源项目EX-chatGPT,这不就是我想要实现的效果么?于是阅读它的源码后才恍然大悟。它的prompt提示词写非常的详细,目的就是让ChatGPT返回约定个格式的数据,然后在调用相关接口。他的提示词如下:
Input: 2023年6月6日 北京天气
ChatGpt response: 抱歉,作为语言模型,我的能力仅限于生成文本,无法提供实时天气信息。建议您查看天气预报网站或应用程序,以获取准确的天气信息。
Instructions:
Your task is to generate a list of API calls to a Question Answering API based on the input query and chatgpt response. The API calls should help extract relevant information and provide a better understanding of the input query and verify the response of chatgpt.
You can make an API call by using the following format: "{"API": "{API}", "query": "{query}"}". Replace "{API}" with one of the following options: 'WikiSearch', 'Calculator', or 'Google'. Replace "{query}" with the specific question you want to ask to extract relevant information.
Note that the WikiSearch API requires an English input consisting of a precise concept word related to the question, such as a person's name. The Google API requires a full, complete question in the same langeuage as the query that includes enough information about the question, such as who, what, when, where, and why. The Calculator API requires a clear, simple mathematical problem in the WolframAlpha format.
Here are some examples of API calls:
Input: Coca-Cola, or Coke, is a carbonated soft drink manufactured by the Coca-Cola Company.
Output: {"calls": [{"API": "Google", "query": "What other name is Coca-Cola known by?"},
{"API": "Google", "query": "Who manufactures Coca-Cola?"}]}
Input: Out of 1400 participants, 400 passed the test.
Output: {"calls":[{"API": "Calculator", "query": "400 / 1400"}]}
Input: 电视剧狂飙怎么样, 和三体比应该看哪一部?
Output: {"calls":[{"API": "Google", "query": "电视剧狂飙"},{"API": "Google", "query": "电视剧狂飙评分"},{"API": "Google", "query": "电视剧三体评分"},{"API": "Google", "query": "三体和狂飙谁更好?"}]}
To ensure better understanding, the Google API question must match the input query language. Additionally, the API calls should thoroughly validate every detail in ChatGPT's response.
Sort the JSON order based on relevance and importance as requested by the API query, with the most relevant item at the beginning of the list for easier understanding.
由于提示词太长,我在这里简单总结一下,大体分为三个部分。
- 输入:将用户的输入和GPT的回答作为输入
- 指令和例子(Instructions:): 给
ChatGPT相应的指令,希望它做什么。并且提供相关的示例供ChatGPT学习。 - 输出:对
ChatGPT的输出进行了比较详情的说明
在使用上面提示调用ChatGPT的API接口能够返回我们期望的JSON数据描述我们需要调用的API,
套用Ex-ChatGPT的提示词我们容易就得到了想要的结果,这就是实现调用插件的核心原理,它原来这么简单,只是用自然语言把这个需求进行了描述,就能得到想要的结果,难怪有人说以后只需要Prompt工程师就行了,不得不说站在巨人肩膀编程还是爽,再次感谢这个开源作者。