谷歌宣佈在Gemini API中推出File Search Tool(文件搜索系統)。這是一個完全託管的檢索增強生成(RAG)解決方案,旨在為開發者提供一個簡單、集成且可擴展的方式,使用自己的數據來“錨定”Gemini模型。
通過該工具,開發者可以上傳文件,系統會自動進行分塊、索引和檢索,從而讓Gemini模型能夠基於用户提供的私有文件內容生成更準確、更具上下文的回覆。
使用示例
from google import genai
from google.genai import types
client = genai.Client()
store = client.file_search_stores.create()
upload_op = client.file_search_stores.upload_to_file_search_store(
file_search_store_name=store.name,
file='path/to/your/document.pdf'
)
while not upload_op.done:
time.sleep(5)
upload_ops = client.operations.get(upload_op)
# Use the file search store as a tool in your generation call
response = client.models.generate_content(
model='gemini-2.5-flash',
contents='What does the research say about ...',
config=types.GenerateContentConfig(
tools=[types.Tool(
file_search=types.FileSearch(
file_search_store_names=[store.name]
)
)]
)
)
print(response.text)
# Support your response with links to the grounding sources.
grounding = response.candidates[0].grounding_metadata
sources = {c.retrieved_context.title for c in grounding.grounding_chunks}
print('Sources:', *sources)
官方文章指出,File Search 自動管理文件存儲、分塊和嵌入生成,按百萬索引詞元 $0.15 收費,僅在查詢時生成嵌入,支持 PDF、DOCX、TXT 和 JSON 等格式。