提问人:Philip7899 提问时间:11/17/2023 更新时间:11/18/2023 访问量:131
自定义 GPT 的 OpenAI 操作:如何修改 OpenAPI 架构以发送文件和字符串
OpenAI actions for custom GPT: How to modify OpenAPI schema to send a file along with string
问:
我正在制作一个连接到我自己的服务器的自定义 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": {}
}
}
答:
你已经非常接近了,但你错过了对象的一些东西,这是可选的,但对有效载荷的描述性要强得多。另一件事是字符串应该在 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-disposition
name
boundary
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
评论
In components section, schemas subsection is not an object
schemas: {}
简而言之,在撰写本文时,实际上不可能从 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
评论