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. ファイル検索呼び出しの ID を含む file_search_call 出力項目
  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 のファイル検索ツールでは、ベクトルストアから取得する結果の数をカスタマイズできます。これによりトークン使用量とレイテンシを削減できますが、回答の品質が低下する可能性があります。

取得する結果数の制限
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 とデータレジデンシー