Bc-AI commited on
Commit
cdc2332
·
verified ·
1 Parent(s): c71881c

Upload 2 files

Browse files
Files changed (2) hide show
  1. shared/chat_history.py +80 -0
  2. shared/models.py +34 -0
shared/chat_history.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import time
4
+ from datetime import datetime
5
+ from typing import List, Dict, Any
6
+ from .models import ChatMessage
7
+
8
+
9
+ def save_chat_history(messages: List[ChatMessage], model_name: str, response: str, filename: str = "chat.md"):
10
+ """
11
+ Save chat history to a markdown file with timestamp and model information
12
+ """
13
+ timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
14
+
15
+ # Prepare the markdown content
16
+ history_content = f"""
17
+ ## Chat Session: {timestamp}
18
+ **Model Used:** {model_name}
19
+
20
+ ---
21
+ """
22
+
23
+ # Add all messages to the markdown file
24
+ for msg in messages:
25
+ role_prefix = "**User:**" if msg.role.lower() == "user" else "**Assistant:**"
26
+ history_content += f"\n{role_prefix} {msg.content}\n\n"
27
+
28
+ # Add the final response from the assistant
29
+ history_content += f"\n**Assistant Response:** {response}\n\n---\n\n"
30
+
31
+ # Append to the chat history file
32
+ with open(filename, "a", encoding="utf-8") as file:
33
+ file.write(history_content)
34
+
35
+
36
+ def save_detailed_chat_log(request_data: Dict[str, Any], response_data: str, model_name: str, processing_time: float, filename: str = "chat.md"):
37
+ """
38
+ Save detailed chat log with metadata
39
+ """
40
+ timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
41
+
42
+ log_content = f"""
43
+ ## Chat Request Log: {timestamp}
44
+ - **Model:** {model_name}
45
+ - **Processing Time:** {processing_time:.2f}s
46
+ - **Max Tokens:** {request_data.get('max_tokens', 512)}
47
+ - **Temperature:** {request_data.get('temperature', 0.8)}
48
+
49
+ ### Input Messages:
50
+ """
51
+
52
+ # Add the messages from the request
53
+ messages = request_data.get('messages', [])
54
+ for msg in messages:
55
+ role = msg.get('role', 'unknown')
56
+ content = msg.get('content', '')
57
+ role_display = "**User**" if role.lower() == 'user' else "**Assistant**"
58
+ log_content += f"- {role_display}: {content}\n"
59
+
60
+ log_content += f"\n### Model Response:\n{response_data}\n\n---\n\n"
61
+
62
+ # Append to the file
63
+ with open(filename, "a", encoding="utf-8") as file:
64
+ file.write(log_content)
65
+
66
+
67
+ def initialize_chat_file(filename: str = "chat.md"):
68
+ """
69
+ Initialize the chat history file with header if it doesn't exist
70
+ """
71
+ if not os.path.exists(filename):
72
+ header = f"""# Chat History
73
+ Last updated: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}
74
+
75
+ This file contains the history of all chat conversations processed by the multi-node API system.
76
+
77
+ ---
78
+ """
79
+ with open(filename, "w", encoding="utf-8") as file:
80
+ file.write(header)
shared/models.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pydantic import BaseModel
2
+ from typing import List, Optional, Dict, Any
3
+
4
+
5
+ class ChatMessage(BaseModel):
6
+ role: str # "user" or "assistant"
7
+ content: str
8
+
9
+
10
+ class ChatRequest(BaseModel):
11
+ messages: List[ChatMessage]
12
+ model: str = "sam-x-nano"
13
+ max_tokens: Optional[int] = 512
14
+ temperature: Optional[float] = 0.8
15
+ top_k: Optional[int] = 40
16
+ top_p: Optional[float] = 0.9
17
+ repetition_penalty: Optional[float] = 1.1
18
+ stream: Optional[bool] = False
19
+
20
+
21
+ class ChatResponse(BaseModel):
22
+ id: str
23
+ object: str = "chat.completion"
24
+ created: int
25
+ model: str
26
+ choices: List[Dict[str, Any]]
27
+ usage: Dict[str, int]
28
+
29
+
30
+ class WorkerStatus(BaseModel):
31
+ model_name: str
32
+ is_active: bool
33
+ load: float
34
+ last_heartbeat: int