Gemini 2.5 Pro 使用指南

Gemini 2.5 Pro 是 Google 最强大的 AI 模型,拥有 100 万 token 上下文窗口、内置思维链推理和出色的编程能力。本文详细介绍其核心特性和使用方法。

Gemini 2.5 Pro 核心优势

100 万 Token 上下文

Gemini 2.5 Pro 的超长上下文是其最大差异化优势。实际应用场景:

from google import genai

client = genai.Client(api_key="YOUR_API_KEY")

# 读取大量源文件
import os
code_files = []
for root, dirs, files in os.walk("./src"):
    for f in files:
        if f.endswith(('.py', '.js', '.ts')):
            path = os.path.join(root, f)
            with open(path) as fh:
                code_files.append(f"=== {path} ===\n{fh.read()}")

all_code = "\n\n".join(code_files)

response = client.models.generate_content(
    model="gemini-2.5-pro",
    contents=f"分析以下代码库的架构,找出潜在的安全问题:\n\n{all_code}"
)
print(response.text)

Thinking Mode(思维链推理)

Gemini 2.5 Pro 默认启用 thinking mode,模型会在回答前进行内部推理。你可以控制 thinking budget:

from google import genai
from google.genai import types

client = genai.Client(api_key="YOUR_API_KEY")

response = client.models.generate_content(
    model="gemini-2.5-pro",
    contents="证明根号2是无理数",
    config=types.GenerateContentConfig(
        thinking_config=types.ThinkingConfig(
            thinking_budget=10000  # 最多 10000 个思考 token
        )
    )
)

# 查看思考过程
for part in response.candidates[0].content.parts:
    if part.thought:
        print("思考过程:", part.text)
    else:
        print("最终回答:", part.text)
提示:Thinking token 的价格($3.50/百万)低于输出 token($10/百万),适度使用 thinking mode 可以提高复杂任务的回答质量,且成本可控。

编程能力

Gemini 2.5 Pro 在编程任务上表现出色:

# 通过 OpenAI 兼容接口使用 Gemini 2.5 Pro
from openai import OpenAI

client = OpenAI(
    api_key="your-relay-key",
    base_url="https://claude4u.com/v1"
)

response = client.chat.completions.create(
    model="gemini-2.5-pro",
    messages=[{
        "role": "user",
        "content": "实现一个高性能的 LRU Cache,支持 O(1) 的 get 和 put 操作"
    }]
)
print(response.choices[0].message.content)

国内使用 Gemini 2.5 Pro

提示:轻舟 AI(claude4u.com)支持 Gemini 2.5 Pro 中转,国内直连,自动处理 503 过载错误。通过 OpenAI 兼容接口调用,无缝集成现有项目。

使用建议

注意:100 万 token 的输入虽然强大,但大量输入也意味着更高的成本。建议只输入与任务相关的内容,避免不必要的 token 消耗。

Start Using 轻舟 AI

Stable, fast AI API relay — supports Claude, OpenAI, Gemini and more

Sign Up Now