自定义 GPT 的 OpenAI 操作:如何修改 OpenAPI 架构以发送文件和字符串

OpenAI actions for custom GPT: How to modify OpenAPI schema to send a file along with string

提问人:Philip7899 提问时间:11/17/2023 更新时间:11/18/2023 访问量:131

问:

我正在制作一个连接到我自己的服务器的自定义 GPT。如果只发送一个字符串,我就能让它工作,但是如果我尝试允许用户通过 chatgpt 接口发送文件(我只需要它来处理图像文件),它不会发送图像,只会发送字符串。如何修改下面的架构以发送图像?

{
  "openapi": "3.1.0",
  "info": {
    "title": "Send an image and a string",
    "description": "Makes it super easy to send an image and a string",
    "version": "v1.0.0"
  },
  "servers": [
    {
      "url": "https://myawesomeserver.loca.lt"
    }
  ],
  "paths": {
    "/api/gpt/create": {
      "post": {
        "description": "Create a string and image",
        "operationId": "CreateImageandString",
        "parameters": [
          {
            "name": "an_awesome_string",
            "in": "query",
            "description": "The value of the string we will create",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "description": "image to be uploaded",
          "required": true,
          "content": {
            "multipart/form-data": {
              "schema": {
                "type": "object",
                "properties": {
                  "image": {
                    "type": "string",
                    "format": "binary"
                  }
                }
              }
            }
          }
        },
        "deprecated": false
      }
    }
  },
  "components": {
    "schemas": {}
  }
}

openapi openai-api chatgpt-plugin

评论


答:

0赞 Jeremy Fiel 11/18/2023 #1

你已经非常接近了,但你错过了对象的一些东西,这是可选的,但对有效载荷的描述性要强得多。另一件事是字符串应该在 json 正文中发送,而不是在查询参数中发送。应为搜索词保留查询参数encoding

{
  "openapi": "3.1.0",
  "info": {
    "title": "Send an image and a string",
    "description": "Makes it super easy to send an image and a string",
    "version": "1.0.0"
  },
  "servers": [
    {
      "url": "https://myawesomeserver.loca.lt"
    }
  ],
  "paths": {
    "/api/gpt/create": {
      "post": {
        "description": "Create a string and image",
        "operationId": "CreateImageandString",
        "parameters": [],
        "requestBody": {
          "description": "image to be uploaded",
          "required": true,
          "content": {
            "multipart/form-data": {
              "schema": {
                "type": "object",
                "properties": {
                  "an_awesome_string": {
                    "type": "string"
                  },
                  "image": {
                    "type": "string",
                    "format": "binary"
                  }
                }
              },
              "encoding": {
                "an_awesome_string": {
                  "headers": {
                    "content-disposition": {
                      "$ref": "#/components/headers/content-disposition"
                    }
                  },
                  "contentType": "application/json"
                },
                "image": {
                  "headers": {
                    "content-disposition": {
                      "$ref": "#/components/headers/content-disposition"
                    }
                  },
                  "contentType": "image/*"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "headers": {
      "content-disposition": {
        "description": "the content-disposition header",
        "schema": {
          "type": "string"
        },
        "required": true
      }
    }
  }
}

然后,您需要确保正文实际上被格式化为表单数据请求,并具有服务所需的正确标头。最主要的是标题和属性是必需的。这些元素通常与数据来源的表单数据元素相关联。确保正确定义,因为这就是定义身体部位的方式。content-dispositionnameboundary

POST https://myawesomeserver.loca.lt/api/gpt/create HTTP/1.1
Content-Type: multipart/form-data; boundary=gc0p4Jq0M2Yt08jU534c0p
  
--gc0p4Jq0M2Yt08jU534c0p
Content-Disposition: form-data; name="image"; filename="image_name.png"
Content-Type: image/png
Content-Length: <number>
 
0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAAPgADAP7/CQAGAAAAAAAAAAAAAAABAAAAJgAAAAAAAAAA
EAAAKAAAAAEAAAD+////AAAAACUAAAD/////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////s
pcEAk8EJBAAA8BK/AAAAAAABEQABAAEACAAADggAAA4AYmpiagf4B/gAAAAAAAAAAAAAAAAAAAAA
AAAJBBYANA4AAGWSAQBlkgEADgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD//w8AAAAA
AAAAAAD//w8AAAAAAAAAAAD//w8AAAAAAAAAAAAAAAAAAAAAALcAAAAAAKwFAAAAAAAArAUAAHwT
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAA=

--gc0p4Jq0M2Yt08jU534c0p
Content-Disposition: form-data; name="an_awesome_string"
Content-Type: application/json
Content-Length: <number>

{
  "an_awesome_string": "test"
}

--gc0p4Jq0M2Yt08jU534c0p--

如果你真的想了解多部分消息,可以在RFC2045中找到更多信息

有关标头的信息可以在 RFC6266 中找到content-disposition

评论

1赞 Philip7899 11/18/2023
感谢您的回复!我尝试实现您的代码,但它给了我一些错误: '在组件部分中,架构子部分不是对象('paths', '/api/gpt/create', 'post', 'requestBody', 'content', 'multipart/form-data', 'encoding', 'contentType'): value 不是有效的字典 ('paths', '/api/gpt/create', 'post', 'requestBody', '$ref'): 字段必填 ' 正如我在标题和问题描述中提到的, 这专门用于 OpenAI 操作的 OpenAPI 架构。无论发生什么,我认为都是特定于 openai 的。
0赞 Jeremy Fiel 11/18/2023
对不起。我有一个错误的括号。我更新了它
0赞 Jeremy Fiel 11/18/2023
不过,我不能说 Open-AI 发生了什么。
1赞 Philip7899 11/18/2023
谢谢,我尝试了你的代码,它告诉我,所以我添加了.但它仍然没有发送图像:(In components section, schemas subsection is not an objectschemas: {}
0赞 Jeremy Fiel 11/18/2023
我甚至没有定义组件模式。所以不知道它为什么要找那个。也许你的代码正在期待它。按照您提到的方式添加它应该满足该要求,但如果您发布控制器代码,也许其他人可以为您提供帮助
0赞 huw 12/3/2023 #2

简而言之,在撰写本文时,实际上不可能从 GPT Action 上传文件。这是因为语言模型 (GPT) 本身会生成 GPT Action 调用的参数,而目前最大的模型输出 (GPT-4-Turbo) 是 4096 个令牌(大约 2 个字符)。这意味着,理论上可以提示模型上传的最大文件必须适合该输出窗口。目前尚不支持通过其他方式将文件直接上传到 GPT 操作。

如果你需要你的模型接受文件,我建议你有一个操作,在你的 Web 服务器上生成一个安全端点,用户可以在其中将文件直接上传到你的服务(比如),要求用户单击该链接并上传他们的文件,然后让另一个操作引用该唯一 ID 来检索和操作文件。请记住,您还不能使用操作将图像传递给 GPT-4 Vision,但您可以使用用户的文件在后端调用 GPT-4 Vision API。这些体验都不会让人感觉天衣无缝,但希望它能为如何进行提供一些灵感。https://your-service/upload?id=uniqueidhere