| import os |
| import json |
| import time |
| from datetime import datetime |
| from typing import List, Dict, Any |
| from .models import ChatMessage |
|
|
|
|
| def save_chat_history(messages: List[ChatMessage], model_name: str, response: str, filename: str = "chat.md"): |
| """ |
| Save chat history to a markdown file with timestamp and model information |
| """ |
| timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
| |
| |
| history_content = f""" |
| ## Chat Session: {timestamp} |
| **Model Used:** {model_name} |
| |
| --- |
| """ |
|
|
| |
| for msg in messages: |
| role_prefix = "**User:**" if msg.role.lower() == "user" else "**Assistant:**" |
| history_content += f"\n{role_prefix} {msg.content}\n\n" |
| |
| |
| history_content += f"\n**Assistant Response:** {response}\n\n---\n\n" |
| |
| |
| with open(filename, "a", encoding="utf-8") as file: |
| file.write(history_content) |
|
|
|
|
| def save_detailed_chat_log(request_data: Dict[str, Any], response_data: str, model_name: str, processing_time: float, filename: str = "chat.md"): |
| """ |
| Save detailed chat log with metadata |
| """ |
| timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
| |
| log_content = f""" |
| ## Chat Request Log: {timestamp} |
| - **Model:** {model_name} |
| - **Processing Time:** {processing_time:.2f}s |
| - **Max Tokens:** {request_data.get('max_tokens', 512)} |
| - **Temperature:** {request_data.get('temperature', 0.8)} |
| |
| ### Input Messages: |
| """ |
| |
| |
| messages = request_data.get('messages', []) |
| for msg in messages: |
| role = msg.get('role', 'unknown') |
| content = msg.get('content', '') |
| role_display = "**User**" if role.lower() == 'user' else "**Assistant**" |
| log_content += f"- {role_display}: {content}\n" |
| |
| log_content += f"\n### Model Response:\n{response_data}\n\n---\n\n" |
| |
| |
| with open(filename, "a", encoding="utf-8") as file: |
| file.write(log_content) |
|
|
|
|
| def initialize_chat_file(filename: str = "chat.md"): |
| """ |
| Initialize the chat history file with header if it doesn't exist |
| """ |
| if not os.path.exists(filename): |
| header = f"""# Chat History |
| Last updated: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")} |
| |
| This file contains the history of all chat conversations processed by the multi-node API system. |
| |
| --- |
| """ |
| with open(filename, "w", encoding="utf-8") as file: |
| file.write(header) |