文件搜索是一个可用的工具,位于 Responses API。它使模型能够通过语义和关键词搜索,在之前上传的文件构成的知识库中检索信息。通过创建向量存储并上传文件,你可以让模型访问这些知识库或 vector_stores.
要了解更多关于向量存储和语义搜索的工作原理,请参阅我们的 检索指南.
这是一个由 OpenAI 托管的工具,这意味着您无需在本地实现代码即可处理其执行。当模型决定使用该工具时,它会自动调用该工具,从您的文件中检索信息,并返回输出。
如何使用
在将文件搜索与 Responses API 结合使用之前,您需要在向量存储中建立知识库并上传相关文件。
创建向量存储并上传文件
知识库设置完成后,您可以将 file_search 工具包含在模型可用的工具列表中,并同时提供需要进行搜索的向量存储列表。
文件搜索工具
python
1
2
3
4
5
6
7
8
9
10
11
12
from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model="gpt-5.5",
input="What is deep research by OpenAI?",
tools=[{
"type": "file_search",
"vector_store_ids": ["<vector_store_id>"]
}]
)
print(response)当模型调用此工具时,您将收到一个包含多个输出的响应:
- A
file_search_calloutput item,其中包含文件搜索调用的 ID。 - A
messageoutput item,其中包含模型的响应以及文件引用。
文件搜索响应
json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
{
"output": [
{
"type": "file_search_call",
"id": "fs_67c09ccea8c48191ade9367e3ba71515",
"status": "completed",
"queries": ["What is deep research?"],
"search_results": null
},
{
"id": "msg_67c09cd3091c819185af2be5d13d87de",
"type": "message",
"role": "assistant",
"content": [
{
"type": "output_text",
"text": "Deep research is a sophisticated capability that allows for extensive inquiry and synthesis of information across various domains. It is designed to conduct multi-step research tasks, gather data from multiple online sources, and provide comprehensive reports similar to what a research analyst would produce. This functionality is particularly useful in fields requiring detailed and accurate information...",
"annotations": [
{
"type": "file_citation",
"index": 992,
"file_id": "file-2dtbBZdjtDKS8eqWxqbgDi",
"filename": "deep_research_blog.pdf"
},
{
"type": "file_citation",
"index": 992,
"file_id": "file-2dtbBZdjtDKS8eqWxqbgDi",
"filename": "deep_research_blog.pdf"
},
{
"type": "file_citation",
"index": 1176,
"file_id": "file-2dtbBZdjtDKS8eqWxqbgDi",
"filename": "deep_research_blog.pdf"
},
{
"type": "file_citation",
"index": 1176,
"file_id": "file-2dtbBZdjtDKS8eqWxqbgDi",
"filename": "deep_research_blog.pdf"
}
]
}
]
}
]
}检索自定义
限制结果数量
将文件搜索工具与 Responses API 结合使用时,您可以自定义要从向量存储中检索的结果数量。这有助于减少 token 用量和延迟,但可能会降低回答质量。
限制结果数量
python
1
2
3
4
5
6
7
8
9
10
response = client.responses.create(
model="gpt-4.1",
input="What is deep research by OpenAI?",
tools=[{
"type": "file_search",
"vector_store_ids": ["<vector_store_id>"],
"max_num_results": 2
}]
)
print(response)在响应中包含搜索结果
虽然您可以在输出文本中看到注解(对文件的引用),但文件搜索调用默认不会返回搜索结果。
要在响应中包含搜索结果,您可以在创建响应时使用 include 参数。
包含搜索结果
python
1
2
3
4
5
6
7
8
9
10
response = client.responses.create(
model="gpt-4.1",
input="What is deep research by OpenAI?",
tools=[{
"type": "file_search",
"vector_store_ids": ["<vector_store_id>"]
}],
include=["file_search_call.results"]
)
print(response)元数据过滤
您可以根据文件的元数据过滤搜索结果。有关更多详细信息,请参阅我们的 检索指南, 内容包括:
- 操作指南 在向量存储文件上设置属性
- 操作指南 定义过滤器
元数据过滤
python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
response = client.responses.create(
model="gpt-4.1",
input="What is deep research by OpenAI?",
tools=[{
"type": "file_search",
"vector_store_ids": ["<vector_store_id>"],
"filters": {
"type": "in",
"key": "category",
"value": ["blog", "announcement"]
}
}]
)
print(response)支持的文件
For text/ MIME 类型,编码必须是以下之一 utf-8, utf-16, or ascii.
| 文件格式 | MIME 类型 |
|---|---|
.c | text/x-c |
.cpp | text/x-c++ |
.cs | text/x-csharp |
.css | text/css |
.doc | application/msword |
.docx | application/vnd.openxmlformats-officedocument.wordprocessingml.document |
.go | text/x-golang |
.html | text/html |
.java | text/x-java |
.js | text/javascript |
.json | application/json |
.md | text/markdown |
.pdf | application/pdf |
.php | text/x-php |
.pptx | application/vnd.openxmlformats-officedocument.presentationml.presentation |
.py | text/x-python |
.py | text/x-script.python |
.rb | text/x-ruby |
.sh | application/x-sh |
.tex | text/x-tex |
.ts | application/typescript |
.txt | text/plain |
使用说明
| API 可用性 | 速率限制 | 备注 |
|---|---|---|
层级 1 第 2 层和第 3 层 第4层和第5层 |