File size: 2,557 Bytes
9a56823
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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")
    
    # Prepare the markdown content
    history_content = f"""
## Chat Session: {timestamp}
**Model Used:** {model_name}

---
"""

    # Add all messages to the markdown file
    for msg in messages:
        role_prefix = "**User:**" if msg.role.lower() == "user" else "**Assistant:**"
        history_content += f"\n{role_prefix} {msg.content}\n\n"
    
    # Add the final response from the assistant
    history_content += f"\n**Assistant Response:** {response}\n\n---\n\n"
    
    # Append to the chat history file
    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:
"""
    
    # Add the messages from the request
    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"
    
    # Append to the file
    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)