banterbot.extensions package

banterbot.extensions.interface module

class banterbot.extensions.interface.Interface(model: OpenAIModel, voice: AzureNeuralVoiceProfile, languages: str | list[str] | None = None, system: str | None = None, tone_model: OpenAIModel | None = None, phrase_list: list[str] | None = None, assistant_name: str | None = None)[source]

Bases: ABC

Interface is an abstract base class for creating frontends for the BanterBot application. It provides a high-level interface for managing conversation with the bot, including sending messages, receiving responses, and updating a conversation area. The interface supports both text and speech-to-text input for user messages.

interrupt(shutdown_time: int | None = None) None[source]

Interrupts all speech-to-text recognition, text-to-speech synthesis, and OpenAI API streams.

Parameters:
  • soft (bool) – If True, allows the recognizer to keep processing data that was recorded prior to interruption.

  • shutdown_time (Optional[int]) – The time at which the listener was deactivated.

listener_activate(name: str | None = None) None[source]

Activate the speech-to-text listener.

Parameters:

name (Optional[str]) – The name of the user sending the message. Defaults to None.

listener_deactivate() None[source]

Deactivate the speech-to-text listener.

prompt(message: str, name: str | None = None) None[source]

Prompt the bot with the specified user message.

Parameters:
  • message (str) – The message content from the user.

  • name (Optional[str]) – The name of the user sending the message. Defaults to None.

respond(init_time: int) None[source]

Get a response from the bot and update the conversation area with the response. This method handles generating the bot’s response using the OpenAIService and updating the conversation area with the response text using text-to-speech synthesis.

abstract run() None[source]

Run the frontend application. This method should be implemented by subclasses to handle the main event loop of the specific GUI framework.

send_message(content: str, role: ChatCompletionRoles = ChatCompletionRoles.USER, name: str | None = None, hidden: bool = False) None[source]

Send a message from the user to the conversation.

Parameters:
  • message (str) – The message content from the user.

  • role (ChatCompletionRoles) – The role (USER, ASSISTANT, SYSTEM) associated with the content.

  • name (Optional[str]) – The name of the user sending the message. Defaults to None.

  • hidden (bool) – If True, does not display the message in the interface.

system_prompt(message: str, name: str | None = None) None[source]

Prompt the bot with the specified message, issuing a command which is not displayed in the conversation area.

Parameters:

message (str) – The message content from the user.

abstract update_conversation_area(word: str) None[source]

Update the conversation area with the specified word, and add the word to the chat log. This method should be implemented by subclasses to handle updating the specific GUI components.

Parameters:

word (str) – The word to add to the conversation area.

banterbot.extensions.option_selector module

class banterbot.extensions.option_selector.OptionSelector(model: OpenAIModel, options: list[str], system: str, prompt: str)[source]

Bases: object

The OptionSelector class facilitates evaluating and selecting the most suitable option from a set of provided options given a conversational context.

This class enhances the capabilities of the base OpenAIService by providing a mechanism for option assessment for potential responses. The options provided can represent any category or attribute, such as emotional tones, topics, sentiment, etc., thus allowing for a variety of uses.

The class accepts three main parameters: a list of options (strings), a prompt, and an initial system message. The system message sets the context for the OptionSelector’s task, while the prompt provides a guideline for the evaluation. The most suitable option is then selected based on the specified conversational context.

Example: Emotional Tone Selection

options = [“angry”, “cheerful”, “excited”, “friendly”, “hopeful”, “sad”, “shouting”, “terrified”, “unfriendly”] prompt = “Choose the most suitable tone/emotion for the assistant’s upcoming response.” system = (

“You are an Emotional Tone Evaluator. Given conversational context, you analyze and select the most ” “appropriate tone/emotion that the assistant is likely to use next.”

)

This example showcases the OptionSelector as an “Emotional Tone Evaluator”. The options are different emotional tones. Based on a conversational context, OptionSelector selects the most suitable tone for the assistant’s next response.

select(messages: list[Message]) str[source]

Select an option by asking the OpenAI ChatCompletion API to pick an answer. The prompt is set up to force the model to return a single token with dummy text preceding it in order to yield consistent results in an efficient way.

Parameters:

messages (list[Message]) – The list of messages to be processed.

Returns:

The randomly selected option.

Return type:

str

banterbot.extensions.prosody_selector module

class banterbot.extensions.prosody_selector.ProsodySelector(manager: OpenAIModel, voice: AzureNeuralVoiceProfile)[source]

Bases: object

The ProsodySelector class is responsible for managing prosody selection/extraction for specified instances of the AzureNeuralVoice class. It uses the OpenAI ChatCompletion API to generate prosody settings for a list of sentences.

_model

The OpenAI model to be used for generating responses.

Type:

OpenAIModel

_openai_manager

An instance of the OpenAIService class.

Type:

OpenAIService

_voice

An instance of the AzureNeuralVoice class.

Type:

AzureNeuralVoice

_valid

A flag indicating whether the voice styles are not None.

Type:

bool

_token_counts

A dictionary to cache the maximum number of tokens for a given number of rows.

Type:

dict

_output_patterns

A dictionary to cache the regex patterns matching the expected ChatCompletion output.

Type:

dict

_system

A list of system and user messages to be used as a prompt for the ChatCompletion API.

Type:

list[Message]

_line_pattern

A regex pattern that matches one line of expected output for the current model.

Type:

str

select(sentences: list[str], context: str | None = None, system: str | None = None) str[source]

Extracts prosody settings for a list of sentences by asking the OpenAI ChatCompletion API to pick a set of options. The prompt is set up to force the model to return an exact number of tokens with dummy text preceding it in order to yield consistent results efficiently.

Parameters:
  • sentences (list[str]) – The list of sentences to be processed.

  • context (Optional[str]) – Useful prior conversational context originating from the same response.

  • system (Optional[str]) – A system prompt to assist the ChatCompletion in picking reactions.

Returns:

The randomly selected option.

Return type:

str