文件
文件用于上传文档,可与优化等功能一起使用。
列出文件
GET https://api.openai.com/v1/files
返回属于用户组织的文件列表。
请求示例
curl https://api.openai.com/v1/files \
-H "Authorization: Bearer $OPENAI_API_KEY"
import os
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")
openai.File.list()
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const response = await openai.listFiles();
返回值
{
"data": [
{
"id": "file-ccdDZrC3iZVNiQVeEA6Z66wf",
"object": "file",
"bytes": 175,
"created_at": 1613677385,
"filename": "train.jsonl",
"purpose": "search"
},
{
"id": "file-XjGxS3KTG0uNmNOK362iJua3",
"object": "file",
"bytes": 140,
"created_at": 1613779121,
"filename": "puppy.jsonl",
"purpose": "search"
}
],
"object": "list"
}
上传文件
POST https://api.openai.com/v1/files
上传包含文件的文档,以供在各个端点/功能中使用。目前,一个组织上传的所有文件的大小可以达到1 GB。如果您需要增加存储限制,请与我们联系。
请求体
file 字符串 必需的 要上传的JSON行文件的名称。
如果目的是“微调”,每行都是一个JSON记录,具有“提示”和“完成”字段,表示您的训练示例。
purpose 字符串 必需的 上传文档的预期目的。
使用“微调”进行微调。这可以让我们验证上传文件的格式。
请求示
curl https://api.openai.com/v1/files \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-F purpose="fine-tune" \
-F file="@mydata.jsonl"
import os
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")
openai.File.create(
file=open("mydata.jsonl", "rb"),
purpose='fine-tune'
)
const fs = require("fs");
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const response = await openai.createFile(
fs.createReadStream("mydata.jsonl"),
"fine-tune"
);
返回值
{
"id": "file-XjGxS3KTG0uNmNOK362iJua3",
"object": "file",
"bytes": 140,
"created_at": 1613779121,
"filename": "mydata.jsonl",
"purpose": "fine-tune"
}
删除文件
DELETE https://api.openai.com/v1/files/{file_id}
删除一个文件。
路径参数
file_id 字符串 必填项 用于此请求的文件ID。
请求示例
curl https://api.openai.com/v1/files/file-XjGxS3KTG0uNmNOK362iJua3 \
-X DELETE \
-H "Authorization: Bearer $OPENAI_API_KEY"
import os
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")
openai.File.delete("file-XjGxS3KTG0uNmNOK362iJua3")
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const response = await openai.deleteFile("file-XjGxS3KTG0uNmNOK362iJua3");
返回值
{
"id": "file-XjGxS3KTG0uNmNOK362iJua3",
"object": "file",
"deleted": true
}
检索文件
GET https://api.openai.com/v1/files/{file_id}
返回关于特定文件的信息。
路径参数
file_id 字符串 必填项 用于此请求的文件ID。
请求示例
curl https://api.openai.com/v1/files/file-XjGxS3KTG0uNmNOK362iJua3 \
-H "Authorization: Bearer $OPENAI_API_KEY"
import os
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")
openai.File.retrieve("file-XjGxS3KTG0uNmNOK362iJua3")
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const response = await openai.retrieveFile("file-XjGxS3KTG0uNmNOK362iJua3");
返回值
{
"id": "file-XjGxS3KTG0uNmNOK362iJua3",
"object": "file",
"bytes": 140,
"created_at": 1613779657,
"filename": "mydata.jsonl",
"purpose": "fine-tune"
}
检索文件内容
GET https://api.openai.com/v1/files/{file_id}/content
返回指定文件的内容
路径参数
file_id 字符串 必需的 用于此请求的文件的 ID。
请求示例
curl https://api.openai.com/v1/files/file-XjGxS3KTG0uNmNOK362iJua3/content \
-H "Authorization: Bearer $OPENAI_API_KEY" > file.jsonl
import os
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")
content = openai.File.download("file-XjGxS3KTG0uNmNOK362iJua3")
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const response = await openai.downloadFile("file-XjGxS3KTG0uNmNOK362iJua3");
最后更新于