MCPcopy Create free account
hub / github.com/PromtEngineer/localGPT / ChatHandler

Class ChatHandler

backend/server.py:36–1080  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

34 allow_reuse_address = True
35
36class ChatHandler(http.server.BaseHTTPRequestHandler):
37 def __init__(self, *args, **kwargs):
38 self.ollama_client = OllamaClient()
39 super().__init__(*args, **kwargs)
40
41 def do_OPTIONS(self):
42 """Handle CORS preflight requests"""
43 self.send_response(200)
44 self.send_header('Access-Control-Allow-Origin', '*')
45 self.send_header('Access-Control-Allow-Methods', 'GET, POST, DELETE, OPTIONS')
46 self.send_header('Access-Control-Allow-Headers', 'Content-Type')
47 self.end_headers()
48
49 def do_GET(self):
50 """Handle GET requests"""
51 parsed_path = urlparse(self.path)
52
53 if parsed_path.path == '/health':
54 self.send_json_response({
55 "status": "ok",
56 "ollama_running": self.ollama_client.is_ollama_running(),
57 "available_models": self.ollama_client.list_models(),
58 "database_stats": db.get_stats()
59 })
60 elif parsed_path.path == '/sessions':
61 self.handle_get_sessions()
62 elif parsed_path.path == '/sessions/cleanup':
63 self.handle_cleanup_sessions()
64 elif parsed_path.path == '/models':
65 self.handle_get_models()
66 elif parsed_path.path == '/indexes':
67 self.handle_get_indexes()
68 elif parsed_path.path.startswith('/indexes/') and parsed_path.path.count('/') == 2:
69 index_id = parsed_path.path.split('/')[-1]
70 self.handle_get_index(index_id)
71 elif parsed_path.path.startswith('/sessions/') and parsed_path.path.endswith('/documents'):
72 session_id = parsed_path.path.split('/')[-2]
73 self.handle_get_session_documents(session_id)
74 elif parsed_path.path.startswith('/sessions/') and parsed_path.path.endswith('/indexes'):
75 session_id = parsed_path.path.split('/')[-2]
76 self.handle_get_session_indexes(session_id)
77 elif parsed_path.path.startswith('/sessions/') and parsed_path.path.count('/') == 2:
78 session_id = parsed_path.path.split('/')[-1]
79 self.handle_get_session(session_id)
80 else:
81 self.send_response(404)
82 self.end_headers()
83
84 def do_POST(self):
85 """Handle POST requests"""
86 parsed_path = urlparse(self.path)
87
88 if parsed_path.path == '/chat':
89 self.handle_chat()
90 elif parsed_path.path == '/sessions':
91 self.handle_create_session()
92 elif parsed_path.path == '/indexes':
93 self.handle_create_index()

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected