Soumik555 commited on
Commit
8f05635
·
1 Parent(s): d64b05a

Report generator

Browse files
Files changed (2) hide show
  1. orchestrator_agent.py +28 -8
  2. 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
- "You have all the tools you need to answer any question."
62
- "If user asking for multiple answers or charts then break the question into multiple proper questions."
63
- "Pass csv_url/path with the questions to the tools to generate the answer."
64
- "Explain the answer in a friendly way."
65
- "**Format images** in Markdown: `![alt_text](direct-image-url)`"
66
- f"Your csv url is {csv_url}"
67
- f"Your csv metadata is {csv_metadata}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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: ![Chart Description](direct-image-url)"
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 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:
 
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: