项目预览
项目数据
项目介绍
Star 增长趋势
本站每日记录该项目 GitHub 星标变化,观测越久曲线越完整
技术标签
使用教程
—
环境要求
- Python 环境(能运行 pip 即可,本项目为纯 Python 库)
- 有效的 Gemini API Key(使用 Gemini Developer API 时必备)
- 使用 Agent Platform 时需具备 GCP 项目 ID 与可用区域(location)
- 如需使用 MCP 工具,需额外安装 mcp 包
安装与启动步骤
-
1安装 SDK
在 Python 环境中用 pip 安装官方库 google-genai;用 uv 管理环境时可用 uv pip 安装
pip install google-genai -
2准备 API Key
使用 Gemini Developer API 时需准备 API Key,后续以 api_key 参数传给客户端
-
3初始化客户端
导入 genai 后创建 Client,Developer API 传 api_key,企业平台传 enterprise、project、location
from google import genai # Only run this block for Gemini Developer API client = genai.Client(api_key='GEMINI_API_KEY') -
4使用 Agent Platform
如走 Gemini 企业/代理平台,改用 enterprise=True 并填写项目 ID 与区域,不再传 api_key
from google import genai # Only run this block for Agent Platform client = genai.Client( enterprise=True, project='your-project-id', location='global' ) -
5安装 MCP 依赖
只有在 Agent Platform 上使用 MCP 服务器工具时才需要安装 mcp 包
pip install mcp -
6运行调用示例
在 generate_content 的 tools 里放入 McpServer 并异步调用,最后 asyncio.run 启动事件循环
import asyncio from google import genai from google.genai import types PROJECT_ID="your-gcp-project" LOCATION="your-location" client = genai.Client(enterprise=True, project=PROJECT_ID, location=LOCATION) async def agent_platform_mcp(): response = await client.aio.models.generate_content( model="gemini-flash-latest", contents=f"List my endpoints in {LOCATION} for my {PROJECT_ID} project.", config = types.GenerateContentConfig( tools=[ types.Tool( mcp_servers=[ types.McpServer(name='endpoints') ] ) ] ) ) if response.text: print(response.text) asyncio.run(agent_platform_mcp())
关键配置
| 配置项 | 必填 | 说明 | 示例 |
|---|---|---|---|
api_key | 是 | Gemini Developer API 的密钥,仅在使用开发者 API 时传入 | GEMINI_API_KEY |
enterprise | 否 | 设为 True 表示使用 Agent Platform 后端 | True |
project | 否 | Agent Platform 模式下使用的 GCP 项目 ID | your-gcp-project |
location | 否 | Agent Platform 模式下使用的区域 | global |
model | 否 | generate_content 调用的模型名 | gemini-flash-latest |
mcp_servers | 否 | Agent Platform 下要接入的 MCP 服务器列表 | [types.McpServer(name='endpoints')] |
如何确认成功
运行示例脚本不报错,且打印出模型返回的文本(response.text)即为成功。
常见问题
Q:安装命令用 pip 还是 uv?
A:两者均可,README 给出 pip install google-genai 和 uv pip install google-genai,按当前环境选择其一。
Q:Developer API 与 Agent Platform 初始化有什么区别?
A:开发者 API 只需 api_key;Agent Platform 需传 enterprise=True 并填写 project 与 location,不传 api_key。
Q:使用 MCP 工具报缺包怎么办?
A:需先安装 mcp 包,README 给出的命令为 pip install mcp。
Q:如何避免升级后接口行为变化?
A:README 建议将 SDK 版本锁定在 < 3.0.0,因为下个大版本会调整自动函数调用(AFC)等行为。
注意事项
- 下个主要版本会调整自动函数调用(AFC):Models.generate_content 将不再支持直接触发 AFC,需改用 Chats 模块。
- Live.send、Live.start_stream、LiveConnectConfig.generation_config 等接口将被移除,新代码请使用替代方法。
- generate_videos 的 prompt/text/image 参数将改为 source 参数,GenerationConfigThinkingConfig 将改为 ThinkingConfig。
- 为避免意外升级影响线上服务,建议将 google-genai 版本锁定在 3.0.0 以下。
核心亮点
- Google 官方维护,新模型和新能力(多模态、函数调用、缓存)跟进及时
- 同时支持 Gemini API 与 Vertex AI 两套后端,同步/异步接口一致
- 内置流式输出、结构化输出和工具调用封装,减少手写请求样板代码
不足之处
- 强绑定 Google 生态,无法切换其他厂商模型
- 部分新特性文档与示例更新节奏不一致,社区问答积累少于第三方库
适用场景
- Python 后端服务中集成 Gemini 做内容生成与摘要
- 构建带函数调用的 Agent 或工具调用流程
- 批量生成文本嵌入用于检索与语义搜索
替代项目
openai-python、langchain、litellm
同类项目推荐
dsh-our-free-model
开源
装个插件就能免密钥白嫖前沿大模型,不限量
在 dsh 里装上这个插件即可,无需登录、注册或填 API Key,就能使用包括 DeepSeek V4.1 Fl···
ComfyUI-sol-attn
开源
在 ComfyUI 中让视频生成注意力更快更省显存
NVIDIA Sol-Attn for ComfyUI / Triton kernel on SM89 - SM121, with zero-copy MiniMax ···
langchain
开源
组装 AI 应用的乐高积木,从想法到上线不换工具
The agent engineering platform.
transformers
开源
全球最大的模型仓库全家桶,想用的模型一把抓
Transformers: the model-definition framework for state-of-the-art machine learning m···
