跳至主要内容

Gemini API 直升 RAG:內建「檔案搜尋工具」全攻略

· 閱讀時間約 7 分鐘
w0x7ce
MySelf

发布于 2025-11-07 21:32:45(微信公众号导出记录)。

本文来自公众号后台的“导出文章内容”功能。博客正文由导出长图进行本地 OCR 转写,并保留原始排版图用于逐段核对。

原文链接:查看原文

OCR 转写有效文字约 4535 字;代码、流程图和版式以文末原始排版图为准。

正文(本地 OCR 转写)

original wex7ce EI V1ajero 2825年11月7日 21:32 中国垂港 搞RAG(機索增强生成)很麻。 NOSr File Search in Sf Gemini API PDF Python

Gemin1 API 现在直接内建楼案搜辱工具:(F1le Search Tool),把整食索流程全包 了。者不用再烦棉鳍存、切现(chunking)、生成嵌入[embeddings)或助注入 上下文。要心做盈品就好。 遗是一個全托管(fully managed)的RAG系统,直接整合在API狸。目標是 Gemin1 回癌的内容更弹础、更相廊,亚且可验證。

File Searsh in Geminr-API

核心侵势:RAG流程全自動化 適图工具商化了整低開發工作流程。

  • 整合验:樓需存、最佳切魂策略、嵌入生成,到最後把核索到的上下文勤慧注人提示

(prompts),全部自動處理。它就在现有的 gcncratccontcnt API 内谨作,激入非常

  • 强力向量搜导:背後是Google最新的GeminiEmbedding模型。它使用向量提辱来

理解用户查韵的“語意和“上下文」,而不堡堡是比刿關键字。

  • 内建引用功能:模型的回答會自動包含引用(c1tat1ons)。清楚標示哪些答案是你上

傳的概些文件中提取的,化了事實验调的步骤。

  • 支援多種格式:可以用来建立知游虚的檔案格式很多元,包括PDF、DOCX、TXT、JSON,

以及多常見的程式括言增案(例如.py,.js..cpp等)。 谨作原理:語意搜导 File Search使用的技術稀為語意搜辱(semanticsearch)。 與傳统的關键字搜寻不同,語意搜导能理解查询背後的意差!。 通作流程大致如下:

1.索引(Indexing):當增案被匯人時,系统會自将其轉换爲為「嵌入」

(embeddings)的数值表示,這些嵌入捕捉了文字的語意。接著,嵌入被存在一但專門 的檔案搜导資料(FileSearchStore)中。

2.查询(Querying):常用户提出問题時,这個同题本身也舍被聘换成一低嵌入。

3.比(Retrieval):系统他在这调资料重中執行报辱,找出买“同题嵌人;最相似、最

相默的「文件區瑰依入」。

1.生成(Generation):这些相的文件显块雷被作上下文(context),速同原始司题

一起提供给Gemini模型,模型窗基龄些「根擦:来生成显终答案。

如何使用:建立到查詢

1.管理 FileSearchStore

Filescarchstore是储存文件嵌入的容馨。 重點:透遇F1le API上傳的原始窄在 48小時後被目除,但疆入到Filesearchstor 中的资料(嵌入)是永久存的,直到手勤刑除。 可以建立多個stores 来组截文件。 fron google inport genai 1nport time client = genai.client() #1. 建立一留 store (可遇 display mame) filc_scarch_storc = client.file_search_stores.crcate[ config={'display_name': 'my-docunent-store }

122.列出所有的 stores

for store in client.file search stores.list(): print(store) #3.取得特定的store ny_store = client.file_search_stores get(nane=filc_search_store.nane) client,file_scarch_stores,delete(nane=file_search_store,nane, config={

2.上傳匯入檔案

有雨種主要方式: 方法一:直接上傳匯入(量推)使用upLcad_to_file_search_storeAPI-次完成。 得超 store 已级建立 store_name = file_search_store.nane 上傅益重人案,提供一個dispLay_nane 鲁在引用中额示 operation = client. flle_search_stores.upload_to_f1le_search_store( file='path/to/your/document-,pdf*, f1le_search_store_name=store_nane, config={ display_nane':‘my-unique-doc-name-pdf",#这需名都會题示在引用中

等待操作完应 while not operation.done: print("指案虚理中...") time sleep(5) operation = cllent operations.get(operation) rat1on) print["橙累素引完成。") 方法二:先上傳權案,再手助匯入如果你需要先建立F1Le物件,可以分开操作。 #1.先用 Files API上傅榴案 sanple_file = client,files,upload( flle='path/to/sanple.txt', config={'nane':'unique file name for citation'} 网标, 适 name 鲁 #2. 理立 store file_search_store = client.file_search_stores.create[ {auois-Jauno-Au, :aweuAedstp.-buos

3. 将已上何的 f1le 人 store

operation = client file_search_stores.inport_file[ file_search_store_namc=file_scarch_store.nane, flle_nare=sanple_file.name 用上—S的 f1lc mame 等特操作完证 while not operation.done: time,sleep(5) operation = client.operations get(operation)

3.查詢模型

在generateContent 呼叫中,将FileSearch常作 tool傅速。 針数期才上停的文件提罚 response = client.nodels .generate_content( nodel="genini-2.5flash*, # 或 gomini-2.5-pro contents=***Can you tell ne about Robert 6raves"" config=types GenerateContentConfigf tools=[ types.Tool( file_search=types.File5earch( filc_scarch_store_nanes=[filc_search_store.nane]

print (response.text) 取得引用来源 grounding = response cand1dates[0] grounding_netadata sources = {c.retrieved_context.title for c in grounding·grounding_chun print[*引用来源:',*sources) 進控制

1.自訂切魂(Chunking)

预設會自勤切境,但可以自訂策略。例如,設定每個區增的最大token数和重叠token 数。 operation = client.file_search_stores.upload_to_file_search_store(

  • 4x*!/na/o1/4ed,==1!

config={ ',1x1'oop-paxunus, :,aueu Aendstp, 'chunking_config′: { ):.btfuos aseds"orum, nax_tokens_per_chunk:2ee,每钢区博最多 200 tokens 'nax_overlap_takens': 28 区埃間重叠28tokens

2.中資料(Metadata)與遇

可以在匿入樓案時加入自訂的key-value 中继資料。 厘人路加人metadata op = cllent.file_search_stores.inport_file( file_search_store_name=file_search_store.nane, file_name=sanple_file,name, custom_metadata=[ {"key": author", "string_value": "Robert Graves"}. {"key": year", “numeric_value": 1934) ( 在查詢時,就可以使用netadata_filter来只报导符合特定修件的文件。 查腐时使用metadata_filter response = client.nodels.generate_content ( nodel-"genini-z.5-flash, contents="*Tell me about the book 'I, Claudius'""", config=types GenerateContentConfig( J=51001 types. Too1( file_search=types.FileSearch( filc_search_store_namcs=[filc_search_store.nane], TJeaA any saneug luagou,-louine, = Janit eiepeiau

print (response.text)

計囊、限制奥支援 創新的計囊模式 计意方式是這次的亮點,目是接間者能以低成本展。

  • 付囊项目:只有在首次需引增案;蓝建立嵌入时才收费。

  • 费率:$0.15/ 186萬tokens(以gemini-cmbcdding-301為例)。

  • 免费项目:

  • 案存(Storage)

  • 查均時勤慧生成嵌入(Query tineembeddings)

  • 常规計费:

  • 检索到的文件 tokens 童被视為一般的“上下文 tokens:(context tokens)针

费。 服務限制

  • 單檔大小上限:100MB

  • 每個專案的Store数量:10

  • 專案總存大小(依層級):

  • Free: 1 GB

  • T1er 1: 10 GB

  • Tier 2:100 GB

  • Tier 3:1 TB

1FileSearchStore限制在20GB以下,以確保最佳检索延。

  • 官方建:每個

支援模型與格式

  • 支援模型:

gemini-2.5-pro

  • 支援檔案類型(部分):

  • Application:PDF,DoCX,XLSX,PPTX

  • TeXt:TXT,JSON,MD,HTML,CSV,PY,JS,JAVA,CPP,C,TS,PHP,

RB,GO,SWIFT,KOTLIN...等多種程式碼與純文字格式。

原始排版图

Gemini API 直升 RAG:內建「檔案搜尋工具」全攻略:微信公众号导出原始排版图