|
17 | 17 | SendMessageResponse, |
18 | 18 | ) |
19 | 19 | from services.project_service import get_project_service |
20 | | -from services.llm_service import llm_service |
| 20 | +from services.langchain_service import langchain_service |
21 | 21 |
|
22 | 22 | router = APIRouter(prefix="/chat", tags=["chat"]) |
23 | 23 | project_service = get_project_service() |
@@ -263,23 +263,31 @@ async def send_message( |
263 | 263 | created_at=datetime.utcnow().isoformat() + "Z", |
264 | 264 | ) |
265 | 265 |
|
266 | | - # Use LLMService for AI response, fallback to mock if not configured |
| 266 | + # Use LangChain service for query processing |
267 | 267 | try: |
268 | | - ai_content = llm_service.run(request.message) |
269 | | - # For now, just echo the LLM response as the AI message content |
270 | | - query_result = QueryResult( |
271 | | - id=str(uuid.uuid4()), |
272 | | - query=request.message, |
273 | | - sql_query="", # To be filled by future agent logic |
274 | | - result_type="summary", |
275 | | - data=[], |
276 | | - execution_time=0.0, |
277 | | - row_count=0, |
278 | | - chart_config=None, |
| 268 | + query_result = langchain_service.process_query( |
| 269 | + request.message, project_id, user_id |
279 | 270 | ) |
| 271 | + |
| 272 | + # Create AI response content based on result type |
| 273 | + if query_result.result_type == "error": |
| 274 | + ai_content = f"I encountered an error: {query_result.error}" |
| 275 | + elif query_result.result_type == "summary": |
| 276 | + ai_content = query_result.summary or "Here's what I found." |
| 277 | + elif query_result.result_type == "table": |
| 278 | + ai_content = f"I found {query_result.row_count} results for your query. Here's the data:" |
| 279 | + if query_result.sql_query: |
| 280 | + ai_content += f"\n\nSQL Query: `{query_result.sql_query}`" |
| 281 | + elif query_result.result_type == "chart": |
| 282 | + ai_content = f"I've created a {query_result.chart_config.get('type', 'chart') if query_result.chart_config else 'chart'} visualization for your query." |
| 283 | + if query_result.sql_query: |
| 284 | + ai_content += f"\n\nSQL Query: `{query_result.sql_query}`" |
| 285 | + else: |
| 286 | + ai_content = "Here are your results." |
| 287 | + |
280 | 288 | except Exception as e: |
281 | | - # Fallback to mock logic if LLM not available |
282 | | - ai_content = f"[MOCK] Here are the results for your query: '{request.message}'" |
| 289 | + # Fallback to mock logic if LangChain service fails |
| 290 | + ai_content = f"[FALLBACK] Processing query: '{request.message}'" |
283 | 291 | query_result = generate_mock_query_result(request.message, project_id) |
284 | 292 |
|
285 | 293 | # Store message in mock database |
@@ -389,7 +397,16 @@ async def get_query_suggestions( |
389 | 397 | except ValueError: |
390 | 398 | raise HTTPException(status_code=400, detail="Invalid project ID") |
391 | 399 |
|
392 | | - # Return mock suggestions |
393 | | - suggestions = [QuerySuggestion(**sug) for sug in MOCK_SUGGESTIONS] |
| 400 | + # Use LangChain service for intelligent suggestions |
| 401 | + try: |
| 402 | + suggestions_data = langchain_service.generate_suggestions(project_id, user_id) |
| 403 | + suggestions = [QuerySuggestion(**sug) for sug in suggestions_data] |
| 404 | + |
| 405 | + # If no suggestions generated, fallback to mock |
| 406 | + if not suggestions: |
| 407 | + suggestions = [QuerySuggestion(**sug) for sug in MOCK_SUGGESTIONS[:3]] |
| 408 | + except Exception as e: |
| 409 | + # Fallback to mock suggestions |
| 410 | + suggestions = [QuerySuggestion(**sug) for sug in MOCK_SUGGESTIONS[:3]] |
394 | 411 |
|
395 | 412 | return ApiResponse(success=True, data=suggestions) |
0 commit comments