Skip to main content

chat_history.py

Source: sunholo/agents/chat_history.py

Functions

extract_chat_history(chat_history=None)

Extracts paired chat history between human and AI messages.

This function takes a chat history and returns a list of pairs of messages, where each pair consists of a human message followed by the corresponding AI response.

Args: chat_history (list): List of chat messages.

Returns: list: List of tuples with paired human and AI messages.

Example:

chat_history = [
{"name": "Human", "text": "Hello, AI!"},
{"name": "AI", "text": "Hello, Human! How can I help you today?"}
]
paired_messages = extract_chat_history(chat_history)
print(paired_messages)
# Output: [("Hello, AI!", "Hello, Human! How can I help you today?")]

create_message_element(message: dict)

Extracts the main content of a message.

Args: message (dict): The message to extract content from.

Returns: str: The text or content of the message.

Raises: KeyError: If neither 'content' nor 'text' fields are found.

Example:

message = {"text": "Hello, AI!"}
content = create_message_element(message)
print(content)
# Output: 'Hello, AI!'

embeds_to_json(message: dict)

Converts the 'embeds' field in a message to a JSON string.

Args: message (dict): The message containing the 'embeds' field.

Returns: str: JSON string representation of the 'embeds' field or an empty string if no embeds are found.

Example:

message = {"embeds": [{"type": "image", "url": "https://example.com/image.png"}]}
json_string = embeds_to_json(message)
print(json_string)
# Output: '[{"type": "image", "url": "https://example.com/image.png"}]'

is_ai(message: dict)

Checks if a message was specifically sent by an AI.

Args: message (dict): The message to check.

Returns: bool: True if the message was sent by an AI, otherwise False.

Example:

message = {"name": "AI"}
print(is_ai(message))
# Output: True

is_bot(message: dict)

Checks if a message was sent by a bot.

Args: message (dict): The message to check.

Returns: bool: True if the message was sent by a bot, otherwise False.

Example:

message = {"name": "AI"}
print(is_bot(message))
# Output: True

is_human(message: dict)

Checks if a message was sent by a human.

Args: message (dict): The message to check.

Returns: bool: True if the message was sent by a human, otherwise False.

Example:

message = {"name": "Human"}
print(is_human(message))
# Output: True
Sunholo Multivac

Get in touch to see if we can help with your GenAI project.

Contact us

Other Links

Sunholo Multivac - GenAIOps

Copyright ©

Holosun ApS 2024