小白也能让 GPT 通过 API 连接各种 App

ChatGPT 把大量的知识都浓缩到一个大模型里,可以通过文字、代码或图片帮用户解决各种问题。尽管如此,它的作用范围仍然是有限的。但是,最近发布的新功能 GPTs 改变了这个局面。所有 Plus 用户都可以创建自己的 GPTs,并连接第三方 App 进一步扩展 GPT 的功能。

从此,大模型进入了可以连接一切的时代。你可以让 ChatGPT 帮你发送邮件、发布社交媒体,安排每天的计划,甚至控制家里的智能设备。

如果你还未创建过自己的 GPTs,可以先阅读这篇文章:人人都可创建自己的GPT啦!来看怎么弄

对于不懂代码的技术小白来说,API 这个名词令人望而生畏。其实 API 就是可以连接各种 App 的一个接口。 这方面的技术问题也可以让 ChatGPT 辅助解决。比如让教你如何调试 API ,如何写 Schema 等等。这些内容本文都会介绍。

如果觉得复杂,可以试试 Zapier。它已经连接了国外 6000多个 App,并且有成熟的对接 GPTs 的方案。比自己研究 API 文档方便很多,我在这篇文章里有介绍:ChatGPT 连接 6000+ App,效率提升百倍!

本文将一步一步造一个可以通过 API 连接 Unsplash 图片库获取图片的 GPT,并可以用 DALL-E 对其进行重绘。

测试 API

首先,我们要获取 API 的访问权限。绝大多数 API 都不是直接就可以调用的,需要我们申请密钥。具体如何申请,每个 App 都有不同的规则。Unsplash 首先需要我们创建一个 App,然后才能获取 API 的密钥。

有了 API 密钥之后,就可以进行测试啦。

Unsplash API 的功能很多,因为我们需要通过 API 在 Unsplash 里搜索图片,我看了下文档,只需要用到 "Search Photos (搜索图片)" 这个功能。

这个文档可以帮助我们了解如何使用 Unsplash 的 API 搜索图片。如果读不懂也没关系,稍后可以喂给 ChatGPT,它是能看懂的。

在创建 GPTs 之前,最好先测试下 API 是否能获得成功的响应。目前 Postman 是最好的工具,如果没有的话可以安装一下。

如果不知道如何用 Postman 进行测试的话,也可以问 ChatGPT 。我是这么写提示词的:

Prompt: How can I use Postman to test an API based on this documentation:

[Relevant API Document]

提示词: 如何使用 Postman 测试基于此文档的 API? [相关 API 文档]

中括号里的内容就是喂给 ChatGPT 的 API 文档。

提交給 ChatGPT 之后,它就生成了一份非常详细的操作步骤,我直接跟着做就好。

主要其实就两步。1. 把 API Key 填入 Postman。

  1. 构造请求。

接着点击 "Send" 按钮就可以发送请求。发送完请求,就收到了响应。并且状态码显示 "200",表示成功响应。

写 Schema

Schema 就是告诉 GPTs,如何跟 Unsplash 的 API 相互沟通。这个 Schema 也可以让 ChatGPT 辅助完成。用 Postman 测试的另一个目的就是,可以把 Postman 生成的 CURL 请求和响应提供给 ChatGPT 做参考。ChatGPT 有了这些信息,生成的 Schema 的准确度更高。

上图就是响应的主体。不过 Unsplash 的响应有点长,直接扔给 ChatGPT 可能会超过 Token 限制。我只选取了其中最相关的部分提交,这样 ChatGPT 写出来的 Schema 也会更简洁一些。

CURL 请求在 Postman 的右上角,如下图所示。

有了这些信息,就可以让 ChatGPT 帮我写 Schema 啦。然而如何为 GPTs 写 Schema 是很新的知识,ChatGPT 还不太了解。为此我找了个可以辅助造 GPTs 的 GPTs,访问地址是:chat.openai.com/g/g-iThwkWD...。用这个 GPTs 写出的 Schema 准确度更高。我写了这样一个提示词让它帮我生成 Schema:

Prompt: Below is the curl request and response from the Unsplash API. Can you generate the schema for me?

[CURL Request and Response]

提示词: 以下是 Unsplash API 的 curl 请求和响应。你能为我生成 Schema 吗?

[CURL 请求和响应]

创建 GPTs

有了 Schema 之后,创建 GPTs 就简单多啦。可以不管其它配置,先把刚才 GPT 生成的 Schema 填进去。

填进去以后发现报错信息:

Could not find a valid URL in servers

无法在 servers 中找到有效 URL

观察一下 OpenAI 给的参考 Schema 就会发现少了 "Servers" 这个键,自己手动补充一下就好。

json 复制代码
{
  "openapi": "3.1.0",
  "info": {
    "title": "Get weather data",
    "description": "Retrieves current weather data for a location.",
    "version": "v1.0.0"
  },
  "servers": [
    {
      "url": "https://weather.example.com"
    }
  ],
  "paths": {
    "/location": {
      "get": {
        "description": "Get temperature for a specific location",
        "operationId": "GetCurrentWeather",
        "parameters": [
          {
            "name": "location",
            "in": "query",
            "description": "The city and state to retrieve the weather for",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "deprecated": false
      }
    }
  },
  "components": {
    "schemas": {}
  }
}

修改之后,就没有报错信息啦。

完整的 Schema 是这样的:

css 复制代码
{
    "openapi": "3.1.0",
    "info": {
      "title": "Unsplash API",
      "version": "1.0.0",
      "description": "Schema for the Unsplash API response for searching photos."
    },
    "servers": [
        {
          "url": "https://api.unsplash.com"
        }
      ],
    "paths": {
      "/search/photos": {
        "get": {
          "summary": "Search photos",
          "operationId": "searchPhotos",
          "parameters": [
            {
              "name": "query",
              "in": "query",
              "required": true,
              "description": "The search query term.",
              "schema": {
                "type": "string"
              }
            }
          ],
          "responses": {
            "200": {
              "description": "Successful response",
              "content": {
                "application/json": {
                  "schema": {
                    "type": "object",
                    "properties": {
                      "total": {
                        "type": "integer",
                        "description": "Total number of results."
                      },
                      "total_pages": {
                        "type": "integer",
                        "description": "Total number of pages."
                      },
                      "results": {
                        "type": "array",
                        "items": {
                          "type": "object",
                          "properties": {
                            "width": {
                              "type": "integer",
                              "description": "Width of the image."
                            },
                            "height": {
                              "type": "integer",
                              "description": "Height of the image."
                            },
                            "color": {
                              "type": "string",
                              "description": "Dominant color of the image."
                            },
                            "alt_description": {
                              "type": "string",
                              "description": "Alternative description of the image."
                            },
                            "urls": {
                              "type": "object",
                              "properties": {
                                "raw": {
                                  "type": "string",
                                  "format": "uri",
                                  "description": "URL of the raw image."
                                },
                                "full": {
                                  "type": "string",
                                  "format": "uri",
                                  "description": "URL of the full-size image."
                                },
                                "regular": {
                                  "type": "string",
                                  "format": "uri",
                                  "description": "URL of the regular-size image."
                                },
                                "small": {
                                  "type": "string",
                                  "format": "uri",
                                  "description": "URL of the small-size image."
                                },
                                "thumb": {
                                  "type": "string",
                                  "format": "uri",
                                  "description": "URL of the thumbnail image."
                                },
                                "small_s3": {
                                  "type": "string",
                                  "format": "uri",
                                  "description": "URL of the small-size image hosted on S3."
                                }
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }

接着就是在 GPTs 里面填好 API Key。取决于 API Key 的类型,一般来说都是下图所示的填法。

然后可以完善 GPTs 的配置部分。Instructions 里面可以先说明如何利用 API,其它内容可以在测试好 GPTs 的 Actions 之后完善。我写的 Instructions 内容如下:

When users initiate an image search on our platform, the system should employ the 'searchPhotos' action to fetch relevant results. The task involves several key steps:

  1. Implement the 'searchPhotos' action to retrieve image search results based on user queries.

  2. For each image result, extract and display the following details:

    • The URL of the image, using 'urls.regular' as the source.
    • A brief description of the image.
    • The dimensions of the image, specifically its width and height.
    • The name of the image's author, sourced from 'user.name'.
  3. Configure the display settings to show a default of three random images per search result.

当用户在我们的平台上发起图片搜索时,系统应使用 "searchPhotos "操作来获取相关结果。这项任务涉及几个关键步骤:

  1. 执行 "searchPhotos "操作,根据用户查询检索图片搜索结果。

  2. 对于每个图像结果,提取并显示以下详细信息:

    • 图片的 URL,使用 "urls.regular "作为来源。
    • 图片的简要描述。
    • 图片的尺寸,尤其是宽度和高度。
    • 图片作者的姓名,来源于 "user.name"。
  3. 配置显示设置,使每个搜索结果默认显示三张随机图片。

接着就可以进行测试啦。如下图所示,测试成功,大功告成。

总结

创建 AI 应用程序放到去年,我连做梦都想不出来。如今,我也可以徒手造一个出来啦。创造的过程本身就是一种幸福。希望本文对你有所启发,也能收获同样的幸福体验。

另外,本公众号搭配一个 AI 学习交流群,欢迎加入!还有,如果文章有帮助的话,不妨点个赞哦。

欢迎访问我的免费学习AI网站:

myaiforce.com.cn/

原文地址:

myaiforce.com.cn/custom-gpts...

相关推荐
龙的爹233310 小时前
论文翻译 | LLaMA-Adapter :具有零初始化注意的语言模型的有效微调
人工智能·gpt·语言模型·自然语言处理·nlp·prompt·llama
罗曼蒂克在消亡12 小时前
github项目——gpt-pilot自动创建应用
gpt·github·github项目
学习前端的小z14 小时前
【AIGC】ChatGPT提示词解析:如何打造个人IP、CSDN爆款技术文案与高效教案设计
人工智能·chatgpt·aigc
wgggfiy1 天前
chatgpt学术科研prompt模板有哪些?chatgpt的学术prompt有哪些?学术gpt,学术科研
论文阅读·人工智能·gpt·chatgpt·prompt·aigc
杭州刘同学1 天前
chatgpt用于数据分析的弊端
chatgpt
程序员陆通1 天前
如何使用ChatGPT API及Bito插件
开发语言·chatgpt·lua
三桥君1 天前
我为什么决定关闭ChatGPT的记忆功能?
人工智能·ai·自然语言处理·chatgpt·prompt·openai·ai产品经理
Ephemeroptera1 天前
通过python-api使用openai的gpt
人工智能·python·gpt
DC10201 天前
GPT 的工作原理:深入解析
java·开发语言·gpt
Dlimeng1 天前
2024年OpenAI DevDay发布实时 API、提示缓存等新功能
人工智能·深度学习·ai·chatgpt·openai·sam altman