gogent

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2025 License: MIT Imports: 13 Imported by: 0

README

GoGent - A Lightweight Agent Framework in Go

GoGent is a lightweight, flexible agent framework written in Go that simplifies the creation and management of AI agents. It provides easy integration with LLM APIs (like OpenAI) and supports tool augmentation, making it perfect for building conversational agents with advanced capabilities.

Features

  • 🧠 Simple integration with OpenAI and other LLM APIs
  • 🔧 Tool-using capabilities with a simple API
  • 💾 Built-in memory management for conversation history
  • 🔄 Automatic session management and cleanup
  • 🛠️ Extensible architecture to add custom features

Installation

go get github.com/renluxiang/gogent

Quick Start

Basic Agent
package main

import (
    "github.com/renluxiang/gogent"
    "fmt"
)

func main() {
    // Create a new agent
    agent := gogent.NewGenericAgent().
        WithName("MyAssistant").
        WithSystemPrompt("You are a helpful assistant.")
    
    // Start the agent
    agent.Start()
    
    // Chat with the agent
    response := agent.Chat("Hello, who are you?", "user1")
    fmt.Println(response)
}
Agent with Tools
package main

import (
    "github.com/renluxiang/gogent"
    "fmt"
)

func main() {
    // Create a calculator tool
    calcTool := MyCalculatorTool{}
    
    // Create and configure an agent with the tool
    agent := gogent.NewGenericAgent().
        WithName("MathBot").
        WithSystemPrompt("You are a mathematics assistant.").
        WithTools([]gogent.ITool{calcTool})
    
    // Start the agent
    agent.Start()
    
    // Chat with the agent
    response := agent.Chat("Can you calculate 15 * 7?", "user1")
    fmt.Println(response)
}

// Tool implementation example
type MyCalculatorTool struct{}

func (t MyCalculatorTool) SetAgentAgent(agent gogent.IAgent) {}
func (t MyCalculatorTool) GetName() string { return "Calculate" }
func (t MyCalculatorTool) GetDescription() string { return "Performs basic math operations" }
func (t MyCalculatorTool) GetNamespace() string { return "math" }
func (t MyCalculatorTool) Close() {}

func (t MyCalculatorTool) Run(args ...any) (any, error) {
    // Tool implementation here
    // ...
    return "105", nil
}

Environment Variables

GoGent uses the following environment variables:

  • OPENAI_API_KEY - Your OpenAI API key (required)
  • OPENAI_API_BASE_URL - Custom API endpoint URL (optional)
  • OPENAI_MODEL - Model to use, defaults to "gpt-4o" if not specified (optional)

Contributing

Contributions are welcome! Please feel free to submit pull requests.

License

MIT

Documentation

Index

Constants

View Source
const RetryPromptPrefix = "The previous response format was incorrect. Please try again and follow the format below."

Variables

This section is empty.

Functions

func StringMsgToOpenAiMsg

func StringMsgToOpenAiMsg(msg string) openai.ChatCompletionMessage

Types

type BaseAgent

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

func (*BaseAgent) GetPrompt

func (b *BaseAgent) GetPrompt() string

func (*BaseAgent) SetName

func (b *BaseAgent) SetName(name string)

type GenericAgent

type GenericAgent struct {
	BaseAgent

	Mutex sync.Mutex
	// contains filtered or unexported fields
}

func NewGenericAgent

func NewGenericAgent() *GenericAgent

func (*GenericAgent) Chat

func (g *GenericAgent) Chat(msg string, session string) string

func (*GenericAgent) GetLogger

func (g *GenericAgent) GetLogger() ILogger

func (*GenericAgent) GetName

func (b *GenericAgent) GetName() string

func (*GenericAgent) GetSystemPrompt

func (b *GenericAgent) GetSystemPrompt() string

func (*GenericAgent) GetTools

func (b *GenericAgent) GetTools() []ITool

func (*GenericAgent) Start

func (g *GenericAgent) Start()

func (*GenericAgent) WithBrain

func (g *GenericAgent) WithBrain(b IBrain) *GenericAgent

func (*GenericAgent) WithLogger

func (g *GenericAgent) WithLogger(logger ILogger) *GenericAgent

func (*GenericAgent) WithMemory

func (g *GenericAgent) WithMemory(memory IMemory) *GenericAgent

func (*GenericAgent) WithName

func (g *GenericAgent) WithName(name string) *GenericAgent

func (*GenericAgent) WithPromptFunc added in v0.0.3

func (b *GenericAgent) WithPromptFunc(pf func() string) *GenericAgent

func (*GenericAgent) WithSystemPrompt

func (b *GenericAgent) WithSystemPrompt(prompt string) *GenericAgent

func (*GenericAgent) WithTools

func (g *GenericAgent) WithTools(tools []ITool) *GenericAgent

type IAgent

type IAgent interface {
	GetName() string
	Chat(msg string, session string) string
}

type IBrain

type IBrain interface {
	ChatCompletion(msgs []openai.ChatCompletionMessage) (msg openai.ChatCompletionMessage, err error)
}

func NewOpenAiBrain

func NewOpenAiBrain() IBrain

type ILogger

type ILogger interface {
	Info(args ...interface{})
}

type IMemory

type IMemory interface {
	GetWithNewMsg(msg openai.ChatCompletionMessage, sessionId string) []openai.ChatCompletionMessage
	Add(msg openai.ChatCompletionMessage, sessionId string)
	SetSystem(msg string)
}

func NewSimpleMemory

func NewSimpleMemory(maxHistory int, system string) IMemory

修改构造函数

type ITool

type ITool interface {
	SetAgentAgent(agent IAgent)
	GetName() string
	GetDescription() string
	GetNamespace() string
	Run(args ...any) (any, error)
	Close()
}

type OpenAiBrain

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

func (*OpenAiBrain) ChatCompletion

func (oab *OpenAiBrain) ChatCompletion(msgs []openai.ChatCompletionMessage) (msg openai.ChatCompletionMessage, err error)

Directories

Path Synopsis
examples
tool_use command

Jump to

Keyboard shortcuts

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