2024-02-22 02:16:58 +03:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import random
|
|
|
|
import string
|
|
|
|
|
2024-03-12 04:06:06 +03:00
|
|
|
from ..typing import Messages
|
2024-02-22 02:16:58 +03:00
|
|
|
|
|
|
|
def format_prompt(messages: Messages, add_special_tokens=False) -> str:
|
|
|
|
"""
|
|
|
|
Format a series of messages into a single string, optionally adding special tokens.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
messages (Messages): A list of message dictionaries, each containing 'role' and 'content'.
|
|
|
|
add_special_tokens (bool): Whether to add special formatting tokens.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str: A formatted string containing all messages.
|
|
|
|
"""
|
|
|
|
if not add_special_tokens and len(messages) <= 1:
|
|
|
|
return messages[0]["content"]
|
|
|
|
formatted = "\n".join([
|
|
|
|
f'{message["role"].capitalize()}: {message["content"]}'
|
|
|
|
for message in messages
|
|
|
|
])
|
|
|
|
return f"{formatted}\nAssistant:"
|
|
|
|
|
|
|
|
def get_random_string(length: int = 10) -> str:
|
|
|
|
"""
|
|
|
|
Generate a random string of specified length, containing lowercase letters and digits.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
length (int, optional): Length of the random string to generate. Defaults to 10.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str: A random string of the specified length.
|
|
|
|
"""
|
|
|
|
return ''.join(
|
|
|
|
random.choice(string.ascii_lowercase + string.digits)
|
|
|
|
for _ in range(length)
|
|
|
|
)
|
|
|
|
|
2024-03-12 20:45:22 +03:00
|
|
|
def get_random_hex(length: int = 32) -> str:
|
2024-02-22 02:16:58 +03:00
|
|
|
"""
|
2024-03-12 20:45:22 +03:00
|
|
|
Generate a random hexadecimal string with n length.
|
2024-02-22 02:16:58 +03:00
|
|
|
|
|
|
|
Returns:
|
2024-03-12 20:45:22 +03:00
|
|
|
str: A random hexadecimal string of n characters.
|
2024-02-22 02:16:58 +03:00
|
|
|
"""
|
2024-03-12 20:45:22 +03:00
|
|
|
return ''.join(
|
|
|
|
random.choice("abcdef" + string.digits)
|
|
|
|
for _ in range(length)
|
2024-04-10 09:14:50 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
def filter_none(**kwargs) -> dict:
|
|
|
|
return {
|
|
|
|
key: value
|
|
|
|
for key, value in kwargs.items()
|
|
|
|
if value is not None
|
|
|
|
}
|