terminal

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package terminal provides an interface to interact with a Generative AI model in a terminal-based chat application. It manages the chat session, user input, AI communication, and displays the chat history.

The package is designed to be simple to use with a focus on a clean user experience. It includes functionality to handle graceful shutdowns, manage chat history, and simulate typing effects for AI responses.

Copyright (c) 2024 H0llyW00dzZ

Index

Constants

View Source
const (
	SignalMessage = "\nReceived an interrupt, shutting down gracefully..."
	StripChars    = "---"
	NewLineChars  = '\n'
	// this animated chars is magic, it used to show the user that the AI is typing just like human would type
	AnimatedChars = "%c"
	// this model is subject to changed in future
	ModelAi     = "gemini-pro"
	TypingDelay = 100 * time.Millisecond
)

Defined constants for the terminal package

View Source
const (
	YouNerd               = "🤓 You: "
	AiNerd                = "🤖 AI: "
	ContextPrompt         = "Hello! How can I assist you today?"
	ShutdownMessage       = "Shutting down gracefully..."
	UnknownCommand        = "Unknown command."
	ContextPromptShutdown = "The user has issued a quit command. Please provide a shutdown message as you are Assistant."
)

Defined constants for language

View Source
const (
	QuitCommand = ":quit"
	PrefixChar  = ":"
)

Defined constants for commands Note: will add more in future based on the need

View Source
const (
	ErrorGettingShutdownMessage = "Error getting shutdown message from AI:"
)

Defined List error message

Variables

This section is empty.

Functions

func CountTokens added in v0.1.2

func CountTokens(apiKey, input string) (int, error)

CountTokens connects to a generative AI model using the provided API key and counts the number of tokens in the given input string. This function is useful for understanding the token usage of text inputs in the context of generative AI, which can help manage API usage and costs.

Parameters:

apiKey string: The API key used to authenticate with the generative AI service.
input  string: The text input for which the number of tokens will be counted.

Returns:

int:   The number of tokens that the input string contains.
error: An error that occurred while creating the client, connecting to the service,
       or counting the tokens. If the operation is successful, the error is nil.

The function creates a new client for each call, which is then closed before returning. It is designed to be a self-contained operation that does not require the caller to manage the lifecycle of the generative AI client.

Note: This function marked as TODO for now, since it is not used in the main because, a current version of chat system it's consider fully stable with better logic.

func GetEmbedding added in v0.1.2

func GetEmbedding(ctx context.Context, client *genai.Client, modelID, text string) ([]float32, error)

GetEmbedding computes the numerical embedding for a given piece of text using the specified generative AI model. Embeddings are useful for a variety of machine learning tasks, such as semantic search, where they can represent the meaning of text in a form that can be processed by algorithms.

Parameters:

ctx     context.Context: The context for controlling the lifetime of the request. It allows
                         the function to be canceled or to time out, and it carries request-scoped values.
client  *genai.Client:   The client used to interact with the generative AI service. It should be
                         already initialized and authenticated before calling this function.
modelID string:          The identifier for the embedding model to be used. This specifies which
                         AI model will generate the embeddings.
text    string:          The input text to be converted into an embedding.

Returns:

[]float32: An array of floating-point numbers representing the embedding of the input text.
error:     An error that may occur during the embedding process. If the operation is successful,
           the error is nil.

The function delegates the embedding task to the genai client's EmbeddingModel method and retrieves the embedding values from the response. It is the caller's responsibility to manage the lifecycle of the genai.Client, including its creation and closure.

Note: This function marked as TODO for now, since it is not used in the main because, a current version of chat system it's consider fully stable with better logic.

func HandleCommand added in v0.1.3

func HandleCommand(input string, session *Session) (bool, error)

HandleCommand checks if the user input is a command and executes it. It supports the :quit command to gracefully shut down the application.

Parameters:

input string: The user input to be checked for commands.
session *Session: The current chat session for context.

Returns:

bool: A boolean indicating if the input was a command.
error: An error that may occur while handling the command.

func PrintTypingChat

func PrintTypingChat(message string, delay time.Duration)

PrintTypingChat simulates the visual effect of typing out a message character by character. It prints each character of a message to the standard output with a delay between each character to give the appearance of real-time typing.

Parameters:

message string: The message to be displayed with the typing effect.
delay time.Duration: The duration to wait between printing each character.

This function does not return any value. It directly prints to the standard output.

func SendMessage

func SendMessage(ctx context.Context, client *genai.Client, chatContext string) (string, error)

SendMessage sends a chat message to the generative AI model and retrieves the response. It uses the provided `genai.Client` to communicate with the AI service and simulates a chat interaction by sending the provided chat context.

Parameters:

ctx context.Context: The context for controlling the cancellation of the request.
client *genai.Client: The client instance used to send messages to the AI model.
chatContext string: The chat context or message to be sent to the AI model.

Returns:

string: The AI's response as a string.
error: An error message if the message sending or response retrieval fails.

Types

type ChatHistory

type ChatHistory struct {
	Messages []string
}

ChatHistory holds the chat messages exchanged during a session. It provides methods to add new messages to the history and to retrieve the current state of the conversation.

func (*ChatHistory) AddMessage

func (h *ChatHistory) AddMessage(user, text string)

AddMessage appends a new message to the chat history. It takes the username and the text of the message as inputs and formats them before adding to the Messages slice.

Parameters:

user string: The username of the individual sending the message.
text string: The content of the message to be added to the history.

This method does not return any value or error. It assumes that all input is valid and safe to add to the chat history.

func (*ChatHistory) GetHistory

func (h *ChatHistory) GetHistory() string

GetHistory concatenates all messages in the chat history into a single string, with each message separated by a newline character. This provides a simple way to view the entire chat history as a single text block.

Returns:

string: A newline-separated string of all messages in the chat history.

func (*ChatHistory) PrintHistory

func (h *ChatHistory) PrintHistory()

PrintHistory outputs all messages in the chat history to the standard output, one message per line. This method is useful for displaying the chat history directly to the terminal.

Each message is printed in the order it was added, preserving the conversation flow. This method does not return any value or error.

type Session

type Session struct {
	Client      *genai.Client      // Client is the generative AI client used to communicate with the AI model.
	ChatHistory ChatHistory        // ChatHistory stores the history of the chat session.
	Ctx         context.Context    // Ctx is the context governing the session, used for cancellation.
	Cancel      context.CancelFunc // Cancel is a function to cancel the context, used for cleanup.
}

Session encapsulates the state and functionality for a chat session with a generative AI model. It holds the AI client, chat history, and context for managing the session lifecycle.

func NewSession

func NewSession(apiKey string) (*Session, error)

NewSession creates a new chat session with the provided API key for authentication. It initializes the generative AI client and sets up a context for managing the session.

Parameters:

apiKey string: The API key used for authenticating requests to the AI service.

Returns:

*Session: A pointer to the newly created Session object.
error: An error object if initialization fails.

func (*Session) Start

func (s *Session) Start()

Start begins the chat session, managing user input and AI responses. It sets up a signal listener for graceful shutdown and enters a loop to read user input and fetch AI responses indefinitely until an interrupt signal is received.

This method handles user input errors and AI communication errors by logging them and exiting. It ensures resources are cleaned up properly on exit by deferring the cancellation of the session's context and the closure of the AI client.

Jump to

Keyboard shortcuts

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