5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Gemini APIのFile Search Toolを使ってみた

Last updated at Posted at 2025-11-15

NotebookLMは便利に使っていて「Googleすげぇなぁ...」とその凄さを感じています。今回、なんとそれを手元で使えるFile Search ToolsをGemini APIで使えるようになったとの事で試してみました。

結果としては、手元で頑張って作ったオレオレRAGより性能が良いのでなんだか困っています...(NotebookLMで試した結果でわかってはいましたが)

ということで、サンプルを見ながら試しましたが、サンプルがちょっと雑なので理解のメモとして残しておきます。

構造

  • file_search_stores.createでファイルサーチストア(入れ物を作る)
  • そこにfile_search_stores.upload_to_file_search_storeでファイルを投入していく
  • 検索時にはtoolsの引数としてfile_searchを渡して検索する

この流れなのですが、file_search_store.createは毎回新しいものが作られていて、サンプルを実行する度に生成されます。そしてこのストアはダッシュボードなどから参照する方法が見つかりませんでした。そのあたりで、戸惑ったのでステップで解説します。

ステップ

基本

Clientを設定します。

from google import genai
api_key = "XXX" # Gemini Studioより
client = genai.Client(api_key=api_key)

作成

file_search_store = client.file_search_stores.create(config={'display_name': 'My Test Store'})

以下のようなStore情報が返ってきます。nameはDisplayNameから生成されたユニークなIDのようです。

FileSearchStore(
  create_time=datetime.datetime(2025, 11, 15, 0, 43, 53, 326951, tzinfo=TzInfo(0)),
  display_name='My Test Store',
  name='fileSearchStores/my-test-store-3kegcxycxxxxx',
  update_time=datetime.datetime(2025, 11, 15, 0, 43, 53, 326951, tzinfo=TzInfo(0))
)

このnameが重要で呼び出しIDになります。これをファイルアップロードの際にはこれを渡す必要があります(これしか必要ありません)

一覧

それではすでに自分のStoreに何があるのか、参照してみましょう。

for file_search_store in client.file_search_stores.list():
    print(file_search_store)

サンプルを実行した方なら同じdisplay_nameのストアがゾロゾロ出てくるかと思います(笑。nameは個別になっています。

ドキュメント投入

ここはサンプルコードのままですが、file_search_store.nameでしか判別していません。上記で取得したnameを入れれば、既存のストアへの追加登録なども可能です。

operation = client.file_search_stores.upload_to_file_search_store(
  file='sample.txt',
  file_search_store_name=file_search_store.name,
  config={
      'display_name' : 'display-file-name',
  }
)

# Wait until import is complete
while not operation.done:
    time.sleep(5)
    operation = client.operations.get(operation)
  • 追加時に完了ループが取得できなくてぐるぐるになる時があります。インデックス完了とは別のはずなのですが?

検索

ここでも、file_search_store.nameを渡すだけになります。結局、このnameが重要なのでした。

注意点として「gemini-2.5-flash」を無料枠で使うと時々エラーになります。「gemini-2.5-flash-lite」でも動きましたので無料枠の方はそちらの方が検証には良いかと思います。(もちろん出力精度は異なります)

response = client.models.generate_content(
    model="gemini-2.5-flash-lite",
    contents="""Can you tell me about Robert Graves""",
    config=types.GenerateContentConfig(
        tools=[
            file_search=(
                  file_search_store_names=[file_search_store.name]
            )
        ]
    )
)

まとめ

  • NotebookLM並みの性能のRAGを簡単に構築できる
  • GeminiAPIから使えて安い
  • PDF、Work、Excel、Markdownなど何でもいける

ということで、RAGっぽいものを作りたいと思っていた人には注目のAPIかと思います。

5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?