跳转到主要内容

推荐 endpoint

最小请求

{
  "model": "gemini-2.5-flash",
  "systemInstruction": {
    "parts": [{ "text": "You are a concise assistant." }]
  },
  "contents": [
    {
      "role": "user",
      "parts": [{ "text": "用三句话介绍 Api.Go。" }]
    }
  ]
}

cURL 示例

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" \
  -d '{
    "systemInstruction": {
      "parts": [{ "text": "You are a concise assistant." }]
    },
    "contents": [
      {
        "role": "user",
        "parts": [{ "text": "用三句话介绍 Api.Go。" }]
      }
    ]
  }'

Python 示例

from google import genai
from google.genai import types

client = genai.Client(api_key="<GEMINI_API_KEY>")

response = client.models.generate_content(
    model="gemini-2.5-flash",
    contents="用三句话介绍 Api.Go。",
    config=types.GenerateContentConfig(
        system_instruction="You are a concise assistant."
    ),
)

print(response.text)

Node.js 示例

import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });

const response = await ai.models.generateContent({
  model: "gemini-2.5-flash",
  contents: "用三句话介绍 Api.Go。",
  config: {
    systemInstruction: "You are a concise assistant."
  }
});

console.log(response.text);

最佳实践

  • 低延迟对话优先从 gemini-2.5-flash 这类模型起步
  • parts 结构保持独立,后续接图片、文件时不用重构请求模型
  • 如果你不需要额外思考成本,可以在 2.5 Flash 上把 thinkingBudget 设成 0