gemini

package module
v0.1.33 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2026 License: Apache-2.0 Imports: 17 Imported by: 19

README

Google Gemini

A Google Gemini implementation for Eino that implements the ToolCallingChatModel interface. This enables seamless integration with Eino's LLM capabilities for enhanced natural language processing and generation.

Features

  • Implements github.com/cloudwego/eino/components/model.Model
  • Easy integration with Eino's model system
  • Configurable model parameters
  • Support for chat completion
  • Support for streaming responses
  • Custom response parsing support
  • Flexible model configuration
  • Caching support for generated responses
  • Automatic handling of duplicate tool call IDs

Important Notes

Tool Call ID Handling

Gemini's API does not provide tool call IDs in its responses. To ensure compatibility with the Eino framework and enable proper tool execution tracking, this implementation automatically generates a unique UUID (v4) for each tool call.

ID Generation:

  • Each tool call receives a freshly generated UUID
  • UUIDs are globally unique across all responses and sessions
  • Format: Standard UUID v4 (e.g., 550e8400-e29b-41d4-a716-446655440000)

Example:

// If Gemini returns multiple calls to "get_weather" for different cities:
// Tool Call 1: ID = "550e8400-e29b-41d4-a716-446655440000", Args = {"city": "Paris"}
// Tool Call 2: ID = "6ba7b810-9dad-11d1-80b4-00c04fd430c8", Args = {"city": "London"}
// Tool Call 3: ID = "7c9e6679-7425-40de-944b-e07fc1f90ae7", Args = {"city": "Tokyo"}

Benefits:

  • Session-wide uniqueness: UUIDs prevent ID collisions across multiple model calls
  • Standard format: Compatible with industry-standard tool tracking systems
  • Simplified implementation: No need to maintain state between calls

This ensures that each tool call has a globally unique identifier, which is essential for proper tool execution tracking and response handling in complex agent workflows with multiple model interactions.

Installation

go get github.com/cloudwego/eino-ext/components/model/gemini@latest

Quick start

Here's a quick example of how to use the Gemini model:

package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"google.golang.org/genai"

	"github.com/cloudwego/eino-ext/components/model/gemini"
	"github.com/cloudwego/eino/schema"
)

func main() {
	apiKey := os.Getenv("GEMINI_API_KEY")
	baseURL := os.Getenv("GEMINI_BASE_URL")

	ctx := context.Background()
	clientConfig := &genai.ClientConfig{
		APIKey: apiKey,
	}
	// Optional: route requests to a custom Gemini-compatible endpoint.
	if baseURL != "" {
		clientConfig.HTTPOptions = genai.HTTPOptions{
			BaseURL: baseURL,
		}
	}
	client, err := genai.NewClient(ctx, clientConfig)
	if err != nil {
		log.Fatalf("NewClient of gemini failed, err=%v", err)
	}

	cm, err := gemini.NewChatModel(ctx, &gemini.Config{
		Client: client,
		Model:  "gemini-2.5-flash",
		ThinkingConfig: &genai.ThinkingConfig{
			IncludeThoughts: true,
			ThinkingBudget:  nil,
		},
	})
	if err != nil {
		log.Fatalf("NewChatModel of gemini failed, err=%v", err)
	}

	// If you are using a model that supports image understanding (e.g., gemini-2.5-flash-image-preview),
	// you can provide both image and text input like this:
	/*
		image, err := os.ReadFile("./path/to/your/image.jpg")
		if err != nil {
			log.Fatalf("os.ReadFile failed, err=%v\n", err)
		}

		imageStr := base64.StdEncoding.EncodeToString(image)

		resp, err := cm.Generate(ctx, []*schema.Message{
			{
				Role: schema.User,
				UserInputMultiContent: []schema.MessageInputPart{
					{
						Type: schema.ChatMessagePartTypeText,
						Text: "What do you see in this image?",
					},
					{
						Type: schema.ChatMessagePartTypeImageURL,
						Image: &schema.MessageInputImage{
							MessagePartCommon: schema.MessagePartCommon{
								Base64Data: &imageStr,
								MIMEType:   "image/jpeg",
							},
							Detail: schema.ImageURLDetailAuto,
						},
					},
				},
			},
		})
	*/

	resp, err := cm.Generate(ctx, []*schema.Message{
		{
			Role:    schema.User,
			Content: "What is the capital of France?",
		},
	})
	if err != nil {
		log.Fatalf("Generate error: %v", err)
	}

	fmt.Printf("Assistant: %s\n", resp.Content)
	if len(resp.ReasoningContent) > 0 {
		fmt.Printf("ReasoningContent: %s\n", resp.ReasoningContent)
	}
}

Set GEMINI_BASE_URL if you need to route request to custom Gemini-compatible endpoint

Configuration

The model can be configured using the gemini.Config struct:

type Config struct {
	// Client is the Gemini API client instance
	// Required for making API calls to Gemini
	Client *genai.Client

	// Model specifies which Gemini model to use
	// Examples: "gemini-pro", "gemini-pro-vision", "gemini-1.5-flash"
	Model string

	// MaxTokens limits the maximum number of tokens in the response
	// Optional. Example: maxTokens := 100
	MaxTokens *int

	// Temperature controls randomness in responses
	// Range: [0.0, 1.0], where 0.0 is more focused and 1.0 is more creative
	// Optional. Example: temperature := float32(0.7)
	Temperature *float32

	// TopP controls diversity via nucleus sampling
	// Range: [0.0, 1.0], where 1.0 disables nucleus sampling
	// Optional. Example: topP := float32(0.95)
	TopP *float32

	// TopK controls diversity by limiting the top K tokens to sample from
	// Optional. Example: topK := int32(40)
	TopK *int32

	// ResponseSchema defines the structure for JSON responses
	// Optional. Used when you want structured output in JSON format
	ResponseSchema *openapi3.Schema

	// EnableCodeExecution allows the model to execute code
	// Warning: Be cautious with code execution in production
	// Optional. Default: false
	EnableCodeExecution bool

	// SafetySettings configures content filtering for different harm categories
	// Controls the model's filtering behavior for potentially harmful content
	// Optional.
	SafetySettings []*genai.SafetySetting

	ThinkingConfig *genai.ThinkingConfig

	// ResponseModalities specifies the modalities the model can return.
	// Optional.
	ResponseModalities []
	
	MediaResolution genai.MediaResolution

	// Cache controls prefix cache settings for the model.
	// Optional. used to CreatePrefixCache for reused inputs.
	Cache *CacheConfig

	// ResponseLogprobs controls whether to return the log probabilities of the
	// tokens chosen by the model at each step. When enabled, logprobs are
	// populated in Message.ResponseMeta.LogProbs.
	// Optional. Default: false.
	ResponseLogprobs bool

	// Logprobs specifies the number of top candidate tokens whose log
	// probabilities are returned at each generation step.
	// Optional. Only takes effect when ResponseLogprobs is true.
	Logprobs *int32
}

// CacheConfig controls prefix cache settings for the model.
type CacheConfig struct {
	// TTL specifies how long cached resources remain valid (now + TTL).
	TTL time.Duration `json:"ttl,omitempty"`
	// ExpireTime sets the absolute expiration timestamp for cached resources.
	ExpireTime time.Time `json:"expireTime,omitempty"`
}

Caching

This component supports two caching strategies to improve latency and reduce API calls:

  • Explicit caching (prefix cache): Build a reusable context from the system instruction, tools, and messages. Use CreatePrefixCache to create the cache and pass its name with gemini.WithCachedContentName(...) in subsequent requests. Configure TTL and absolute expiry via CacheConfig (TTL, ExpireTime). When a cached content is used, the request omits system instruction and tools and relies on the cached prefix.
  • Implicit caching: Managed by Gemini itself. The service may reuse prior requests or responses automatically. Expiry and reuse are controlled by Gemini and cannot be configured.
toolInfoList := []*schema.ToolInfo{
	{
		Name:        "tool_a",
		Desc:        "desc",
		ParamsOneOf: schema.NewParamsOneOfByParams(map[string]*schema.ParameterInfo{}),
	},
}
cacheInfo, _ := cm.CreatePrefixCache(ctx, []*schema.Message{
		{
			Role: schema.System,
			Content: `aaa`,
		},
		{
			Role: schema.User,
			Content: `bbb`,
		},
	}, model.WithTools(toolInfoList))


msg, err := cm.Generate(ctx, []*schema.Message{
		{
			Role:    schema.User,
			Content: "give a very short summary about this transcript",
		},
	}, gemini.WithCachedContentName(cacheInfo.Name))

The example above shows how to create a prefix cache and reuse it in a follow-up call.

Logprobs

Gemini can return the log probabilities of the tokens chosen at each generation step, plus the top-K candidate tokens at each step.

  • Enable via Config.ResponseLogprobs = true (or per-call gemini.WithResponseLogprobs(true)).
  • Configure the number of top candidates via Config.Logprobs (or per-call gemini.WithLogprobs(k)).
  • Results are populated in message.ResponseMeta.LogProbs:
    • LogProbs.Content[i].Token / LogProb — chosen token at step i
    • LogProbs.Content[i].TopLogProbs — top-K candidate tokens at step i
topK := int32(5)
cm, _ := gemini.NewChatModel(ctx, &gemini.Config{
    Client:           client,
    Model:            "gemini-2.0-flash",
    ResponseLogprobs: true,
    Logprobs:         &topK,
})

// Per-call override is also supported:
msg, _ := cm.Generate(ctx, input,
    gemini.WithResponseLogprobs(true),
    gemini.WithLogprobs(3),
)

if lp := msg.ResponseMeta.LogProbs; lp != nil {
    for _, item := range lp.Content {
        fmt.Printf("token=%s logprob=%f\n", item.Token, item.LogProb)
    }
}

Note: Logprobs only takes effect when ResponseLogprobs is true. To disable logprobs at call time, use gemini.WithResponseLogprobs(false).

Examples

See the following examples for more usage:

For More Details

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetGroundMetadata

func GetGroundMetadata(m *schema.Message) *genai.GroundingMetadata

func GetInputVideoMetaData

func GetInputVideoMetaData(part *schema.MessageInputVideo) *genai.VideoMetadata

func GetThoughtSignatureFromExtra

func GetThoughtSignatureFromExtra(extra map[string]any) ([]byte, bool)

GetThoughtSignatureFromExtra tries to read thought_signature from an Extra map.

thought_signature should be read from:

  • message.AssistantGenMultiContent[i].Extra: thought_signature on each generated output part
  • toolCall.Extra: thought_signature on toolCall
  • message.Extra: thought_signature on generated content (legacy, only used when message.AssistantGenMultiContent are absent)

The returned bool indicates whether thought_signature key exists in Extra. The returned []byte is the thought signature if available

func GetVideoMetaData deprecated

func GetVideoMetaData(part *schema.ChatMessageVideoURL) *genai.VideoMetadata

Deprecated: use GetInputVideoMetaData instead.

func SetInputVideoMetaData

func SetInputVideoMetaData(part *schema.MessageInputVideo, metaData *genai.VideoMetadata)

func SetMultiModalToolResultDisplayName

func SetMultiModalToolResultDisplayName(input schema.MessageInputPart, displayName string) schema.MessageInputPart

func SetVideoMetaData deprecated

func SetVideoMetaData(part *schema.ChatMessageVideoURL, metaData *genai.VideoMetadata)

Deprecated: use SetInputVideoMetaData instead.

func WithCachedContentName

func WithCachedContentName(name string) model.Option

WithCachedContentName the name of the content cached to use as context to serve the prediction. Format: cachedContents/{cachedContent}

func WithImageConfig

func WithImageConfig(cfg *genai.ImageConfig) model.Option

WithImageConfig sets the image generation configuration. Note: an error will be returned for a model that does not support the configuration options. Optional.

func WithLogprobs

func WithLogprobs(k int32) model.Option

WithLogprobs sets the number of top candidate tokens to return the log probabilities for at each generation step. Only takes effect when ResponseLogprobs is enabled.

func WithResponseJSONSchema

func WithResponseJSONSchema(s *jsonschema.Schema) model.Option

func WithResponseLogprobs

func WithResponseLogprobs(enable bool) model.Option

WithResponseLogprobs sets whether to return the log probabilities of the tokens chosen by the model at each step. This overrides Config.ResponseLogprobs.

func WithResponseModalities

func WithResponseModalities(m []GeminiResponseModality) model.Option

func WithThinkingConfig

func WithThinkingConfig(t *genai.ThinkingConfig) model.Option

func WithTopK

func WithTopK(k int32) model.Option

Types

type CacheConfig

type CacheConfig struct {
	// TTL specifies how long cached resources remain valid (now + TTL).
	TTL time.Duration `json:"ttl,omitempty"`
	// ExpireTime sets the absolute expiration timestamp for cached resources.
	ExpireTime time.Time `json:"expireTime,omitempty"`
}

CacheConfig controls prefix cache settings for the model.

type ChatModel

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

func NewChatModel

func NewChatModel(_ context.Context, cfg *Config) (*ChatModel, error)

NewChatModel creates a new Gemini chat model instance

Parameters:

  • ctx: The context for the operation
  • cfg: Configuration for the Gemini model

Returns:

  • model.ChatModel: A chat model interface implementation
  • error: Any error that occurred during creation

Example:

model, err := gemini.NewChatModel(ctx, &gemini.Config{
    Client: client,
    Model: "gemini-pro",
})

func (*ChatModel) BindForcedTools

func (cm *ChatModel) BindForcedTools(tools []*schema.ToolInfo) error

func (*ChatModel) BindTools

func (cm *ChatModel) BindTools(tools []*schema.ToolInfo) error

func (*ChatModel) CreatePrefixCache

func (cm *ChatModel) CreatePrefixCache(ctx context.Context, prefixMsgs []*schema.Message, opts ...model.Option) (
	*genai.CachedContent, error)

CreatePrefixCache assembles inputs the same as Generate/Stream and writes the final system instruction, tools, and messages into a reusable prefix cache.

func (*ChatModel) Generate

func (cm *ChatModel) Generate(ctx context.Context, input []*schema.Message, opts ...model.Option) (message *schema.Message, err error)

func (*ChatModel) GetType

func (cm *ChatModel) GetType() string

func (*ChatModel) IsCallbacksEnabled

func (cm *ChatModel) IsCallbacksEnabled() bool

func (*ChatModel) Stream

func (cm *ChatModel) Stream(ctx context.Context, input []*schema.Message, opts ...model.Option) (result *schema.StreamReader[*schema.Message], err error)

func (*ChatModel) WithTools

func (cm *ChatModel) WithTools(tools []*schema.ToolInfo) (model.ToolCallingChatModel, error)

type Config

type Config struct {
	// Client is the Gemini API client instance
	// Required for making API calls to Gemini
	Client *genai.Client

	// Model specifies which Gemini model to use
	// Examples: "gemini-pro", "gemini-pro-vision", "gemini-1.5-flash"
	Model string

	// MaxTokens limits the maximum number of tokens in the response
	// Optional. Example: maxTokens := 100
	MaxTokens *int

	// Temperature controls randomness in responses
	// Range: [0.0, 1.0], where 0.0 is more focused and 1.0 is more creative
	// Optional. Example: temperature := float32(0.7)
	Temperature *float32

	// TopP controls diversity via nucleus sampling
	// Range: [0.0, 1.0], where 1.0 disables nucleus sampling
	// Optional. Example: topP := float32(0.95)
	TopP *float32

	// TopK controls diversity by limiting the top K tokens to sample from
	// Optional. Example: topK := int32(40)
	TopK *int32

	// ResponseJSONSchema defines the structure for JSON responses
	// Optional. Used when you want structured output in JSON format
	ResponseJSONSchema *jsonschema.Schema

	// EnableCodeExecution allows the model to execute code
	// Warning: Be cautious with code execution in production
	// Optional. Default: false
	EnableCodeExecution bool

	EnableGoogleSearch          *genai.GoogleSearch
	EnableGoogleSearchRetrieval *genai.GoogleSearchRetrieval
	EnableComputerUse           *genai.ComputerUse
	EnableURLContext            *genai.URLContext
	EnableFileSearch            *genai.FileSearch
	EnableGoogleMaps            *genai.GoogleMaps

	// SafetySettings configures content filtering for different harm categories
	// Controls the model's filtering behavior for potentially harmful content
	// Optional.
	SafetySettings []*genai.SafetySetting

	ThinkingConfig *genai.ThinkingConfig

	// ImageConfig is the image generation configuration.
	// Note: an error will be returned if this field is set for a model that does not support the configuration options.
	// Optional.
	ImageConfig *genai.ImageConfig

	// ResponseModalities specifies the modalities the model can return.
	// Optional.
	ResponseModalities []GeminiResponseModality

	MediaResolution genai.MediaResolution

	// Cache controls prefix cache settings for the model.
	// Optional. used to CreatePrefixCache for reused inputs.
	Cache *CacheConfig

	// ResponseLogprobs controls whether to return the log probabilities of the
	// tokens that were chosen by the model at each step.
	// When enabled, response logprobs are populated in Message.ResponseMeta.LogProbs.
	// Optional. Default: false. Configure top-K candidates via Logprobs.
	ResponseLogprobs bool

	// Logprobs specifies the number of top candidate tokens to return the
	// log probabilities for at each generation step.
	// Optional. Only takes effect when ResponseLogprobs is true.
	Logprobs *int32
}

Config contains the configuration options for the Gemini model

type GeminiResponseModality

type GeminiResponseModality string
const (
	GeminiResponseModalityText  GeminiResponseModality = "TEXT"
	GeminiResponseModalityImage GeminiResponseModality = "IMAGE"
	GeminiResponseModalityAudio GeminiResponseModality = "AUDIO"
)

Directories

Path Synopsis
examples
generate command
image_generate command
intent_tool command
react command
stream command

Jump to

Keyboard shortcuts

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