Gemini 官方 SDK
以下为 Python 示例,Java / Go 等语言用法完全相同,只改 api_key 和 base_url 即可。
参考官方文档:ai.google.dev/gemini-api/docs
安装依赖
bash
pip install google-genai对话 + 思考过程
python
from google import genai
from google.genai import types
client = genai.Client(
api_key='sk-xxxxxx', # 你的令牌
http_options={
'base_url': 'https://www.dianlitoken.com' # 接口地址
}
)
response = client.models.generate_content(
model='gemini-3-pro-preview',
contents='你是谁',
config=types.GenerateContentConfig(
thinkingConfig=types.ThinkingConfig(
includeThoughts=True # 是否返回思考过程
)
)
)
for part in response.candidates[0].content.parts:
if not part.text:
continue
if part.thought:
print('Thought summary:')
print(part.text)
else:
print('Answer:')
print(part.text)思考过程的获取方式
response.candidates[0].content.parts 是一个数组,每个 part 可能是:
part.thought = True— 思考过程part.thought = False / undefined— 最终答案
流式请求
python
response = client.models.generate_content_stream(
model='gemini-3-pro-preview',
contents='解释一下量子纠缠'
)
for chunk in response:
if chunk.text:
print(chunk.text, end='', flush=True)多模态(图片输入)
python
import base64
with open('image.jpg', 'rb') as f:
image_data = base64.b64encode(f.read()).decode()
response = client.models.generate_content(
model='gemini-3-pro-preview',
contents=[
{'role': 'user', 'parts': [
{'text': '这张图里是什么?'},
{'inline_data': {'mime_type': 'image/jpeg', 'data': image_data}}
]}
]
)
print(response.text)下一步:在 客户端工具配置 把这些 SDK 集成到你常用的工具里。