文件对象
文件对象
文件对象是上传文件后返回的数据结构,包含文件的元数据信息。
对象示例
{
"id": "file-abc123",
"object": "file",
"bytes": 120000,
"created_at": 1677610602,
"filename": "document.pdf",
"purpose": "assistants",
"status": "processed"
}
字段说明
| 字段 | 类型 | 说明 |
|---|---|---|
id |
string | 文件唯一 ID,格式 file-xxx |
object |
string | 固定为 "file" |
bytes |
integer | 文件大小(字节) |
created_at |
integer | 创建时间(Unix 时间戳) |
filename |
string | 原始文件名 |
purpose |
string | 文件用途:assistants / fine-tune / vision / batch |
status |
string | 处理状态:uploaded / processed / error |
使用文件
上传文件后,可在对话中引用:
from openai import OpenAI
client = OpenAI(base_url="https://api000.com/v1", api_key="sk-xxxxxxxxxxxxxxxx")
# 上传文件
with open("report.pdf", "rb") as f:
file = client.files.create(file=f, purpose="assistants")
# 在对话中引用
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "请总结这份报告的主要内容"},
{"type": "file", "file": {"file_id": file.id}}
]
}
]
)