项目预览
项目数据
项目介绍
Star 增长趋势
本站每日记录该项目 GitHub 星标变化,观测越久曲线越完整
数据来源:GitHub API 逐日快照 · 收录于 2026-10-19 · 当前星标 ★ 4111
使用教程
ℹ 本教程由 AI 依据该项目 README 整理,个别步骤可能需要按你的环境微调。
环境要求
- 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
同类项目推荐
reflex
开源
纯 Python 写 Web 应用,不写一行前端代码
️ Web apps in pure Python
Graphite
开源
把多个 AI 模型拖进节点图,边想边画边出结果
A private, modular node based AI workspace for thinking and creation. Seamlessly con···
skills
开源
把工程经验写成 AI 能直接用的技能文件
Skills for Real Engineers. Straight from my .agents directory.
langchain-skills
开源
用积木式技能快速拼出 LangChain 智能体
ai-agents-laravel
开源
在 Laravel 中快速构建 AI 代理,几行代码接入 Claude/GPT。
Build AI Agents for popular LLMs quick and easy in Laravel
local-rag-researcher-deepseek
开源
本地跑通深度研究,私密数据不泄露,自动生成报告
Local RAG researcher agent built using Langgraph, DeepSeek R1 and Ollama
translator
开源
一个层,连接所有智能体、工具和 API,让集成不再头疼。
layer that lets you connect any agent, any tool, any api together.
dsh-codex-connect
开源
在 DeepSeek Harness 里用 ChatGPT 账号直接调 Codex 和画图
Use openai-codex models and image generation via ChatGPT OAuth for DeepSeek Harness.
streamdown
开源
AI 流式输出 Markdown,边生成边稳定渲染不闪烁
A drop-in replacement for react-markdown, designed for AI-powered streaming.
