目录
什么是 规范?
如何由 规范文件生成代码?
小结
2023 年北京时间 3 月 24 凌晨, 在推特上官宣:
指向的链接是:
你可以立即申请加入等待列表:
或阅读开发插件的文档:
插件可以干什么事?
总而言之,可以充许你通过 向你的同事或用户提供你能提供的在线服务。
举个例子:
如果用户在线问 :我应该在巴黎的哪里住几晚?
模型可以选择调用酒店“预订插件 API”,接收 API 响应,并将 API 返回的数据组合起来,基于其自然语言处理能力,向用户返回人类友好的答案。
这里提到了 API,那么什么是 API?
什么是 API?
API 代表应用程序编程接口,它是一组用于构建和集成应用程序软件的定义和协议。
你可以把 API 理解为餐馆的服务员,举个例子,假如我们去一家饭馆,桌面上摆着一份菜单,后厨能提供菜单上的菜式。服务员是我们与后厨沟通的桥梁,你告诉服务员你选的菜,然后他再将菜带回到你的餐桌上。在这接口调用中,相当于服务员带着你的 去告诉 ,然后将 带回给你。
至此,全世界都可以开放自己系统的 API,然后提供给 调用,而 作为“前端”,向用户提供在线服务。
插件的宣布,宣告着智能革命的开始,从此 可以访问在线服务和知识内容了。以各大平台仅存的优势就真的只剩下数据了。
如何申请?

你可以在这里申请加入等待列表:
如何开发 插件?
创建 插件需要三个步骤:
具体步骤,以官方构建一个简单的待办事项列表插件为例说明一下:
第 1 步
首先,使用以下字段定义一个 .json 文件:
{"schema_version": "v1","name_for_human": "TODO Plugin (no auth)","name_for_model": "todo","description_for_human": "Plugin for managing a TODO list, you can add, remove and view your TODOs.","description_for_model": "Plugin for managing a TODO list, you can add, remove and view your TODOs.","auth": {"type": "none"},"api": {"type": "openapi","url": "PLUGIN_HOSTNAME/openapi.yaml","is_user_authenticated": false},"logo_url": "PLUGIN_HOSTNAME/logo.png","contact_email": "dummy@email.com","legal_info_url": "http://www.example.com/legal"}
该文件将在/.well-known/ai-.json 这个位置提供。
第 2 步
接下来,我们可以定义几个简单的 接口,为特定用户提供创建、删除和获取 todo 列表项的功能。
import jsonimport quartimport quart_corsfrom quart import requestapp = quart_cors.cors(quart.Quart(__name__), allow_origin="*")_TODOS = {}@app.post("/todos/")async def add_todo(username):request = await quart.request.get_json(force=True)if username not in _TODOS:_TODOS[username] = []_TODOS[username].append(request["todo"])return quart.Response(response='OK', status=200)@app.get("/todos/")async def get_todos(username):return quart.Response(response=json.dumps(_TODOS.get(username, [])), status=200)@app.delete("/todos/")async def delete_todo(username):request = await quart.request.get_json(force=True)todo_idx = request["todo_idx"]# fail silently, it's a simple pluginif 0 <= todo_idx < len(_TODOS[username]):_TODOS[username].pop(todo_idx)return quart.Response(response='OK', status=200)@app.get("/logo.png")async def plugin_logo():filename = 'logo.png'return await quart.send_file(filename, mimetype='image/png')@app.get("/.well-known/ai-plugin.json")async def plugin_manifest():host = request.headers['Host']with open("manifest.json") as f:text = f.read()text = text.replace("PLUGIN_HOSTNAME", f"https://{host}")return quart.Response(text, mimetype="text/json")@app.get("/openapi.yaml")async def openapi_spec():host = request.headers['Host']with open("openapi.yaml") as f:text = f.read()text = text.replace("PLUGIN_HOSTNAME", f"https://{host}")return quart.Response(text, mimetype="text/yaml")def main():app.run(debug=True, host="0.0.0.0", port=5002)if __name__ == "__main__":main()
这是一个在内存中实现简单的增删改查功能的 Py 示例。它同时做为 Web 程序还提供了 需要的 ai-.json 和 .yaml。
第 3 步
最后,我们需要设置和定义 规范,以匹配本地或远程服务器上定义的接口。您不需要通过规范公开 API 的全部功能,而是可以选择性地让 只访问某些功能。
有不少工具可以自动将服务器定义代码转换为 规范,因此您不需要手动执行此操作。对于上面的 代码,它的 规范说明看起来像这样:
openapi: 3.0.1info:title: TODO Plugindescription: A plugin that allows the user to create and manage a TODO list using ChatGPT. If you do not know the user's username, ask them first before making queries to the plugin. Otherwise, use the username "global".version: 'v1'servers:- url: PLUGIN_HOSTNAMEpaths:/todos/{username}:get:operationId: getTodossummary: Get the list of todosparameters:- in: pathname: usernameschema:type: stringrequired: truedescription: The name of the user.responses:"200":description: OKcontent:application/json:schema:$ref: '#/components/schemas/getTodosResponse'post:operationId: addTodosummary: Add a todo to the listparameters:- in: pathname: usernameschema:type: stringrequired: truedescription: The name of the user.requestBody:required: truecontent:application/json:schema:$ref: '#/components/schemas/addTodoRequest'responses:"200":description: OKdelete:operationId: deleteTodosummary: Delete a todo from the listparameters:- in: pathname: usernameschema:type: stringrequired: truedescription: The name of the user.requestBody:required: truecontent:application/json:schema:$ref: '#/components/schemas/deleteTodoRequest'responses:"200":description: OKcomponents:schemas:getTodosResponse:type: objectproperties:todos:type: arrayitems:type: stringdescription: The list of todos.addTodoRequest:type: objectrequired:- todoproperties:todo:type: stringdescription: The todo to add to the list.required: truedeleteTodoRequest:type: objectrequired:- todo_idxproperties:todo_idx:type: integerdescription: The index of the todo to delete.required: true
这是 .yaml 文件的内容,它将告诉 ,如何调用接口。
这里提代了 规范,可能有读者不了解,那么什么是 规范?
什么是 规范?
规范(OAS),是定义一个标准的、与具体编程语言无关的 API 的规范。
规范使得人类和计算机都能在“不接触任何程序源代码和文档、不监控网络通信”的情况下理解一个在线服务的作用。

遵循该规范的好处是什么?
如果您遵循 规范来定义您的 API,那么您就可以用文档生成工具来展示您的 API,用代码生成工具来自动生成其它编程语言的服务器端和客户端代码,用自动测试工具进行测试等等。
简而言之,通过该规范,我们即可以由接口程序代码生成规范,也可以由规范生成其它后端语言实现的接口程序代码。
这里有一个份关于 规范的中文文档,可以考虑:
如何由 规范文件生成代码?
可以使用 –cli。
它的工具仓库地址是:
第 1 步,安装工具
npm install @openapitools/openapi-generator-cli -g
第 2 步,由规范生成代码
下面是一个使用 生成 服务端代码的示例。
$ openapi-generator generate -i /path/to/openapi.yaml -g python-flask -o /output/directory
其中,/path/to/.yaml 是 规范文件的路径,-g 指定使用的生成器(这里是 -flask),-o 指定输出目录。
小结
1946年电子计算机被发明出来的时候,当时没有多少人意识到它将发挥多大作用,我们现在知道的信息革命其实是我们后来总结的。今天开始的智能革命,其实具有同样的特征,如果说的发布还不能让你感到震撼的话,那么今天它发布的 ,应该能够引起你足够的重视了。一个联网的智能机器人,无穷的数据+无穷的算力,谁也不知道将产生怎样的结果。智能革命已经到来,AI或许不会取代的人,但会使用AI的人,一定会把不会使用AI的人取代。
你对了解多少,开始试用了吗?如果你是一名程序员,开始研究它的API文档了吗?
相关文章链接:
-End –
大多数人的问题是,不够聪明也不够蠢,事情做不好还想得多
小步快跑,正向反馈;面向未来,不求完美