For the complete documentation index, see llms.txt. Markdown versions of documentation pages are available by appending .md to the page URL.
主导航

文件搜索

让模型在生成响应前,从您的文件中搜索相关信息。

文件搜索是 Responses API 提供的一项工具。 它让模型能够通过语义搜索和关键词搜索,从已上传文件构成的知识库中检索信息。 您可以创建向量存储并向其中上传文件,让模型访问这些知识库(即 vector_stores),以补充模型已有的知识。

如需了解向量存储和语义搜索的工作原理,请参阅我们的 检索指南

这是由 OpenAI 管理的托管工具,因此您无需自行编写代码来处理工具的执行。 当模型决定使用该工具时,它会自动调用工具,从您的文件中检索信息并返回输出。

使用方法

在通过 Responses API 使用文件搜索之前,您需要先在向量存储中建立知识库,并向其中上传文件。

知识库设置完成后,您可以将 file_search 工具添加到模型可用的工具列表中,并提供要搜索的向量存储列表。

文件搜索工具
from openai import OpenAI

client = OpenAI()

response = client.responses.create(
    model="gpt-6-astra",
    input="What is deep research by OpenAI?",
    tools=[{"type": "file_search", "vector_store_ids": ["<vector_store_id>"]}],
)
print(response)

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

  1. 一个 file_search_call 输出项,其中包含文件搜索调用的 ID。
  2. 一个 message 输出项,其中包含模型的回复以及文件引用。
文件搜索响应
{
  "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 用量并降低延迟,但可能会降低回答质量。

限制结果数量
response = client.responses.create(
    model="gpt-6-astra",
    input="What is deep research by OpenAI?",
    tools=[
        {
            "type": "file_search",
            "vector_store_ids": ["<vector_store_id>"],
            "max_num_results": 2,
        }
    ],
)
print(response)

在响应中包含搜索结果

虽然您可以在输出文本中看到注释(对文件的引用),但文件搜索调用默认不会返回搜索结果。

要在响应中包含搜索结果,您可以在创建响应时使用 include 参数。

包含搜索结果
response = client.responses.create(
    model="gpt-6-astra",
    input="What is deep research by OpenAI?",
    tools=[
        {
            "type": "file_search",
            "vector_store_ids": ["<vector_store_id>"],
        }
    ],
    include=["file_search_call.results"],
)
print(response)

元数据筛选

您可以根据文件的元数据筛选搜索结果。如需了解详情,请参阅我们的检索指南,其中介绍了:

元数据筛选
response = client.responses.create(
    model="gpt-6-astra",
    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)

支持的文件

对于 text/ MIME 类型,编码必须是 utf-8utf-16ascii 之一。

文件格式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 和数据驻留