大数跨境

实战 ChatGPT Plugin 开发:从零构建AI插件生态

实战 ChatGPT Plugin 开发:从零构建AI插件生态 知识代码AI
2026-09-18
2

🔌 实战 ChatGPT Plugin 开发:从零构建AI插件生态

一、本章概览

第8章《实战 ChatGPT Plugin 开发》是从"调用API"迈向"构建AI生态"的关键实战章,全面讲解 ChatGPT 插件的开发流程、核心概念与实战项目。

核心内容:

  1. ChatGPT Plugin 介绍与生态愿景
  2. 插件开发全流程(Plugin Flow)
  3. 三大核心文件(ai-plugin.json / openapi.yaml / main.py)
  4. 实战:待办(Todo)管理插件
  5. 实战:天气预报(Weather Forecast)插件
  6. 高德天气开放平台对接
  7. Function Calling vs ChatGPT Plugin 对比
  8. Quart Web 框架快速上手
  9. 课程项目:openai-quickstart

二、ChatGPT Plugin 介绍与生态

1. 官方定义

ChatGPT 已实现插件的初始支持。插件是专门为语言模型设计的工具,以安全性为核心原则,帮助 ChatGPT 访问最新信息、运行计算或使用第三方服务

2. 愿景

基于 ChatGPT 能力,以 Plugin 形式,赋能千行百业。

3. 首批插件生态一览

插件
功能
🌍 Expedia
将旅行计划变为现实——到达、住宿、游玩、交通和租车推荐
📊 FiscalNote
提供法律、政治和监管领域的实时数据与信息
🛒 Instacart
从本地杂货店下单
✈️ KAYAK
搜索航班、住宿和租车,获取预算内推荐
🛍️ Klarna Shopping
搜索并比较数千家在线商店的价格
👶 Milo Family AI
赋予父母超能力,将混乱变为魔法,每天20分钟
🍽️ OpenTable
提供餐厅推荐,并可直接链接预订
🎁 Shop
搜索全球最伟大品牌的数百万种产品
🗣️ Speak
AI 驱动的语言导师,学习如何说任何语言
🧮 Wolfram
通过 Wolfram Alpha 访问计算、数学、知识和实时数据
🔗 Zapier
与 5000+ 应用交互(Google Sheets、Trello、Gmail 等)

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
API 接口规范定义
/openapi.yaml
main.py
服务接口实现
/main.py

五、实战一:待办(Todo)管理插件

1. 演示效果

用户输入
ChatGPT 输出
我有哪些 todo
您目前的待办事项列表是空的。
明天10点提醒我开会
已经添加了您的待办事项:"明天10点开会"。
周日19点上课
已经添加了您的待办事项:"周日19点上课"。
这周有什么安排
这周安排:1.明天10点开会 2.周日19点上课
那日程安排刷新下,告诉我
最新待办事项:1.明天(星期四)10点开会 2.周日19点上课

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. 插件设计

属性
名称
Weather Forecast
功能
查询全球任意城市的当前天气和未来天气预报
端口
localhost:5002
技术栈
Quart + 高德天气 API

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
GET
返回插件 Logo
/.well-known/ai-plugin.json
GET
返回插件元数据
/openapi.yaml
GET
返回 OpenAPI 规范
/weather/current
GET
查询当前天气
/weather/forecast
GET
查询未来天气预报

5. 高德开放平台天气查询对接

平台简介:高德开放平台(阿里巴巴集团旗下)提供 Web 服务 API。

天气查询流程:

① 注册高德地图 API 账号
       ↓
② 创建工程,获取 Key
       ↓
③ 调用天气查询 API(传入 adcode)
       ↓
④ 获取目标区域当前/未来天气数据
项目
说明
接口类型
Web 服务 API
输入参数
adcode(区域编码)
查询内容
当前/未来天气
数据来源
中国气象数据
文档更新时间
2023年04月03日

七、Quart 框架简介

Quart 是一个快速的 Python Web 微框架,与 Flask 兼容,但支持异步操作。

核心能力:

功能
说明
📝 JSON API
编写 RESTful API
🖼️ HTML 渲染
渲染和提供 HTML 页面
🔗 WebSocket
提供 WebSocket 服务
📺 流式响应
视频流式传输
🎯 全能
以上全部功能可在单个应用中实现

💡 为什么选择 Quart? 支持异步(async/await),适合处理高并发请求,且与 Flask 语法高度兼容,学习成本低。


八、Function Calling vs ChatGPT Plugin

对比维度
Function Calling
ChatGPT Plugin
本质
直接调用函数
通过插件商店分发
使用方式
代码调用
用户手动激活
生态
单应用内部工具
AIGC APP Store 模式
分发
嵌入代码
插件商店发现/安装/管理
典型场景
执行 SQL 查询、计算
联网搜索、PDF 读取、订餐、旅行
复杂度
轻量级
需要完整插件三件套
用户交互
开发者控制
用户自主选择激活

典型插件示例:

  • Link Reader:读取网页、PDF、TXT、图片、Word 等链接内容
  • ChatWithPDF:通过 PDF 提问、深入探索
  • WebPilot:浏览和问答网页/PDF/数据
  • PromptPerfect:制作完美提示词
  • Speak:AI 驱动的语言导师

九、课程项目:GitHub openai-quickstart

项目信息
内容
项目名称
OpenAI Quickstart
License
Apache-2.0
⭐ Stars
409
👀 Watch
15
🍴 Forks
226
定位
一站式大语言模型学习资源
涵盖
理论 + API 开发基础 + LangChain 实战 + AIGC 应用

项目目录结构

openai-quickstart/
├── openai_api/          # OpenAI API 实战
├── langchain/           # LangChain 集成示例
├── openai-translator/   # OpenAI 翻译工具
├── chatgpt-plugins/     # ChatGPT 插件开发(本章核心)
├── docs/                # 文档与学习资料
└── selected_homework/   # 学员优秀作业

📌 本章核心要点总结

序号
核心要点
一句话总结
1
Plugin 愿景
基于 ChatGPT 能力,以插件形式赋能千行百业
2
三大核心文件
ai-plugin.json(元数据)、openapi.yaml(API 规范)、main.py(服务实现)
3
开发四步走
创建 manifest → 注册 UI → 用户激活 → 对话调用
4
Quart 框架
异步 Python Web 微框架,适合构建插件服务
5
Todo 插件
增删改查待办事项,演示完整的三文件开发流程
6
天气插件
对接高德开放平台天气 API,实现实时/预报天气查询
7
Function Calling vs Plugin
Function Calling 轻量内部调用,Plugin 构建生态分发
8
安全原则
POST 请求需用户确认;插件描述计入上下文限制


【声明】内容源于网络
0
0
知识代码AI
技术基底 机器视觉全栈 × 光学成像 × 图像处理算法 编程栈 C++/C#工业开发 | Python智能建模 工具链 Halcon/VisionPro工业部署 | PyTorch/TensorFlow模型炼金术 | 模型压缩&嵌入式移植
内容 410
粉丝 0
知识代码AI 技术基底 机器视觉全栈 × 光学成像 × 图像处理算法 编程栈 C++/C#工业开发 | Python智能建模 工具链 Halcon/VisionPro工业部署 | PyTorch/TensorFlow模型炼金术 | 模型压缩&嵌入式移植
总阅读7.4k
粉丝0
内容410