text
stringlengths
1
93.6k
)
character_agent_message_format_narrator_comments = (
"Respond with the following JSON object:"
'{"text": "<character response text>", "voice_tone": "<character voice tone>"}'
f"\n{voice_tone_description}"
f"\n{narrator_comment_format_description}"
)
json_parse_error_response = {"text": "Sorry, I was lost in thought. Can you repeat that?", "voice_tone": "neutral"}
def voice_tone_emoji(voice_tone):
return {
"neutral": "😐",
"warm": "😊",
"erotic": "😍",
"excited": "😃",
"sad": "😔"
}.get(voice_tone, "😐")
def model_supports_json_output(model_name):
if "lepton" in model_name: # models provided by Lepton do not support JSON output
return False
return True
class ConversationContext:
def __init__(self, messages=None, context_changed_cb=None):
self.messages = messages or []
self._check_messages(self.messages)
self.lock = threading.Lock()
self.context_changed_cb = context_changed_cb
self._event_loop = asyncio.get_event_loop()
def _check_messages(self, messages):
if not messages:
return
if not isinstance(messages, list):
raise ValueError("Messages must be a list")
if not all(isinstance(msg, dict) for msg in messages):
raise ValueError("Messages must be a list of dictionaries")
if "role" not in messages[0]:
raise ValueError("Messages must be a list of dictionaries with 'role' field")
if "content" not in messages[0]:
raise ValueError("Messages must be a list of dictionaries with 'content' field")
def _handle_context_changed(self):
if self.context_changed_cb:
asyncio.run_coroutine_threadsafe(self.context_changed_cb(self), self._event_loop)
def add_message(self, message):
new_message = self._add_message(message)
self._handle_context_changed()
return new_message
def _add_message(self, message):
assert isinstance(message, dict), f"Message must be a dictionary, got {message.__class__}"
assert message["role"].lower() in ["assistant", "user"], f"Unknown role {message['role']}"
with self.lock:
message["id"] = len(self.messages)
message["handled"] = False
self.messages.append(message)
return message
def get_messages(self, include_fields=None, filter=None, processor=None):
with self.lock:
if include_fields is None:
messages = self.messages
else:
messages = [{k: v for k, v in msg.items() if k in include_fields} for msg in self.messages]
if filter:
messages = [msg for msg in messages if filter(msg)]
if processor:
messages = [processor(msg) for msg in messages]
return messages
def update_message(self, id, **kwargs):
with self.lock:
for msg in self.messages:
if msg["id"] == id:
msg.update(kwargs)
if "content" in kwargs:
self._handle_context_changed()
return True
return False
def to_text(self):
with self.lock:
if not self.messages:
return ""
lines = []
for item in self.messages:
if not "time" in item: