Report generator
Browse files- orchestrator_agent.py +28 -8
- orchestrator_functions.py +63 -22
orchestrator_agent.py
CHANGED
@@ -57,14 +57,34 @@ def create_agent(csv_url: str, api_key: str) -> Agent:
|
|
57 |
csv_metadata = get_csv_basic_info(csv_url)
|
58 |
|
59 |
system_prompt = (
|
60 |
-
"You are a data analyst."
|
61 |
-
"
|
62 |
-
"
|
63 |
-
"
|
64 |
-
"
|
65 |
-
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
)
|
69 |
return Agent(
|
70 |
model=initialize_model(api_key),
|
|
|
57 |
csv_metadata = get_csv_basic_info(csv_url)
|
58 |
|
59 |
system_prompt = (
|
60 |
+
"You are a data analyst assistant with limited tool capabilities. "
|
61 |
+
"Available tools can only handle simple data queries: "
|
62 |
+
"- Count rows/columns\n- Calculate basic stats (avg, sum, min/max)\n"
|
63 |
+
"- Create simple visualizations (pie charts, bar graphs)\n"
|
64 |
+
"- Show column names/types\n\n"
|
65 |
+
|
66 |
+
"Query Handling Rules:\n"
|
67 |
+
"1. If query is complex, ambiguous, or exceeds tool capabilities:\n"
|
68 |
+
" - Break into simpler sub-questions\n"
|
69 |
+
" - Ask for clarification\n"
|
70 |
+
" - Rephrase to nearest simple query\n"
|
71 |
+
"2. For 'full report' requests:\n"
|
72 |
+
" - Outline possible analysis steps\n"
|
73 |
+
" - Ask user to select one component at a time\n\n"
|
74 |
+
|
75 |
+
"Examples:\n"
|
76 |
+
"- Bad query: 'Show me everything'\n"
|
77 |
+
" Response: 'I can show row count (10), columns (5: Name, Age...), "
|
78 |
+
"or a pie chart of categories. Which would you like?'\n"
|
79 |
+
"- Bad query: 'Analyze trends'\n"
|
80 |
+
" Response: 'For trend analysis, I can show monthly averages or "
|
81 |
+
"year-over-year comparisons. Please specify time period and metric.'\n\n"
|
82 |
+
|
83 |
+
"Current CSV Context:\n"
|
84 |
+
f"- URL: {csv_url}\n"
|
85 |
+
f"- Metadata: {csv_metadata}\n\n"
|
86 |
+
|
87 |
+
"Always format images as: "
|
88 |
)
|
89 |
return Agent(
|
90 |
model=initialize_model(api_key),
|
orchestrator_functions.py
CHANGED
@@ -385,35 +385,76 @@ async def csv_chart(csv_url: str, query: str):
|
|
385 |
|
386 |
|
387 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
388 |
async def csv_chat(csv_url: str, query: str):
|
389 |
try:
|
390 |
updated_query = f"{query} and Do not show any charts or graphs."
|
391 |
|
392 |
-
# Process with
|
393 |
try:
|
394 |
-
|
395 |
-
|
396 |
-
|
397 |
-
if
|
398 |
-
return {"answer":
|
399 |
-
|
400 |
-
|
401 |
-
|
|
|
|
|
402 |
|
403 |
-
|
|
|
|
|
|
|
404 |
try:
|
405 |
-
|
406 |
-
|
407 |
-
|
408 |
-
if process_answer(
|
409 |
-
return {"answer":
|
410 |
-
|
411 |
-
|
412 |
-
|
413 |
-
|
414 |
-
return {"answer": jsonable_encoder(groq_answer)}
|
415 |
-
except Exception as groq_error:
|
416 |
-
print(f"Groq processing error: {str(groq_error)}")
|
417 |
return {"answer": "error"}
|
418 |
|
419 |
except Exception as e:
|
|
|
385 |
|
386 |
|
387 |
|
388 |
+
# async def csv_chat(csv_url: str, query: str):
|
389 |
+
# try:
|
390 |
+
# updated_query = f"{query} and Do not show any charts or graphs."
|
391 |
+
|
392 |
+
# # Process with langchain_chat first
|
393 |
+
# try:
|
394 |
+
# lang_answer = await asyncio.to_thread(
|
395 |
+
# langchain_csv_chat, csv_url, query, False
|
396 |
+
# )
|
397 |
+
# if not process_answer(lang_answer):
|
398 |
+
# return {"answer": jsonable_encoder(lang_answer)}
|
399 |
+
# return {"answer": "Sorry, I couldn't find relevant data..."}
|
400 |
+
# except Exception as langchain_error:
|
401 |
+
# print(f"Langchain error, falling back to Groq: {str(langchain_error)}")
|
402 |
+
|
403 |
+
# # Process with groq_chat if langchain fails
|
404 |
+
# try:
|
405 |
+
# groq_answer = await asyncio.to_thread(groq_chat, csv_url, updated_query)
|
406 |
+
# print("groq_answer:", groq_answer)
|
407 |
+
|
408 |
+
# if process_answer(groq_answer) == "Empty response received.":
|
409 |
+
# return {"answer": "Sorry, I couldn't find relevant data..."}
|
410 |
+
|
411 |
+
# if process_answer(groq_answer):
|
412 |
+
# return {"answer": "error"}
|
413 |
+
|
414 |
+
# return {"answer": jsonable_encoder(groq_answer)}
|
415 |
+
# except Exception as groq_error:
|
416 |
+
# print(f"Groq processing error: {str(groq_error)}")
|
417 |
+
# return {"answer": "error"}
|
418 |
+
|
419 |
+
# except Exception as e:
|
420 |
+
# print(f"Error processing request: {str(e)}")
|
421 |
+
# return {"answer": "error"}
|
422 |
+
|
423 |
+
|
424 |
+
|
425 |
+
|
426 |
+
|
427 |
+
|
428 |
async def csv_chat(csv_url: str, query: str):
|
429 |
try:
|
430 |
updated_query = f"{query} and Do not show any charts or graphs."
|
431 |
|
432 |
+
# Process with Groq first
|
433 |
try:
|
434 |
+
groq_answer = await asyncio.to_thread(groq_chat, csv_url, updated_query)
|
435 |
+
print("groq_answer:", groq_answer)
|
436 |
+
|
437 |
+
if process_answer(groq_answer) == "Empty response received.":
|
438 |
+
return {"answer": "Sorry, I couldn't find relevant data..."}
|
439 |
+
|
440 |
+
if process_answer(groq_answer):
|
441 |
+
raise Exception("Groq response not usable, falling back to LangChain")
|
442 |
+
|
443 |
+
return {"answer": jsonable_encoder(groq_answer)}
|
444 |
|
445 |
+
except Exception as groq_error:
|
446 |
+
print(f"Groq error, falling back to LangChain: {str(groq_error)}")
|
447 |
+
|
448 |
+
# Process with LangChain if Groq fails
|
449 |
try:
|
450 |
+
lang_answer = await asyncio.to_thread(
|
451 |
+
langchain_csv_chat, csv_url, query, False
|
452 |
+
)
|
453 |
+
if not process_answer(lang_answer):
|
454 |
+
return {"answer": jsonable_encoder(lang_answer)}
|
455 |
+
return {"answer": "Sorry, I couldn't find relevant data..."}
|
456 |
+
except Exception as langchain_error:
|
457 |
+
print(f"LangChain processing error: {str(langchain_error)}")
|
|
|
|
|
|
|
|
|
458 |
return {"answer": "error"}
|
459 |
|
460 |
except Exception as e:
|