Documentation
¶
Overview ¶
Package gemini 实现 Google Gemini LLM Provider
支持 Gemini API 和 Vertex AI 两种后端。
基础使用 ¶
provider, err := gemini.New(&gemini.Config{
APIKey: "your-api-key",
Model: "gemini-1.5-flash",
})
resp, err := provider.Complete(ctx, messages, opts)
Vertex AI 后端 ¶
provider, err := gemini.New(&gemini.Config{
Model: "gemini-1.5-flash",
VertexProject: "your-project",
VertexLocation: "us-central1",
VertexCredFile: "/path/to/credentials.json",
})
Thinking 模式 ¶
Gemini 2.5 系列支持 thinking 能力:
provider, err := gemini.New(&gemini.Config{
Model: "gemini-2.5-flash",
EnableThinking: true,
ThinkingBudget: 24576, // 最大 24K tokens
})
支持的模型 ¶
- gemini-2.5-pro: 最强模型,32K thinking tokens
- gemini-2.5-flash: 快速模型,24K thinking tokens
- gemini-2.5-flash-lite: 轻量模型,不支持 thinking
- gemini-2.0-flash: 旧版快速模型
- gemini-1.5-pro/flash: 旧版模型
Index ¶
- Constants
- type Client
- func (c *Client) BuildCompleteEndpoint() string
- func (c *Client) BuildRequest(messages []llm.Message, opts *llm.Options, stream bool) (map[string]any, error)
- func (c *Client) BuildStreamEndpoint() string
- func (c *Client) Close() error
- func (c *Client) Complete(ctx context.Context, messages []llm.Message, opts *llm.Options) (*llm.Response, error)
- func (c *Client) Stream(ctx context.Context, messages []llm.Message, opts *llm.Options) (<-chan *llm.StreamFrame, error)
- type Config
Constants ¶
View Source
const ( // DefaultBaseURL Gemini API 默认地址 DefaultBaseURL = "https://generativelanguage.googleapis.com/v1beta" // DefaultModel 默认模型 DefaultModel = "gemini-1.5-flash" // DefaultTimeout 默认超时时间 DefaultTimeout = 120 * time.Second // DefaultMaxTokens 默认最大输出 tokens DefaultMaxTokens = 8192 )
View Source
const ( ModelGemini25Pro = "gemini-2.5-pro" ModelGemini25Flash = "gemini-2.5-flash" ModelGemini25FlashLite = "gemini-2.5-flash-lite" ModelGemini20Flash = "gemini-2.0-flash" ModelGemini15Pro = "gemini-1.5-pro" ModelGemini15Flash = "gemini-1.5-flash" )
模型常量
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
*core.BaseClient
// contains filtered or unexported fields
}
Client Gemini LLM 客户端
实现 llm.Provider 接口,支持同步和流式完成。
架构设计:
- 嵌入 core.BaseClient 复用通用逻辑
- 保留 transformer 用于 buildRequest
- 支持 Gemini API 和 Vertex AI 两种模式
func (*Client) BuildCompleteEndpoint ¶ added in v0.3.260116
BuildCompleteEndpoint 构建 Complete 端点 实现 core.EndpointBuilder 接口
func (*Client) BuildRequest ¶ added in v0.3.260116
func (c *Client) BuildRequest(messages []llm.Message, opts *llm.Options, stream bool) (map[string]any, error)
BuildRequest 实现 core.RequestBuilder 接口
func (*Client) BuildStreamEndpoint ¶ added in v0.3.260116
BuildStreamEndpoint 构建 Stream 端点 实现 core.EndpointBuilder 接口
type Config ¶
type Config struct {
// APIKey Gemini API 密钥(Gemini API 后端必需)
APIKey string
// BaseURL API 基础地址,默认 https://generativelanguage.googleapis.com/v1beta
BaseURL string
// Model 默认模型名称
Model string
// Timeout 请求超时时间,默认 120 秒
Timeout time.Duration
// Headers 额外的请求头
Headers map[string]string
// Thinking 配置(Gemini 2.5 系列)
EnableThinking bool // 启用 thinking 模式
ThinkingBudget int32 // thinking tokens 预算,0 表示动态
// Vertex AI 配置
VertexProject string // GCP 项目 ID
VertexLocation string // GCP 区域,默认 us-central1
VertexCredFile string // 服务账户凭证文件路径
}
Config 客户端配置
func (*Config) BuildHeaders ¶ added in v0.3.260116
BuildHeaders 构建请求头 Gemini 不需要在请求头中包含 API key(使用 URL 参数或 Vertex AI 认证)
func (*Config) GetDefaults ¶ added in v0.3.260116
GetDefaults 获取默认值
func (*Config) ProviderName ¶ added in v0.3.260116
ProviderName 返回 Provider 名称
Click to show internal directories.
Click to hide internal directories.