gemini

package
v0.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 26, 2026 License: None detected not legal advice Imports: 0 Imported by: 0

Documentation

Overview

Package gemini 实现 Google Gemini API 的 ModelProvider.

支持两种接入方式:

  1. Google AI Studio - 通过 API key(?key=... 查询参数)访问 入口:https://aistudio.google.com/,免费额度慷慨,适合开发测试
  2. Vertex AI - 通过 GCP OAuth2 Bearer token 访问 入口:https://cloud.google.com/vertex-ai,企业级 SLA,支持 VPC Service Controls

两种模式由 Config.BearerToken 区分:

  • BearerToken 为空 → Google AI Studio(使用 APIKey)
  • BearerToken 非空 → Vertex AI(APIKey 被忽略)

升华改进(ELEVATED): 早期实现 无 Gemini provider--首次实现. Gemini 是第三种 SSE 协议,与 Anthropic / OpenAI 均不同: 每块是完整 GenerateContentResponse,thinking 用 thought: true 标记, functionCall 完整到达无需拼接 arguments. wire.GeminiClient 封装了全部协议差异,本包只负责配置工厂和维护静态模型表.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// APIKey 是 Google AI Studio API 密钥(从 aistudio.google.com 获取).
	// Vertex AI 模式下此字段被忽略.
	APIKey string

	// BearerToken 是 Vertex AI 的 GCP OAuth2 Bearer token.
	// 非空时切换为 Vertex AI 模式,APIKey 不再使用.
	// 生产环境建议用 google.golang.org/api/oauth2 自动刷新 token,
	// 但引擎核心不引入外部依赖,由调用方在外部刷新后注入.
	BearerToken string

	// BaseURL 覆盖 API 端点.
	// Google AI 默认:https://generativelanguage.googleapis.com
	// Vertex AI 示例:https://us-central1-aiplatform.googleapis.com/v1/projects/my-project/locations/us-central1/publishers/google
	BaseURL string

	// ThinkingBudget 启用扩展思考(Gemini 2.5+,-exp 标签模型).
	// 0 = 禁用;建议值 1024–16384.
	// 通过 generationConfig.thinkingConfig.thinkingBudget 传递.
	ThinkingBudget int

	// HTTPClient 注入自定义 HTTP 客户端(代理,超时等).
	// nil = 使用默认 http.Client (带 Timeout 字段配置的 ResponseHeaderTimeout).
	// 非 nil 时 consumer 完全接管超时责任, 下面的 Timeout 字段被忽略.
	HTTPClient *http.Client

	// Timeout 限制"从请求发出到收到响应首字节"的时间.
	//
	// 通过 http.Transport.ResponseHeaderTimeout 实现, **不影响** SSE 流式响应的后续
	// body 读取 - 长流式回复 (2-5 分钟) 可以正常读完.
	//
	// 精妙之处(CLEVER): 不要误用 http.Client.Timeout - 那会把 SSE 流砍死.
	// 详见 internal/wire/gemini.go 和 anthropic.go §3.1 完整论证.
	//
	// 0 = 使用 defaultTimeout (60s, 适合 Gemini 云端 AI Studio / Vertex AI).
	// 仅当 HTTPClient 为 nil 时生效; 提供自定义 HTTPClient 时此字段被忽略.
	Timeout time.Duration

	// ModelOverrides 覆盖静态模型表.
	// 用于 Google 新发布但我们尚未维护的模型.
	ModelOverrides []flyto.ModelInfo
}

Config 是 Gemini provider 的配置.

func (Config) GoString

func (c Config) GoString() string

GoString 实现 fmt.GoStringer,防止 %#v 打印时泄露 APIKey 或 BearerToken. 升华改进(ELEVATED): Gemini 有双密钥路径(AI Studio APIKey vs Vertex BearerToken),两个都要遮蔽. 替代方案:<只遮蔽当前激活路径的密钥> - 当前两个都遮蔽,更安全且实现简单.

type Provider

type Provider struct {
	// contains filtered or unexported fields
}

Provider 是 Gemini ModelProvider 实现.

func New

func New(cfg Config) *Provider

New 创建 Gemini ModelProvider.

func (*Provider) Models

func (p *Provider) Models(_ context.Context) ([]flyto.ModelInfo, error)

Models 返回 Gemini 可用模型列表(静态表).

func (*Provider) Name

func (p *Provider) Name() string

Name 返回 provider 标识.

func (*Provider) Stream

func (p *Provider) Stream(ctx context.Context, req *flyto.Request) (<-chan flyto.Event, error)

Stream 向 Gemini API 发起流式请求.

内部使用 wire.GeminiClient,SSE 解析逻辑见 internal/wire/gemini.go.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL