谷歌推出 File Search Tool 文件搜索系统 ,集成至 Gemini API
谷歌宣布在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 等格式。
