aicompanion

package module
v0.3.4 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2025 License: MIT Imports: 8 Imported by: 0

README

AI Companion CLI Interface Documentation

Overview

This document provides an overview and detailed explanation of the AICompanion interface, which is defined in the Go file aicompanion_interface.go. The AICompanion interface is designed to abstract interactions with various AI models, providing a unified way to interact with different API providers such as Ollama and OpenAI.

Table of Contents

  1. Interface Definition
  2. Configuration and Initialization
  3. Utility Functions
  4. Interactions
  5. Example Usage

1. Interface Definition

The AICompanion interface defines a set of methods that must be implemented by any AI companion implementation:

  • PrepareConversation(message models.Message) []models.Message: Prepares the conversation with a new message.
  • CreateMessage(role models.Role, input string) models.Message: Creates a new message with the specified role and content.
  • CreateMessageWithImages(role models.Role, message string, images *[]models.Base64Image) models.Message: Creates a new message with images.
  • CreateUserMessage(input string, images *[]models.Base64Image) models.Message: Creates a user message with the specified content and optional images.
  • CreateAssistantMessage(input string) models.Message: Creates an assistant message with the specified content.
  • AddMessage(message models.Message): Adds a new message to the conversation.
  • GetConfig() models.Configuration: Retrieves the current configuration for the AI companion.
  • SetConfig(config models.Configuration): Sets a new configuration for the AI companion.
  • GetSystemRole() models.Message: Retrieves the current system role message.
  • SetSystemRole(prompt string): Sets a new system role prompt.
  • GetEnrichmentPrompt() string: Retrieves the current enrichment prompt.
  • SetEnrichmentPrompt(prompt string): Sets a new enrichment prompt.
  • GetFunctionsPrompt() string: Retrieves the current functions prompt.
  • SetFunctionsPrompt(prompt string): Sets a new functions prompt.
  • GetSummarizationPrompt() string: Retrieves the current summarization prompt.
  • SetSummarizationPrompt(prompt string): Sets a new summarization prompt.
  • GetConversation() []models.Message: Retrieves the current conversation.
  • SetConversation(conversation []models.Message): Sets the current conversation.
  • GetHttpClient() *http.Client: Retrieves the current HTTP client used for requests.
  • SetHttpClient(client *http.Client): Sets a new HTTP client for requests.
  • GetModels() ([]models.Model, error): Retrieves all models supported by the endpoint.
  • SendChatRequest(message models.MessageRequest, streaming bool, callback func(m models.Message) error) (models.Message, error): Sends a chat request to an AI model and handles the response.
  • SendGenerateRequest(message models.MessageRequest, streaming bool, callback func(m models.Message) error) (models.Message, error): Sends a generate request to an AI model and handles the response.
  • SendEmbeddingRequest(embedding models.EmbeddingRequest) (models.EmbeddingResponse, error): Sends an embedding request to an AI model and retrieves the response.
  • SendModerationRequest(moderationRequest models.ModerationRequest) (models.ModerationResponse, error): Sends a moderation request to an AI model and retrieves the response.
  • HandleStreamResponse(resp *http.Response, streamType models.StreamType, callback func(m models.Message) error) (models.Message, error): Handles streaming responses from chat requests.
  • SetVectorDB(vectorDb *vectordb.VectorDb): Sets the vector database for vectordb (Retrieval Augmented Generation).
  • GetVectorDB() *vectordb.VectorDb: Retrieves the vector database.
  • RunFunction(function models.Function) (models.FunctionResponse, error): Runs a function provided by an AI model and returns the response.

2. Configuration and Initialization

The NewCompanion function initializes a new companion instance with the provided configuration:

func NewCompanion(config models.Configuration, vectordb *vectordb.VectorDb) AICompanion {
    // Implementation details...
}

The NewDefaultConfig function creates a new default configuration for the API provider, token, chat model, and embedding model:

func NewDefaultConfig(apiProvider models.ApiProvider, apiToken, chatModel, embeddingModel string) *models.Configuration {
    // Implementation details...
}

3. Utility Functions

The ReadImageFromFile function reads an image from the specified filepath and returns a Base64 encoded image:

func ReadImageFromFile(filepath string) (models.Base64Image, error) {
    // Implementation details...
}

4. Interactions

The AICompanion interface supports various interactions with AI models, including chat, generate requests, embedding, and moderation:

  • Chat Requests: The SendChatRequest method sends a chat request to an AI model and handles the response.
  • Generate Requests: The SendGenerateRequest method sends a generate request to an AI model and handles the response.
  • Embedding Requests: The SendEmbeddingRequest method sends an embedding request to an AI model and retrieves the response.
  • Moderation Requests: The SendModerationRequest method sends a moderation request to an AI model and retrieves the response.

5. Example Usage

Below is an example of how to use the AICompanion interface:

func main() {
    config := NewDefaultConfig(models.Ollama, "your_api_token", "chat_model", "embedding_model")
    companion := NewCompanion(*config, nil)
    
    // Example usage of methods...
}

Documentation

Index

Constants

View Source
const (
	SystemPrompt        = "You are a helpful assistant"
	EnrichmentPrompt    = "Answer the following query with the provided context"
	SummarizationPrompt = "" /* 145-byte string literal not displayed */

	DefaultHTTPTimeout = 300
	DefaultMaxMessages = 20
)

Variables

View Source
var OllamaEndpoints = models.ApiEndpointUrls{
	ApiChatURL:       "http://localhost:11434/api/chat",
	ApiGenerateURL:   "http://localhost:11434/api/generate",
	ApiEmbedURL:      "http://localhost:11434/api/embed",
	ApiModerationURL: "http://localhost:11434/api/generate",
	ApiModelsURL:     "http://localhost:11434/api/tags",
}
View Source
var OpenAIEndpoints = models.ApiEndpointUrls{
	ApiChatURL:       "https://api.openai.com/v1/chat/completions",
	ApiGenerateURL:   "https://api.openai.com/v1/completions",
	ApiEmbedURL:      "https://api.openai.com/v1/embeddings",
	ApiModerationURL: "https://api.openai.com/v1/moderations",
	ApiModelsURL:     "https://api.openai.com/v1/models",
}

Functions

func NewDefaultConfig added in v0.1.4

func NewDefaultConfig(apiProvider models.ApiProvider, apiToken, chatModel, generateModel, embeddingModel string) *models.Configuration

NewDefaultConfig creates a new default configuration with the provided API provider, API token, and model.

func ReadImageFromFile

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

ReadImageFromFile reads an image from the specified filepath and returns a Base64 encoded image.

Types

type AICompanion

type AICompanion interface {
	// PrepareConversation prepares the conversation by appending system role and current conversation messages.
	PrepareConversation(message models.Message, includeStrategy models.IncludeStrategy) []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)

	// GetEnrichmentPrompt returns the current enrichment prompt
	GetEnrichmentPrompt() string

	// SetEnrichmentPrompt sets a new enrichment prompt
	SetEnrichmentPrompt(prompt string)

	// GetSummarizationPrompt returns the current summarization prompt
	GetSummarizationPrompt() string

	// SetSummarizationPrompt sets a summarization prompt
	SetSummarizationPrompt(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
	GetHttpClient() *http.Client

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

	// interactions
	// GetModels returns all models that the endpoint supports
	GetModels() ([]models.Model, error)

	// SendChatRequest sends a chat request to an AI model and returns a response message
	SendChatRequest(message models.MessageRequest, streaming bool, callback func(m models.Message) error) (models.Message, error)

	// SendCompletionRequest sends a completion request to an AI model and returns a response message
	SendGenerateRequest(message models.MessageRequest, streaming bool, callback func(m models.Message) error) (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)

	// HandleStreamResponse handles streaming responses from an HTTP request.
	HandleStreamResponse(resp *http.Response, streamType models.StreamType, callback func(m models.Message) error) (models.Message, error)

	SendToolRequest(message models.MessageRequest) (models.Message, error)

	// RunFunction runs a function and returns the response
	RunFunction(tool models.Tool, payload models.FunctionPayload) (models.FunctionResponse, 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
impl

Jump to

Keyboard shortcuts

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