llm.go

Overview

The llm.go file defines core interfaces and data structures for interacting with Large Language Models (LLMs) within the system. It provides an abstraction layer that encapsulates LLM invocation, request formulation, and response handling. This file serves as a foundational component in the LLM Integration and Agents topic, enabling higher-level agents and workflows to communicate with underlying LLM providers in a consistent manner.

Key elements of this file include:

This abstraction allows seamless integration with various LLM backends, supports streaming responses, and provides hooks for metadata like citations, usage, and grounding.


Types and Interfaces

LLM Interface

type LLM interface {
    Name() string
    GenerateContent(ctx context.Context, req *LLMRequest, stream bool) iter.Seq2[*LLMResponse, error]
}
var llmClient LLM = /* some concrete implementation */
ctx := context.Background()
request := &LLMRequest{
    Model: "gpt-4",
    Contents: []*genai.Content{...},
    Config: &genai.GenerateContentConfig{...},
}
responseSeq := llmClient.GenerateContent(ctx, request, true)

for {
    response, err := responseSeq.Next()
    if err != nil {
        log.Fatal(err)
    }
    if response == nil {
        break
    }
    fmt.Println("Received content:", response.Content)
    if response.TurnComplete {
        break
    }
}

LLMRequest Struct

type LLMRequest struct {
    Model    string
    Contents []*genai.Content
    Config   *genai.GenerateContentConfig
    Tools    map[string]any `json:"-"`
}

LLMResponse Struct

type LLMResponse struct {
    Content           *genai.Content
    CitationMetadata  *genai.CitationMetadata
    GroundingMetadata *genai.GroundingMetadata
    UsageMetadata     *genai.GenerateContentResponseUsageMetadata
    CustomMetadata    map[string]any
    LogprobsResult    *genai.LogprobsResult
    Partial           bool
    TurnComplete      bool
    Interrupted       bool
    ErrorCode         string
    ErrorMessage      string
    FinishReason      genai.FinishReason
    AvgLogprobs       float64
}

Implementation Details


Interactions with Other System Components


Diagram: Structure of llm.go

classDiagram
class LLM {
+Name() string
+GenerateContent(ctx, req, stream) Seq2[LLMResponse, error]
}
class LLMRequest {
+Model: string
+Contents: []*Content
+Config: *GenerateContentConfig
+Tools: map[string]any
}
class LLMResponse {
+Content: *Content
+CitationMetadata: *CitationMetadata
+GroundingMetadata: *GroundingMetadata
+UsageMetadata: *GenerateContentResponseUsageMetadata
+CustomMetadata: map[string]any
+LogprobsResult: *LogprobsResult
+Partial: bool
+TurnComplete: bool
+Interrupted: bool
+ErrorCode: string
+ErrorMessage: string
+FinishReason: FinishReason
+AvgLogprobs: float64
}
LLM <|.. LLMRequest : uses
LLM ..> LLMResponse : returns sequence of

This documentation covers the purpose, types, methods, and integration points of the llm.go file, providing a detailed reference for developers working with large language model interfaces in the system. For usage in agent construction and orchestration, refer to the related topics on LLM Integration and Agents and Tooling System.