agenticgemini

package module
v0.2.2 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: 16 Imported by: 0

README

Google Gemini

A Google Gemini implementation for Eino that implements the model.AgentModel 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.AgentModel
  • 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

Installation

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

Quick start

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

package main

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

	"google.golang.org/genai"

	"github.com/cloudwego/eino/schema"

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

func main() {
	apiKey := os.Getenv("GEMINI_API_KEY")
	modelName := os.Getenv("GEMINI_MODEL")

	ctx := context.Background()
	client, err := genai.NewClient(ctx, &genai.ClientConfig{
		APIKey: apiKey,
	})
	if err != nil {
		log.Fatalf("NewClient of gemini failed, err=%v", err)
	}

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

	resp, err := cm.Generate(ctx, []*schema.AgenticMessage{schema.UserAgenticMessage("What's the capital of France")})
	if err != nil {
		log.Fatalf("Generate error: %v", err)
	}

	fmt.Printf("\n%s\n\n\n", resp.String())

	resp, err = cm.Generate(ctx, []*schema.AgenticMessage{
		schema.UserAgenticMessage("What's the capital of France"),
		resp,
		schema.UserAgenticMessage("What's the capital of England"),
	})
	if err != nil {
		log.Fatalf("Generate error: %v", err)
	}

	fmt.Printf("\n%s\n\n\n", resp.String())
}


Configuration

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

// Config contains the configuration options for the Gemini agentic model
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
    
    // 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 []genai.Modality
    
    MediaResolution genai.MediaResolution
    
    // CacheExpiration configures the expiration policy for prefix cache resources.
    // Optional.
    CacheExpiration *CacheExpiration
}

Extension Fields

Several fields in the Eino agentic schema are typed as any so that each model implementation can attach provider-specific data. When you consume responses produced by this package, you must type-assert these any fields to the concrete types defined here before you can read them. This section documents every such field and the exact type it carries.

ResponseMeta

Each returned *schema.AgenticMessage carries a ResponseMeta *schema.AgenticResponseMeta. This package populates the strongly-typed GeminiExtension field (no assertion required); the generic Extension any field is left unused.

type AgenticResponseMeta struct {
    // TokenUsage is populated with prompt / completion / total token counts.
    TokenUsage *TokenUsage

    // GeminiExtension is populated by this package (strongly typed, no assertion needed).
    GeminiExtension *gemini.ResponseMetaExtension

    // OpenAIExtension / ClaudeExtension / Extension are unused by this package.
}

GeminiExtension is *github.com/cloudwego/eino/schema/gemini.ResponseMetaExtension. This package fills in the finish reason and, when grounding (Google Search) is used, the grounding metadata:

type ResponseMetaExtension struct {
    ID            string             // response ID (concatenated from stream chunks)
    FinishReason  string             // e.g. "STOP", "MAX_TOKENS", "SAFETY"
    GroundingMeta *GroundingMetadata // non-nil only when grounding/search is used
}
ext := msg.ResponseMeta.GeminiExtension // strongly typed, no assertion needed
ServerToolCall & ServerToolResult

When the model uses the built-in code execution server tool, the resulting content blocks carry *schema.ServerToolCall and *schema.ServerToolResult. Both wrap their payload in an any field, which this package always populates with its own concrete types. The Name field is agenticgemini.ServerToolNameCodeExecution.

type ServerToolCall struct {
    Name      string // "CodeExecution" (agenticgemini.ServerToolNameCodeExecution)
    CallID    string
    Arguments any    // concrete type: *agenticgemini.ServerToolCallArguments
}

type ServerToolResult struct {
    Name    string
    CallID  string
    Content any    // concrete type: *agenticgemini.ServerToolCallResult
}
ServerToolCall.Arguments (any)

Assert to *agenticgemini.ServerToolCallArguments. It carries the executable code the model produced:

type ServerToolCallArguments struct {
    ExecutableCode *ExecutableCode // Code string + Language (e.g. agenticgemini.LanguagePython)
}
// The concrete type is always *agenticgemini.ServerToolCallArguments.
args, ok := msg.ContentBlocks[i].ServerToolCall.Arguments.(*agenticgemini.ServerToolCallArguments)
ServerToolResult.Content (any)

Assert to *agenticgemini.ServerToolCallResult. It carries the outcome and output of the executed code:

type ServerToolCallResult struct {
    CodeExecutionResult *CodeExecutionResult // Outcome (e.g. agenticgemini.OutcomeOK) + Output string
}
// The concrete type is always *agenticgemini.ServerToolCallResult.
result, ok := msg.ContentBlocks[i].ServerToolResult.Content.(*agenticgemini.ServerToolCallResult)

For More Details

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

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 WithResponseJSONSchema

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

func WithResponseModalities

func WithResponseModalities(m []genai.Modality) model.Option

func WithServerTools

func WithServerTools(tools []*ServerToolConfig) model.Option

WithServerTools sets Gemini server-side tools for this request.

func WithThinkingConfig

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

func WithTopK

func WithTopK(k int32) model.Option

Types

type CacheExpiration

type CacheExpiration struct {
	// TTL specifies how long prefix cache resources remain valid (now + TTL).
	TTL *time.Duration
	// ExpireTime sets the absolute expiration timestamp for prefix cache resources.
	ExpireTime *time.Time
}

CacheExpiration configures the expiration policy for prefix cache resources. Exactly one field should be set.

type CodeExecutionResult

type CodeExecutionResult struct {
	Outcome Outcome `json:"outcome,omitempty" mapstructure:"outcome,omitempty"`
	Output  string  `json:"output,omitempty" mapstructure:"output,omitempty"`
}

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

	// 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 []genai.Modality

	MediaResolution genai.MediaResolution

	// CacheExpiration configures the expiration policy for prefix cache resources.
	// Optional.
	CacheExpiration *CacheExpiration
}

Config contains the configuration options for the Gemini agentic model

type ExecutableCode

type ExecutableCode struct {
	Code     string   `json:"code,omitempty" mapstructure:"code,omitempty"`
	Language Language `json:"language,omitempty" mapstructure:"language,omitempty"`
}

type Language

type Language string
const (
	LanguageUnspecified Language = "LANGUAGE_UNSPECIFIED"
	LanguagePython      Language = "PYTHON"
)

type Model

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

func New

func New(_ context.Context, cfg *Config) (*Model, error)

New creates a new Gemini agentic model instance

func (*Model) CreatePrefixCache

func (g *Model) CreatePrefixCache(ctx context.Context, prefixMsgs []*schema.AgenticMessage, 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 (*Model) Generate

func (g *Model) Generate(ctx context.Context, input []*schema.AgenticMessage, opts ...model.Option) (*schema.AgenticMessage, error)

func (*Model) GetType

func (g *Model) GetType() string

func (*Model) IsCallbacksEnabled

func (g *Model) IsCallbacksEnabled() bool

func (*Model) Stream

type Outcome

type Outcome string
const (
	// OutcomeUnspecified specifies that unspecified status. This value should not be used.
	OutcomeUnspecified Outcome = "OUTCOME_UNSPECIFIED"
	// OutcomeOK specifies that code execution completed successfully.
	OutcomeOK Outcome = "OUTCOME_OK"
	// OutcomeFailed specifies that code execution finished but with a failure. `stderr` should contain the reason.
	OutcomeFailed Outcome = "OUTCOME_FAILED"
	// OutcomeDeadlineExceeded specifies that code execution ran for too long, and was cancelled. There may or may not be a partial
	// output present.
	OutcomeDeadlineExceeded Outcome = "OUTCOME_DEADLINE_EXCEEDED"
)

type ServerToolCallArguments

type ServerToolCallArguments struct {
	ExecutableCode *ExecutableCode `json:"executable_code,omitempty" mapstructure:"executable_code,omitempty"`
}

type ServerToolCallResult

type ServerToolCallResult struct {
	CodeExecutionResult *CodeExecutionResult `json:"code_execution_result,omitempty" mapstructure:"code_execution_result,omitempty"`
}

type ServerToolConfig

type ServerToolConfig struct {
	CodeExecution         *genai.ToolCodeExecution
	GoogleSearch          *genai.GoogleSearch
	GoogleSearchRetrieval *genai.GoogleSearchRetrieval
	URLContext            *genai.URLContext
	FileSearch            *genai.FileSearch
	GoogleMaps            *genai.GoogleMaps
}

type ServerToolName

type ServerToolName string
const (
	ServerToolNameCodeExecution ServerToolName = "CodeExecution"
)

Directories

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

Jump to

Keyboard shortcuts

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