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 ¶
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" )
Defined constants for the terminal package
const ( YouNerd = "🤓 You: " AiNerd = "🤖 AI: " )
Defined constants for language
Variables ¶
This section is empty.
Functions ¶
func PrintTypingChat ¶
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 ¶
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 ¶
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.