How to

【How to】开发ChatGPT插件:全面指南

ChatGPT 插件开发:全面指南

摘要: 本文详细介绍如何从零开始构建 ChatGPT 插件。通过 ai-plugin.json和 OpenAPI 规范定义功能,结合 Serverless 架构部署后端服务。文章还探讨了插件在 AI 应用编排中的商业价值及 Dify 平台的集成支持。


ChatGPT 插件提供了一种强大的方式,可以通过 API 整合额外功能来扩展 AI 客户端的能力。在本篇博客中,我们将引导你完成创建 ChatGPT 插件的全过程,并讨论一些潜在的商业应用场景。最后,我们将简要介绍 Dify 如何支持内部和第三方插件的集成,以实现无缝的 AI 应用编排。

什么是 ChatGPT 插件?

从核心来看,ChatGPT 插件是一个 ai-plugin.json文件,它描述了插件的功能和 API 信息。以下是一个示例:

{
  "schema_version": "v1",
  "name_for_human": "TODO Manager",
  "name_for_model": "todo_manager",
  "description_for_human": "Manages your TODOs!",
  "description_for_model": "An app for managing a user's TODOs",
  "api": {
    "url": "/openapi.json"
  },
  "auth": {
    "type": "none"
  },
  "logo_url": "https://example.com/logo.png",
  "legal_info_url": "http://example.com",
  "contact_email": "hello@example.com"
}

通过访问这个 JSON 文件,ChatGPT 或其他 AI 客户端可以了解插件的目的和功能。这使得它们能够将插件集成到工具箱中,并在需要时使用它。这类似于 Langchain 的代理模式(Proxy Mode),其中公共和私有 API 都可以作为 AI 客户端的插件,充当它们的“眼睛”和“双手”。

如何从零开始开发 ChatGPT 插件?

如果你打算从头创建一个 ChatGPT 插件(例如执行 Google 搜索的功能),可以按照以下步骤开发和部署到 Serverless 平台:

第一步:确定任务

为你的插件选择一个具体的任务。例如,使用 Google Search API 执行 Google 搜索。

第二步:创建 Serverless 项目

使用你喜欢的 Serverless 框架创建一个新项目。常见的选择包括 AWS Lambda、Google Cloud Functions 或 Azure Functions。

第三步:编写核心代码

在你的 Serverless 项目中,编写处理 Google 搜索功能的必要代码。确保处理好以下事项:
* 身份验证:配置必要的 API Key 或其他认证方式。
* 输入验证:检查用户输入的合法性。
* 错误处理:优雅地处理可能出现的异常。

第四步:定义 OpenAPI 规范

在 Serverless 项目中创建一个 openapi.json文件,以定义插件所需的 API 端点和数据结构。该文件应遵循 OpenAPI Specification (OAS) 标准。你可以使用 Swagger Editor 或 OpenAPI Generator等在线工具来辅助创建此文件。

以下是一个简单的 Google Search 插件的 openapi.json示例:

{
  "openapi": "3.0.0",
  "info": {
    "title": "Google Search Plugin",
    "version": "1.0.0"
  },
  "paths": {
    "/search": {
      "get": {
        "summary": "Perform a Google Search",
        "parameters": [
          {
            "name": "query",
            "in": "query",
            "description": "Search query",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Successful search",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "type": "object",
                    "properties": {
                      "title": {
                        "type": "string"
                      },
                      "link": {
                        "type": "string"
                      },
                      "snippet": {
                        "type": "string"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

第五步:部署项目

将你的 Serverless 项目部署到你选择的平台上。部署完成后,你将获得一个公共 URL,用于访问插件的 API。

第六步:配置 ai-plugin.json

创建你插件的 ai-plugin.json文件(如前文所述)。更新其中的 api.url字段,填入你在第五步中从 Serverless 平台获得的公共 URL。

以下是一个 Google Search 插件的 ai-plugin.json示例:

{
  "schema_version": "v1",
  "name_for_human": "Google Search",
  "name_for_model": "google_search",
  "description_for_human": "Perform a Google Search using the Google Search API!",
  "description_for_model": "An app for performing Google Searches using the Google Search API",
  "api": {
    "url": "https://your-serverless-function-url.com/openapi.json"
  },
  "auth": {
    "type": "api_key",
    "key_name": "google_search_api_key"
  },
  "logo_url": "https://example.com/google_search_logo.png",
  "legal_info_url": "http://example.com",
  "contact_email": "hello@example.com"
}

Dify 对插件的支持

虽然手动开发插件提供了极大的灵活性,但对于大多数开发者而言,集成现有的工具更为高效。Dify 平台原生支持内部和第三方插件的集成。这意味着你可以轻松地将上述开发的自定义插件或市场上成熟的第三方插件接入到你的 AI 工作流中。这种无缝集成的能力使得在 Dify 上编排复杂的 AI 应用变得简单而强大。

文章来源: https://dify.ai/blog/chatgpt-plugin
← 返回文章列表