coeus

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2025 License: Apache-2.0, MIT Imports: 22 Imported by: 0

README

Coeus - LLM Library

Coeus is a Golang library designed for customizing existing Large Language Models (LLMs) to work according to user preferences. It provides tools for managing conversations, tool calls, memory, Retrieval-Augmented Generation (RAG), and more.

Features

  • Multi-provider support: Works with OpenAI, Azure, and Ollama.
  • System prompts: Shape the LLM’s behavior using custom prompts.
  • LLM tools: Extend the model’s capabilities with custom function calls.
  • Memory management: Configurable conversation memory options.
  • Retrieval-Augmented Generation (RAG): Enhance responses with external knowledge.
  • Debug dashboard: A web-based tool to test your setup and responses.

Installation

To install:

# Install Coeus
go get github.com/AlexHeier/Coeus

Usage

The simplest way to use Coeus:

err := coeus.Ollama("10.0.0.1", "11434", "llama3.1:8b")
if err != nil {
	log.Fatal(err)
}

conv := coeus.BeginConversation()

resp, err := conv.Prompt("Hello Ollama!")
if err != nil {
	log.Fatal(err)
}

fmt.Println(resp.Response)
Supported LLM Providers

Coeus supports multiple LLM providers:

err := coeus.OpenAI("gpt-4o-mini", "OPENAI_API_KEY")
err := coeus.Azure("AZURE_ENDPOINT", "AZURE_API_KEY", temperature float64, maxTokens int)
err := coeus.Ollama("10.0.0.1", "11434", "llama3.1:8b")
System Prompts

To set a system prompt for shaping the LLM’s responses:

coeus.SetSystemPrompt("You are an AI assistant")
LLM Tools

Define functions that the LLM can call:

coeus.NewTool("MULTIPLY", "Multiplies two integers", Multiply)

func Multiply(a, b int) int {
	return a * b
}
Memory Management

The default memory mode is MemoryAllMessage(), which retains the entire conversation. To change the memory mode:

coeus.MemoryVersion(MemoryFunc)

Available memory modes:

// Default: Uses all conversation messages.
coeus.MemoryVersion(MemoryAllMessage)

// Uses only the last X messages.
coeus.MemoryVersion(MemoryLastMessage, messageCount int)

// Uses messages within the last X minutes.
coeus.MemoryVersion(MemoryTime, messageAge int)

To implement custom memory management functions, ensure they follow to the following signature:

func(c *Conversation) ([]HistoryStruct, error)

Requirements:

  • Functions can accept any number of predefined input arguments found in the memArgs slice:

    memArgs []interface{}
    
  • The function should return a slice of HistoryStruct and an error value.

For reference, check how predefined memory functions are implemented in coeus/llm_memory.go.

Retrieval-Augmented Generation (RAG)

Coeus includes a custom RAG model based on a skip-gram word2vec model (supports English only). The RAG training process is available here.

To enable RAG:

err := coeus.EnableRAG(host, dbname, user, password string, port int)
if err != nil {
	log.Fatal(err)
}

Configuration options:

err := coeus.RAGConfig(context, chunkSize int, overlapRatio, multiplier float64, folder string)

Parameters:

  • context: Number of closest results used as context (default: 2).
  • chunkSize: Text chunk size (default: 300).
  • overlapRatio: Overlapping ratio between chunks (default: 0.25).
  • multiplier: Vector scaling multiplier (default: 2).
  • folder: Storage folder for RAG files (default: "./RAG").

To test what the RAG would retrive given a user prompt:

fmt.Println(coeus.GetRAG("Test prompt"))
Debug Dashboard

To start a debug window as a local web interface:

err := coeus.StartDashboard(port string)
if err != nil {
	log.Fatal(err)
}

Access it at localhost:PORT.

Automatic Conversation Cleanup

To enable automatic conversation cleanup that will remove unactive conversations from memory:

err := coeus.ConversationTTL(ttl int)
if err != nil {
	log.Fatal(err)
}

TTL is the number of minutes a conversation can be inactive before terminated.

Running Tests

To run tests for Coeus:

go test -v ./...

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ConvAll struct {
	Mutex         sync.Mutex
	Conversations []*Conversation
}

Struct for containing all the conversations

View Source
var Provider interface{}

Provider will change to the provider struct of the chosen provider

View Source
var Tools []toolStruct

Tools is a list of all the tools

Functions

func Azure

func Azure(endpoint, apikey string, temperature float64, maxTokens int) error

Azure sets the provider to Azure.

@param endpoint: String which contains the URL of Azure endpoint

@param apikey: Azure API key

@param temperature: Specifies the amount of freedom an LLM should have when answering

@param maxTokens: Specifies the max amount of tokens an LLM answer should use

func ConversationTTL

func ConversationTTL(ttl int) error

ConversationTTL is a function that automatically cleans up conversations by checking their Time To Live (TTL).

@param ttl Time in minutes a conversation can be dormant before being deleted.

@return error If ttl is not a positive number.

func EnableRAG

func EnableRAG(host, dbname, user, password string, port int, folder string) error

EnableRAG enables RAG (Retrieval-Augmented Generation) mode. This mode allows the model to use external knowledge sources to improve its responses.

Recommended to use RAGConfig before Enable to remove racecondition between start of tokenization and config.

@param host: The host of the database.

@param dbname: The name of the database.

@param user: The user to connect to the database.

@param password: The password to connect to the database.

@param port: The port to connect to the database.

@return An error if any of the fields are empty or invalid.

func FindTool

func FindTool(name string) (toolStruct, error)

Find finds a tool by its name and returns the tool struct.

@param name: the name of the tool

@return: the tool struct

@return: an error if the tool is not found

func GetRAG

func GetRAG(userPrompt string) string

GetRAG retrieves the closests chunks from the RAG database based of the user prompt.

func MemoryVersion

func MemoryVersion(newFunc ...interface{})

MemoryVersion changes the function used for memory management. Default is All messages.

@param newFunc: The new function to use for memory management.

func NewTool

func NewTool(name, desc string, function interface{})

NewTool creates a new tool and adds it to the list of tools.

@param name: the name of the tool

@param desc: the description of the tool sendt to the LLM

@param function: the function to be executed

func Ollama

func Ollama(ip, port, model string, temperature float64) error

Ollama is a function that sets the provider to Ollama.

@param ip: the IP address of the Ollama server

@param port: the port of the Ollama server

@param model: the model to use

@return An error if the IP address, port or model is invalid

func OpenAI

func OpenAI(model, apikey string, temperature float32) error

OpenAI is a function that sets the provider to OpenAI.

@param model: the model to use

@param apikey: the api key to use

@return An error if the model or api key is empty

func RAGConfig

func RAGConfig(context, chunkSize int, overlapRatio, multiplier float64) error

RAGConfig sets the configuration for the RAG (Retrieval-Augmented Generation) mode.

@param context: The number of closest results to use as context for the model. Default is 2.

@param chunkSize: The size of the chunks to split the text into. Default is 300.

@param overlapRatio: The ratio of overlap between chunks. Default is 0.25.

@param multiplier: The multiplier for the vector scaling. Default is 2.

@param folder: The folder where the RAG files are stored. Default is "./RAG".

@param error: An error if any of the fields are invalid.

func SetSystemPrompt

func SetSystemPrompt(prompt string)

SetSystemPrompt is a function that sets the system prompt for the LLM.

@param prompt: the system prompt to set

func StartDashboard

func StartDashboard(port string) error

Start starts the dashboard on the specified port. The dashboard is a web interface for the LLM chatbot used for trubleshooting and testing the chatbot.

@param Port string - The port the dashboard should listen on.

@return error - Returns an error if the server could not start.

Types

type Conversation

type Conversation struct {
	Mutex      sync.Mutex
	MainPrompt string
	ToolsResp  []interface{}
	History    []HistoryStruct
	Summary    string
	UserPrompt string
	LastActive time.Time
}

Struct for a single conversation.

func BeginConversation

func BeginConversation() *Conversation

BeginConversation is a function that creates a new conversation and returns it.

@return A pointer to the new conversation

func (*Conversation) DumpConversation

func (c *Conversation) DumpConversation() string

DumpConversation is a function that returns the conversation history as a string.

@receiver c: The conversation to dump

func (*Conversation) Prompt

func (c *Conversation) Prompt(userPrompt string) (ResponseStruct, error)

Prompt is a function that sends a prompt to the LLM and returns the response.

@receiver c: The conversation to send the prompt from

@param userPrompt: The prompt to send to the LLM

@return A ResponseStruct and an error if the request fails

type HistoryStruct

type HistoryStruct struct {
	Role       string     `json:"role"`
	Content    string     `json:"content"`
	ToolCallID string     `json:"tool_call_id,omitempty"`
	ToolCalls  []ToolCall `json:"tool_calls,omitempty"`
	TimeStamp  time.Time
}

func MemoryAllMessage

func MemoryAllMessage(c *Conversation) ([]HistoryStruct, error)

MemoryAllMessage is a function that will use all messages as memory.

@return Array of history objects to use as memory.

func MemoryLastMessage

func MemoryLastMessage(c *Conversation) ([]HistoryStruct, error)

MemoryLastMessage is a function that will use the last int x messages as memory.

@extra param: The number of last user messages to use as memory.

@return Array of the last X amount of messages from user and everything between.

func MemoryTime

func MemoryTime(c *Conversation) ([]HistoryStruct, error)

MemoryTime is a function that will use the messages within the last int x minutes as memory.

@extra param: The number of last minutes to use as memory.

@return Array of the last X amount of messages within the last Y minutes

type RequestStruct

type RequestStruct struct {
	History      *[]HistoryStruct
	Systemprompt string
	Userprompt   string
}

type ResponseStruct

type ResponseStruct struct {
	Response           string
	TotalLoadDuration  float64
	Eval_count         float64
	PromptEvalCount    float64
	PromptEvalDuration float64
}

ResponseStruct is the struct that will be returned from the provider. It contains the response from the LLM, the total load duration, the eval count, the prompt eval count and the prompt eval duration.

Response: The response from the LLM

TotalLoadDuration: The total load duration of the request

Eval_count: The number of evals that were made

PromptEvalCount: The number of prompt evals that were made

PromptEvalDuration: The duration of the prompt evals

func Send

func Send(con *Conversation) (ResponseStruct, error)

Send function will send the request to any provider and return the response

@param con ConversationStruct

@return ResponseStruct, error

type ToolCall

type ToolCall struct {
	Index    *int   `json:"index,omitempty"`
	ID       string `json:"id,omitempty"`
	Type     string `json:"type"`
	Function struct {
		Name      string `json:"name,omitempty"`
		Arguments string `json:"arguments,omitempty"`
	} `json:"function"`
}

Jump to

Keyboard shortcuts

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