在典型的 AI 工作流中,您可能会反复将相同的输入令牌传递给模型。借助 Gemini API 上下文缓存功能,您可以将一些内容一次性传递给模型,缓存输入令牌,然后在后续请求中引用缓存的令牌。在某些数据量下,使用缓存的令牌比重复传入同一令牌集更经济。
缓存一组令牌时,您可以选择缓存在令牌自动删除之前存在的时长。此缓存时长称为存留时间 (TTL)。如果未设置,TTL 默认为 1 小时。缓存的开销取决于输入令牌的大小以及您希望令牌保留多长时间。
上下文缓存同时支持 Gemini 1.5 Pro 和 Gemini 1.5 Flash。
应在何时使用上下文缓存
上下文缓存特别适合较短的请求重复引用大量初始上下文的场景。例如,对于以下使用场景,可以考虑使用上下文缓存:
- 有大量系统指令的聊天机器人
- 对较长的视频文件进行的重复分析
- 针对大型文档集的定期查询
- 频繁的代码库分析或 bug 修复
缓存如何降低费用
虽然上下文缓存是一项付费功能,但它的目的是为了降低整体的运营成本。结算取决于以下因素:
- 缓存词元数:缓存的输入词元数,如果相同的词元在后续提示中被重复使用,则按折扣费率计费。
- 存储时长:所缓存词元的存储时长 (TTL),按缓存词元数的 TTL 时长计费。TTL 没有最小值或最大值。
- 其他因素:可能还会产生其他费用,例如非缓存输入词元和输出词元的费用。
如需了解最新的价格详情,请参阅 Gemini API 价格页面。如需了解如何计算令牌数,请参阅令牌指南。
如何使用上下文缓存
本部分假定您已安装 Gemini SDK(或已安装 curl),并且已配置 API 密钥,如快速入门中所示。
使用缓存生成内容
以下示例展示了如何使用缓存的系统说明和视频文件生成内容。
import os
import pathlib
import requests
import time
from google import genai
from google.genai import types
# Get your API key from https://aistudio.google.com/app/apikey
# Put it in a "GOOGLE_API_KEY" environment variable.
# For more details, see
# https://github.com/google-gemini/cookbook/blob/main/quickstarts/Authentication.ipynb
client = genai.Client()
# Download video file
url = 'https://storage.googleapis.com/generativeai-downloads/data/Sherlock_Jr_FullMovie.mp4'
path_to_video_file = pathlib.Path('Sherlock_Jr_FullMovie.mp4')
if not path_to_video_file.exists():
with path_to_video_file.open('wb') as wf:
response = requests.get(url, stream=True)
for chunk in response.iter_content(chunk_size=32768):
wf.write(chunk)
# Upload the video using the Files API
video_file = client.files.upload(file=path_to_video_file)
# Wait for the file to finish processing
while video_file.state.name == 'PROCESSING':
print('Waiting for video to be processed.')
time.sleep(2)
video_file = client.files.get(name=video_file.name)
print(f'Video processing complete: {video_file.uri}')
# You must use an explicit version suffix. "-flash-001", not just "-flash".
model='models/gemini-1.5-flash-001'
# Create a cache with a 5 minute TTL
cache = client.caches.create(
model=model,
config=types.CreateCachedContentConfig(
display_name='sherlock jr movie', # used to identify the cache
system_instruction=(
'You are an expert video analyzer, and your job is to answer '
'the user\'s query based on the video file you have access to.'
),
contents=[video_file],
ttl="300s",
)
)
# Construct a GenerativeModel which uses the created cache.
response = client.models.generate_content(
model = model,
contents= (
'Introduce different characters in the movie by describing '
'their personality, looks, and names. Also list the timestamps '
'they were introduced for the first time.'),
config=types.GenerateContentConfig(cached_content=cache.name)
)
print(response.usage_metadata)
# The output should look something like this:
#
# prompt_token_count: 696219
# cached_content_token_count: 696190
# candidates_token_count: 214
# total_token_count: 696433
print(response.text)
列出缓存
您无法检索或查看缓存的内容,但可以检索缓存元数据(name
、model
、display_name
、usage_metadata
、create_time
、update_time
和 expire_time
)。
如需列出所有已上传缓存的元数据,请使用 CachedContent.list()
:
for cache in client.caches.list():
print(cache)
如需提取某个缓存对象的元数据(如果您知道其名称),请使用 get
:
client.caches.get(name=name)
更新缓存
您可以为缓存设置新的 ttl
或 expire_time
。不支持更改缓存的任何其他内容。
以下示例展示了如何使用 client.caches.update()
更新缓存的 ttl
。
from google import genai
from google.genai import types
client.caches.update(
name = cache.name,
config = types.UpdateCachedContentConfig(
ttl='300s'
)
)
如需设置到期时间,它将接受 datetime
对象或 ISO 格式的日期时间字符串 (dt.isoformat()
,例如 2025-01-27T16:02:36.473528+00:00
)。时间必须包含时区(datetime.utcnow()
不附加时区,datetime.now(datetime.timezone.utc)
会附加时区)。
from google import genai
from google.genai import types
import datetime
# You must use a time zone-aware time.
in10min = datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(minutes=10)
client.caches.update(
name = cache.name,
config = types.UpdateCachedContentConfig(
expire_time=in10min
)
)
删除缓存
缓存服务提供了删除操作,用于手动从缓存中移除内容。以下示例展示了如何删除缓存:
client.caches.delete(cache.name)
其他注意事项
使用上下文缓存时,请注意以下事项:
- 上下文缓存的输入令牌数下限为 32,768,输入令牌数上限与给定模型的上限相同。(如需详细了解如何统计令牌,请参阅令牌指南。)
- 该模型不会区分缓存的令牌和常规输入令牌。缓存的内容只是提示的前缀。
- 上下文缓存没有特殊的速率或使用限制;适用
GenerateContent
的标准速率限制,并且令牌限制包括缓存的令牌。 - 缓存的令牌数量会在缓存服务的创建、获取和列表操作的
usage_metadata
中返回,在使用缓存时也会在GenerateContent
中返回。