Chatbot

Conversation Class

class fmus_vox.chatbot.conversation.Conversation(system_prompt: str | None = None, max_history: int = 50, metadata: Dict[str, Any] | None = None, llm_provider=None)[source]

Bases: object

Manages a conversation session with message history and context.

This class handles the state of a conversation, including message history, user and system context, and integration with language models.

__init__(system_prompt: str | None = None, max_history: int = 50, metadata: Dict[str, Any] | None = None, llm_provider=None)[source]

Initialize a conversation session.

Parameters:
  • system_prompt – Initial system prompt to set context.

  • max_history – Maximum number of messages to keep in history.

  • metadata – Additional metadata about the conversation.

  • llm_provider – LLM provider to use for generating responses.

add_message(content: str, role: Role | str, audio: Audio | None = None, metadata: Dict[str, Any] | None = None) Message[source]

Add a new message to the conversation.

Parameters:
  • content – Text content of the message.

  • role – Role of the message sender.

  • audio – Associated audio data (if applicable).

  • metadata – Additional metadata about the message.

Returns:

The created Message object.

add_user_message(content: str, audio: Audio | None = None, metadata: Dict[str, Any] | None = None) Message[source]

Add a user message to the conversation.

Parameters:
  • content – Text content of the message.

  • audio – Associated audio data (if applicable).

  • metadata – Additional metadata about the message.

Returns:

The created Message object.

add_assistant_message(content: str, audio: Audio | None = None, metadata: Dict[str, Any] | None = None) Message[source]

Add an assistant message to the conversation.

Parameters:
  • content – Text content of the message.

  • audio – Associated audio data (if applicable).

  • metadata – Additional metadata about the message.

Returns:

The created Message object.

add_system_message(content: str, metadata: Dict[str, Any] | None = None) Message[source]

Add a system message to the conversation.

Parameters:
  • content – Text content of the message.

  • metadata – Additional metadata about the message.

Returns:

The created Message object.

get_last_message(role: Role | None = None) Message | None[source]

Get the last message in the conversation, optionally filtered by role.

Parameters:

role – If provided, find the last message with this role.

Returns:

The last message, or None if no messages match the criteria.

async generate_response(user_message: str | None = None, audio: Audio | None = None, metadata: Dict[str, Any] | None = None, **kwargs) Message[source]

Generate a response to the current conversation.

Parameters:
  • user_message – Optional new user message to add before generating.

  • audio – Associated audio for the user message.

  • metadata – Additional metadata for the user message.

  • **kwargs – Additional parameters to pass to the LLM provider.

Returns:

The generated assistant message.

Raises:

ValueError – If no LLM provider is configured.

get_messages_for_llm() List[Dict[str, Any]][source]

Format messages for sending to an LLM provider.

Returns:

List of message dictionaries in the format expected by LLM APIs.

on_new_message(callback: Callable[[Message], None]) None[source]

Register a callback to be called when a new message is added.

Parameters:

callback – Function to call with the new message.

clear_history(keep_system_prompt: bool = True) None[source]

Clear the conversation history.

Parameters:

keep_system_prompt – If True, retain the first system message.

save_to_file(filepath: str) None[source]

Save the conversation to a file.

Parameters:

filepath – Path to save the conversation to.

classmethod load_from_file(filepath: str) Conversation[source]

Load a conversation from a file.

Parameters:

filepath – Path to load the conversation from.

Returns:

Loaded Conversation instance.

__len__() int[source]

Return the number of messages in the conversation.

__str__() str[source]

Return a string representation of the conversation.