MCPcopy
hub / github.com/PromtEngineer/localGPT / handle_chat

Method handle_chat

rag_system/api_server.py:145–302  ·  view source on GitHub ↗

Handles a chat query by calling the agentic RAG pipeline.

(self)

Source from the content-addressed store, hash-verified

143 self.send_json_response({"error": "Not Found"}, status_code=404)
144
145 def handle_chat(self):
146 """Handles a chat query by calling the agentic RAG pipeline."""
147 try:
148 content_length = int(self.headers['Content-Length'])
149 post_data = self.rfile.read(content_length)
150 data = json.loads(post_data.decode('utf-8'))
151
152 query = data.get('query')
153 session_id = data.get('session_id')
154 compose_flag = data.get('compose_sub_answers')
155 decomp_flag = data.get('query_decompose')
156 ai_rerank_flag = data.get('ai_rerank')
157 ctx_expand_flag = data.get('context_expand')
158 verify_flag = data.get('verify')
159
160 # ✨ NEW RETRIEVAL PARAMETERS
161 retrieval_k = data.get('retrieval_k', 20)
162 context_window_size = data.get('context_window_size', 1)
163 reranker_top_k = data.get('reranker_top_k', 10)
164 search_type = data.get('search_type', 'hybrid')
165 dense_weight = data.get('dense_weight', 0.7)
166
167 # 🚩 NEW: Force RAG override from frontend
168 force_rag = bool(data.get('force_rag', False))
169
170 # 🌿 Provence sentence pruning
171 provence_prune = data.get('provence_prune')
172 provence_threshold = data.get('provence_threshold')
173
174 # User-selected generation model
175 requested_model = data.get('model')
176 if isinstance(requested_model,str) and requested_model:
177 RAG_AGENT.ollama_config['generation_model']=requested_model
178
179 if not query:
180 self.send_json_response({"error": "Query is required"}, status_code=400)
181 return
182
183 # 🔄 UPDATE SESSION TITLE: If this is the first message in the session, update the title
184 if session_id:
185 try:
186 # Check if this is the first message by calling the backend server
187 backend_url = f"http://localhost:8000/sessions/{session_id}"
188 session_resp = requests.get(backend_url)
189 if session_resp.status_code == 200:
190 session_data = session_resp.json()
191 session = session_data.get('session', {})
192 # If message_count is 0, this is the first message
193 if session.get('message_count', 0) == 0:
194 # Generate a title from the first message
195 title = generate_session_title(query)
196 # Update the session title via backend API
197 # We'll need to add this endpoint to the backend, for now let's make a direct database call
198 # This is a temporary solution until we add a proper API endpoint
199 db.update_session_title(session_id, title)
200 print(f"📝 Updated session title to: {title}")
201
202 # 💾 STORE USER MESSAGE: Add the user message to the database

Callers 1

do_POSTMethod · 0.95

Calls 9

send_json_responseMethod · 0.95
generate_session_titleFunction · 0.90
update_session_titleMethod · 0.80
add_messageMethod · 0.80
runMethod · 0.45

Tested by

no test coverage detected