agent

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2025 License: MIT Imports: 13 Imported by: 1

README

Agent

A simple and flexible ReAct (Reasoning and Acting) agent for Go. This library implements the ReAct pattern, allowing AI agents to reason about problems and take actions using tools to solve complex tasks.

Features

  • ReAct Pattern Implementation: Agents can reason about problems and take actions in a structured loop
  • Tool System: Easy-to-implement tool interface for extending agent capabilities
  • Concurrent Tool Execution: Tools run in parallel for better performance
  • Customizable System Prompts: Template-based system prompts with tool descriptions
  • Callback Support: Hooks for monitoring agent reasoning and actions
  • Task History: Built-in conversation and task history management

Instalation

  • For the agent package (for developers): go get github.com/JoshPattman/agent
  • For the agent TUI (interactive way to chat with an agent): go install github.com/JoshPattman/agent/cmd/jchat@latest

Quick Start

package main

import (
    "fmt"
    "github.com/JoshPattman/agent"
)

func main() {
    // Create a model builder (you'll need to implement this)
    builder := &YourModelBuilder{}
    
    // Create an agent with tools
    a := agent.NewAgent(
        builder,
        agent.WithTools(&yourTool1{}, &yourTool2{}),
        agent.WithSystemPromptTemplate("You are a helpful assistant..."),
    )
    
    // Ask the agent a question
    answer, err := a.Answer("What's the weather like today?")
    if err != nil {
        panic(err)
    }
    
    fmt.Println(answer)
}

Core Concepts

ReAct Pattern

The ReAct pattern combines reasoning and acting in a loop:

  1. Reason: The agent thinks about what to do next
  2. Act: The agent calls appropriate tools
  3. Observe: The agent processes tool results
  4. Repeat: Continue until the task is complete
Tools

Tools are the actions an agent can take. Implement the Tool interface:

// A function which can be described to and called by an agent.
type Tool interface {
	// The name of the tool to be used by the agent.
	Name() string
	// Describe the tool in short bullet points to the agent.
	Description() []string
	// Call the tool, providing a formatted response or an error if the tool call failed.
	Call(map[string]any) (string, error)
}
Agent Options

Configure your agent with various options (when not specified, sensible defaults will be used):

  • WithTools(...Tool): Add tools for the agent to use
  • WithSystemPromptTemplate(string): Customize the system prompt
  • WithTaskPrefix(string): Change a prefix for starting a task
  • WithFinalAnswerMessage(string): Change the message to tell the agent to create a final answer

Model Builder Interface

The agent requires a model builder to interface with language models. The interface creates a jpf model, which allows you to easily compose retry logic, caching, and other useful features. Implement the AgentModelBuilder interface:

// Specifies an object that can be called to create a model for the agent to use.
type AgentModelBuilder interface {
	// Create a model with a structured output schema for the object provided.
	BuildAgentModel(responseType any) jpf.Model
}

The model builder is responsible for:

  • Configuring the language model (API keys, model selection, etc.)
  • Creating appropriate prompts for reasoning vs. answering
  • Handling the actual API calls to your chosen language model
  • Managing conversation context and tool descriptions

Callbacks

Monitor agent behavior with callbacks:

agent.SetOnReActInitCallback(func(response ReActResponse) {
    fmt.Printf("Agent is has reasoned and called tools: %s\n", response)
})

agent.SetOnReActCompleteCallback(func(step ReActStep) {
    fmt.Printf("%d tool calls have been returned to agent\n", len(step.ActionObservations))
})

License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateSubAgentTools

func CreateSubAgentTools(agentStorage map[string]Agent, createFuncs map[string]SubAgentConstructor) (Tool, Tool)

Types

type Action

type Action struct {
	Name string      `json:"name"`
	Args []ActionArg `json:"args"`
}

type ActionArg

type ActionArg struct {
	ArgName string `json:"arg_name"`
	ArgData any    `json:"arg_data"`
}

type ActionObservation

type ActionObservation struct {
	Action      Action      `json:"action"`
	Observation Observation `json:"observation"`
}

type Agent

type Agent interface {
	Answer(query string) (string, error)
	SetOnReActInitCallback(callback func(string, []Action))
	SetOnReActCompleteCallback(callback func(string, []ActionObservation))
	SetOnBeginStreamAnswerCallback(callback func())
	SetOnStreamAnswerChunkCallback(callback func(string))
}

type AgentModelBuilder

type AgentModelBuilder interface {
	// Create a model with a structured output schema for the object provided.
	BuildAgentModel(responseType any, onInitFinalStream func(), onDataFinalStream func(string)) jpf.Model
}

Specifies an object that can be called to create a model for the agent to use.

type Observation

type Observation struct {
	Observed string `json:"observed"`
}

type Scenario

type Scenario struct {
	Headline  string   `json:"headline"`
	Takeaways []string `json:"takeaways"`
}

type SubAgentConstructor

type SubAgentConstructor struct {
	Build func() Agent
	Desc  string
}

type Tool

type Tool interface {
	// The name of the tool to be used by the agent.
	Name() string
	// Describe the tool in short bullet points to the agent.
	Description() []string
	// Call the tool, providing a formatted response or an error if the tool call failed.
	Call(map[string]any) (string, error)
}

A function which can be described to and called by an agent.

func AgentAsTool

func AgentAsTool(buildAgent func() Agent, name string, description []string) Tool

func FunctionalTool

func FunctionalTool(do func(map[string]any) (string, error), name string, description []string) Tool

func MapFuncTool

func MapFuncTool[T any](mf jpf.MapFunc[T, string], name string, description []string) Tool

func NewAgentQuickQuestionTool

func NewAgentQuickQuestionTool(buildAgent func() Agent, name string, agentDescription string) Tool

NewAgentQuickQuestionTool creates a tool that builds a fresh agent and asks a query. The tool's name will be agent_<name> and the description is formatted using agentDescription.

func NewCustomExecuteCommandTool added in v0.0.4

func NewCustomExecuteCommandTool(name string, description []string, commandPath string, commandArgs ...string) Tool

func NewExecuteCommandTool added in v0.0.4

func NewExecuteCommandTool() Tool

func NewListDirectoryTool added in v0.0.2

func NewListDirectoryTool() Tool

func NewReadFileTool added in v0.0.2

func NewReadFileTool() Tool

func NewScenarioRetrieverTool

func NewScenarioRetrieverTool(scenarios map[string]Scenario) Tool

func NewTimeTool added in v0.0.2

func NewTimeTool() Tool

Directories

Path Synopsis
agentmcp module
cmd
jchat module
craig implements a Combined ReAct Intelligent aGent, where the agent reasons and acts in a single message.
craig implements a Combined ReAct Intelligent aGent, where the agent reasons and acts in a single message.

Jump to

Keyboard shortcuts

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