agent.go

Overview

The agent.go file defines core internal structures and interfaces for managing large language model (LLM) agents within the system. It encapsulates the state and configuration needed to run an LLM agent, including the selected model, tools, instructions, and schemas for input/output validation. This file establishes the internal representation (State) of an agent and exposes controlled access via the Agent interface and utility functions. It serves as a foundational building block in the LLM Integration and Agents topic, enabling agents to be configured and executed with complex instruction handling and tool integration.


Types and Interfaces

Agent Interface

type Agent interface {
	internal() *State
}

State Struct

type State struct {
	Model model.LLM

	Tools    []tool.Tool
	Toolsets []tool.Toolset

	IncludeContents string

	GenerateContentConfig *genai.GenerateContentConfig

	Instruction               string
	InstructionProvider       InstructionProvider
	GlobalInstruction         string
	GlobalInstructionProvider InstructionProvider

	DisallowTransferToParent bool
	DisallowTransferToPeers  bool

	InputSchema  *genai.Schema
	OutputSchema *genai.Schema

	OutputKey string
}

InstructionProvider Type

type InstructionProvider func(ctx agent.ReadonlyContext) (string, error)

Functions

(s *State) internal()

func (s *State) internal() *State { return s }

Reveal

func Reveal(a Agent) *State { return a.internal() }

Important Implementation Details


Interaction with Other Components


Example Usage

// Create a new agent state with a specified LLM and tools
state := &State{
    Model: myLLMInstance,
    Tools: []tool.Tool{searchTool, calculatorTool},
    Instruction: "Answer the user's query based on the provided context.",
    InputSchema:  userInputSchema,
    OutputSchema: outputResponseSchema,
}

// Wrap the state as an Agent
var agent Agent = state

// Retrieve internal state for configuration or inspection
internalState := Reveal(agent)

// Use internalState to modify instructions dynamically if needed
internalState.Instruction = "Updated instruction with new context."

Mermaid Diagram: Agent Internal Structure

classDiagram
class State {
+Model
+Tools
+Toolsets
+IncludeContents
+GenerateContentConfig
+Instruction
+InstructionProvider
+GlobalInstruction
+GlobalInstructionProvider
+DisallowTransferToParent
+DisallowTransferToPeers
+InputSchema
+OutputSchema
+OutputKey
+internal()
}
class Agent {
+internal()
}
Agent <|.. State

This class diagram illustrates the relationship between the Agent interface and the State struct, showing that State implements Agent by providing the internal() method. The properties of State represent the agent's configuration and runtime state components.


This file is a key internal module underpinning the agent framework within the LLM Integration and Agents topic, enabling comprehensive configuration and management of LLM agents with dynamic instruction handling, tool support, and schema validations.