English
主导航

旧版 API

文件搜索

允许模型在生成响应之前搜索您的文件以获取相关信息。

文件搜索是一个可用的工具,位于 Responses API。它使模型能够通过语义和关键词搜索,在之前上传的文件构成的知识库中检索信息。通过创建向量存储并上传文件,你可以让模型访问这些知识库或 vector_stores.

要了解更多关于向量存储和语义搜索的工作原理,请参阅我们的 检索指南.

这是一个由 OpenAI 托管的工具,这意味着您无需在本地实现代码即可处理其执行。当模型决定使用该工具时,它会自动调用该工具,从您的文件中检索信息,并返回输出。

如何使用

在将文件搜索与 Responses API 结合使用之前,您需要在向量存储中建立知识库并上传相关文件。

知识库设置完成后,您可以将 file_search 工具包含在模型可用的工具列表中,并同时提供需要进行搜索的向量存储列表。

文件搜索工具
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)

当模型调用此工具时,您将收到一个包含多个输出的响应:

  1. A file_search_call output item,其中包含文件搜索调用的 ID。
  2. A message output item,其中包含模型的响应以及文件引用。
文件搜索响应
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 用量和延迟,但可能会降低回答质量。

限制结果数量
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 参数。

包含搜索结果
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)

元数据过滤

您可以根据文件的元数据过滤搜索结果。有关更多详细信息,请参阅我们的 检索指南, 内容包括:

元数据过滤
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 类型
.ctext/x-c
.cpptext/x-c++
.cstext/x-csharp
.csstext/css
.docapplication/msword
.docxapplication/vnd.openxmlformats-officedocument.wordprocessingml.document
.gotext/x-golang
.htmltext/html
.javatext/x-java
.jstext/javascript
.jsonapplication/json
.mdtext/markdown
.pdfapplication/pdf
.phptext/x-php
.pptxapplication/vnd.openxmlformats-officedocument.presentationml.presentation
.pytext/x-python
.pytext/x-script.python
.rbtext/x-ruby
.shapplication/x-sh
.textext/x-tex
.tsapplication/typescript
.txttext/plain

使用说明

API 可用性速率限制备注

层级 1
100 RPM

第 2 层和第 3 层
500 RPM

第4层和第5层
1000 RPM

定价
ZDR 和数据驻留