Gemini API ช่วยให้สร้างการดึงข้อมูลเสริม (Retrieval Augmented Generation หรือ "RAG") ได้ผ่านเครื่องมือค้นหาไฟล์ File Search จะนำเข้า แบ่งเป็นกลุ่ม และจัดทำดัชนีข้อมูลเพื่อช่วยให้ดึงข้อมูลที่เกี่ยวข้องได้อย่างรวดเร็วตามพรอมต์ของผู้ใช้ จากนั้นระบบจะให้ข้อมูลนี้เป็นบริบทแก่โมเดล ซึ่งจะช่วยให้โมเดล ให้คำตอบที่ถูกต้องและเกี่ยวข้องมากขึ้นได้
คุณสามารถใช้ uploadToFileSearchStore
API เพื่ออัปโหลดไฟล์ที่มีอยู่ไปยังที่เก็บไฟล์ค้นหาโดยตรง หรือจะอัปโหลดแยกกันแล้วimportFile
หากต้องการสร้างไฟล์พร้อมกันก็ได้
อัปโหลดไปยังร้านค้า File Search โดยตรง
ตัวอย่างนี้แสดงวิธีอัปโหลดไฟล์ไปยังที่เก็บไฟล์โดยตรง
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
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 file name which will be visible in citations
operation = client.file_search_stores.upload_to_file_search_store(
file='sample.txt',
file_search_store_name=file_search_store.name,
config={
'display_name' : 'display-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)
JavaScript
const { GoogleGenAI } = require('@google/genai');
const ai = new GoogleGenAI({});
async function run() {
// Create the File Search store with an optional display name
const fileSearchStore = await ai.fileSearchStores.create({
config: { displayName: 'your-fileSearchStore-name' }
});
// Upload and import a file into the File Search store, supply a file name which will be visible in citations
let operation = await ai.fileSearchStores.uploadToFileSearchStore({
file: 'file.txt',
fileSearchStoreName: fileSearchStore.name,
config: {
displayName: 'file-name',
}
});
// Wait until import is complete
while (!operation.done) {
await new Promise(resolve => setTimeout(resolve, 5000));
operation = await ai.operations.get({ operation });
}
// Ask a question about the file
const response = await ai.models.generateContent({
model: "gemini-2.5-flash",
contents: "Can you tell me about Robert Graves",
config: {
tools: [
{
fileSearch: {
fileSearchStoreNames: [fileSearchStore.name]
}
}
]
}
});
console.log(response.text);
}
run();
REST
FILE_PATH="path/to/sample.pdf"
MIME_TYPE=$(file -b --mime-type "${FILE_PATH}")
NUM_BYTES=$(wc -c < "${FILE_PATH}")
# Create a FileSearchStore
STORE_RESPONSE=$(curl -s -X POST "https://generativelanguage.googleapis.com/v1beta/fileSearchStores?key=${GEMINI_API_KEY}" \
-H "Content-Type: application/json" \
-d '{ "displayName": "My Store" }')
# Extract the store name (format: fileSearchStores/xxxxxxx)
STORE_NAME=$(echo $STORE_RESPONSE | jq -r '.name')
# Initiate Resumable Upload to the Store
TMP_HEADER="upload-header.tmp"
curl -s -D "${TMP_HEADER}" \ "https://generativelanguage.googleapis.com/upload/v1beta/${STORE_NAME}:uploadToFileSearchStore?key=${GEMINI_API_KEY}" \
-H "X-Goog-Upload-Protocol: resumable" \
-H "X-Goog-Upload-Command: start" \
-H "X-Goog-Upload-Header-Content-Length: ${NUM_BYTES}" \
-H "X-Goog-Upload-Header-Content-Type: ${MIME_TYPE}" \
-H "Content-Type: application/json" > /dev/null
# Extract upload_url from headers
UPLOAD_URL=$(grep -i "x-goog-upload-url: " "${TMP_HEADER}" | cut -d" " -f2 | tr -d "\r")
rm "${TMP_HEADER}"
# --- Upload the actual bytes ---
curl "${UPLOAD_URL}" \
-H "Content-Length: ${NUM_BYTES}" \
-H "X-Goog-Upload-Offset: 0" \
-H "X-Goog-Upload-Command: upload, finalize" \
--data-binary "@${FILE_PATH}" 2> /dev/null
# Generate content using the FileSearchStore
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [{
"parts":[{"text": "What does the research say about ..."}]
}],
"tools": [{
"file_search": { "file_search_store_names":["'$STORE_NAME'"] }
}]
}' 2> /dev/null > response.json
cat response.json
ดูข้อมูลเพิ่มเติมได้ที่เอกสารอ้างอิง API สำหรับ uploadToFileSearchStore
การนำเข้าไฟล์
หรือจะอัปโหลดไฟล์ที่มีอยู่แล้วและนำเข้าไปยังที่เก็บไฟล์ก็ได้ โดยทำดังนี้
Python
from google import genai
from google.genai import types
import time
client = genai.Client()
# Upload the file using the Files API, supply a file name which will be visible in citations
sample_file = client.files.upload(file='sample.txt', config={'name': 'display_file_name'})
# Create the File Search store with an optional display name
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)
JavaScript
const { GoogleGenAI } = require('@google/genai');
const ai = new GoogleGenAI({});
async function run() {
// Upload the file using the Files API, supply a file name which will be visible in citations
const sampleFile = await ai.files.upload({
file: 'sample.txt',
config: { name: 'file-name' }
});
// Create the File Search store with an optional display name
const fileSearchStore = await ai.fileSearchStores.create({
config: { displayName: 'your-fileSearchStore-name' }
});
// Import the file into the File Search store
let operation = await ai.fileSearchStores.importFile({
fileSearchStoreName: fileSearchStore.name,
fileName: sampleFile.name
});
// Wait until import is complete
while (!operation.done) {
await new Promise(resolve => setTimeout(resolve, 5000));
operation = await ai.operations.get({ operation: operation });
}
// Ask a question about the file
const response = await ai.models.generateContent({
model: "gemini-2.5-flash",
contents: "Can you tell me about Robert Graves",
config: {
tools: [
{
fileSearch: {
fileSearchStoreNames: [fileSearchStore.name]
}
}
]
}
});
console.log(response.text);
}
run();
REST
FILE_PATH="path/to/sample.pdf"
MIME_TYPE=$(file -b --mime-type "${FILE_PATH}")
NUM_BYTES=$(wc -c < "${FILE_PATH}")
# Create a FileSearchStore
STORE_RESPONSE=$(curl -s -X POST "https://generativelanguage.googleapis.com/v1beta/fileSearchStores?key=${GEMINI_API_KEY}" \
-H "Content-Type: application/json" \
-d '{ "displayName": "My Store" }')
STORE_NAME=$(echo $STORE_RESPONSE | jq -r '.name')
# Initiate Resumable Upload to the Store
TMP_HEADER="upload-header.tmp"
curl -s -X POST "https://generativelanguage.googleapis.com/upload/v1beta/files?key=${GEMINI_API_KEY}" \
-D "${TMP_HEADER}" \
-H "X-Goog-Upload-Protocol: resumable" \
-H "X-Goog-Upload-Command: start" \
-H "X-Goog-Upload-Header-Content-Length: ${NUM_BYTES}" \
-H "X-Goog-Upload-Header-Content-Type: ${MIME_TYPE}" \
-H "Content-Type: application/json" 2> /dev/null
UPLOAD_URL=$(grep -i "x-goog-upload-url: " "${TMP_HEADER}" | cut -d" " -f2 | tr -d "\r")
rm "${TMP_HEADER}"
# Upload the actual bytes.
curl -s -X POST "${UPLOAD_URL}" \
-H "Content-Length: ${NUM_BYTES}" \
-H "X-Goog-Upload-Offset: 0" \
-H "X-Goog-Upload-Command: upload, finalize" \
--data-binary "@${FILE_PATH}" 2> /dev/null > file_info.json
file_uri=$(jq ".file.name" file_info.json)
# Import files into the file search store
operation_name=$(curl "https://generativelanguage.googleapis.com/v1beta/${STORE_NAME}:importFile?key=${GEMINI_API_KEY}" \
-H "Content-Type: application/json" \
-X POST \
-d '{
"file_name":'$file_uri'
}' | jq -r .name)
# Wait for long running operation to complete
while true; do
# Get the full JSON status and store it in a variable.
status_response=$(curl -s -H "x-goog-api-key: $GEMINI_API_KEY" "https://generativelanguage.googleapis.com/v1beta/${operation_name}")
# Check the "done" field from the JSON stored in the variable.
is_done=$(echo "${status_response}" | jq .done)
if [ "${is_done}" = "true" ]; then
break
fi
# Wait for 10 seconds before checking again.
sleep 10
done
# Generate content using the FileSearchStore
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key=${GEMINI_API_KEY}" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [{
"parts":[{"text": "What does the research say about ..."}]
}],
"tools": [{
"file_search": { "file_search_store_names":["'$STORE_NAME'"] }
}]
}' 2> /dev/null > response.json
cat response.json
ดูข้อมูลเพิ่มเติมได้ที่เอกสารอ้างอิง API สำหรับ importFile
การกำหนดค่าการแบ่งกลุ่ม
เมื่อนำเข้าไฟล์ไปยังที่เก็บข้อมูลการค้นหาไฟล์ ระบบจะแบ่งไฟล์ออกเป็น
ก้อนๆ ฝัง จัดทำดัชนี และอัปโหลดไปยังที่เก็บข้อมูลการค้นหาไฟล์โดยอัตโนมัติ หากต้องการควบคุมกลยุทธ์การแบ่งกลุ่มเพิ่มเติม คุณสามารถระบุการตั้งค่า chunking_config
เพื่อตั้งค่าจำนวนโทเค็นสูงสุดต่อกลุ่มและจำนวนโทเค็นที่ทับซ้อนกันสูงสุดได้
Python
# 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
}
}
}
)
JavaScript
// Upload and import and upload the file into the File Search store with a custom chunking configuration
let operation = await ai.fileSearchStores.uploadToFileSearchStore({
file: 'file.txt',
fileSearchStoreName: fileSearchStore.name,
config: {
displayName: 'file-name',
chunkingConfig: {
whiteSpaceConfig: {
maxTokensPerChunk: 200,
maxOverlapTokens: 20
}
}
}
});
REST
FILE_PATH="path/to/sample.pdf"
MIME_TYPE=$(file -b --mime-type "${FILE_PATH}")
NUM_BYTES=$(wc -c < "${FILE_PATH}")
# Create a FileSearchStore
STORE_RESPONSE=$(curl -s -X POST "https://generativelanguage.googleapis.com/v1beta/fileSearchStores?key=${GEMINI_API_KEY}" \
-H "Content-Type: application/json" \
-d '{ "displayName": "My Store" }')
# Extract the store name (format: fileSearchStores/xxxxxxx)
STORE_NAME=$(echo $STORE_RESPONSE | jq -r '.name')
# Initiate Resumable Upload to the Store
TMP_HEADER="upload-header.tmp"
curl -s -D "${TMP_HEADER}" \ "https://generativelanguage.googleapis.com/upload/v1beta/${STORE_NAME}:uploadToFileSearchStore?key=${GEMINI_API_KEY}" \
-H "X-Goog-Upload-Protocol: resumable" \
-H "X-Goog-Upload-Command: start" \
-H "X-Goog-Upload-Header-Content-Length: ${NUM_BYTES}" \
-H "X-Goog-Upload-Header-Content-Type: ${MIME_TYPE}" \
-H "Content-Type: application/json" > /dev/null
-d '{
"chunking_config": {
"white_space_config": {
"max_tokens_per_chunk": 200,
"max_overlap_tokens": 20
}
}
}'
# Extract upload_url from headers
UPLOAD_URL=$(grep -i "x-goog-upload-url: " "${TMP_HEADER}" | cut -d" " -f2 | tr -d "\r")
rm "${TMP_HEADER}"
# --- Upload the actual bytes ---
curl "${UPLOAD_URL}" \
-H "Content-Length: ${NUM_BYTES}" \
-H "X-Goog-Upload-Offset: 0" \
-H "X-Goog-Upload-Command: upload, finalize" \
--data-binary "@${FILE_PATH}" 2> /dev/null
หากต้องการใช้ที่เก็บข้อมูลการค้นหาไฟล์ ให้ส่งเป็นเครื่องมือไปยังเมธอด generateContent
ดังที่แสดงในตัวอย่างอัปโหลดและนำเข้า
วิธีการทำงาน
การค้นหาไฟล์ใช้เทคนิคที่เรียกว่าการค้นหาเชิงความหมายเพื่อค้นหาข้อมูลที่เกี่ยวข้องกับพรอมต์ของผู้ใช้ การค้นหาเชิงความหมายต่างจากการค้นหาแบบดั้งเดิมที่อิงตามคีย์เวิร์ด ตรงที่เข้าใจความหมายและบริบทของคำค้นหา
เมื่อนำเข้าไฟล์ ระบบจะแปลงไฟล์เป็นตัวแทนเชิงตัวเลขที่เรียกว่า การฝัง ซึ่งจะบันทึกความหมายเชิงความหมายของ ข้อความ โดยระบบจะจัดเก็บการฝังเหล่านี้ไว้ในฐานข้อมูลการค้นหาไฟล์เฉพาะ เมื่อคุณทำการค้นหา ระบบจะแปลงการค้นหานั้นเป็น Embedding ด้วย จากนั้นระบบจะ ทำการค้นหาไฟล์เพื่อค้นหาเอกสารที่คล้ายกันและเกี่ยวข้องมากที่สุด จากที่เก็บข้อมูลการค้นหาไฟล์
ขั้นตอนการใช้ File Search
uploadToFileSearchStore API มีดังนี้
สร้างที่เก็บข้อมูลการค้นหาไฟล์: ที่เก็บข้อมูลการค้นหาไฟล์มีข้อมูลที่ประมวลผลแล้วจากไฟล์ ซึ่งเป็นคอนเทนเนอร์แบบถาวรสำหรับ Embedding ที่การค้นหาเชิงความหมายจะทำงานด้วย
อัปโหลดไฟล์และนำเข้าไปยังร้านค้า File Search: อัปโหลดไฟล์พร้อมกัน และนำเข้าผลลัพธ์ไปยังร้านค้า File Search ซึ่งจะสร้าง
Fileออบเจ็กต์ชั่วคราว ซึ่งเป็นข้อมูลอ้างอิงถึงเอกสารดิบ จากนั้นระบบจะแบ่งข้อมูลออกเป็นส่วนๆ แปลงเป็นข้อมูลฝังสำหรับการค้นหาไฟล์ และจัดทำดัชนีFileระบบจะลบออบเจ็กต์หลังจาก 48 ชั่วโมง ส่วนข้อมูลที่นำเข้าไปยังที่เก็บข้อมูลการค้นหาไฟล์ จะจัดเก็บไว้เรื่อยๆ จนกว่าคุณจะเลือกให้ลบค้นหาด้วยการค้นหาไฟล์: สุดท้าย คุณใช้เครื่องมือ
FileSearchในการโทรgenerateContentในการกำหนดค่าเครื่องมือ คุณจะระบุFileSearchRetrievalResourceซึ่งชี้ไปยังFileSearchStoreที่ต้องการ ค้นหา ซึ่งจะบอกโมเดลให้ทำการค้นหาเชิงความหมายในที่เก็บข้อมูลการค้นหาไฟล์นั้นๆ เพื่อค้นหาข้อมูลที่เกี่ยวข้องมาใช้เป็นพื้นฐานในการตอบ
ในแผนภาพนี้ เส้นประจากเอกสารไปยังโมเดลการฝัง
(ใช้ gemini-embedding-001)
แสดงถึง uploadToFileSearchStore API (ข้ามที่เก็บไฟล์)
ไม่เช่นนั้น การใช้ Files API เพื่อสร้างแยกกัน
แล้วนำเข้าไฟล์จะย้ายกระบวนการจัดทำดัชนีจาก Documents ไปยัง
File storage แล้วไปยัง Embedding model
File Search stores
ที่เก็บการค้นหาไฟล์คือคอนเทนเนอร์สำหรับการฝังเอกสาร แม้ว่าระบบจะลบไฟล์ดิบที่อัปโหลดผ่าน File API หลังจาก 48 ชั่วโมง แต่ข้อมูลที่นำเข้าไปยังที่เก็บข้อมูลการค้นหาไฟล์จะจัดเก็บไว้เรื่อยๆ จนกว่าคุณจะลบด้วยตนเอง คุณสามารถ
สร้างที่เก็บการค้นหาไฟล์หลายรายการเพื่อจัดระเบียบเอกสารได้
FileSearchStore API ช่วยให้คุณสร้าง แสดงรายการ รับ และลบเพื่อจัดการร้านค้า
การค้นหาไฟล์ได้ ชื่อร้านค้าของ File Search จะมีขอบเขตทั่วโลก
ตัวอย่างวิธีจัดการร้านค้าที่ค้นหาไฟล์มีดังนี้
Python
# 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})
JavaScript
// Create a File Search store (including optional display_name for easier reference)
const fileSearchStore = await ai.fileSearchStores.create({
config: { displayName: 'my-file_search-store-123' }
});
// List all your File Search stores
const fileSearchStores = await ai.fileSearchStores.list();
for await (const store of fileSearchStores) {
console.log(store);
}
// Get a specific File Search store by name
const myFileSearchStore = await ai.fileSearchStores.get({
name: 'fileSearchStores/my-file_search-store-123'
});
// Delete a File Search store
await ai.fileSearchStores.delete({
name: 'fileSearchStores/my-file_search-store-123',
config: { force: true }
});
REST
# Create a File Search store (including optional display_name for easier reference)
curl -X POST "https://generativelanguage.googleapis.com/v1beta/fileSearchStores?key=${GEMINI_API_KEY}" \
-H "Content-Type: application/json"
-d '{ "displayName": "My Store" }'
# List all your File Search stores
curl "https://generativelanguage.googleapis.com/v1beta/fileSearchStores?key=${GEMINI_API_KEY}" \
# Get a specific File Search store by name
curl "https://generativelanguage.googleapis.com/v1beta/fileSearchStores/my-file_search-store-123?key=${GEMINI_API_KEY}"
# Delete a File Search store
curl -X DELETE "https://generativelanguage.googleapis.com/v1beta/fileSearchStores/my-file_search-store-123?key=${GEMINI_API_KEY}"
ข้อมูลอ้างอิง API ของเอกสารการค้นหาไฟล์สำหรับ เมธอดและฟิลด์ที่เกี่ยวข้องกับการจัดการเอกสารในที่เก็บไฟล์
ข้อมูลเมตาของไฟล์
คุณเพิ่มข้อมูลเมตาที่กำหนดเองลงในไฟล์เพื่อช่วยกรองหรือให้บริบทเพิ่มเติมได้ ข้อมูลเมตาคือชุดคู่คีย์-ค่า
Python
# 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}
]
)
JavaScript
// Import the file into the File Search store with custom metadata
let operation = await ai.fileSearchStores.importFile({
fileSearchStoreName: fileSearchStore.name,
fileName: sampleFile.name,
config: {
customMetadata: [
{ key: "author", stringValue: "Robert Graves" },
{ key: "year", numericValue: 1934 }
]
}
});
REST
FILE_PATH="path/to/sample.pdf"
MIME_TYPE=$(file -b --mime-type "${FILE_PATH}")
NUM_BYTES=$(wc -c < "${FILE_PATH}")
# Create a FileSearchStore
STORE_RESPONSE=$(curl -s -X POST "https://generativelanguage.googleapis.com/v1beta/fileSearchStores?key=${GEMINI_API_KEY}" \
-H "Content-Type: application/json" \
-d '{ "displayName": "My Store" }')
# Extract the store name (format: fileSearchStores/xxxxxxx)
STORE_NAME=$(echo $STORE_RESPONSE | jq -r '.name')
# Initiate Resumable Upload to the Store
TMP_HEADER="upload-header.tmp"
curl -s -D "${TMP_HEADER}" \
"https://generativelanguage.googleapis.com/upload/v1beta/${STORE_NAME}:uploadToFileSearchStore?key=${GEMINI_API_KEY}" \
-H "X-Goog-Upload-Protocol: resumable" \
-H "X-Goog-Upload-Command: start" \
-H "X-Goog-Upload-Header-Content-Length: ${NUM_BYTES}" \
-H "X-Goog-Upload-Header-Content-Type: ${MIME_TYPE}" \
-H "Content-Type: application/json" \
-d '{
"custom_metadata": [
{"key": "author", "string_value": "Robert Graves"},
{"key": "year", "numeric_value": 1934}
]
}' > /dev/null
# Extract upload_url from headers
UPLOAD_URL=$(grep -i "x-goog-upload-url: " "${TMP_HEADER}" | cut -d" " -f2 | tr -d "\r")
rm "${TMP_HEADER}"
# --- Upload the actual bytes ---
curl "${UPLOAD_URL}" \
-H "Content-Length: ${NUM_BYTES}" \
-H "X-Goog-Upload-Offset: 0" \
-H "X-Goog-Upload-Command: upload, finalize" \
--data-binary "@${FILE_PATH}" 2> /dev/null
ซึ่งจะมีประโยชน์เมื่อคุณมีเอกสารหลายฉบับในที่เก็บการค้นหาไฟล์และต้องการ ค้นหาเฉพาะชุดย่อยของเอกสารเหล่านั้น
Python
# 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=Robert Graves",
)
)
]
)
)
print(response.text)
JavaScript
// Use the metadata filter to search within a subset of documents
const response = await ai.models.generateContent({
model: "gemini-2.5-flash",
contents: "Tell me about the book 'I, Claudius'",
config: {
tools: [
{
fileSearch: {
fileSearchStoreNames: [fileSearchStore.name],
metadataFilter: 'author="Robert Graves"',
}
}
]
}
});
console.log(response.text);
REST
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key=${GEMINI_API_KEY}" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [{
"parts":[{"text": "Tell me about the book I, Claudius"}]
}],
"tools": [{
"file_search": {
"file_search_store_names":["'$STORE_NAME'"],
"metadata_filter": "author = \"Robert Graves\""
}
}]
}' 2> /dev/null > response.json
cat response.json
ดูคำแนะนำในการใช้ไวยากรณ์ตัวกรองรายการสำหรับ metadata_filter ได้ที่ google.aip.dev/160
การอ้างอิง
เมื่อคุณใช้การค้นหาไฟล์ คำตอบของโมเดลอาจมีการอ้างอิงที่ระบุส่วนของเอกสารที่คุณอัปโหลดซึ่งใช้ในการสร้างคำตอบ ซึ่งจะช่วยในการตรวจสอบข้อเท็จจริงและการยืนยัน
คุณเข้าถึงข้อมูลการอ้างอิงได้ผ่านgrounding_metadataแอตทริบิวต์
ของคำตอบ
Python
print(response.candidates[0].grounding_metadata)
JavaScript
console.log(JSON.stringify(response.candidates?.[0]?.groundingMetadata, null, 2));
โมเดลที่รองรับ
รุ่นต่อไปนี้รองรับการค้นหาไฟล์
ประเภทไฟล์ที่สนับสนุน
การค้นหาไฟล์รองรับรูปแบบไฟล์ที่หลากหลายตามที่ระบุไว้ในส่วนต่อไปนี้
ประเภทไฟล์แอปพลิเคชัน
application/dartapplication/ecmascriptapplication/jsonapplication/ms-javaapplication/mswordapplication/pdfapplication/sqlapplication/typescriptapplication/vnd.curlapplication/vnd.dartapplication/vnd.ibm.secure-containerapplication/vnd.jupyterapplication/vnd.ms-excelapplication/vnd.oasis.opendocument.textapplication/vnd.openxmlformats-officedocument.presentationml.presentationapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheetapplication/vnd.openxmlformats-officedocument.wordprocessingml.documentapplication/vnd.openxmlformats-officedocument.wordprocessingml.templateapplication/x-cshapplication/x-hwpapplication/x-hwp-v5application/x-latexapplication/x-phpapplication/x-powershellapplication/x-shapplication/x-shellscriptapplication/x-texapplication/x-zshapplication/xmlapplication/zip
ประเภทไฟล์ข้อความ
text/1d-interleaved-parityfectext/REDtext/SGMLtext/cache-manifesttext/calendartext/cqltext/cql-extensiontext/cql-identifiertext/csstext/csvtext/csv-schematext/dnstext/encaprtptext/enrichedtext/exampletext/fhirpathtext/flexfectext/fwdredtext/gff3text/grammar-ref-listtext/hl7v2text/htmltext/javascripttext/jcr-cndtext/jsxtext/markdowntext/mizartext/n3text/parameterstext/parityfectext/phptext/plaintext/provenance-notationtext/prs.fallenstein.rsttext/prs.lines.tagtext/prs.prop.logictext/raptorfectext/rfc822-headerstext/rtftext/rtp-enc-aescm128text/rtploopbacktext/rtxtext/sgmltext/shaclctext/shextext/spdxtext/stringstext/t140text/tab-separated-valuestext/texmacstext/trofftext/tsvtext/tsxtext/turtletext/ulpfectext/uri-listtext/vcardtext/vnd.DMClientScripttext/vnd.IPTC.NITFtext/vnd.IPTC.NewsMLtext/vnd.atext/vnd.abctext/vnd.ascii-arttext/vnd.curltext/vnd.debian.copyrighttext/vnd.dvb.subtitletext/vnd.esmertec.theme-descriptortext/vnd.exchangeabletext/vnd.familysearch.gedcomtext/vnd.ficlab.flttext/vnd.flytext/vnd.fmi.flexstortext/vnd.gmltext/vnd.graphviztext/vnd.hanstext/vnd.hgltext/vnd.in3d.3dmltext/vnd.in3d.spottext/vnd.latex-ztext/vnd.motorola.reflextext/vnd.ms-mediapackagetext/vnd.net2phone.commcenter.commandtext/vnd.radisys.msml-basic-layouttext/vnd.senx.warpscripttext/vnd.sositext/vnd.sun.j2me.app-descriptortext/vnd.trolltech.linguisttext/vnd.wap.sitext/vnd.wap.sltext/vnd.wap.wmltext/vnd.wap.wmlscripttext/vtttext/wgsltext/x-asmtext/x-bibtextext/x-bootext/x-ctext/x-c++hdrtext/x-c++srctext/x-cassandratext/x-chdrtext/x-coffeescripttext/x-componenttext/x-cshtext/x-csharptext/x-csrctext/x-cudatext/x-dtext/x-difftext/x-dsrctext/x-emacs-lisptext/x-erlangtext/x-gff3text/x-gotext/x-haskelltext/x-javatext/x-java-propertiestext/x-java-sourcetext/x-kotlintext/x-lilypondtext/x-lisptext/x-literate-haskelltext/x-luatext/x-moctext/x-objcsrctext/x-pascaltext/x-pcs-gcdtext/x-perltext/x-perl-scripttext/x-pythontext/x-python-scripttext/x-r-markdowntext/x-rsrctext/x-rsttext/x-ruby-scripttext/x-rusttext/x-sasstext/x-scalatext/x-schemetext/x-script.pythontext/x-scsstext/x-setexttext/x-sfvtext/x-shtext/x-siestatext/x-sostext/x-sqltext/x-swifttext/x-tcltext/x-textext/x-vbasictext/x-vcalendartext/xmltext/xml-dtdtext/xml-external-parsed-entitytext/yaml
ขีดจำกัดอัตรา
File Search API มีขีดจำกัดต่อไปนี้เพื่อบังคับใช้ความเสถียรของบริการ
- ขนาดไฟล์สูงสุด / ขีดจำกัดต่อเอกสาร: 100 MB
- ขนาดรวมของที่เก็บข้อมูลการค้นหาไฟล์ของโปรเจ็กต์ (อิงตามระดับผู้ใช้)
- ฟรี: 1 GB
- ระดับ 1: 10 GB
- ระดับ 2: 100 GB
- ระดับ 3: 1 TB
- คำแนะนำ: จำกัดขนาดของที่เก็บข้อมูลการค้นหาไฟล์แต่ละรายการให้ต่ำกว่า 20 GB เพื่อให้มั่นใจว่าเวลาในการดึงข้อมูลจะเหมาะสมที่สุด
ราคา
- ระบบจะเรียกเก็บเงินค่าการฝังจากนักพัฒนาแอปในเวลาที่จัดทำดัชนีโดยอิงตามราคาการฝังที่มีอยู่ ($0.15 ต่อโทเค็น 1 ล้านรายการ)
- พื้นที่เก็บข้อมูลไม่มีค่าใช้จ่าย
- การฝังเวลาการค้นหาไม่มีค่าใช้จ่าย
- ระบบจะเรียกเก็บเงินโทเค็นเอกสารที่ดึงมาเป็นโทเค็นบริบทปกติ
ขั้นตอนถัดไป
- ไปที่ข้อมูลอ้างอิง API สำหรับร้านค้าค้นหาไฟล์และเอกสารการค้นหาไฟล์