from __future__ import annotations from pydantic import BaseModel from typing import Literal import time import re class Message(BaseModel): content: str time: int = None author: Literal['user', 'assistant'] = 'user' class Metadata(BaseModel): details: str = "" class Response(BaseModel): data : Message metadata: Metadata class CustomException(Exception): """A custom exception with a status code.""" def __init__(self, message, status_code): super().__init__(message) self.status_code = status_code def create_message(content: str, author: str = 'assistant') -> Message: """Creates message with current time and given content""" output_message = remove_pattern(content) return Message(content=output_message, time=int(time.time()), author=author) def create_response(message: Message, details: str = "") -> Response: """Creates a Response object with the given Message and Metadata details.""" metadata = Metadata(details=details) return Response(data=message, metadata=metadata) def remove_pattern(input_string): pattern = re.compile(r'\【\d+\†\w+\】') output_string = re.sub(pattern, '', input_string) return output_string def format_message(message): return f'- {message["author"]}: "{message["content"]}"' def format_conversation(messages): if len(messages) == 1: return messages[0]['content'] else: conversation = '\n'.join(format_message(message) for message in messages[: len(messages) - 1]) return (f"This is our conversation history:" f"\n{conversation}" f"\n\nReply to this message:" f"\n{messages[-1]['content']}")