coder-vansh commited on
Commit
ab8b6f4
·
1 Parent(s): 53ae4ec

empty files

Browse files
Files changed (1) hide show
  1. test.py +230 -0
test.py ADDED
@@ -0,0 +1,230 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ CLI TEST INTERFACE
3
+ ==================
4
+ PURPOSE:
5
+ Command-line interface for testing JARVIS functionality.
6
+ Provides interactive chat with mode switching and session management.
7
+
8
+ WHAT THIS FILE DOES:
9
+ - Provides interactive CLI for testing both chat modes
10
+ - Supports switching between general and realtime chat
11
+ - Displays chat history and session information
12
+ - Handles user commands (/history, /clear, /quit, etc.)
13
+
14
+ USAGE:
15
+ python test.py
16
+
17
+ COMMANDS:
18
+ 1 - Switch to General Chat mode
19
+ 2 - Switch to Realtime Chat mode
20
+ /history - View chat history
21
+ /clear - Start new session
22
+ /quit or /exit - Exit the program
23
+ """
24
+
25
+ import requests
26
+ import sys
27
+ from app.utils.time_info import get_greeting, get_time_info
28
+
29
+ # API configuration
30
+ API_BASE_URL = "http://localhost:8000"
31
+
32
+ # Chat modes
33
+ MODE_GENERAL = "general"
34
+ MODE_REALTIME = "realtime"
35
+
36
+ # Global state
37
+ current_mode = MODE_GENERAL
38
+ session_id = None
39
+
40
+
41
+ def print_banner():
42
+ """Print welcome banner."""
43
+ print("=" * 70)
44
+ print(" J.A.R.V.I.S - Just A Rather Very Intelligent System")
45
+ print(" CLI Test Interface")
46
+ print("=" * 70)
47
+
48
+
49
+ def print_time_info():
50
+ """Print current time information."""
51
+ time_info = get_time_info()
52
+ print(f"\n{get_greeting()}!")
53
+ print(f"Date: {time_info['date']} | Time: {time_info['time']} | Day: {time_info['day']}")
54
+
55
+
56
+ def print_mode_info():
57
+ """Print current chat mode."""
58
+ mode_name = "General Chat (No Web Search)" if current_mode == MODE_GENERAL else "Realtime Chat (With Web Search)"
59
+ print(f"\nCurrent Mode: {mode_name}")
60
+ print(f"Session ID: {session_id if session_id else 'Not started'}")
61
+
62
+
63
+ def print_help():
64
+ """Print available commands."""
65
+ print("\n" + "=" * 70)
66
+ print("Available Commands:")
67
+ print(" 1 - Switch to General Chat mode")
68
+ print(" 2 - Switch to Realtime Chat mode")
69
+ print(" /history - View chat history")
70
+ print(" /clear - Start new session")
71
+ print(" /quit - Exit the program")
72
+ print(" /exit - Exit the program")
73
+ print("=" * 70)
74
+
75
+
76
+ def send_message(message: str) -> str:
77
+ """
78
+ Send message to JARVIS API.
79
+
80
+ Args:
81
+ message: User message
82
+
83
+ Returns:
84
+ Assistant response
85
+ """
86
+ global session_id
87
+
88
+ # Determine endpoint based on mode
89
+ endpoint = "/chat" if current_mode == MODE_GENERAL else "/chat/realtime"
90
+ url = f"{API_BASE_URL}{endpoint}"
91
+
92
+ # Prepare request
93
+ payload = {
94
+ "message": message,
95
+ "session_id": session_id
96
+ }
97
+
98
+ try:
99
+ # Send request
100
+ response = requests.post(url, json=payload, timeout=60)
101
+ response.raise_for_status()
102
+
103
+ # Parse response
104
+ data = response.json()
105
+ session_id = data.get("session_id")
106
+ return data.get("response", "")
107
+
108
+ except requests.exceptions.ConnectionError:
109
+ return "Error: Cannot connect to JARVIS server. Make sure it's running (python run.py)"
110
+ except requests.exceptions.Timeout:
111
+ return "Error: Request timed out. The server might be busy."
112
+ except requests.exceptions.HTTPError as e:
113
+ return f"Error: HTTP {e.response.status_code} - {e.response.text}"
114
+ except Exception as e:
115
+ return f"Error: {str(e)}"
116
+
117
+
118
+ def get_history():
119
+ """Get and display chat history."""
120
+ global session_id
121
+
122
+ if not session_id:
123
+ print("\nNo active session. Start chatting to create one.")
124
+ return
125
+
126
+ url = f"{API_BASE_URL}/chat/history/{session_id}"
127
+
128
+ try:
129
+ response = requests.get(url, timeout=10)
130
+ response.raise_for_status()
131
+
132
+ data = response.json()
133
+ messages = data.get("messages", [])
134
+
135
+ if not messages:
136
+ print("\nNo messages in this session yet.")
137
+ return
138
+
139
+ print("\n" + "=" * 70)
140
+ print("Chat History:")
141
+ print("=" * 70)
142
+
143
+ for msg in messages:
144
+ role = msg.get("role", "unknown").upper()
145
+ content = msg.get("content", "")
146
+ print(f"\n[{role}]")
147
+ print(content)
148
+
149
+ print("\n" + "=" * 70)
150
+
151
+ except Exception as e:
152
+ print(f"\nError getting history: {e}")
153
+
154
+
155
+ def clear_session():
156
+ """Clear current session and start new one."""
157
+ global session_id
158
+ session_id = None
159
+ print("\nSession cleared. Your next message will start a new session.")
160
+
161
+
162
+ def switch_mode(new_mode: str):
163
+ """Switch chat mode."""
164
+ global current_mode
165
+ current_mode = new_mode
166
+ mode_name = "General Chat" if new_mode == MODE_GENERAL else "Realtime Chat"
167
+ print(f"\nSwitched to {mode_name} mode")
168
+
169
+
170
+ def main():
171
+ """Main CLI loop."""
172
+ global current_mode, session_id
173
+
174
+ # Print banner and info
175
+ print_banner()
176
+ print_time_info()
177
+ print_mode_info()
178
+ print_help()
179
+
180
+ print("\nType your message and press Enter to chat with JARVIS")
181
+ print("Type a command (e.g., /help) for available commands\n")
182
+
183
+ # Main loop
184
+ while True:
185
+ try:
186
+ # Get user input
187
+ user_input = input("\nYou: ").strip()
188
+
189
+ if not user_input:
190
+ continue
191
+
192
+ # Handle commands
193
+ if user_input == "/quit" or user_input == "/exit":
194
+ print("\nGoodbye!")
195
+ sys.exit(0)
196
+
197
+ elif user_input == "/history":
198
+ get_history()
199
+ continue
200
+
201
+ elif user_input == "/clear":
202
+ clear_session()
203
+ continue
204
+
205
+ elif user_input == "/help":
206
+ print_help()
207
+ continue
208
+
209
+ elif user_input == "1":
210
+ switch_mode(MODE_GENERAL)
211
+ continue
212
+
213
+ elif user_input == "2":
214
+ switch_mode(MODE_REALTIME)
215
+ continue
216
+
217
+ # Send message to JARVIS
218
+ print("\nJARVIS: ", end="", flush=True)
219
+ response = send_message(user_input)
220
+ print(response)
221
+
222
+ except KeyboardInterrupt:
223
+ print("\n\nGoodbye!")
224
+ sys.exit(0)
225
+ except Exception as e:
226
+ print(f"\nError: {e}")
227
+
228
+
229
+ if __name__ == "__main__":
230
+ main()