Handle legacy chat requests (without sessions)
(self)
| 133 | self.end_headers() |
| 134 | |
| 135 | def handle_chat(self): |
| 136 | """Handle legacy chat requests (without sessions)""" |
| 137 | try: |
| 138 | content_length = int(self.headers['Content-Length']) |
| 139 | post_data = self.rfile.read(content_length) |
| 140 | data = json.loads(post_data.decode('utf-8')) |
| 141 | |
| 142 | message = data.get('message', '') |
| 143 | model = data.get('model', 'llama3.2:latest') |
| 144 | conversation_history = data.get('conversation_history', []) |
| 145 | |
| 146 | if not message: |
| 147 | self.send_json_response({ |
| 148 | "error": "Message is required" |
| 149 | }, status_code=400) |
| 150 | return |
| 151 | |
| 152 | # Check if Ollama is running |
| 153 | if not self.ollama_client.is_ollama_running(): |
| 154 | self.send_json_response({ |
| 155 | "error": "Ollama is not running. Please start Ollama first." |
| 156 | }, status_code=503) |
| 157 | return |
| 158 | |
| 159 | # Get response from Ollama |
| 160 | response = self.ollama_client.chat(message, model, conversation_history) |
| 161 | |
| 162 | self.send_json_response({ |
| 163 | "response": response, |
| 164 | "model": model, |
| 165 | "message_count": len(conversation_history) + 1 |
| 166 | }) |
| 167 | |
| 168 | except json.JSONDecodeError: |
| 169 | self.send_json_response({ |
| 170 | "error": "Invalid JSON" |
| 171 | }, status_code=400) |
| 172 | except Exception as e: |
| 173 | self.send_json_response({ |
| 174 | "error": f"Server error: {str(e)}" |
| 175 | }, status_code=500) |
| 176 | |
| 177 | def handle_get_sessions(self): |
| 178 | """Get all chat sessions""" |
no test coverage detected