GPTs的创建与使用,自定义GPTs中的Actions示例用法 定义和执行特定任务的功能模块 通过API与外部系统或服务的交互

Name 等

Logo:自动生成
Name 介绍
Description 介绍
Instructions 要求或命令等 比如用中文回复,角色。
Knowledge 上传你的知识库,如果你有某一垂直行业的数据,基于数据来回答。比如我有某个芯片的指令集。
Capabilities 都要
Actions:就这个难以理解一点,下面详说含义用法。
Addtional Settings 目前只有是否允许使用对话数据提高模型。
在这里插入图片描述
在这里插入图片描述

Actions 示例1:json格式

在这里插入图片描述

在ChatGPT中,Actions 是用于定义和执行特定任务的功能模块。这些任务通常涉及到与外部系统或服务的交互。你提供的示例是一个开放API(应用程序编程接口)的规格说明,用于获取特定地点的当前天气数据。让我来解释一下这个示例中各部分的含义:

  1. openapi: 指定OpenAPI规格的版本,这里是3.1.0版本。

  2. info: 提供API的基本信息。

    • title: API的名称,这里是“Get weather data”。
    • description: API的简要描述,说明它用于检索特定位置的当前天气数据。
    • version: API的版本,这里是v1.0.0。
  3. servers: 定义API的服务器列表。

    • 这里只有一个服务器URL:“https://weather.example.com”。
  4. paths: 描述API的各种路径及其操作。

    • /location: 这是一个路径,用于获取特定位置的天气数据。
      • get: 表示这是一个GET请求。
        • description: 该操作的描述,这里是“获取特定位置的温度”。
        • operationId: 操作的唯一标识符,这里是“GetCurrentWeather”。
        • parameters: 定义传递给API的参数。
          • name: 参数的名称,这里是“location”。
          • in: 参数的位置,这里是在查询字符串中。
          • description: 参数的描述,这里是“要检索天气的城市和州”。
          • required: 是否必需,这里是true。
          • schema: 参数的数据类型,这里是字符串。
  5. components: 定义API使用的通用组件,这里的“schemas”是空的,表示没有定义额外的数据结构。

在ChatGPT的上下文中,若要使用这样的API,你需要根据这个规格描述构建相应的请求。例如,如果要获取某个城市的天气数据,你会向"https://weather.example.com/location"发送一个带有位置参数的GET请求。然而,由于ChatGPT当前的运行环境不支持直接与外部API交互,这类操作通常无法直接执行。

//用中文解释一下chatGPT中Actions的用法 
{
  "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": {}
  }
}

Actions 示例2:YAML格式

# Taken from https://github.com/OAI/OpenAPI-Specification/blob/main/examples/v3.0/petstore.yaml

openapi: "3.0.0"
info:
  version: 1.0.0
  title: Swagger Petstore
  license:
    name: MIT
servers:
  - url: https://petstore.swagger.io/v1
paths:
  /pets:
    get:
      summary: List all pets
      operationId: listPets
      tags:
        - pets
      parameters:
        - name: limit
          in: query
          description: How many items to return at one time (max 100)
          required: false
          schema:
            type: integer
            maximum: 100
            format: int32
      responses:
        '200':
          description: A paged array of pets
          headers:
            x-next:
              description: A link to the next page of responses
              schema:
                type: string
          content:
            application/json:    
              schema:
                $ref: "#/components/schemas/Pets"
        default:
          description: unexpected error
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
    post:
      summary: Create a pet
      operationId: createPets
      tags:
        - pets
      responses:
        '201':
          description: Null response
        default:
          description: unexpected error
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
  /pets/{petId}:
    get:
      summary: Info for a specific pet
      operationId: showPetById
      tags:
        - pets
      parameters:
        - name: petId
          in: path
          required: true
          description: The id of the pet to retrieve
          schema:
            type: string
      responses:
        '200':
          description: Expected response to a valid request
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Pet"
        default:
          description: unexpected error
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
components:
  schemas:
    Pet:
      type: object
      required:
        - id
        - name
      properties:
        id:
          type: integer
          format: int64
        name:
          type: string
        tag:
          type: string
    Pets:
      type: array
      maxItems: 100
      items:
        $ref: "#/components/schemas/Pet"
    Error:
      type: object
      required:
        - code
        - message
      properties:
        code:
          type: integer
          format: int32
        message:
          type: string

这个YAML文件是一个OpenAPI 3.0规范的示例,用于定义一个名为“Swagger Petstore”的API。这个API提供了操作宠物商店数据的接口。以下是文件的主要部分及其功能:

  1. openapi: 指定OpenAPI规格的版本,这里是3.0.0版本。

  2. info: 提供API的基本信息。

    • version: API的版本,这里是1.0.0。
    • title: API的名称,这里是“Swagger Petstore”。
    • license: 指定API的许可证,这里使用的是MIT许可证。
  3. servers: 定义API的服务器列表。

    • 这里的URL是"https://petstore.swagger.io/v1",代表API的主机地址。
  4. paths: 描述API的各种路径及其操作。

    • /pets: 这是一个路径,用于处理与宠物相关的请求。
      • get: 获取宠物列表的操作。
        • summary: 操作的简要描述,这里是“列出所有宠物”。
        • parameters: 请求参数,这里是一个名为“limit”的查询参数,用于指定返回的项目数(最多100个)。
        • responses: 定义了可能的响应。
      • post: 创建新宠物的操作。
    • /pets/{petId}: 获取特定宠物的详细信息的路径。
      • get: 操作的描述,这里是“获取特定宠物的信息”。
  5. components: 定义API使用的通用组件。

    • schemas: 定义了多个模式,包括“Pet”、“Pets”和“Error”,用于描述数据结构。

在这个API中,例如,如果想要获取宠物列表,可以向"/pets"路径发送一个GET请求,可能会带有“limit”参数来限制返回的数量。如果想要获取特定宠物的详细信息,可以向"/pets/{petId}“发送一个GET请求,其中”{petId}"是宠物的唯一标识符。

由于ChatGPT的运行环境不支持直接与外部API交互,这些操作无法直接在当前环境中执行。但是,这个文件提供了一个完整的API规范,可以用于生成API文档或在服务器上实现相应的功能。

Actions 示例3:json格式空白模板

{
  "openapi": "3.1.0",
  "info": {
    "title": "Untitled",
    "description": "Your OpenAPI specification",
    "version": "v1.0.0"
  },
  "servers": [
    {
      "url": ""
    }
  ],
  "paths": {},
  "components": {
    "schemas": {}
  }
}

外部系统示例(提供简单的API来响应天气查询)

以下示例展示了如何设计一个外部系统来处理来自ChatGPT的请求,并返回JSON格式的天气数据。这个示例中,假设外部系统是一个基于HTTP的Web服务,它提供了一个简单的API来响应天气查询。

API设计

  • 路径: /getWeather
  • 方法: GET
  • 请求参数:
    • location: 要查询天气的地点(例如城市名称)

请求示例

假设外部系统的基础URL是 https://example-weather-api.com。则一个向该API发送请求的URL可能是:

https://example-weather-api.com/getWeather?location=Beijing

处理请求

当外部系统收到一个请求时,它会解析查询参数中的location,然后根据这个地点获取当前的天气数据。

响应格式

响应将以JSON格式返回,包含地点、温度、天气状况等信息。

响应示例

{
  "location": "Beijing",
  "temperature": "15°C",
  "condition": "Sunny",
  "humidity": "40%",
  "wind": "10 km/h",
  "forecast": [
    { "day": "Monday", "temperature": "16°C", "condition": "Partly Cloudy" },
    { "day": "Tuesday", "temperature": "14°C", "condition": "Rain" },
    { "day": "Wednesday", "temperature": "13°C", "condition": "Thunderstorms" }
  ]
}

实现注意事项

  1. 安全性: 确保处理请求时考虑到安全性,例如通过验证输入,防止注入攻击等。

  2. 性能: 根据预期的负载选择合适的服务器规格和扩展策略。

  3. 数据源: 此API可能依赖于第三方天气服务提供数据,因此需要考虑数据源的可靠性和更新频率。

  4. 错误处理: 应妥善处理可能出现的错误情况,并返回相应的错误信息。

请注意,这个示例是理论上的设计,具体实现会根据技术栈、应用场景和业务需求有所不同。

用php写一个外部系统示例(提供简单的API来响应天气查询)

创建一个简单的PHP脚本来模拟上述描述的外部系统是完全可行的。这个脚本将接收一个查询参数(location),然后返回硬编码的天气数据。在实际应用中,你可能会从数据库或第三方API中获取这些数据,但为了简单起见,我们在这里使用静态数据。

示例PHP脚本

<?php
// 模拟的天气数据
$weatherData = [
    "Beijing" => [
        "temperature" => "15°C",
        "condition" => "Sunny",
        "humidity" => "40%",
        "wind" => "10 km/h",
        "forecast" => [
            ["day" => "Monday", "temperature" => "16°C", "condition" => "Partly Cloudy"],
            ["day" => "Tuesday", "temperature" => "14°C", "condition" => "Rain"],
            ["day" => "Wednesday", "temperature" => "13°C", "condition" => "Thunderstorms"]
        ]
    ],
    // 更多地点的数据可以在此添加
];

header('Content-Type: application/json');

// 获取查询参数
$location = $_GET['location'] ?? 'Unknown';

// 查找对应地点的天气数据
$response = $weatherData[$location] ?? null;

if ($response) {
    // 如果找到了数据,则返回
    echo json_encode(["location" => $location] + $response);
} else {
    // 没有找到数据,则返回错误信息
    echo json_encode(["error" => "Weather data not found for location: " . $location]);
}

使用方法

  1. 将上述代码保存为一个.php文件,例如getWeather.php
  2. 部署这个文件到一个PHP服务器上。你可以使用任何标准的LAMP(Linux, Apache, MySQL, PHP)或WAMP(Windows, Apache, MySQL, PHP)堆栈。
  3. 通过浏览器或任何HTTP客户端访问这个脚本,如:http://yourserver.com/getWeather.php?location=Beijing

注意事项

  • 这个示例非常基础,只用于演示目的。
  • 它不包含任何安全性或错误处理功能,这在生产环境中是必需的。
  • 在实际应用中,你可能需要从数据库或第三方API中动态获取天气数据。
  • 确保你的PHP服务器配置正确,可以处理GET请求并返回JSON数据。
  • 41
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小黄人软件

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值