谷歌近日宣布,其gemini api正式推出file search tool(文件搜索系统)。该工具是一项完全托管的检索增强生成(rag)服务,旨在为开发者提供一种简单、集成且可扩展的方法,利用自有数据对gemini模型进行“锚定”,从而提升生成内容的相关性与准确性。

借助这一功能,开发者可以上传私有文档,系统将自动完成文本分块、索引构建以及检索处理。这使得Gemini模型在响应查询时,能够基于用户提供的文件内容生成更具上下文感知能力的回答。
使用示例代码如下:
from google import genai from google.genai import typesclient = 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)
在生成请求中将文件搜索存储作为工具使用
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)
使用来源链接支持回答内容
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等。
源码地址:点击下载











