aicompanion

package module
v0.1.10 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2025 License: MIT Imports: 7 Imported by: 0

README

ai-companion

poc to communicate with LLMs via API

Configuration

The configuration file is a JSON file that contains the following properties:

  • ai_type: The type of AI model to use (e.g. chat, embed, moderation).
  • ai_model: The name of the AI model to use.
  • api_provider: The name of the AI model provider (e.g. OpenAI, Ollama).
  • api_key: The API key to use for authentication with the AI model provider.
  • api_chat_url: The URL for the API endpoint to use for chat requests.
  • api_generate_url: The URL for the API endpoint to use for generate requests.
  • api_embed_url: The URL for the API endpoint to use for embed requests.
  • api_moderation_url: The URL for the API endpoint to use for moderation requests.
  • term_output: whether to output chat responses to terminal.
  • term_color: The color scheme to use in terminal output.
  • max_messages: The maximum number of messages allowed in a chat conversation.
  • max_input_length: The maximum length of allowed input.
  • http_client_timeout: The timeout value for HTTP client requests, in seconds.
  • buffer_size: The buffer size for reading and writing data, in bytes.
Configuration Options
ai_type
Allowed value Description
chat The AI model is used for chat.
embed The AI model is used for generating embeddings.
moderation The AI model is used for moderation tasks.
api_provider
Allowed value Description
Ollama The API provider is Ollama.
OpenAI The API provider is OpenAI.
term_color
Allowed value Description
green Green color scheme.
red Red color scheme.
blue Blue color scheme.
yellow Yellow color scheme.
cyan Cyan color scheme.
magenta Magenta color scheme.
white White color scheme.
black Black color scheme.
Example Configuration File
{
  "ai_type": "chat|embed|moderation",
  "ai_model": "llama3.2:3b",
  "api_provider": "Ollama|OpenAI",
  "api_key": "my-api-key",
  "api_chat_url": "https://<your.ollama.endpoint>/api/chat",
  "api_generate_url": "https://<your.ollama.endpoint>/api/generate",
  "api_embed_url": "https://<your.ollama.endpoint>/api/embed",
  "api_moderation_url": "https://<your.ollama.endpoint>/api/moderation",
  "term_color": "green",
  "term_output": true,
  "max_messages": 20,
  "max_input_length": 500,
  "http_client_timeout": 300,
  "buffer_size": 1024
}

Examples

basic usage
func main() {
    companion := aicompanion.NewCompanion(models.Configuration{
        ApiProvider:       "Ollama",
        ApiKey:            "",
        AiModel:           "llama3.1:8b",
        AIType:            "chat",
        ApiChatURL:        "http://localhost:11434/api/chat",
        ApiGenerateURL:    "http://localhost:11434/api/generate",
        ApiEmbedURL:       "http://localhost:11434/api/embed",
        ApiModerationURL:  "http://localhost:11434/api/moderation",
        MaxInputLength:    500,
        HTTPClientTimeout: 300,
        BufferSize:        1024,
        MaxMessages:       20,
        Color:             terminal.Green,
        Output:            true,
    })

    companion.SetSystemRole("you are a helpful assistant that only replies in haikus")
    companion.SendChatRequest(companion.CreateMessage(models.User, "hi! Who are you?"))
}
% go run main.go 
> Assistant at hand
Helping you with care and ease
Friendly, here to aid
ai vs ai
func main() {
    companion1 := NewCompanion(*Config)
    companion2 := NewCompanion(*Config)
    companion1.SetSystemRole("you are Batman") 
    companion2.SetSystemRole("you are the Joker") 

    message := companion1.createMessage(User, "*the scene opens*")

    var assistant1 bool = true

    for {
        if assistant1 {
            message, _ = companion1.ProcessUserInput(message)
        } else {
            message, _ = companion2.ProcessUserInput(message)
        }
        message.Role = User
        assistant1 = !assistant1
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReadImageFromFile

func ReadImageFromFile(filepath string) (models.Base64Image, error)

Types

type AICompanion

type AICompanion interface {
	// PrepareConversation initializes a new conversation with a default system message
	PrepareConversation() []models.Message

	// CreateMessage creates a new message with the given role and input string
	CreateMessage(role models.Role, input string) models.Message

	// CreateMessageWithImages creates a new message with the given role, input string, and images
	CreateMessageWithImages(role models.Role, message string, images []models.Base64Image) models.Message

	// AddMessage adds a new message to the conversation
	AddMessage(message models.Message)

	// GetConfig returns the current configuration for the AI companion
	GetConfig() models.Configuration

	// SetConfig sets a new configuration for the AI companion
	SetConfig(config models.Configuration)

	// GetSystemRole returns the current system role message
	GetSystemRole() models.Message

	// SetSystemRole sets a new system role message
	SetSystemRole(prompt string)

	// GetConversation returns the current conversation
	GetConversation() []models.Message

	// SetConversation sets the current conversation
	SetConversation(conversation []models.Message)

	// GetClient returns the current HTTP client used for requests
	GetClient() *http.Client

	// SetClient sets a new HTTP client for requests
	SetClient(client *http.Client)

	// interactions
	// SendChatRequest sends a chat request to an AI model and returns a response message
	SendChatRequest(message models.Message) (models.Message, error)

	// SendCompletionRequest sends a completion request to an AI model and returns a response message
	SendGenerateRequest(message models.Message) (models.Message, error)

	// SendEmbeddingRequest sends an embedding request to an AI model and returns a response
	SendEmbeddingRequest(embedding models.EmbeddingRequest) (models.EmbeddingResponse, error)

	// SendModerationRequest sends a moderation request to an AI model and returns a response
	SendModerationRequest(moderationRequest models.ModerationRequest) (models.ModerationResponse, error)
}

AICompanion defines the interface for interacting with AI models.

func NewCompanion

func NewCompanion(config models.Configuration) AICompanion

NewCompanion creates a new Companion instance with the provided configuration.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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