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
- Variables
- func ApplyBold(text string, delimiter string, color string) stringdeprecated
- func ApplyFormatting(text string, delimiter string, color string, formatting map[string]string) string
- func ApplyOptions(m *genai.GenerativeModel, configs ...ModelConfig) (bool, error)
- func CheckLatestVersion(currentVersion string) (isLatest bool, latestVersion string, err error)
- func Colorize(text string, colorPairs []string, keepDelimiters map[string]bool, ...) string
- func ConfigureModel(model *genai.GenerativeModel, opts ...ModelConfig)
- func ConfigureModelForSession(ctx context.Context, client *genai.Client, session *Session) *genai.GenerativeModel
- func CountTokens(apiKey, input string) (int, error)
- func FilterLanguageFromCodeBlock(text string) string
- func GetEmbedding(ctx context.Context, client *genai.Client, modelID, text string) ([]float32, error)
- func HandleCommand(input string, session *Session) (bool, error)
- func HandleUnrecognizedCommand(command string, session *Session, parts []string) (bool, error)deprecated
- func IsANSISequence(runes []rune, index int) booldeprecated
- func IsLastMessage(index int, messages []string) booldeprecated
- func PrintANSISequence(runes []rune, index int) intdeprecated
- func PrintAnotherVisualSeparator()deprecated
- func PrintPrefixWithTimeStamp(prefix string)
- func PrintTypingChat(message string, delay time.Duration)
- func RecoverFromPanic() func()deprecated
- func ReplaceTripleBackticks(text, placeholder string) stringdeprecated
- func SendDummyMessage(client *genai.Client) (bool, error)
- func SendMessage(ctx context.Context, client *genai.Client, chatContext string, ...) (string, error)
- func SingleCharColorize(text string, delimiter string, color string) string
- func ToASCIIArt(input string, style ASCIIArtStyle) (string, error)
- type ANSIColorCodes
- type ASCIIArtChar
- type ASCIIArtStyle
- type BinaryAnsiChars
- type ChatConfig
- type ChatHistory
- func (h *ChatHistory) AddMessage(user string, text string, config *ChatConfig)
- func (h *ChatHistory) Clear()
- func (h *ChatHistory) ClearAllSystemMessages()
- func (h *ChatHistory) FilterMessages(predicate func(string) bool) []string
- func (h *ChatHistory) GetHistory(config *ChatConfig) string
- func (h *ChatHistory) GetMessageStats() MessageStats
- func (h *ChatHistory) IncrementMessageTypeCount(messageType MessageType) booldeprecated
- func (h *ChatHistory) PrintHistory()deprecated
- func (h *ChatHistory) RemoveMessages(numMessages int, messageContent string)
- func (h *ChatHistory) SanitizeMessage(message string) string
- type ChatWorker
- type CommandHandler
- type CommandRegistry
- type DebugOrErrorLogger
- func (l *DebugOrErrorLogger) Any(format string, v ...interface{})
- func (l *DebugOrErrorLogger) Debug(format string, v ...interface{})
- func (l *DebugOrErrorLogger) Error(format string, v ...interface{})
- func (l *DebugOrErrorLogger) HandleGoogleAPIError(err error) bool
- func (l *DebugOrErrorLogger) HandleOtherStupidAPIError(err error, apiName string) bool
- func (l *DebugOrErrorLogger) Info(format string, v ...interface{})
- func (l *DebugOrErrorLogger) RecoverFromPanic()
- type ErrorHandlerFunc
- type GitHubRelease
- type MessageStats
- type MessageType
- type ModelConfig
- type NewLineChar
- type RetryableFunc
- type SafetyOption
- type SafetySetterdeprecated
- type SafetySettings
- type Session
- type TypingChars
- type Worker
Constants ¶
const ( // Note: By replacing the ANSI escape sequence from "\033" to "\x1b", might can avoid a rare bug that sometimes occurs on different machines, // although the original code works fine on mine (Author: @H0llyW00dzZ). ColorRed = "\x1b[31m" ColorGreen = "\x1b[32m" ColorYellow = "\x1b[33m" ColorBlue = "\x1b[34m" ColorPurple = "\x1b[35m" ColorCyan = "\x1b[36m" // ColorHex95b806 represents the color #95b806 using an ANSI escape sequence for 24-bit color. ColorHex95b806 = "\x1b[38;2;149;184;6m" // ColorCyan24Bit represents the color #11F0F7 using an ANSI escape sequence for 24-bit color. ColorCyan24Bit = "\x1b[38;2;17;240;247m" ColorPurple24Bit = "\x1b[38;2;255;0;255m" ColorReset = "\x1b[0m" )
ANSI color codes
const ( // bold text. BoldText = "\x1b[1m" // reset bold text formatting. ResetBoldText = "\x1b[22m" // italic text ItalicText = "\x1B[3m" // reset italic text formatting. ResetItalicText = "\x1B[23m" )
ANSI Text Formatting.
const ( // GitHubAPIURL is the endpoint for the latest release information of the application. GitHubAPIURL = "https://api.github.com/repos/H0llyW00dzZ/GoGenAI-Terminal-Chat/releases/latest" GitHubReleaseFUll = "https://api.github.com/repos/H0llyW00dzZ/GoGenAI-Terminal-Chat/releases/tags/%s" // CurrentVersion represents the current version of the application. CurrentVersion = "v0.6.9" )
Defined List of GitHub API
const ( SignalMessage = " Received an interrupt, shutting down gracefully..." // fix formatting ^C in linux/unix RecoverGopher = "%sRecovered from panic:%s %s%v%s" ObjectHighLevelString = "%s %s" // Catch High level string ObjectHighLevelStringWithSpace = "%s %s " // Catch High level string with space ObjectHighLevelStringWithNewLine = "%s %s\n" // Catch High level string With NewLine ObjectTripleHighLevelString = "%%%s%%" // Catch High level triple string ObjectHighLevelContextString = "%s\n%s" // Catch High level context string // TimeFormat is tailored for AI responses, providing a layout conducive to formatting chat transcripts. TimeFormat = "2006/01/02 15:04:05" OtherTimeFormat = "January 2, 2006 at 15:04:05" 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" // this may subject to changed in future for example can customize the delay TypingDelay = 60 * time.Millisecond // this clearing chat history in secret storage ChatHistoryClear = ColorHex95b806 + "All Chat history cleared." + ColorReset // reset total token usage ResetTotalTokenUsage = ColorHex95b806 + "Total token usage has been reset." + ColorReset // clear sys summary messagess ChatSysSummaryMessages = ColorHex95b806 + "All System Summary Messages have been cleared." + ColorReset )
Defined constants for the terminal package
const ( YouNerd = "🤓 You:" AiNerd = "🤖 AI:" TokenEmoji = "🪙 Token count:" StatisticsEmoji = "📈 Total Token:" ShieldEmoji = "☠️ Safety:" ContextPrompt = "Hello! How can I assist you today?" ShutdownMessage = "Shutting down gracefully..." ContextCancel = "Context canceled, shutting down..." // sending a messages to gopher officer ANewVersionIsAvailable = StripChars + "\nA newer version is available: %s\n\n" ReleaseName = "- %s\n\n" FullChangeLog = "**%s**\n" DummyMessages = "Hello, AI! from @H0llyW00dzZ" // Better prompt instead of typing manually hahaha ApplicationName = "GoGenAI Terminal Chat" // Check Version Prompt commands YouAreusingLatest = StripChars + "\nThe user invoked the command: **%s**\n" + "The current version of **%s** is: **%s**.\n" + "This is the latest version available.\n" + "Please inform the user that no update is necessary at this time." // Better Response for AI ReleaseNotesPrompt = StripChars + "\nThe user invoked the command: **%s**\n" + "The current version of the application **%s** is: **%s**.\n" + "There is a newer version available: **%s**.\n\n" + "Details of the latest release:\n" + "- Release Name: **%s**\n" + "- Published Date: **%s**\n\n" + "Release Notes:\n%s\n" // Better Response for AI // Quit Prompt commands ContextPromptShutdown = StripChars + "\nThe user has attempted the command: **%s** in **%s**.\n" + "AI, please provide an appropriate shutdown message." // Help Prompt commands HelpCommandPrompt = StripChars + "\n**This a System messages**:**%s**\n\n" + "The user attempted an command: **%s**\n" + "Can you provide help information for the available commands?\n\n" + "List of Available Commands:\n" + "**%s** or **%s**: Quit the application.\n" + "**%s** or **%s**: Show this help information.\n" + "**%s**: Check the application version.\n" + "**%s**: Set the safety level - **%s** (low), **%s** (default), **%s** (high).\n" + "**%s** <text> **%s** <target language>: Translate text to the specified language.\n" + "**%s** **%s** <number>: Generate a random string of the specified length.\n" + "**%s**: Summarize a current conversation\n\n" + "**Note**: When you summarize a current conversation, it will be displayed at the top of the chat history.\n\n" + "**%s %s** **%s**: Show the chat history.\n\n" + "**%s** **%s**: Show the chat statistic.\n\n" + "**%s** **%s**: Clear all system summary messages from the chat history.\n\n" + "**%s** **%s**: Clear all chat history and reset the total token usage count if enabled.\n\n" + "**%s** **%s** <**path/file/data.txt**> or <**data.txt**>: Counts a token from the specified file.\n\n" + "**Note**: The token count file feature only supports files with the extensions .md and .txt.\n\n" + "**Additional Note**: There are no additional commands or HTML Markdown available " + "because this is a terminal application and is limited.\n" // TranslateCommandPrompt commands AITranslateCommandPrompt = "**This a System messages**:**%s**\n\n" + "The user attempted an command: **%s**\n" + "Can you translate requested by user?\n" + "Text:\n**%s**\n" + "Translate To:\n **%s**" )
Defined constants for language
const ( QuitCommand = ":quit" ShortQuitCommand = ":q" // Short quit command VersionCommand = ":checkversion" HelpCommand = ":help" ShortHelpCommand = ":h" // Short help command SafetyCommand = ":safety" AITranslateCommand = ":aitranslate" LangArgs = ":lang" CryptoRandCommand = ":cryptorand" LengthArgs = ":length" ShowCommands = ":show" ChatCommands = ":chat" SummarizeCommands = ":summarize" ClearCommand = ":clear" StatsCommand = ":stats" TokenCountCommands = ":tokencount" FileCommands = ":file" PingCommand = ":ping" // Currently marked as TODO PrefixChar = ":" // List args ChatHistoryArgs = "history" )
Defined constants for commands
Note: will add more in future based on the need, for example, to change the model, or to change the delay, another thing is syncing ai with goroutine (known as gopher)
const ( ErrorGettingShutdownMessage = "Error getting shutdown message from AI: %v" ErrorHandlingCommand = "Error handling command: %v" ErrorCountingTokens = "Error counting tokens: %v\n" ErrorSendingMessage = "Error sending message to AI: %v" ErrorReadingUserInput = "Error reading user input: %v" ErrorFailedToFetchReleaseInfo = "Failed to fetch the latest release info: %v" ErrorReceivedNon200StatusCode = "[Github] [Check Version] Received non-200 status code: %v Skip Retrying" // Github non 500 lmao ErrorFailedToReadTheResponseBody = "Failed to read the response body: %v" ErrorFaileduUnmarshalTheReleaseData = "Failed to unmarshal the release data: %v" ErrorFailedTagToFetchReleaseInfo = "Failed to fetch release info for tag '%s': %v" ErrorFailedTagUnmarshalTheReleaseData = "Failed to unmarshal release data for tag '%s': %v" ErrorFailedTosendmessagesToAI = "Failed to send messages to AI: %v" ErrorFailedToCreateNewAiClient = "Failed to create new AI client: %v" ErrorFailedToStartAIChatSessionAttempt = "Failed to start AI chat session, attempt %d/%d" ErrorFailedtoStartAiChatSessionAfter = "Failed to start AI chat session after %d attempts" ErrorChatSessionisnill = "chat session is nil" ErrorFailedtoStartAiChatSession = "failed to start AI chat session" ErrorFailedToRenewSession = "Failed to renew session: %v" ErrorAiChatSessionStillNill = "AI chat session is still nil after renewal attempt" ErrorLowLevelFailedtoStartAiChatSession = "failed to start a new AI chat session: %w" ErrorUserAttemptUnrecognizedCommandPrompt = "**From System**:**%s**\n\nThe user attempted an unrecognized command: **%s**" // Better Response for AI ErrorFailedtoSendUnrecognizedCommandToAI = "Failed to send unrecognized command to AI: %v" HumanErrorWhileTypingCommandArgs = "Invalid Command Arguments: %v" ErrorPingFailed = "Ping failed: %v" ErrorUnrecognizedCommand = "Unrecognized command: %s" ErrorUnrecognizedSubCommand = "Unrecognized %s command sub commands/args : %s" ErrorLowLevelCommand = "command cannot be empty" ErrorUnknown = "An error occurred: %v" ErrorUnknownSafetyLevel = "Unknown safety level: %s" ErrorInvalidApiKey = "Invalid API key: %v" ErrorFailedToStartSession = "Failed To Start Session: %v" ErrorLowLevelNoResponse = "no response from AI service" ErrorLowLevelMaximumRetries = "maximum retries reached without success" // low level ErrorLowLevelFailedToCountTokensAfterRetries = "failed to count tokens after retries" // low level ErrorNonretryableerror = "Failed to send messages after %d retries due to a non-retryable error: %v" ErrorFailedToSendHelpMessage = "Failed to send help message: %v" ErrorFailedToSendHelpMessagesAfterRetries = "Failed to send help message after retries" // low level ErrorFailedToSendShutdownMessage = "Failed to send shutdown message: %v" ErrorFailedToSendVersionCheckMessage = "Failed to send version check message: %v" ErrorFailedToSendVersionCheckMessageAfterReties = "Failed to send version check message after retries" // low level ErrorFailedToSendTranslationMessage = "Failed to send translation message: %v" ErrorFailedToSendTranslationMessageAfterRetries = "Failed to send translation message after retries" // low level ErrorFailedToApplyModelConfiguration = "failed to apply model configuration" // low level ErrorMaxOutputTokenMustbe = "maxOutputTokens must be 50 or higher, got %d" // low level ErrorFailedToSendSummarizeMessage = "Failed To Send Summarize Message: %v" ErrorFailedToSendSummarizeMessageAfterRetries = "failed to send summarize message after retries" // low level ErrorUnrecognizedSubcommandForClear = "Unrecognized subcommand for clear: %s" ErrorFailedToReadFile = "Failed to read file: %v" ErrorFailedToCountTokens = "Failed to count tokens: %v" ErrorUnrecognizedSubcommandForTokenCount = "Unrecognized subcommand for token count: %s" ErrorInvalidFileExtension = "Invalid file extension: %v" ErrorFileTypeNotSupported = "file type not supported: only .md and .txt files are allowed." // Low level error ErrorFailedToSendCommandToAI = "Failed to send command to AI: %v" // List Error not because of this go codes, it literally google apis issue // that so bad can't handle this a powerful terminal Error500GoogleApi = "googleapi: Error 500:" ErrorGoogleInternal = "Google Internal Error: %s" ErrorGenAiReceiveNil = "received a nil option function" // low level ErrorGenAI = "GenAI Error: %v" // List Error Figlet include high and low level error ErrorStyleIsEmpty = "style is empty" // low level ErrorCharacterNotFoundinStyle = "character %q not found in style" // low level ErrorToASCIIArtbuildOutput = "ToASCIIArt buildOutput error: %v" // High Level ErrorToASCIIArtcheckstyle = "ToASCIIArt checkStyle error: %v" // High Level // List Error Tools ErrorInvalidLengthArgs = "Invalid length argument: %v" // high level ErrorFailedtoGenerateRandomString = "Failed to generate random string: %v" // high level // List Other Error not because of this go codes // ErrorOtherAPI represents an error received from an external API server. // It indicates non-client-related issues, such as server-side errors (e.g., HTTP 500 errors) indicate that so fucking bad hahaha. ErrorOtherAPI = "Error: %s API server error: %v" )
Defined List error message
const ( SingleAsterisk = "*" DoubleAsterisk = "**" SingleBacktick = "`" TripleBacktick = "```" SingleUnderscore = "_" StringNewLine = "\n" BinaryAnsiChar = '\x1b' BinaryLeftSquareBracket = '[' BinaryAnsiSquenseChar = 'm' BinaryAnsiSquenseString = "m" BinaryRegexAnsi = `\x1b\[[0-9;]*m` CodeBlockRegex = "```\\w+" SanitizeTextAIResponse = "\n---\n" )
Defined List of characters
const ( DEBUG_MODE = "DEBUG_MODE" DEBUGPREFIX = "🔎 DEBUG:" // Note: Currently only executing CMD,RetryPolicy, will add more later DEBUGEXECUTINGCMD = "Executing " + ColorHex95b806 + "%s" + ColorReset + " command with parts: " + ColorHex95b806 + "%#v" + ColorReset DEBUGRETRYPOLICY = "Retry Policy Attempt %d: error occurred - %v" SHOW_PROMPT_FEEDBACK = "SHOW_PROMPT_FEEDBACK" PROMPTFEEDBACK = "Rating for category " + ColorHex95b806 + "%s" + ColorReset + ": " + ColorHex95b806 + "%s" + ColorReset SHOW_TOKEN_COUNT = "SHOW_TOKEN_COUNT" TokenCount = ColorHex95b806 + "%d" + ColorReset + " tokens\n" TotalTokenCount = "usage of this Session " + ColorHex95b806 + "%d" + ColorReset + " tokens" // Note: This is separate from the main package and is used for the token counter. The token counter is external and not a part of the Gemini session. API_KEY = "API_KEY" )
Defined List of Environment variables
const ( // Note: This is a prefix for the system SYSTEMPREFIX = "⚙️ SYSTEM:" SystemSafety = "Safety level set to " + ColorHex95b806 + "%s" + ColorReset + "." Low = "low" Default = "default" High = "high" MonitoringSignal = "Received signal: %v.\n" ShowChatHistory = "Chat History:\n\n%s" SummarizePrompt = StripChars + "\nIn 200 words or less, provide a brief summary of the ongoing discussion.\n" + "This summary will serve as a prompt for contextual reference in future interactions:\n\n" ListChatStats = statsEmoji + " List of Chat Statistics for This Session:\n\n" + youNerd + " User messages: " + ColorHex95b806 + BoldText + "%d" + ResetBoldText + ColorReset + "\n" + aiNerd + " AI messages: " + ColorHex95b806 + BoldText + "%d" + ResetBoldText + ColorReset + "\n" + sysEmoji + " System messages: " + ColorHex95b806 + BoldText + "%d" + ResetBoldText + ColorReset InfoTokenCountFile = "The file " + ColorHex95b806 + BoldText + "%s" + ResetBoldText + ColorReset + " contains " + ColorHex95b806 + BoldText + "%d" + ResetBoldText + ColorReset + " tokens." )
Defined Prefix System
const ( CryptoRandLength = "Length: %s" CryptoRandStringRes = "Random String: %s" CryptoRandRes = "Length: %s\n\nRandom String: %s" )
Defined Tools
const ( // NOTE: ' is rune not a string G = 'G' V = 'V' N = 'N' A_ = "/ /_/ / /_/ / /_/ / __/ / / / ___ |_/ / " I_ = "\\____/\\____/\\____/\\___/_/ /_/_/ |_/___/ " // Blank Art BLANK_ = " " )
ASCII Art
const ( Current_Version = "Current Version: " + ColorHex95b806 + CurrentVersion + ColorReset // Acknowledgment of the original author is appreciated as this project is developed in an open-source environment. Copyright = "Copyright (©️) 2024 @H0llyW00dzZ All rights reserved." TIP = "* " + ColorHex95b806 + "Use the commands " + ColorReset + BoldText + ColorYellow + ShortHelpCommand + ColorYellow + BoldText + ColorHex95b806 + " or " + ColorReset + BoldText + ColorYellow + HelpCommand + ColorReset + BoldText + ColorHex95b806 + " to display a list of available commands." + ColorReset )
Text
const (
Code500 = "500" // indicate that server so bad hahaha
)
List RestfulAPI Error
const (
ContextUserInvokeTranslateCommands = "Translating to %s: %s"
)
Context RAM's labyrinth
Variables ¶
var AsciiColors = map[rune]string{ G: BoldText + colors.ColorHex95b806, V: BoldText + colors.ColorCyan24Bit, }
Define a map for character Ascii colors
Deprecated: This variable is no longer used, and was replaced by NewASCIIArtStyle().
var AsciiPatterns = map[rune][]string{ G: { _G, _O, _GEN, A_, I_, }, V: { BLANK_, BLANK_, BLANK_, Current_Version, Copyright, }, }
Define the ASCII patterns for the 'slant' font for the characters
Deprecated: This variable is no longer used, and was replaced by NewASCIIArtStyle().
Functions ¶
func ApplyBold
deprecated
added in
v0.3.7
func ApplyFormatting ¶ added in v0.3.7
func ApplyFormatting(text string, delimiter string, color string, formatting map[string]string) string
ApplyFormatting applies text formatting based on the provided delimiter. If the delimiter is recognized, it applies the appropriate ANSI formatting codes.
Parameters:
text string: The text to format. delimiter string: The delimiter that indicates what kind of formatting to apply. color string: The ANSI color code to apply to the text. formatting map[string]string: A map of delimiters to their corresponding ANSI formatting codes.
Returns:
string: The formatted text.
func ApplyOptions ¶ added in v0.5.6
func ApplyOptions(m *genai.GenerativeModel, configs ...ModelConfig) (bool, error)
ApplyOptions is a convenience function that applies a series of configuration options to a GenerativeModel. This allows for flexible and dynamic model configuration at runtime.
Parameters:
m *genai.GenerativeModel: The generative AI model to configure. configs ...ModelConfig: A variadic number of configuration options.
Returns:
bool: A boolean indicating whether the options were applied successfully. error: An error if any of the configuration options are nil.
func CheckLatestVersion ¶ added in v0.2.1
CheckLatestVersion compares the current application version against the latest version available on GitHub. It fetches the latest release information from the repository specified by GitHubAPIURL and determines if an update is available.
Parameters:
currentVersion string: The version string of the currently running application.
Returns:
isLatest bool: A boolean indicating if the current version is the latest available. latestVersion string: The tag name of the latest release, if newer than current; otherwise, an empty string. err error: An error if the request fails or if there is an issue parsing the response.
func Colorize ¶ added in v0.1.6
func Colorize(text string, colorPairs []string, keepDelimiters map[string]bool, formatting map[string]string) string
Colorize applies ANSI color codes to the text surrounded by specified delimiters. It can process multiple delimiters, each with a corresponding color. The function can also conditionally retain or remove the delimiters in the final output.
Parameters:
text string: The text to be colorized. colorPairs []string: A slice where each pair of elements represents a delimiter and its color. keepDelimiters map[string]bool: A map to indicate whether to keep the delimiter in the output. formatting map[string]string: A map of delimiters to their corresponding ANSI formatting codes.
Returns:
string: The colorized text.
Note: This function may not work as expected in Windows Command Prompt due to its limited support for ANSI color codes. It is designed for terminals that support ANSI, such as those in Linux/Unix environments.
func ConfigureModel ¶ added in v0.5.8
func ConfigureModel(model *genai.GenerativeModel, opts ...ModelConfig)
ConfigureModel applies a series of configuration options to a GenerativeModel. This function is variadic, meaning it can accept multiple configuration options that will be applied in the order they are provided.
Parameters:
model *genai.GenerativeModel: The generative AI model to configure. opts ...ModelConfig: A variadic number of configuration options.
func ConfigureModelForSession ¶ added in v0.6.7
func ConfigureModelForSession(ctx context.Context, client *genai.Client, session *Session) *genai.GenerativeModel
ConfigureModelForSession prepares and configures a generative AI model for use in a chat session. It applies safety settings from the session to the model and sets additional configuration options such as temperature. This function is essential for ensuring that the AI model behaves according to the desired safety guidelines and operational parameters before engaging in a chat session.
Parameters:
ctx context.Context: A context.Context that carries deadlines, cancellation signals, and other request-scoped values across API boundaries and between processes. client *genai.Client: A pointer to a genai.Client, which provides the functionality to interact with the generative AI service. session *Session: A pointer to a Session struct that contains the current chat session's state, including safety settings and chat history.
Returns:
*genai.GenerativeModel: A pointer to a generative AI model that is configured and ready for initiating a chat session.
Note: The function assumes that the client has been properly initialized and that the session contains valid safety settings. If no safety settings are present in the session, default safety settings are applied.
func CountTokens ¶ added in v0.1.2
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.
func FilterLanguageFromCodeBlock ¶ added in v0.4.9
FilterLanguageFromCodeBlock searches for Markdown code block delimiters with a language identifier (e.g., "```go") and removes the language identifier, leaving just the code block delimiters. This function is useful when the language identifier is not required, such as when rendering plain text or when the syntax highlighting is not supported.
The function uses a precompiled regular expression `filterCodeBlock` that matches the pattern of triple backticks followed by any word characters (representing the language identifier). It replaces this pattern with just the triple backticks, effectively stripping the language identifier from the code block.
Parameters:
text (string): The input text containing Markdown code blocks with language identifiers.
Returns:
string: The modified text with language identifiers removed from all code blocks.
Example:
input := "Here is some Go code:\n```go\nfmt.Println(\"Hello, World!\")\n```" output := FilterLanguageFromCodeBlock(input) // output now contains "Here is some Go code:\n```\nfmt.Println(\"Hello, World!\")\n```"
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
HandleCommand interprets the user input as a command and executes the associated action. It uses a map of command strings to their corresponding handler functions to manage different commands and their execution. If the command is recognized, the respective handler is called; otherwise, an unknown command message is displayed.
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 and was handled. error: An error that may occur while handling the command.
func HandleUnrecognizedCommand
deprecated
added in
v0.4.1
HandleUnrecognizedCommand takes an unrecognized command and the current session, constructs a prompt to inform the AI about the unrecognized command, and sends this information to the AI service. This function is typically called when a user input is detected as a command but does not match any of the known command handlers.
Parameters:
command string: The unrecognized command input by the user. session *Session: The current chat session containing state and context, including the AI client.
Returns:
bool: Always returns false as this function does not result in a command execution. error: Returns an error if sending the message to the AI fails; otherwise, nil.
The function constructs an error prompt using the application's name and the unrecognized command, retrieves the current chat history, and sends this information to the AI service. If an error occurs while sending the message, the function logs the error and returns an error to the caller.
Deprecated: This method is no longer used, and was replaced by CommandRegistry.
func IsANSISequence
deprecated
added in
v0.3.5
func IsLastMessage
deprecated
added in
v0.5.9
func PrintANSISequence
deprecated
added in
v0.3.5
func PrintAnotherVisualSeparator
deprecated
added in
v0.4.9
func PrintAnotherVisualSeparator()
PrintAnotherVisualSeparator prints a visual separator to the standard output.
Deprecated: This method is no longer used, and was replaced by NewASCIIArtStyle().
func PrintPrefixWithTimeStamp ¶ added in v0.2.2
func PrintPrefixWithTimeStamp(prefix string)
PrintPrefixWithTimeStamp prints a message to the terminal, prefixed with a formatted timestamp. The timestamp is formatted according to the TimeFormat constant.
For example, with TimeFormat set to "2006/01/02 15:04:05" and the prefix "🤓 You: ", the output might be "2024/01/10 16:30:00 🤓 You:".
This function is designed for terminal outputs that benefit from a timestamped context, providing clarity and temporal reference for the message displayed.
The prefix parameter is appended to the timestamp and can be a log level, a descriptor, or any other string that aids in categorizing or highlighting the message.
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.
Note: This is particularly useful for simulating the Gopher's lifecycle (Known as Goroutines) events in a user-friendly manner. For instance, when a Gopher completes a task or job and transitions to a resting state, this function can print a message with a typing effect to visually represent the Gopher's "sleeping" activities.
func RecoverFromPanic
deprecated
added in
v0.2.2
func RecoverFromPanic() func()
RecoverFromPanic returns a deferred function that recovers from panics within a goroutine or function, preventing the panic from propagating and potentially causing the program to crash. Instead, it logs the panic information using the standard logger, allowing for post-mortem analysis without interrupting the program's execution flow.
Usage:
defer terminal.RecoverFromPanic()()
The function returned by RecoverFromPanic should be called by deferring it at the start of a goroutine or function. When a panic occurs, the deferred function will handle the panic by logging its message and stack trace, as provided by the recover built-in function.
Deprecated: This method is deprecated was replaced by logger.RecoverFromPanic.
func ReplaceTripleBackticks
deprecated
added in
v0.4.10
func SendDummyMessage ¶ added in v0.4.2
SendDummyMessage verifies the validity of the API key by sending a dummy message.
Parameters:
client *genai.Client: The AI client used to send the message.
Returns:
A boolean indicating the validity of the API key. An error if sending the dummy message fails.
func SendMessage ¶
func SendMessage(ctx context.Context, client *genai.Client, chatContext string, session *Session) (string, error)
SendMessage sends a chat message to the generative AI model and retrieves the response. It constructs a chat session using the provided `genai.Client`, which is used to communicate with the AI service. The function simulates a chat interaction by sending the chat context, which may include a portion of the previous chat history determined by the session's ChatConfig, to the AI model for generating a response.
Parameters:
ctx context.Context: The context for controlling the cancellation of the request. client *genai.Client: The client instance used to create a generative model session and send messages to the AI model. chatContext string: The chat context or message to be sent to the AI model. session *Session: The current chat session containing the ChatHistory and ChatConfig. The ChatConfig determines how much of the chat history is sent to the AI for context.
Returns:
string: The AI's response as a string, which includes the AI's message with a simulated typing effect. error: An error message if the message sending or response retrieval fails. If the operation is successful, the error is nil.
The function initializes a new chat session and sends the chat context, along with the portion of chat history specified by the session's ChatConfig, to the generative AI model. It then calls `printResponse` to process and print the AI's response. The final AI response is returned as a concatenated string of all parts from the AI response.
func SingleCharColorize ¶ added in v0.1.8
SingleCharColorize applies ANSI color codes to text surrounded by single-character delimiters. It is particularly useful when dealing with text that contains list items or other elements that should be highlighted, and it ensures that the colorization is only applied to the specified delimiter at the beginning of a line.
Parameters:
text string: The text containing elements to be colorized. delimiter string: The single-character delimiter indicating the start of a colorizable element. color string: The ANSI color code to be applied to the elements starting with the delimiter.
Returns:
string: The resulting string with colorized elements as specified by the delimiter.
This function handles each line separately and checks for the presence of the delimiter at the beginning after trimming whitespace. If the delimiter is found, it colorizes the delimiter and the following character (typically a space). The rest of the line remains unaltered. If the delimiter is not at the beginning of a line, the line is added to the result without colorization.
Note: As with the Colorize function, SingleCharColorize may not function correctly in Windows Command Prompt or other environments that do not support ANSI color codes. It is best used in terminals that support these codes, such as most Linux/Unix terminals.
func ToASCIIArt ¶ added in v0.4.5
func ToASCIIArt(input string, style ASCIIArtStyle) (string, error)
ToASCIIArt converts a string to its ASCII art representation using a given style. Each character in the input string is mapped to an ASCIIArtChar based on the provided style, and the resulting ASCII art is constructed line by line. If a character in the input string does not have an ASCII art representation in the style, it will be omitted from the output.
The function returns the complete ASCII art as a string, where each line of the art is separated by a newline character. If the style is empty or a character is not found in the style, an error is returned.
Example usage:
art, err := ToASCIIArt("G", slantStyle) if err != nil { log.Fatal(err) } fmt.Println(art)
Parameters:
input - The string to be converted into ASCII art. style - The ASCIIArtStyle used to style the ASCII art representation.
Returns:
A string representing the ASCII art of the input text, and an error if the style is empty or a character is not found in the style.
Types ¶
type ANSIColorCodes ¶ added in v0.2.9
type ANSIColorCodes struct { ColorRed string ColorGreen string ColorYellow string ColorBlue string ColorPurple string ColorCyan string ColorHex95b806 string // 24-bit color ColorCyan24Bit string // 24-bit color ColorPurple24Bit string // 24-bit color ColorReset string }
ANSIColorCodes defines a struct for holding ANSI color escape sequences.
type ASCIIArtChar ¶ added in v0.4.6
type ASCIIArtChar struct { Pattern []string // Lines of the ASCII representation of the character. Color string // Color code or label for the character's color. }
ASCIIArtChar represents a styled ASCII character with its pattern and color. The Pattern field is a slice of strings, with each string representing a line of the character's ASCII representation. The Color field specifies the color to be used when displaying the character.
type ASCIIArtStyle ¶ added in v0.4.6
type ASCIIArtStyle map[rune]ASCIIArtChar
ASCIIArtStyle maps runes to their corresponding ASCIIArtChar representations. It defines the styling for each character that can be rendered in ASCII art.
func MergeStyles ¶ added in v0.4.6
func MergeStyles(styles ...ASCIIArtStyle) ASCIIArtStyle
MergeStyles combines multiple ASCIIArtStyle objects into a single style. In case of overlapping characters, the patterns from the styles appearing later in the arguments will overwrite those from earlier ones. This function is useful when you want to create a composite style that includes patterns from multiple sources or to override specific characters' styles in a base style.
Parameters:
styles ...ASCIIArtStyle - A variadic slice of ASCIIArtStyle objects to be merged.
Returns:
A new ASCIIArtStyle object that is the union of all provided styles.
func NewASCIIArtStyle ¶ added in v0.4.6
func NewASCIIArtStyle() ASCIIArtStyle
NewASCIIArtStyle creates and returns a new ASCIIArtStyle map. It initializes an empty map that can be populated with ASCII art characters using the AddChar method or by direct assignment.
func (ASCIIArtStyle) AddChar ¶ added in v0.4.6
func (style ASCIIArtStyle) AddChar(char rune, pattern []string, color string)
AddChar adds a new character with its ASCII art representation to the style. If the character already exists in the style, its pattern and color are updated with the new values provided.
Parameters:
char - The rune representing the character to add or update. pattern - A slice of strings representing the ASCII art pattern for the character. color - A string representing the color for the character.
type BinaryAnsiChars ¶ added in v0.3.0
type BinaryAnsiChars struct { BinaryAnsiChar rune BinaryAnsiSquenseChar rune BinaryAnsiSquenseString string BinaryLeftSquareBracket rune }
BinaryAnsiChars is a struct that contains the ANSI characters used to print the typing effect.
type ChatConfig ¶ added in v0.5.5
type ChatConfig struct { // HistorySize specifies the total number of chat messages to retain in the session's history. // This helps in limiting the memory footprint and ensures that only recent interactions // are considered for maintaining context. HistorySize int // HistorySendToAI indicates the number of recent messages from the history to be included // when sending context to the AI. This allows the AI to generate responses that are // relevant to the current conversation flow without being overwhelmed by too much history. HistorySendToAI int }
ChatConfig encapsulates settings that affect the management of chat history during a session with the generative AI. It determines the amount of chat history retained in memory and the portion of that history used to provide context to the AI.
func DefaultChatConfig ¶ added in v0.5.5
func DefaultChatConfig() *ChatConfig
DefaultChatConfig constructs a new ChatConfig with pre-defined default values. These defaults are chosen to balance the need for context awareness by the AI and efficient memory usage. The function is useful for initializing chat sessions with standard behavior without requiring manual configuration.
Returns:
*ChatConfig: A pointer to a ChatConfig instance populated with default settings.
type ChatHistory ¶
type ChatHistory struct { Messages []string // Messages contains all the chat messages in chronological order. Hashes map[string]int // Hashes maps the SHA-256 hash of each message to its index in Messages. UserMessageCount int // UserMessageCount holds the total number of user messages. AIMessageCount int // AIMessageCount holds the total number of AI messages. SystemMessageCount int // SystemMessageCount holds the total number of system messages. // contains filtered or unexported fields }
ChatHistory manages the state of chat messages exchanged during a session. It tracks the messages, their unique hashes, and counts of different types of messages (user, AI, system). This struct also ensures concurrent access safety using a read-write mutex.
func NewChatHistory ¶ added in v0.4.8
func NewChatHistory() *ChatHistory
NewChatHistory creates and initializes a new ChatHistory struct. It sets up an empty slice for storing messages and initializes the hash map used to track unique messages. A new, random seed is generated for hashing to ensure the uniqueness of hash values across different instances.
Returns:
*ChatHistory: A pointer to the newly created ChatHistory struct ready for use.
func (*ChatHistory) AddMessage ¶
func (h *ChatHistory) AddMessage(user string, text string, config *ChatConfig)
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. config *ChatConfig: Configuration parameters for the chat session, including history size.
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) Clear ¶ added in v0.3.2
func (h *ChatHistory) Clear()
Clear removes all messages from the chat history, effectively resetting it.
func (*ChatHistory) ClearAllSystemMessages ¶ added in v0.5.10
func (h *ChatHistory) ClearAllSystemMessages()
ClearAllSystemMessages removes all system messages from the chat history.
func (*ChatHistory) FilterMessages ¶ added in v0.4.8
func (h *ChatHistory) FilterMessages(predicate func(string) bool) []string
FilterMessages returns a slice of messages that match the predicate function.
Parameters:
predicate func(string) bool: A function that returns true for messages that should be included.
Returns:
[]string: A slice of messages that match the predicate.
TODO: Filtering Messages
func (*ChatHistory) GetHistory ¶
func (h *ChatHistory) GetHistory(config *ChatConfig) 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) GetMessageStats ¶ added in v0.6.3
func (h *ChatHistory) GetMessageStats() MessageStats
GetMessageStats safely retrieves the message counts from the ChatHistory instance. It returns a MessageStats struct containing the counts of user, AI, and system messages. Access to the ChatHistory's message counts is read-locked to ensure thread safety.
func (*ChatHistory) IncrementMessageTypeCount
deprecated
added in
v0.6.0
func (h *ChatHistory) IncrementMessageTypeCount(messageType MessageType) bool
IncrementMessageTypeCount updates the count of messages for the given type. It specifically flags if a SystemMessage is encountered, as it may require special handling. Returns true if the incremented message type is a system message.
Deprecated: This method is no longer used, and was replaced by ChatHistory.AddMessage
func (*ChatHistory) PrintHistory
deprecated
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.
Deprecated: This method is deprecated was replaced by GetHistory. It used to be used for debugging purposes while made the chat system without storage such as database.
func (*ChatHistory) RemoveMessages ¶ added in v0.2.5
func (h *ChatHistory) RemoveMessages(numMessages int, messageContent string)
RemoveMessages removes messages from the chat history. If a specific message is provided, it removes messages that contain that text; otherwise, it removes the specified number of most recent messages.
Parameters:
numMessages int: The number of most recent messages to remove. If set to 0 and a specific message is provided, all instances of that message are removed. messageContent string: The specific content of messages to remove. If empty, it removes the number of most recent messages specified by numMessages.
This method does not return any value. It updates the chat history in place.
Note: This currently marked as TODO since it's not used anywhere in the code. It's a good idea to add this feature in the future. Also it used to be maintain the RAM's labyrinth hahaha and automated handle by Garbage Collector.
func (*ChatHistory) SanitizeMessage ¶ added in v0.3.3
func (h *ChatHistory) SanitizeMessage(message string) string
SanitizeMessage removes ANSI color codes and other non-content prefixes from a message.
Parameters:
message string: The message to be sanitized.
Returns:
string: The sanitized message.
type ChatWorker ¶ added in v0.4.4
type ChatWorker struct {
// contains filtered or unexported fields
}
ChatWorker is responsible for handling background tasks related to chat sessions.
func NewChatWorker ¶ added in v0.4.4
func NewChatWorker(session *Session) *ChatWorker
NewChatWorker creates a new ChatWorker for a given chat session.
func (*ChatWorker) Start ¶ added in v0.4.4
func (cw *ChatWorker) Start(ctx context.Context) error
Start begins the background work loop of the ChatWorker.
func (*ChatWorker) Stop ¶ added in v0.4.4
func (cw *ChatWorker) Stop() error
Stop signals the ChatWorker to stop its work loop.
type CommandHandler ¶ added in v0.1.10
type CommandHandler interface { // Note: The list of command handlers here does not use os.Args; instead, it employs advanced idiomatic Go practices. 🤪 Execute(session *Session, parts []string) (bool, error) // new method IsValid(parts []string) bool // new method HandleSubcommand(subcommand string, session *Session, parts []string) (bool, error) // New method }
CommandHandler defines the function signature for handling chat commands. Each command handler function must conform to this signature.
type CommandRegistry ¶ added in v0.4.1
type CommandRegistry struct {
// contains filtered or unexported fields
}
CommandRegistry is a centralized registry to manage chat commands. It maps command names to their corresponding CommandHandler implementations. This allows for a scalable and maintainable way to manage chat commands and their execution within a chat session.
func NewCommandRegistry ¶ added in v0.4.1
func NewCommandRegistry() *CommandRegistry
NewCommandRegistry initializes a new instance of CommandRegistry. It creates a CommandRegistry with an empty map ready to register command handlers.
Returns:
*CommandRegistry: A pointer to a newly created CommandRegistry with initialized command map.
func (*CommandRegistry) ExecuteCommand ¶ added in v0.4.1
func (r *CommandRegistry) ExecuteCommand(name string, session *Session, parts []string) (bool, error)
ExecuteCommand looks up and executes a command based on its name. It first validates the command arguments using the IsValid method of the command handler. If the command is valid, it executes the command using the Execute method. If the command name is not registered, it logs an error.
Parameters:
name string: The name of the command to execute. session *Session: The current chat session, providing context for the command execution. parts []string: The arguments passed along with the command.
Returns:
bool: A boolean indicating if the command execution should terminate the session. error: An error if one occurs during command validation or execution. Returns nil if no error occurs.
Note:
If the command is unrecognized, it logs an error but does not return it, as the error is already handled within the method.
func (*CommandRegistry) Register ¶ added in v0.4.1
func (r *CommandRegistry) Register(name string, cmd CommandHandler)
Register adds a new command and its associated handler to the registry. If a command with the same name is already registered, it will be overwritten.
Parameters:
name string: The name of the command to register. cmd CommandHandler: The handler that will be associated with the command.
func (*CommandRegistry) RegisterSubcommand ¶ added in v0.6.2
func (r *CommandRegistry) RegisterSubcommand(baseCommand, subcommand string, handler CommandHandler)
Register a subcommand for a base command.
type DebugOrErrorLogger ¶ added in v0.1.9
type DebugOrErrorLogger struct { PrintTypingChat func(string, time.Duration) // contains filtered or unexported fields }
DebugOrErrorLogger provides a simple logger with support for debug and error logging. It encapsulates a standard log.Logger and adds functionality for conditional debug logging and colorized error output.
func NewDebugOrErrorLogger ¶ added in v0.1.9
func NewDebugOrErrorLogger() *DebugOrErrorLogger
NewDebugOrErrorLogger initializes a new DebugOrErrorLogger with a logger that writes to os.Stderr with the standard log flags.
Returns:
*DebugOrErrorLogger: A pointer to a newly created DebugOrErrorLogger.
func (*DebugOrErrorLogger) Any ¶ added in v0.6.5
func (l *DebugOrErrorLogger) Any(format string, v ...interface{})
Any logs a general message without any colorization. It behaves like Println and allows for formatted messages.
Parameters:
format string: The format string for the general message. v ...interface{}: The values to be formatted according to the format string.
func (*DebugOrErrorLogger) Debug ¶ added in v0.1.9
func (l *DebugOrErrorLogger) Debug(format string, v ...interface{})
Debug logs a formatted debug message if the DEBUG_MODE environment variable is set to "true". It behaves like Printf and allows for formatted messages.
Parameters:
format string: The format string for the debug message. v ...interface{}: The values to be formatted according to the format string.
func (*DebugOrErrorLogger) Error ¶ added in v0.1.9
func (l *DebugOrErrorLogger) Error(format string, v ...interface{})
Error logs a formatted error message in red color to signify error conditions. It behaves like Println and allows for formatted messages.
Parameters:
format string: The format string for the error message. v ...interface{}: The values to be formatted according to the format string.
func (*DebugOrErrorLogger) HandleGoogleAPIError ¶ added in v0.4.4
func (l *DebugOrErrorLogger) HandleGoogleAPIError(err error) bool
HandleGoogleAPIError checks for Google API server errors and logs them. If it's a server error, it returns true, indicating a retry might be warranted.
Parameters:
err error: The error returned from a Google API call.
Returns:
bool: Indicates whether the error is a server error (500).
func (*DebugOrErrorLogger) HandleOtherStupidAPIError ¶ added in v0.5.0
func (l *DebugOrErrorLogger) HandleOtherStupidAPIError(err error, apiName string) bool
HandleOtherStupidAPIError checks for non-Google API 500 server errors and logs them. If it's a server error, it returns true, indicating a retry might be warranted.
Parameters:
err error: The error returned from an API call. apiName string: The name of the API for logging purposes.
Returns:
bool: Indicates whether the error is a bad server error (500).
func (*DebugOrErrorLogger) Info ¶ added in v0.4.7
func (l *DebugOrErrorLogger) Info(format string, v ...interface{})
Info logs a formatted information message. It behaves like Println and allows for formatted messages.
Parameters:
format string: The format string for the information message. v ...interface{}: The values to be formatted according to the format string.
func (*DebugOrErrorLogger) RecoverFromPanic ¶ added in v0.2.2
func (l *DebugOrErrorLogger) RecoverFromPanic()
RecoverFromPanic should be deferred at the beginning of a function or goroutine to handle any panics that may occur. It logs the panic information with a colorized output to distinguish the log message clearly in the terminal.
The message "Recovered from panic:" is displayed in green, followed by the panic value in red. This method ensures that the panic does not cause the program to crash and provides a clear indication in the logs that a panic was caught and handled.
Usage:
func someFunction() { logger := terminal.NewDebugOrErrorLogger() defer logger.RecoverFromPanic() // ... function logic that might panic ... }
It is essential to call this method using defer right after obtaining a logger instance. This ensures that it can catch and handle panics from anywhere within the scope of the function or goroutine.
type ErrorHandlerFunc ¶ added in v0.5.0
ErrorHandlerFunc is a type that represents a function that handles an error and decides whether the operation should be retried.
type GitHubRelease ¶ added in v0.2.1
type GitHubRelease struct { TagName string `json:"tag_name"` // The tag associated with the release, e.g., "v1.2.3" Name string `json:"name"` // The official name of the release Body string `json:"body"` // Detailed description or changelog for the release Date string `json:"published_at"` // Published Date }
GitHubRelease represents the metadata of a software release from GitHub. It includes information such as the tag name, release name, and a description body, typically containing the changelog or release notes.
func GetFullReleaseInfo ¶ added in v0.2.1
func GetFullReleaseInfo(tagName string) (release *GitHubRelease, err error)
GetFullReleaseInfo retrieves detailed information about a specific release from GitHub. It constructs the request URL based on the provided tag name and fetches the data from the GitHub API.
Parameters:
tagName: The name of the tag for which release information is requested.
Returns:
release *GitHubRelease: A pointer to the GitHubRelease struct containing the release information. err error: An error if the request fails or if there is an issue parsing the response.
type MessageStats ¶ added in v0.6.3
type MessageStats struct { UserMessages int // UserMessages is the count of messages sent by users. AIMessages int // AIMessages is the count of messages sent by the AI. SystemMessages int // SystemMessages is the count of system-generated messages. }
MessageStats encapsulates the counts of different types of messages in the chat history. It holds separate counts for user messages, AI messages, and system messages.
type MessageType ¶ added in v0.6.0
type MessageType int
MessageType categorizes the source of a chat message.
const ( // UserMessage indicates a message that originates from a human user. UserMessage MessageType = iota // magic // AIMessage indicates a message that originates from an AI or bot. AIMessage // SystemMessage indicates a message that provides system-level information. SystemMessage )
func DetermineMessageType ¶ added in v0.6.0
func DetermineMessageType(message string) MessageType
DetermineMessageType analyzes the content of a message to classify its type. It returns the MessageType based on predefined criteria for identifying user, AI, and system messages.
type ModelConfig ¶ added in v0.5.8
type ModelConfig func(m *genai.GenerativeModel)
ModelConfig defines a function type for configuring a GenerativeModel. Functions of this type take a pointer to a GenerativeModel and apply specific settings to it.
func WithMaxOutputTokens ¶ added in v0.5.6
func WithMaxOutputTokens(maxOutputTokens int32) (ModelConfig, error)
WithMaxOutputTokens creates a ModelConfig function to set the maximum number of output tokens for a GenerativeModel. This parameter limits the length of the AI's responses.
Parameters:
maxOutputTokens int32: The maximum number of tokens to set.
Returns:
ModelConfig: A function that sets the maximum number of output tokens when applied to a model. error: An error if maxOutputTokens is below 50.
func WithTemperature ¶ added in v0.5.6
func WithTemperature(temperature float32) ModelConfig
WithTemperature creates a ModelConfig function to set the temperature of a GenerativeModel. Temperature controls the randomness of the AI's responses, with higher values leading to more varied output.
Parameters:
temperature float32: The temperature value to set.
Returns:
ModelConfig: A function that sets the temperature when applied to a model.
func WithTopK ¶ added in v0.5.6
func WithTopK(topK int32) ModelConfig
WithTopK creates a ModelConfig function to set the top_k parameter of a GenerativeModel. Top_k restricts the sampling pool to the k most likely tokens, where a lower value increases the likelihood of high-probability tokens.
Parameters:
topK int32: The top_k value to set.
Returns:
ModelConfig: A function that sets the top_k value when applied to a model.
func WithTopP ¶ added in v0.5.6
func WithTopP(topP float32) ModelConfig
WithTopP creates a ModelConfig function to set the top_p parameter of a GenerativeModel. Top_p controls the nucleus sampling strategy, where a smaller value leads to less randomness in token selection.
Parameters:
topP float32: The top_p value to set.
Returns:
ModelConfig: A function that sets the top_p value when applied to a model.
type NewLineChar ¶ added in v0.3.0
type NewLineChar struct {
NewLineChars rune
}
NewLineChar is a struct that containt Rune for New Line Character
type RetryableFunc ¶ added in v0.4.10
RetryableFunc is a type that represents a function that can be retried.
type SafetyOption ¶ added in v0.4.3
type SafetyOption struct { Setter func(s *SafetySettings) Valid bool }
SafetyOption is a function type that takes a pointer to a SafetySettings instance and applies a specific safety configuration to it. It is used to abstract the different safety level settings (e.g., low, high, default) and allows for a flexible and scalable way to manage safety configurations through function mapping.
type SafetySetter
deprecated
added in
v0.4.2
type SafetySetter func(*SafetySettings)
Deprecated: This method is no longer used, and was replaced by SafetyOption.
type SafetySettings ¶ added in v0.3.8
type SafetySettings struct { // DangerousContentThreshold defines the threshold for filtering dangerous content. DangerousContentThreshold genai.HarmBlockThreshold // HarassmentContentThreshold defines the threshold for filtering harassment-related content. HarassmentContentThreshold genai.HarmBlockThreshold // SexuallyExplicitContentThreshold defines the threshold for filtering sexually explicit content. SexuallyExplicitContentThreshold genai.HarmBlockThreshold // MedicalThreshold defines the threshold for filtering medical-related content. MedicalThreshold genai.HarmBlockThreshold // ViolenceThreshold defines the threshold for filtering violent content. ViolenceThreshold genai.HarmBlockThreshold // HateSpeechThreshold defines the threshold for filtering hate speech. HateSpeechThreshold genai.HarmBlockThreshold // ToxicityThreshold defines the threshold for filtering toxic content. ToxicityThreshold genai.HarmBlockThreshold // DerogatoryThershold defines the threshold for filtering derogatory content. DerogatoryThershold genai.HarmBlockThreshold }
SafetySettings encapsulates the content safety configuration for the AI model. It defines thresholds for various categories of potentially harmful content, allowing users to set the desired level of content filtering based on the application's requirements and user preferences.
func DefaultSafetySettings ¶ added in v0.3.8
func DefaultSafetySettings() *SafetySettings
DefaultSafetySettings returns a SafetySettings instance with a default configuration where all categories are set to block medium and above levels of harmful content. This default setting provides a balanced approach to content safety, suitable for general use cases.
func (*SafetySettings) ApplyToModel ¶ added in v0.3.8
func (s *SafetySettings) ApplyToModel(model *genai.GenerativeModel)
ApplyToModel applies the configured safety settings to a given generative AI model. This method updates the model's safety settings to match the thresholds specified in the SafetySettings instance, affecting how the model filters generated content.
func (*SafetySettings) SetHighSafety ¶ added in v0.3.8
func (s *SafetySettings) SetHighSafety()
SetHighSafety raises the safety settings to a higher threshold, providing stricter content filtering. This setting is useful in environments that require a high degree of content moderation to ensure user safety or to comply with strict regulatory standards.
func (*SafetySettings) SetLowSafety ¶ added in v0.3.8
func (s *SafetySettings) SetLowSafety()
SetLowSafety adjusts the safety settings to a lower threshold, allowing more content through the filter. This setting may be appropriate for environments where content restrictions can be more relaxed, or where users are expected to handle a wider range of content types.
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. ChatConfig *ChatConfig // ChatConfig contains the settings for managing the chat history size. 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. Ended bool // Ended indicates whether the session has ended. SafetySettings *SafetySettings // Holds the current safety settings for the session. // contains filtered or unexported fields }
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.
func (*Session) HasEnded ¶ added in v0.2.2
HasEnded reports whether the chat session has ended. It can be called at any point to check the session's state without altering it.
Return value:
ended bool: A boolean indicating true if the session has ended, or false if it is still active.
TODO: Utilize this in multiple goroutines, such as for task queues, terminal control, etc.
func (*Session) RenewSession ¶ added in v0.2.2
RenewSession attempts to renew the client session with the AI service by reinitializing the genai.Client with the provided API key. This method is useful when the existing client session has expired or is no longer valid and a new session needs to be established to continue communication with the AI service.
The method ensures thread-safe access by using a mutex lock during the client reinitialization process. If a client session already exists, it is properly closed and a new client is created.
Parameters:
apiKey string: The API key used for authenticating requests to the AI service.
Returns:
error: An error object if reinitializing the client fails. If the operation is successful, the error is nil.
Upon successful completion, the Session's Client field is updated to reference the new genai.Client instance. In case of failure, an error is returned and the Client field is set to nil.
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.
type TypingChars ¶ added in v0.3.0
type TypingChars struct {
AnimatedChars string
}
TypingChars is a struct that contains the Animated Chars used to print the typing effect.
Source Files ¶
- ai_stuff.go
- chat.go
- colorize.go
- commands.go
- commands_types.go
- config.go
- constant.go
- debug_or_error.go
- deprecated.go
- docs.go
- executecmd_stuff.go
- figlet_frontend.go
- front_end.go
- genai.go
- genai_embedding.go
- init.go
- release.go
- retry_policy.go
- safety_settings.go
- session.go
- token_counter.go
- unimplemented.go
- worker.go
Directories ¶
Path | Synopsis |
---|---|
Package tools encapsulates a collection of utility functions designed to support various common operations throughout the application.
|
Package tools encapsulates a collection of utility functions designed to support various common operations throughout the application. |
Package tour provides a collection of string manipulation utilities.
|
Package tour provides a collection of string manipulation utilities. |