Google 发布 Gemini 4 系列模型,不支持显式上下文缓存
来源标题:Get Started with Gemini 4: Explicit Context Caching Not Supported
Google 推出 Gemini 4 模型家族,包含 Gemini 4 Hydrogen、Gemini 4 Carbon 和 Gemini 4 Argon 三款模型,但该系列不支持显式上下文缓存。官方指南说明了首次请求方式、新特性以及从 Gemini 3.8 Flash 或 Gemini 3.1 Pro 迁移时的变化,并提示需先通过 API key 或 ADC 完成认证。
以下正文同步自 Hacker News 最新,版权归原站所有,已转换为易读排版。
The Gemini 4 model family includes the following models:
Gemini 4 Hydrogen (gemini-4-hydrogen): Built for speed and efficiency across everyday coding and agentic workflows, multimodal analysis, and tool use.
Gemini 4 Carbon (gemini-4-carbon): Google's frontier model that excels at deep work in complex, real-world tasks across domains including coding, finance, legal, cybersecurity, and science.
Gemini 4 Argon (gemini-4-argon): Google's frontier model that excels at deep work in complex, real-world tasks across domains including coding, finance, legal, cybersecurity, and science.
This guide shows you how to make your first Gemini 4 request, describes what's new in Gemini 4, and explains what changes when you migrate from Gemini 3.8 Flash or Gemini 3.1 Pro. For model-specific details, see the developer's guide to Gemini 4 Hydrogen, the developer's guide to Gemini 4 Carbon, and the developer's guide to Gemini 4 Argon.
Before you begin, you must authenticate to Gemini Enterprise Agent Platform using an API key or application default credentials (ADC). See authentication methods for more information.
Upgrade to the latest google-genai SDK (>= 2.34.0 for Python, @google/genai >= 2.32.0 for JavaScript/TypeScript):
pip install --upgrade "google-genai>=2.34.0"
Set environment variables
Replace GOOGLE_CLOUD_PROJECT with your Google Cloud project ID. Gemini 4 Hydrogen and Gemini 4 Carbon are available on the global endpoint and in the us and eu multi-regions (Gemini 4 Argon is also available in europe-west2 and asia-southeast1):
export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT export GOOGLE_CLOUD_LOCATION=global export GOOGLE_GENAI_USE_ENTERPRISE=True
Make your first request
Python
from google import genai
client = genai.Client()
response = client.models.generate_content( model="gemini-4-hydrogen", # Or "gemini-4-carbon" contents="How does AI work?", ) print(response.text)
REST
curl -X POST
-H "Authorization: Bearer $(gcloud auth print-access-token)"
-H "Content-Type: application/json"
https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/publishers/google/models/gemini-4-carbon:generateContent
-d '{
"contents": {
"role": "USER",
"parts": { "text": "How does AI work?" }
}
}'
What's new in Gemini 4
Gemini 4 models introduce the following capabilities and API changes:
Gemini 4 models have a unified 1,048,576-token limit that includes both input and output tokens (where output includes thinking, function calls, and the final response). Output can use up to 1,048,576 tokens minus your input tokens. By default, a single synchronous request generates up to 65,536 output tokens (or 32,768 tokens for non-streaming direct HTTP requests on 4 Carbon and 4 Argon).
When a single turn requires more than 65,536 output tokens (or 32,768 tokens for non-streaming foreground calls on 4 Carbon and 4 Argon), such as formal proofs, complex math or science problems, or multi-file code generation, Gemini 4 models support long decodes up to 1,048,576 tokens (minus your input tokens) on both the Interactions API and GenerateContent (v1beta1). With the latest Google Gen AI SDK (google-genai >= 2.34.0 for Python, @google/genai >= 2.32.0 for JavaScript/TypeScript, Go >= 1.77.0, Java >= 1.80.0), setting max_output_tokens greater than 65,536 (or greater than 32,768 for non-streaming calls on 4 Carbon and 4 Argon) automatically follows continuation_token across segments to return a single response or stream. On the Interactions API, you can also run server-managed asynchronous long decodes with background=true or chain continuation_token manually in stored (store=true) or stateless (store=false) loops.
For code samples, SDK version requirements, and implementation rules, see the developer's guide to Gemini 4 Hydrogen, the developer's guide to Gemini 4 Carbon, or the developer's guide to Gemini 4 Argon.
Use thinking_level to control how much the model reasons before it responds. Gemini 4 models support LOW, MEDIUM (default on gemini-4-carbon and gemini-4-argon), and HIGH (default on gemini-4-hydrogen, gemini-4-carbon-cyber-permissive, and gemini-4-argon-cyber-permissive). gemini-4-hydrogen also supports XHIGH for extended reasoning depth:
from google import genai from google.genai import types
client = genai.Client()
response = client.models.generate_content( model="gemini-4-carbon", contents="Design a lock-free ring buffer in Rust for multi-producer, single-consumer telemetry ingestion.", config=types.GenerateContentConfig( thinking_config=types.ThinkingConfig( thinking_level=types.ThinkingLevel.HIGH, ), ), ) print(response.text)
Implicit context caching
Gemini 4 models support implicit caching for shared prompt prefixes of 4,096 tokens or more, with durable caching enabled by default. To benefit from it, put static content, such as a code repository or long documents, at the start of your prompt.
Agentic video understanding is generally available. It's off by default: uploaded videos use media_processing="static". To turn it on, set media_processing to "agentic".
Gemini 4 models ignore custom values for temperature, top_p, and top_k, as Gemini 3 models do. To control output, use thinking_level and structured output (response_schema).
When migrating from Gemini 3.8 Flash to Gemini 4 Hydrogen, or from Gemini 3.1 Pro to Gemini 4 Carbon or Gemini 4 Argon, account for the following behavioral differences in Gemini 4 models:
To migrate from gemini-3.8-flash or gemini-3.1-pro-preview, do the following:
Change the model ID to gemini-4-hydrogen (from gemini-3.8-flash) or gemini-4-carbon or gemini-4-argon (from gemini-3.1-pro-preview) and upgrade to the latest Google Gen AI SDK (google-genai >= 2.34.0 for Python, @google/genai >= 2.32.0 for JavaScript/TypeScript).
Account for the unified 1,048,576-token input and output limit and the 65,536-token single-request output cap (32,768 tokens for non-streaming foreground calls on 4 Carbon and 4 Argon), or configure long decodes for larger outputs on the Interactions API or GenerateContent.
Replace thinking_budget and thinking_level="MINIMAL" with thinking_level set to LOW, MEDIUM, HIGH, or XHIGH (XHIGH is supported on gemini-4-hydrogen).
Remove explicit cached_content references.
Preserve thought_signature across all previous turns (both functionCall and final text parts), keep it only on the first functionCall part of parallel tool-calling turns, and set prefix_mismatch_behavior="DROP" if your application compacts or edits prior turns.
Remove temperature, top_p, and top_k, which have no effect.
Gemini 4 Carbon for cyber and Gemini 4 Argon Cyber Permissive are versions of Gemini 4 Carbon and Gemini 4 Argon for defensive cybersecurity use cases, available behind an allowlist. For differences and access, see Gemini 4 Carbon for cyber and Gemini 4 Argon Cyber Permissive.
Read the developer's guide to Gemini 4 Hydrogen, the developer's guide to Gemini 4 Carbon, and the developer's guide to Gemini 4 Argon.
See the Gemini 4 Hydrogen model page, the Gemini 4 Carbon model page, and the Gemini 4 Argon model page for specifications, supported features, and locations.
Try the Intro to Gemini 4 Carbon and Intro to Gemini 4 Argon notebook tutorials.
正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。