首页 >> 大全

简单实现一个ChatGPT Plugin

2023-08-13 大全 31 作者:考证青年

对 Plus用户开放了的权限。初步体验下来虽然bug不少,不过效果很不错,是一个较为成熟的。个人认为, 的体验要远优于New Bing(bing还要继续努力..)

今天从零开始开发一个 ,试水一下的潜力。

如何工作?

通过访问外部API来:

具体来讲,需要以下两个文件:

.well-known/ai-.json:类似于 开发中的.json。该文件记录的一些。这些信息将用来在插件商店展示该插件,并用来告诉这个插件是干啥的。

.yaml:是一个标准化文档,向解释了API所提供的。并说明了如何调用和 的的具体格式等。

接下来我们开发一个 ,实现查询未来几个月即将举办的AI会议,管理会议投稿收藏会议并制定To-do list等功能。

.well-known/ai-.json

我们首先在这个文件中声明的基础信息,包括名字,版本号,介绍(有给人类看的介绍,也有给GPT看的描述),logo,联系方式等等。这里不涉及任何具体功能的实现。

{    "schema_version": "v1",    "name_for_human": "AI Conferences",    "name_for_model": "AIConf",    "description_for_human": "Plugin to get information about upcoming AI conferences.",    "description_for_model": "Plugin to get information about upcoming AI conferences.",    "auth": {      "type": "none"    },    "api": {      "type": "openapi",      "url": "http://localhost:5023/openapi.yaml",      "is_user_authenticated": false    },    "logo_url": "http://localhost:5023/logo.png",    "contact_email": "yuchengli@example.com",    "legal_info_url": "http://example.com/AIConf"  }

如果需要进行身份验证(比如预定酒店等),可以在auth 这里使用OAuth,不过这里我们不需要身份验证。

在本地实现我们的

这里我们用Flask跑一个本地的来实现的具体功能。

from flask import Flask, send_from_directoryfrom flask_cors import CORS
app = Flask(__name__)CORS(app, origins='https://chat.openai.com/')
@app.get("/logo.png")def logo():    return send_from_directory(app.root_path, "logo.png")
@app.get("/.well-known/ai-plugin.json")def plugin_manifest():    with open(".well-known/ai-plugin.json") as f:        return f.read()
@app.get("/openapi.yaml")def openapi():    with open("openapi.yaml") as f:        return f.read()
# 从Github搞来最近的AI会议数据def update_db():    confs_yaml_file = requests.get('https://raw.githubusercontent.com/paperswithcode/ai-deadlines/gh-pages/_data/conferences.yml').text    confs = yaml_load(confs_yaml_file, Loader=Loader)
    global up_coming_conferences    up_coming_conferences = []    for conf in confs:        if 'abstract_deadline' in conf:            deadline = conf['abstract_deadline']        elif 'deadline' in conf:            deadline = conf['deadline']        try:            deadline = datetime.strptime(deadline, "%Y-%m-%d %H:%M:%S")        except:            deadline = datetime.strptime(deadline, "%Y-%m-%d %H:%M")        if deadline > datetime.now():            up_coming_conferences.append(conf)
# 当GPT访问http://localhost:5023/all时,返回list of conference。每个conference包含了title,year,link,place,sub,deadline等信息。@app.get("/all")def all():    update_db()    results = []    # we only need title, year, link, place, sub, and deadline    for conf in up_coming_conferences:        result = {}        result['title'] = conf['title']        result['year'] = conf['year']        result['link'] = conf['link']        result['place'] = conf['place']        result['sub'] = conf['sub']        result['deadline'] = conf['deadline']        results.append(result)    responses = json.dumps(results)    return responses
if __name__ == '__main__':    app.run(debug=True, host='0.0.0.0', port=5023)

我们在def ()搞来最近的会议数据。

在@app.get("/all")里确定了Api的。访问该,也即:5023/all ,将返回一个list的信息。

其他需要注意的是:

我们需要CORS来允许访问我们的。

@app.get("/logo.png"): 一个logo文件。

@app.get("/.well-known/ai-.json"): 文件。

@app.get("/.yaml"): api说明文件。

这里我们为我们的插件精心挑选一个清心寡欲的logo,像极了写论文时的我:

简单实现水肥一体化_简单实现一个列表渲染_

.yaml

该文件是标准的格式的文档,目的是告诉GPT我们的提供了哪些,该如何调用这些,和的输出形式是什么。

分开来讲:首先是插件基本信息的插件的url地址。

openapi: 3.0.1info:  title: AI Conferences  description: Plugin to get information about upcoming AI conferences.  version: 'v1'servers:  - url: http://localhost:5023

:/all ,也即访问:5023/all 的方法和结果。我们这里,我们告诉GPT使用get与服务器交流。则告诉GPT这个是干啥的用的。

paths:  /all:    get:      operationId: getAllUpcomingConf      summary: Get information about all the upcoming conferences      responses:        "200":          description: OK          content:            application/json:              schema:                $ref: '#/components/schemas/getAllUpcomingConfResponses'

格式:这里告诉GPT该怎么解读返回的结果。

components:  schemas:    getAllUpcomingConfResponses:      type: array      items:        type: object        properties:          link:            type: string            description: The link to the conference website.          place:            type: string            description: The place where the conference will be held.          sub:            type: string            description: The subfield of AI that the conference is about.          deadline:            type: string            description: The deadline for submitting papers.

放在一起有:

openapi: 3.0.1info:  title: AI Conferences  description: Plugin to get information about upcoming AI conferences.  version: 'v1'servers:  - url: http://localhost:5023paths:  /all:    get:      operationId: getAllUpcomingConf      summary: Get information about all the upcoming conferences      responses:        "200":          description: OK          content:            application/json:              schema:                $ref: '#/components/schemas/getAllUpcomingConfResponses'components:  schemas:    getAllUpcomingConfResponses:      type: array      items:        type: object        properties:          link:            type: string            description: The link to the conference website.          place:            type: string            description: The place where the conference will be held.          sub:            type: string            description: The subfield of AI that the conference is about.          deadline:            type: string            description: The deadline for submitting papers.

在页面注册

搞定上面的之后,需要去的商店注册我们的插件。

打开 store.

点 your own .

输入我们的地址

这里点,安装我们清新寡欲的

安装好了在这里选定,开始对话!

使用体验

_简单实现一个列表渲染_简单实现水肥一体化

首先,我们问一下最近都有哪些会议可以投稿:

!看起来挺靠谱,点一下最上方的展开按钮,我们可以看到我们服务器返回给GPT的真实内容:

可以看到就是把得到的内容翻译成了自然语言。

添加新功能

OK,我们再实现一个新功能,让可以为我们收藏目标会议,以便今后打算。

starred_conferences = []@app.get("/star/")def star(conf_name):    update_db()    for conf in up_coming_conferences:        if conf['title'] == conf_name:            starred_conferences.append(conf)            return "OK"    return "Not Found"

如代码所示,我们添加了一个新的,让GPT可以通过GET请求,收藏一个特定的会议。

根据这个功能更新.yaml(具体改动略,详见文末的 repo) 。

Done!光收藏还不行,收藏完了我们总是要查看我们收藏了什么。所以我们需要实现查看已收藏会议的方法。添加新的 /用于查询已收藏内容

@app.get("/starred")def starred():    results = []    for conf in starred_conferences:        result = {}        result['title'] = conf['title']        result['year'] = conf['year']        result['link'] = conf['link']        result['place'] = conf['place']        result['sub'] = conf['sub']        result['deadline'] = conf['deadline']        results.append(result)    responses = json.dumps(results)    return responses

同时更新.yaml (具体改动略,详见文末的 repo)。

测试一下新功能:

这里我们成功收藏了EMNLP会议,并随后查询了所收藏会议的一些信息。

是怎么教会GPT使用的?

很好奇是怎么调教GPT来让它使用的。根据关于的文档,基本可以确定是将文件和.yaml直接给GPT过目。不过不能确定这里是不是用了某些很神奇的。

简单问一下GPT,看会不会漏出什么马脚。

并没有漏出什么马脚,所以我们暂时无法得知具体用什么指令来指导GPT了。

参考

完整的实现参见 repo:

对的说明:

关于我们

最火推荐

小编推荐

联系我们


版权声明:本站内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 88@qq.com 举报,一经查实,本站将立刻删除。备案号:桂ICP备2021009421号
Powered By Z-BlogPHP.
复制成功
微信号:
我知道了