🔌 实战 ChatGPT Plugin 开发:从零构建AI插件生态
一、本章概览
第8章《实战 ChatGPT Plugin 开发》是从"调用API"迈向"构建AI生态"的关键实战章,全面讲解 ChatGPT 插件的开发流程、核心概念与实战项目。
核心内容:
-
ChatGPT Plugin 介绍与生态愿景 -
插件开发全流程(Plugin Flow) -
三大核心文件(ai-plugin.json / openapi.yaml / main.py) -
实战:待办(Todo)管理插件 -
实战:天气预报(Weather Forecast)插件 -
高德天气开放平台对接 -
Function Calling vs ChatGPT Plugin 对比 -
Quart Web 框架快速上手 -
课程项目:openai-quickstart
二、ChatGPT Plugin 介绍与生态
1. 官方定义
★ChatGPT 已实现插件的初始支持。插件是专门为语言模型设计的工具,以安全性为核心原则,帮助 ChatGPT 访问最新信息、运行计算或使用第三方服务。
2. 愿景
★基于 ChatGPT 能力,以 Plugin 形式,赋能千行百业。
3. 首批插件生态一览
|
|
|
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4. Plugin Store 界面
插件商店包含分类:
-
Popular(热门) -
New(最新) -
Installed(已安装) -
支持搜索插件 -
提供三个入口:安装未验证插件 / 开发自己的插件 / 关于插件
三、插件开发推荐步骤(Plugin Flow)
端到端的插件构建流程分为 4大步骤:
第1步:创建 manifest 文件并托管
↓
第2步:在 ChatGPT UI 中注册插件
↓
第3步:用户激活插件
↓
第4步:用户开始对话
步骤1:创建 manifest 文件并托管
-
在您的域名下创建 yourdomain.com/.well-known/ai-plugin.json -
文件包含:插件元数据(名称、Logo)、认证要求、OpenAPI 规范 -
模型会看到 OpenAPI 的 description 字段,用于自然语言理解 -
⚠️ 建议:一开始仅暴露 1-2 个端点、最少参数,以最小化文本长度(插件描述 + API 请求 + API 响应都会被插入对话,计入上下文限制)
步骤2:在 ChatGPT UI 中注册
-
顶部下拉菜单 → 选择 Plugins 模式 -
Plugin Store → Develop your own plugin -
如需认证,提供 OAuth 2 client_id / client_secret 或 API Key
步骤3:用户激活插件
-
用户必须在 ChatGPT UI 中手动激活插件(默认不会使用) -
可将插件分享给另外 100 个用户(仅开发者可安装未验证插件) -
需 OAuth 时,用户将重定向到插件进行登录
步骤4:用户开始对话
① OpenAI 注入插件描述(对用户不可见,含端点、描述、示例)
↓
② 用户提问 → 模型判断是否调用插件 API
↓
③ POST 请求需用户确认(避免破坏性操作)
↓
④ API 调用结果 → 模型纳入响应
↓
⑤ 链接 → 富预览(Open Graph 协议)
↓
⑥ 数据 → Markdown 自动渲染
四、三大核心文件
ChatGPT Plugin 由三个核心文件构成:
|
|
|
|
|---|---|---|
| ai-plugin.json |
|
/.well-known/ai-plugin.json |
| openapi.yaml |
|
/openapi.yaml |
| main.py |
|
/main.py |
五、实战一:待办(Todo)管理插件
1. 演示效果
|
|
|
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2. ai-plugin.json
{
"schema_version": "v1",
"name_for_human": "ToDo List",
"name_for_model": "todo",
"description_for_human": "Manage your todos",
"description_for_model": "Plugin for managing todo lists",
"auth": { "type": "none" },
"api": {
"type": "openapi",
"url": "http://localhost:5003/openapi.yaml"
},
"logo_url": "http://localhost:5003/logo.png",
"contact_email": "legal@example.com",
"legal_info_url": "http://example.com/legal"
}
3. openapi.yaml(核心片段)
openapi: 3.0.1
info:
title: ToDo Plugin
description: A plugin that allows the user to create and manage todos
version: 'v1'
servers:
- url: http://localhost:5003
paths:
/todos/{username}:
get:
operationId: getTodos
summary: Get the list of todos
parameters:
- in: path
name: username
schema: { type: string }
required: true
description: The name of the user.
responses:
"200":
description: OK
4. main.py 完整代码
import json
import quart
import quart_cors
from quart import request
app = quart_cors.cors(
quart.Quart(__name__),
allow_origin="https://chat.openai.com"
)
# 内存存储,重启后会清空
_TODOS = {}
@app.post("/todos/<string:username>")
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/<string:username>")
async def get_todos(username):
return quart.Response(
response=json.dumps(_TODOS.get(username, [])),
status=200
)
@app.delete("/todos/<string:username>")
async def delete_todo(username):
request = await quart.request.get_json(force=True)
todo_idx = request["todo_idx"]
if 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')
六、实战二:天气预报(Weather Forecast)插件
1. 插件设计
|
|
|
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
2. ai-plugin.json
{
"schema_version": "v1",
"name_for_human": "Weather Forecast",
"name_for_model": "weather",
"description_for_human": "Global Weather Forecast. You can ask the current or future weather of any city.",
"description_for_model": "plugin for managing weather forecasts. Search current weather and future forecasts.",
"auth": { "type": "none" },
"api": {
"type": "openapi",
"url": "http://localhost:5002/openapi.yaml"
},
"logo_url": "http://localhost:5002/logo.png",
"contact_email": "pjt73651@gmail.com",
"legal_info_url": "http://example.com/legal"
}
3. openapi.yaml
openapi: 3.0.1
info:
title: Weather Forecast
description: A Plugin that allows the user to forecast current or future weather
version: 'v1'
servers:
- url: http://localhost:5002
paths:
/weather/current:
get:
operationId: getCurrentWeather
summary: Get the current weather of the city
parameters:
- in: query
name: city
schema: { type: string }
required: true
description: The city and state, e.g. San Francisco, CA.
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/getCurrentWeather'
/weather/forecast:
get:
operationId: getNDayWeatherForecast
summary: Forecast the weather in a few days
parameters:
- in: query
name: num_days
schema: { type: integer }
required: true
description: The number of days to forecast, e.g. 5
- in: query
name: city
schema: { type: string }
required: true
description: The city and state, e.g. San Francisco, CA.
responses:
"200":
description: OK
components:
schemas:
getCurrentWeather:
type: object
properties:
weather:
type: string
description: The current weather of the city.
getNDayWeatherForecast:
type: object
properties:
weather:
type: string
description: The weather of the city in a few days.
4. main.py 路由结构
|
|
|
|
|---|---|---|
/logo.png |
|
|
/.well-known/ai-plugin.json |
|
|
/openapi.yaml |
|
|
/weather/current |
|
|
/weather/forecast |
|
|
5. 高德开放平台天气查询对接
平台简介:高德开放平台(阿里巴巴集团旗下)提供 Web 服务 API。
天气查询流程:
① 注册高德地图 API 账号
↓
② 创建工程,获取 Key
↓
③ 调用天气查询 API(传入 adcode)
↓
④ 获取目标区域当前/未来天气数据
|
|
|
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
七、Quart 框架简介
Quart 是一个快速的 Python Web 微框架,与 Flask 兼容,但支持异步操作。
核心能力:
|
|
|
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
★💡 为什么选择 Quart? 支持异步(async/await),适合处理高并发请求,且与 Flask 语法高度兼容,学习成本低。
八、Function Calling vs ChatGPT Plugin
|
|
|
|
|---|---|---|
| 本质 |
|
|
| 使用方式 |
|
|
| 生态 |
|
AIGC APP Store 模式 |
| 分发 |
|
|
| 典型场景 |
|
|
| 复杂度 |
|
|
| 用户交互 |
|
|
典型插件示例:
-
Link Reader:读取网页、PDF、TXT、图片、Word 等链接内容 -
ChatWithPDF:通过 PDF 提问、深入探索 -
WebPilot:浏览和问答网页/PDF/数据 -
PromptPerfect:制作完美提示词 -
Speak:AI 驱动的语言导师
九、课程项目:GitHub openai-quickstart
|
|
|
|---|---|
| 项目名称 |
|
| License |
|
| ⭐ Stars |
|
| 👀 Watch |
|
| 🍴 Forks |
|
| 定位 |
|
| 涵盖 |
|
项目目录结构
openai-quickstart/
├── openai_api/ # OpenAI API 实战
├── langchain/ # LangChain 集成示例
├── openai-translator/ # OpenAI 翻译工具
├── chatgpt-plugins/ # ChatGPT 插件开发(本章核心)
├── docs/ # 文档与学习资料
└── selected_homework/ # 学员优秀作业
📌 本章核心要点总结
|
|
|
|
|---|---|---|
|
|
Plugin 愿景 |
|
|
|
三大核心文件 |
|
|
|
开发四步走 |
|
|
|
Quart 框架 |
|
|
|
Todo 插件 |
|
|
|
天气插件 |
|
|
|
Function Calling vs Plugin |
|
|
|
安全原则 |
|

