gemini

package
v0.7.260125 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2026 License: MIT Imports: 7 Imported by: 0

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

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 New

func New(config *Config) (*Client, error)

New 创建新的 Gemini 客户端

参数 config 必须包含 APIKey(Gemini API)或 VertexProject(Vertex AI)。

func (*Client) BuildCompleteEndpoint added in v0.3.260116

func (c *Client) BuildCompleteEndpoint() string

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

func (c *Client) BuildStreamEndpoint() string

BuildStreamEndpoint 构建 Stream 端点 实现 core.EndpointBuilder 接口

func (*Client) Close

func (c *Client) Close() error

Close 关闭客户端

实现 llm.Provider 接口。当前实现为空操作。

func (*Client) Complete

func (c *Client) Complete(ctx context.Context, messages []llm.Message, opts *llm.Options) (*llm.Response, error)

Complete 同步完成

实现 llm.Provider 接口。发送消息到 Gemini 并等待完整响应。

func (*Client) Stream

func (c *Client) Stream(ctx context.Context, messages []llm.Message, opts *llm.Options) (<-chan *llm.StreamFrame, error)

Stream 流式完成

实现 llm.Provider 接口。返回一个 channel,逐块接收 Gemini 响应。

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

func (c *Config) BuildHeaders() map[string]string

BuildHeaders 构建请求头 Gemini 不需要在请求头中包含 API key(使用 URL 参数或 Vertex AI 认证)

func (*Config) GetDefaults added in v0.3.260116

func (c *Config) GetDefaults() (string, string, time.Duration)

GetDefaults 获取默认值

func (*Config) GetModel added in v0.3.260116

func (c *Config) GetModel() string

GetModel 返回模型名称(辅助方法)

func (*Config) ProviderName added in v0.3.260116

func (c *Config) ProviderName() string

ProviderName 返回 Provider 名称

func (*Config) Validate added in v0.3.260116

func (c *Config) Validate() error

Validate 验证配置

Jump to

Keyboard shortcuts

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