gemini.go

Overview

The gemini.go file provides an implementation of the [model.LLM] interface tailored for Gemini language models using the Gemini API. It encapsulates the logic for interacting with Gemini models via the genai.Client, handling synchronous and streaming text generation requests. This file establishes a bridge between the generic LLM interface used throughout the system and the specifics of the Gemini API, including request construction, header management, response conversion, and streaming aggregation.

Key responsibilities include:

This implementation is foundational for integrating Gemini models as pluggable LLM backends within the broader AI agent and tooling framework.

Types and Functions

Type: geminiModel

type geminiModel struct {
	client             *genai.Client
	name               string
	versionHeaderValue string
}

Function: NewModel

func NewModel(ctx context.Context, modelName string, cfg *genai.ClientConfig) (model.LLM, error)

Method: (*geminiModel) Name

func (m *geminiModel) Name() string

Method: (*geminiModel) GenerateContent

func (m *geminiModel) GenerateContent(ctx context.Context, req *model.LLMRequest, stream bool) iter.Seq2[*model.LLMResponse, error]

Method: (*geminiModel) addHeaders

func (m *geminiModel) addHeaders(headers http.Header)

Method: (*geminiModel) generate

func (m *geminiModel) generate(ctx context.Context, req *model.LLMRequest) (*model.LLMResponse, error)

Method: (*geminiModel) generateStream

func (m *geminiModel) generateStream(ctx context.Context, req *model.LLMRequest) iter.Seq2[*model.LLMResponse, error]

Method: (*geminiModel) maybeAppendUserContent

func (m *geminiModel) maybeAppendUserContent(req *model.LLMRequest)

Important Implementation Details

Interactions with Other Components


Diagram: geminiModel Structure and Method Relationships

classDiagram
class geminiModel {
-client: genai.Client
-name: string
-versionHeaderValue: string
+Name()
+GenerateContent()
+addHeaders()
+generate()
+generateStream()
+maybeAppendUserContent()
}

This class diagram illustrates the geminiModel struct with its private fields and public methods. The methods coordinate to fulfill the [model.LLM] interface, supporting both synchronous and streaming content generation for Gemini models.