ค้นหาไฟล์

Gemini API ช่วยให้สร้างการดึงข้อมูลเสริม (Retrieval Augmented Generation หรือ "RAG") ได้ผ่านเครื่องมือค้นหาไฟล์ File Search จะนำเข้า แบ่งเป็นกลุ่ม และจัดทำดัชนีข้อมูลเพื่อช่วยให้ดึงข้อมูลที่เกี่ยวข้องได้อย่างรวดเร็วตามพรอมต์ของผู้ใช้ จากนั้นระบบจะให้ข้อมูลนี้เป็นบริบทแก่โมเดล ซึ่งจะช่วยให้โมเดล ให้คำตอบที่ถูกต้องและเกี่ยวข้องมากขึ้นได้

คุณใช้ uploadToFileSearchStore API เพื่ออัปโหลดไฟล์ที่มีอยู่ไปยังที่เก็บไฟล์ค้นหาได้โดยตรง หรือจะอัปโหลดแยกกันแล้วจึงนำเข้าไฟล์หากต้องการสร้างไฟล์พร้อมกันก็ได้

อัปโหลดไปยังร้านค้าค้นหาไฟล์โดยตรง

ตัวอย่างนี้แสดงวิธีอัปโหลดไฟล์ไปยังที่เก็บไฟล์โดยตรง

Python

from google import genai
from google.genai import types
import time

client = genai.Client()

# Create the file search store with an optional display name that shows in the grounding metadata
file_search_store = client.file_search_stores.create(config={'display_name': 'your-fileSearchStore-name'})

# Upload and import a file into the file search store, supply a unique file name which will be visible in citations
operation = client.file_search_stores.upload_to_file_search_store(
  file='path/to/your/file.txt',
  file_search_store_name='unique_file_name'
)

# Wait until import is complete
while not operation.done:
    time.sleep(5)
    operation = client.operations.get(operation)

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

print(response.text)

การนำเข้าไฟล์

หรือจะอัปโหลดไฟล์ที่มีอยู่แล้วและนำเข้าไปยังที่เก็บไฟล์ก็ได้ โดยทำดังนี้

Python

from google import genai
from google.genai import types
import time

client = genai.Client()

# Upload the file using the Files API, supply a unique file name which will be visible in citations
sample_file = client.files.upload(file='sample.txt', config={'name': 'unique_file_name'})

# Create the file search store with an optional display name that shows in the grounding metadata
file_search_store = client.file_search_stores.create(config={'display_name': 'your-fileSearchStore-name'})

# Import the file into the file search store
operation = client.file_search_stores.import_file(
    file_search_store_name=file_search_store.name,
    file_name=sample_file.name
)

# Wait until import is complete
while not operation.done:
    time.sleep(5)
    operation = client.operations.get(operation)

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

print(response.text)

การกำหนดค่าการแบ่งกลุ่ม

เมื่อนำเข้าไฟล์ไปยังที่เก็บการค้นหาไฟล์ ระบบจะแบ่งไฟล์ออกเป็น ก้อนๆ ฝัง จัดทำดัชนี และอัปโหลดไปยังที่เก็บการค้นหาไฟล์โดยอัตโนมัติ หากต้องการควบคุมกลยุทธ์การแบ่งกลุ่มให้มากขึ้น คุณสามารถระบุchunking_configการตั้งค่าเพื่อกำหนดจำนวนโทเค็นสูงสุดต่อก้อนและจำนวนโทเค็นที่ทับซ้อนกันสูงสุดได้

# Upload and import and upload the file into the file search store with a custom chunking configuration
operation = client.file_search_stores.upload_to_file_search_store(
    file_search_store_name=file_search_store.name,
    file_name=sample_file.name,
    config={
        'chunking_config': {
          'white_space_config': {
            'max_tokens_per_chunk': 200,
            'max_overlap_tokens': 20
          }
        }
    }
)

หากต้องการใช้ที่เก็บการค้นหาไฟล์ ให้ส่งเป็นเครื่องมือไปยังเมธอด generateContent ดังที่แสดงในตัวอย่างอัปโหลดและนำเข้า

วิธีการทำงาน

การค้นหาไฟล์ใช้เทคนิคที่เรียกว่าการค้นหาเชิงความหมายเพื่อค้นหาข้อมูลที่เกี่ยวข้องกับพรอมต์ของผู้ใช้ การค้นหาเชิงความหมายต่างจากการค้นหาแบบดั้งเดิมที่อิงตามคีย์เวิร์ด ตรงที่เข้าใจความหมายและบริบทของคำค้นหา

เมื่อนำเข้าไฟล์ ระบบจะแปลงไฟล์เป็นตัวแทนเชิงตัวเลขที่เรียกว่า การฝัง ซึ่งจะบันทึกความหมายเชิงความหมายของ ข้อความ ระบบจะจัดเก็บการฝังเหล่านี้ไว้ในฐานข้อมูลการค้นหาไฟล์เฉพาะ เมื่อคุณทำการค้นหา ระบบจะแปลงการค้นหานั้นเป็น Embedding ด้วย จากนั้นระบบจะ ค้นหาไฟล์เพื่อหาข้อความในเอกสารที่คล้ายกันและเกี่ยวข้องมากที่สุด จากที่เก็บข้อมูลการค้นหาไฟล์

ขั้นตอนการใช้ File Search uploadToFileSearchStore API มีดังนี้

  1. สร้างที่เก็บการค้นหาไฟล์: ที่เก็บการค้นหาไฟล์มีข้อมูลที่ประมวลผลแล้วจากไฟล์ ซึ่งเป็นคอนเทนเนอร์แบบถาวรสำหรับ Embedding ที่การค้นหาเชิงความหมายจะทำงานด้วย

  2. อัปโหลดไฟล์และนำเข้าไปยังร้านค้าการค้นหาไฟล์: อัปโหลดไฟล์พร้อมกัน และนำเข้าผลลัพธ์ไปยังร้านค้าการค้นหาไฟล์ ซึ่งจะสร้างFileออบเจ็กต์ชั่วคราว ซึ่งเป็นข้อมูลอ้างอิงถึงเอกสารดิบ จากนั้นระบบจะแบ่งข้อมูลออกเป็นส่วนๆ แปลงเป็นเวกเตอร์การค้นหาไฟล์ และจัดทำดัชนี File ระบบจะลบออบเจ็กต์หลังจาก 48 ชั่วโมง ส่วนข้อมูลที่นำเข้าไปยังที่เก็บข้อมูลการค้นหาไฟล์ จะจัดเก็บไว้เรื่อยๆ จนกว่าคุณจะเลือกที่จะลบ

  3. ค้นหาด้วยการค้นหาไฟล์: สุดท้าย คุณใช้เครื่องมือ FileSearch ในการโทร generateContent ในการกำหนดค่าเครื่องมือ คุณจะระบุ FileSearchRetrievalResourceซึ่งชี้ไปยัง FileSearchStore ที่ต้องการ ค้นหา ซึ่งจะบอกโมเดลให้ทำการค้นหาเชิงความหมายในที่เก็บการค้นหาไฟล์นั้นๆ เพื่อค้นหาข้อมูลที่เกี่ยวข้องเพื่อใช้เป็นพื้นฐานในการตอบ

กระบวนการจัดทำดัชนีและการค้นหาของเครื่องมือค้นหาไฟล์
กระบวนการจัดทำดัชนีและการค้นหาของ File Search

ในแผนภาพนี้ เส้นประจากเอกสารไปยังโมเดลการฝัง (ใช้ gemini-embedding-001) แสดงถึง uploadToFileSearchStore API (ข้ามที่เก็บไฟล์) ไม่เช่นนั้น การใช้ Files API เพื่อสร้างแยกกัน แล้วนำเข้าไฟล์จะย้ายกระบวนการจัดทำดัชนีจาก Documents ไปยัง File storage แล้วไปยัง Embedding model

ร้านค้าที่ค้นหาไฟล์

ที่เก็บการค้นหาไฟล์คือคอนเทนเนอร์สำหรับการฝังเอกสาร แม้ว่าระบบจะลบไฟล์ดิบที่อัปโหลดผ่าน File API หลังจาก 48 ชั่วโมง แต่ข้อมูลที่นำเข้าไปยังที่เก็บการค้นหาไฟล์จะจัดเก็บไว้อย่างไม่มีกำหนดจนกว่าคุณจะลบด้วยตนเอง คุณสามารถ สร้างที่เก็บการค้นหาไฟล์หลายรายการเพื่อจัดระเบียบเอกสารได้ FileSearchStore API ช่วยให้คุณสร้าง แสดงรายการ รับ และลบเพื่อจัดการร้านค้า การค้นหาไฟล์ได้ ชื่อร้านค้าในการค้นหาไฟล์จะมีขอบเขตทั่วโลก

ตัวอย่างวิธีจัดการร้านค้าที่ค้นหาไฟล์มีดังนี้

# Create a file search store (including optional display_name for easier reference)
file_search_store = client.file_search_stores.create(config={'display_name': 'my-file_search-store-123'})

# List all your file search stores
for file_search_store in client.file_search_stores.list():
    print(file_search_store)

# Get a specific file search store by name
my_file_search_store = client.file_search_stores.get(name='fileSearchStores/my-file_search-store-123')

# Delete a file search store
client.file_search_stores.delete(name='fileSearchStores/my-file_search-store-123', config={'force': True})

ข้อมูลเมตาของไฟล์

คุณเพิ่มข้อมูลเมตาที่กำหนดเองลงในไฟล์เพื่อช่วยกรองหรือให้บริบทเพิ่มเติมได้ ข้อมูลเมตาคือชุดคู่คีย์-ค่า

# Import the file into the file search store with custom metadata
op = client.file_search_stores.import_file(
    file_search_store_name=file_search_store.name,
    file_name=sample_file.name,
    custom_metadata=[
        {"key": "author", "string_value": "Robert Graves"},
        {"key": "year", "numeric_value": 1934}
    ]
)

ซึ่งจะมีประโยชน์เมื่อคุณมีเอกสารหลายรายการในที่เก็บการค้นหาไฟล์และต้องการ ค้นหาเฉพาะชุดย่อยของเอกสารเหล่านั้น

# Use the metadata filter to search within a subset of documents
response = client.models.generate_content(
    model="gemini-2.5-flash",
    contents="""Tell me about the book 'I, Claudius'""",
    config=types.GenerateContentConfig(
        tools=[
            types.Tool(
                file_search=types.FileSearch(
                  file_search_store_names=[file_search_store.name],
                  metadata_filter = 'author=Robet Graves',
                  )
            )
        ]
    )
)

print(response.text)

ดูคำแนะนำในการใช้ไวยากรณ์ตัวกรองรายการสำหรับ metadata_filter ได้ที่ google.aip.dev/160

การอ้างอิง

เมื่อคุณใช้การค้นหาไฟล์ คำตอบของโมเดลอาจมีการอ้างอิงที่ระบุส่วนของเอกสารที่คุณอัปโหลดซึ่งใช้ในการสร้างคำตอบ ซึ่งจะช่วยในการตรวจสอบข้อเท็จจริงและการยืนยัน

คุณเข้าถึงข้อมูลการอ้างอิงได้ผ่านgrounding_metadataแอตทริบิวต์ ของคำตอบ

print(response.candidates[0].grounding_metadata)

โมเดลที่รองรับ

รุ่นต่อไปนี้รองรับการค้นหาไฟล์

ประเภทไฟล์ที่สนับสนุน

การค้นหาไฟล์รองรับรูปแบบไฟล์ที่หลากหลายตามที่ระบุไว้ในส่วนต่อไปนี้

ประเภทไฟล์แอปพลิเคชัน

  • application/dart
  • application/ecmascript
  • application/json
  • application/ms-java
  • application/msword
  • application/pdf
  • application/sql
  • application/typescript
  • application/vnd.curl
  • application/vnd.dart
  • application/vnd.ibm.secure-container
  • application/vnd.jupyter
  • application/vnd.ms-excel
  • application/vnd.oasis.opendocument.text
  • application/vnd.openxmlformats-officedocument.presentationml.presentation
  • application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
  • application/vnd.openxmlformats-officedocument.wordprocessingml.document
  • application/vnd.openxmlformats-officedocument.wordprocessingml.template
  • application/x-csh
  • application/x-hwp
  • application/x-hwp-v5
  • application/x-latex
  • application/x-php
  • application/x-powershell
  • application/x-sh
  • application/x-shellscript
  • application/x-tex
  • application/x-zsh
  • application/xml
  • application/zip

ประเภทไฟล์ข้อความ

  • text/1d-interleaved-parityfec
  • text/RED
  • text/SGML
  • text/cache-manifest
  • text/calendar
  • text/cql
  • text/cql-extension
  • text/cql-identifier
  • text/css
  • text/csv
  • text/csv-schema
  • text/dns
  • text/encaprtp
  • text/enriched
  • text/example
  • text/fhirpath
  • text/flexfec
  • text/fwdred
  • text/gff3
  • text/grammar-ref-list
  • text/hl7v2
  • text/html
  • text/javascript
  • text/jcr-cnd
  • text/jsx
  • text/markdown
  • text/mizar
  • text/n3
  • text/parameters
  • text/parityfec
  • text/php
  • text/plain
  • text/provenance-notation
  • text/prs.fallenstein.rst
  • text/prs.lines.tag
  • text/prs.prop.logic
  • text/raptorfec
  • text/rfc822-headers
  • text/rtf
  • text/rtp-enc-aescm128
  • text/rtploopback
  • text/rtx
  • text/sgml
  • text/shaclc
  • text/shex
  • text/spdx
  • text/strings
  • text/t140
  • text/tab-separated-values
  • text/texmacs
  • text/troff
  • text/tsv
  • text/tsx
  • text/turtle
  • text/ulpfec
  • text/uri-list
  • text/vcard
  • text/vnd.DMClientScript
  • text/vnd.IPTC.NITF
  • text/vnd.IPTC.NewsML
  • text/vnd.a
  • text/vnd.abc
  • text/vnd.ascii-art
  • text/vnd.curl
  • text/vnd.debian.copyright
  • text/vnd.dvb.subtitle
  • text/vnd.esmertec.theme-descriptor
  • text/vnd.exchangeable
  • text/vnd.familysearch.gedcom
  • text/vnd.ficlab.flt
  • text/vnd.fly
  • text/vnd.fmi.flexstor
  • text/vnd.gml
  • text/vnd.graphviz
  • text/vnd.hans
  • text/vnd.hgl
  • text/vnd.in3d.3dml
  • text/vnd.in3d.spot
  • text/vnd.latex-z
  • text/vnd.motorola.reflex
  • text/vnd.ms-mediapackage
  • text/vnd.net2phone.commcenter.command
  • text/vnd.radisys.msml-basic-layout
  • text/vnd.senx.warpscript
  • text/vnd.sosi
  • text/vnd.sun.j2me.app-descriptor
  • text/vnd.trolltech.linguist
  • text/vnd.wap.si
  • text/vnd.wap.sl
  • text/vnd.wap.wml
  • text/vnd.wap.wmlscript
  • text/vtt
  • text/wgsl
  • text/x-asm
  • text/x-bibtex
  • text/x-boo
  • text/x-c
  • text/x-c++hdr
  • text/x-c++src
  • text/x-cassandra
  • text/x-chdr
  • text/x-coffeescript
  • text/x-component
  • text/x-csh
  • text/x-csharp
  • text/x-csrc
  • text/x-cuda
  • text/x-d
  • text/x-diff
  • text/x-dsrc
  • text/x-emacs-lisp
  • text/x-erlang
  • text/x-gff3
  • text/x-go
  • text/x-haskell
  • text/x-java
  • text/x-java-properties
  • text/x-java-source
  • text/x-kotlin
  • text/x-lilypond
  • text/x-lisp
  • text/x-literate-haskell
  • text/x-lua
  • text/x-moc
  • text/x-objcsrc
  • text/x-pascal
  • text/x-pcs-gcd
  • text/x-perl
  • text/x-perl-script
  • text/x-python
  • text/x-python-script
  • text/x-r-markdown
  • text/x-rsrc
  • text/x-rst
  • text/x-ruby-script
  • text/x-rust
  • text/x-sass
  • text/x-scala
  • text/x-scheme
  • text/x-script.python
  • text/x-scss
  • text/x-setext
  • text/x-sfv
  • text/x-sh
  • text/x-siesta
  • text/x-sos
  • text/x-sql
  • text/x-swift
  • text/x-tcl
  • text/x-tex
  • text/x-vbasic
  • text/x-vcalendar
  • text/xml
  • text/xml-dtd
  • text/xml-external-parsed-entity
  • text/yaml

ขีดจำกัดอัตรา

File Search API มีขีดจำกัดต่อไปนี้เพื่อบังคับใช้ความเสถียรของบริการ

  • ขนาดไฟล์สูงสุด / ขีดจำกัดต่อเอกสาร: 100 MB
  • การค้นหาไฟล์ที่จัดเก็บต่อโปรเจ็กต์: 10
  • ขนาดรวมของที่เก็บข้อมูลการค้นหาไฟล์โปรเจ็กต์ (อิงตามระดับผู้ใช้)
    • ฟรี: 1 GB
    • ระดับ 1: 10 GB
    • ระดับ 2: 100 GB
    • ระดับ 3: 1 TB
  • คำแนะนำ: จำกัดขนาดของที่เก็บข้อมูลการค้นหาแต่ละไฟล์ให้มีขนาดไม่เกิน 20 GB เพื่อให้มั่นใจว่าเวลาในการดึงข้อมูลจะเหมาะสมที่สุด

ราคา

  • ระบบจะเรียกเก็บเงินค่าการฝังจากนักพัฒนาแอปในเวลาที่จัดทำดัชนีโดยอิงตามราคาการฝังที่มีอยู่ ($0.15 ต่อโทเค็น 1 ล้านรายการ)
  • พื้นที่เก็บข้อมูลไม่มีค่าใช้จ่าย
  • การฝังเวลาการค้นหาไม่มีค่าใช้จ่าย
  • ระบบจะเรียกเก็บเงินโทเค็นเอกสารที่ดึงมาเป็นโทเค็นบริบทปกติ