Chatbot Effect

Copyright 2023 Ethan Christensen

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

class bruhanimate.bruheffect.chatbot_effect.Key(character: str, representation: list[str], value: int, x: int, y: int)[source]

Bases: object

A class representing a key on the keyboard with its position and value.

__init__(character: str, representation: list[str], value: int, x: int, y: int)[source]

Initializes a Key object with the given parameters.

Parameters:
  • character (str) – The character represented by the key.

  • representation (list[str]) – The visual representation of the key.

  • value (int) – The value of the key.

  • x (int) – The x-coordinate of the key on the screen.

  • y (int) – The y-coordinate of the key on the screen.

class bruhanimate.bruheffect.chatbot_effect.GradientNoise(x: int, y: int, length: int, char_halt: int = 1, color_halt: int = 1, gradient_length: int = 1)[source]

Bases: object

A class representing a noise effect with a color gradient.

__init__(x: int, y: int, length: int, char_halt: int = 1, color_halt: int = 1, gradient_length: int = 1)[source]

Initializes a GradientNoise object with the given parameters.

Parameters:
  • x (int) – The x-coordinate of the noise effect on the screen.

  • y (int) – The y-coordinate of the noise effect on the screen.

  • length (int) – The length of the noise effect on the screen.

  • char_halt (int, optional) – The halt of characters changing (frame_number % character_halt == 0). Defaults to 1.

  • color_halt (int, optional) – The halt of colors changing (frame_number % color_halt == 0). Defaults to 1.

  • gradient_length (int, optional) – Length of the gradient. Defaults to 1.

update_gradient(gradient: list[int])[source]

Updates the gradient of the noise.

Parameters:

gradient (list[int]) – The new gradient to use.

generate(frame_number: int)[source]

Generates the next frame of the noise.

Parameters:

frame_number (int) – The current frame number.

mark_done()[source]
class bruhanimate.bruheffect.chatbot_effect.Loading(animate_part: GradientNoise)[source]

Bases: object

A class to handle the loading animation.

__init__(animate_part: GradientNoise)[source]
update(frame: int)[source]
mark_done()[source]
class bruhanimate.bruheffect.chatbot_effect.StringStreamer(x: int, y: int, text: str, start_frame: int, halt: int = 1)[source]

Bases: object

A class to handle the string streamer animation.

__init__(x: int, y: int, text: str, start_frame: int, halt: int = 1)[source]

Initialize the StringStreamer class.

Parameters:
  • x (int) – The x position of the string streamer.

  • y (int) – The y position of the string streamer.

  • text (str) – The text to be displayed.

  • start_frame (int) – The frame number to start the animation.

  • halt (int, optional) – Halt value to delay the animation (frame_number & halt == 0). Defaults to 1.

generate(frame: int)[source]

Generate the string streamer animation for a given frame number.

Parameters:

frame (int) – The current frame number.

class bruhanimate.bruheffect.chatbot_effect.OllamaApiCaller(model: str, use_message_history: bool = False, message_history_cap: int = 5)[source]

Bases: object

A class to interact with the Ollama API.

__init__(model: str, use_message_history: bool = False, message_history_cap: int = 5)[source]

Initialize the OllamaApiCaller class.

Parameters:
  • model (str) – The Ollama model to use.

  • use_message_history (bool, optional) – Whether or not to use message history. Defaults to False.

  • message_history_cap (int, optional) – How many messages should we use, sliding window. Defaults to 5.

chat(message: str, user: str | None, previous_messages: list[str] | None = None) str[source]

Send a chat message to the Ollama API and get a response.

Parameters:
  • message (str) – The message to send.

  • user (str | None) – The user who sent the message.

  • previous_messages (list[str] | None, optional) – Past sent messages. Defaults to None.

Returns:

Response from ollama.

Return type:

str

class bruhanimate.bruheffect.chatbot_effect.OpenAiCaller(client: OpenAI | AzureOpenAI, model: str, use_message_history: bool = False, message_history_cap: int = 5)[source]

Bases: object

Class to interact with the OpenAI API using the openai Python package.

__init__(client: OpenAI | AzureOpenAI, model: str, use_message_history: bool = False, message_history_cap: int = 5)[source]

Initialize the OpenAiCaller class.

Parameters:
  • client (openai.OpenAI | openai.AzureOpenAI) – The OpenAI client object.

  • model (str) – The OpenAI model to be used.

  • use_message_history (bool, optional) – Whether or not to use message history. Defaults to False.

  • message_history_cap (int, optional) – Amount of messages from history to use, sliding window. Defaults to 5.

chat(message: str, user: str | None) str[source]

Send a chat message to the OpenAI API and get a response.

Parameters:
  • message (str) – The message to be sent to the OpenAI API.

  • user (str | None) – The user who sent the message.

Returns:

The response from the OpenAI API.

Return type:

str

class bruhanimate.bruheffect.chatbot_effect.ChatbotEffect(screen: Screen, buffer: Buffer, back_buffer: Buffer, background: str = ' ')[source]

Bases: BaseEffect

A class to create a chatbot effect.

__init__(screen: Screen, buffer: Buffer, back_buffer: Buffer, background: str = ' ')[source]

Initialize the ChatbotEffect class.

Parameters:
  • screen (Screen) – Our instance of the terminal window

  • buffer (Buffer) – Effect buffer to push updates to.

  • back_buffer (Buffer) – The buffer to push the effect updates to.

  • background (str, optional) – Character or string to use for the background. Defaults to “ “.

set_chatbot_properties(interface: str | None, model: str, user: str | None = None, client: OpenAI | AzureOpenAI | None = None, use_message_history: bool = False, message_history_cap: int = 5)[source]

Sets the properties for the chatbot.

Parameters:
  • interface (str | None) – The interface type, e.g., “OpenAI” or “Azure OpenAI”.

  • model (str) – The name of the AI model to use.

  • user (str | None, optional) – The name of the user. Defaults to None.

  • client (openai.OpenAI | openai.AzureOpenAI | None, optional) – The client to use. Defaults to None.

  • use_message_history (bool, optional) – Whether or not to use message history. Defaults to False.

  • message_history_cap (int, optional) – How many messages from history to use, sliding window. Defaults to 5.

Raises:

Exception – If the interface is not recognized, an exception will be raised.

set_second_effect(effect: str)[source]

Sets the second effect for the chatbot.

Parameters:

effect (str) – The effect to use

set_chatbot_print_halt(halt: int)[source]

Sets the chatbot print halt value to control how often it prints messages.

Parameters:

halt (int) – (frame_number % halt == 0)

set_gradient_noise_halts(char_halt: int | None = None, color_halt: int | None = None)[source]

Sets the gradient noise halts for character and color shifts.

Parameters:
  • char_halt (int | None, optional) – Sets the character halt for gradient noise. Defaults to None.

  • color_halt (int | None, optional) – Sets the color halt for gradient noise. Defaults to None.

set_chatbot_user_colors(chatbot_text_color: int | str | None = None, chatbot_background_color: int | str | None = None, chatbot_avatar_color: int | str | None = None, chatbot_avatar_text_color: int | str | None = None, user_text_color: int | str | None = None, user_background_color: int | str | None = None, user_avatar_color: int | str | None = None, user_avatar_text_color: int | str | None = None)[source]

Sets the colors for the chatbot and user messages.

Parameters:
  • chatbot_text_color (int | str | None, optional) – Color of chatbot output text. Defaults to None.

  • chatbot_background_color (int | str | None, optional) – Background of chatbot output text. Defaults to None.

  • chatbot_avatar_color (int | str | None, optional) – Text color of avatar logo. Defaults to None.

  • chatbot_avatar_text_color (int | str | None, optional) – Background color of avatar logo. Defaults to None.

  • user_text_color (int | str | None, optional) – Color of user text. Defaults to None.

  • user_background_color (int | str | None, optional) – Background color of user text. Defaults to None.

  • user_avatar_color (int | str | None, optional) – Text color of user avatar. Defaults to None.

  • user_avatar_text_color (int | str | None, optional) – Background color of user avatar. Defaults to None.

set_avatar_properties(size: int)[source]

Set avatar properties for user and chatbot.

Parameters:

size (int) – Length of the avatars on left side of screen.

set_chatbot_stats(show: bool = False)[source]

Set chatbot stats on the right side of screen.

Parameters:

show (bool, optional) – Whether or not to show chatbot stats. Defaults to False.

Set chatbot blink and halt properties.

Parameters:

halt (int) – (frame_number % halt == 0)

set_divider_flag(divider: bool, divider_character: str = '-')[source]

Set the divider flag and character for screen.

Parameters:
  • divider (bool) – Whether or not to show the divider.

  • divider_character (str, optional) – Character to use for the divider. Defaults to “-“.

set_chatbot_cursor_colors(color_one: int | str, color_two: int | str)[source]

Set the colors for chatbot cursor.

Parameters:
  • color_one (int | str) – First color of cursor.

  • color_two (int | str) – Second color of cursor.

set_chatbot_text_gradient(gradient: list[int | str], mul: int)[source]

Set the text color for chatbot to use a gradient.

Parameters:
  • gradient (list[int | str]) – Gradient color to use for chatbot loading.

  • mul (int) – Multiplier for the gradient effect.

scroll_keys(shift: int = 1)[source]

Scroll the keys up or down by one line

Parameters:

shift (int, optional) – How much to scroll. Defaults to 1.

place_all_keys()[source]

Place all keys on to the buffer.

render_frame(frame_number: int)[source]

Render the current state of the buffer to the terminal.

Parameters:

frame_number (int) – The frame number to render.