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 與資料駐留