added gemini too
Browse files- orchestrator_functions.py +256 -90
orchestrator_functions.py
CHANGED
@@ -343,71 +343,71 @@ def langchain_csv_chart(csv_url: str, question: str, chart_required: bool):
|
|
343 |
|
344 |
|
345 |
|
346 |
-
async def csv_chart(csv_url: str, query: str):
|
347 |
-
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
|
362 |
-
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
|
374 |
-
|
375 |
-
|
376 |
|
377 |
-
|
378 |
-
|
379 |
-
|
380 |
-
|
381 |
|
382 |
-
|
383 |
-
|
384 |
-
|
385 |
-
|
386 |
-
|
387 |
-
|
388 |
|
389 |
-
|
390 |
-
|
391 |
-
|
392 |
-
|
393 |
-
|
394 |
-
|
395 |
-
|
396 |
-
|
397 |
-
|
398 |
-
|
399 |
-
|
400 |
-
|
401 |
-
|
402 |
-
|
403 |
-
|
404 |
-
|
405 |
-
|
406 |
-
|
407 |
-
|
408 |
-
|
409 |
-
|
410 |
-
|
411 |
|
412 |
|
413 |
|
@@ -416,62 +416,228 @@ async def csv_chart(csv_url: str, query: str):
|
|
416 |
|
417 |
|
418 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
419 |
async def csv_chat(csv_url: str, query: str):
|
420 |
"""
|
421 |
Generate a response based on the provided CSV URL and query.
|
|
|
|
|
422 |
Parameters:
|
423 |
- csv_url (str): The URL of the CSV file.
|
424 |
- query (str): The query for generating the response.
|
|
|
425 |
Returns:
|
426 |
- dict: A dictionary containing the generated response.
|
|
|
427 |
Example:
|
428 |
- csv_url: "https://example.com/data.csv"
|
429 |
- query: "What is the total sales for the year 2022?"
|
430 |
Returns:
|
431 |
-
- dict: {"answer": "The total sales for 2022 is $100,000."}
|
432 |
"""
|
433 |
try:
|
434 |
updated_query = f"{query} and Do not show any charts or graphs."
|
435 |
|
436 |
-
#
|
437 |
try:
|
438 |
-
|
439 |
-
|
|
|
|
|
440 |
|
441 |
-
if process_answer(
|
442 |
-
return {"answer":
|
443 |
|
444 |
-
|
445 |
-
raise Exception("Groq response not usable, falling back to LangChain")
|
446 |
|
447 |
-
|
|
|
448 |
|
449 |
-
|
450 |
-
print(f"Groq error, falling back to LangChain: {str(groq_error)}")
|
451 |
-
|
452 |
-
# Process with LangChain if Groq fails
|
453 |
try:
|
454 |
-
|
455 |
-
langchain_csv_chat, csv_url,
|
456 |
)
|
457 |
-
|
458 |
-
|
459 |
-
|
460 |
-
|
461 |
-
print(f"LangChain processing error: {str(langchain_error)}")
|
462 |
|
463 |
-
|
|
|
|
|
|
|
|
|
|
|
464 |
try:
|
465 |
-
|
466 |
-
|
467 |
-
|
468 |
-
if
|
469 |
-
return {"answer":
|
470 |
-
|
471 |
-
|
472 |
-
|
|
|
|
|
|
|
|
|
|
|
473 |
return {"answer": "error"}
|
474 |
|
475 |
except Exception as e:
|
476 |
-
print(f"
|
477 |
-
return {"answer": "error"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
343 |
|
344 |
|
345 |
|
346 |
+
# async def csv_chart(csv_url: str, query: str):
|
347 |
+
# """
|
348 |
+
# Generate a chart based on the provided CSV URL and query.
|
349 |
+
# Parameters:
|
350 |
+
# - csv_url (str): The URL of the CSV file.
|
351 |
+
# - query (str): The query for generating the chart.
|
352 |
+
# Returns:
|
353 |
+
# - dict: A dictionary containing the generated chart image URL.
|
354 |
+
# Example:
|
355 |
+
# - csv_url: "https://example.com/data.csv"
|
356 |
+
# - query: "Generate a bar chart showing sales by region."
|
357 |
+
# Returns:
|
358 |
+
# - dict: {"image_url": "https://example.com/chart.png"}.
|
359 |
+
|
360 |
+
# """
|
361 |
|
362 |
+
# try:
|
363 |
+
# # First try Groq-based chart generation
|
364 |
+
# try:
|
365 |
+
# groq_result = await asyncio.to_thread(groq_chart, csv_url, query)
|
366 |
+
# print(f"Generated Chart (Groq): {groq_result}")
|
367 |
|
368 |
+
# if groq_result != 'Chart not generated':
|
369 |
+
# unique_file_name = f'{str(uuid.uuid4())}.png'
|
370 |
+
# image_public_url = await upload_image_to_supabase(groq_result, unique_file_name)
|
371 |
+
# print(f"Image uploaded to Supabase: {image_public_url}")
|
372 |
+
# return {"image_url": image_public_url}
|
373 |
|
374 |
+
# except Exception as groq_error:
|
375 |
+
# print(f"Groq chart generation failed, falling back to Langchain: {str(groq_error)}")
|
376 |
|
377 |
+
# # Fallback to Langchain if Groq fails
|
378 |
+
# try:
|
379 |
+
# langchain_paths = await asyncio.to_thread(langchain_csv_chart, csv_url, query, True)
|
380 |
+
# print("Fallback langchain chart result:", langchain_paths)
|
381 |
|
382 |
+
# if isinstance(langchain_paths, list) and len(langchain_paths) > 0:
|
383 |
+
# unique_file_name = f'{str(uuid.uuid4())}.png'
|
384 |
+
# print("Uploading the chart to supabase...")
|
385 |
+
# image_public_url = await upload_image_to_supabase(langchain_paths[0], unique_file_name)
|
386 |
+
# print("Image uploaded to Supabase and Image URL is... ", image_public_url)
|
387 |
+
# return {"image_url": image_public_url}
|
388 |
|
389 |
+
# except Exception as langchain_error:
|
390 |
+
# print(f"Langchain chart generation also failed: {str(langchain_error)}")
|
391 |
+
# try:
|
392 |
+
# # Last resort: Try with the gemini langchain agent
|
393 |
+
# print("Trying with the gemini langchain agent...")
|
394 |
+
# lc_gemini_chart_result = await asyncio.to_thread(langchain_gemini_csv_handler, csv_url, query, True)
|
395 |
+
# if lc_gemini_chart_result is not None:
|
396 |
+
# clean_path = lc_gemini_chart_result.strip()
|
397 |
+
# unique_file_name = f'{str(uuid.uuid4())}.png'
|
398 |
+
# print("Uploading the chart to supabase...")
|
399 |
+
# image_public_url = await upload_image_to_supabase(clean_path, unique_file_name)
|
400 |
+
# print("Image uploaded to Supabase and Image URL is... ", image_public_url)
|
401 |
+
# return {"image_url": image_public_url}
|
402 |
+
# except Exception as gemini_error:
|
403 |
+
# print(f"Gemini Langchain chart generation also failed: {str(gemini_error)}")
|
404 |
+
|
405 |
+
# # If both methods fail
|
406 |
+
# return {"error": "Could not generate the chart, please try again."}
|
407 |
+
|
408 |
+
# except Exception as e:
|
409 |
+
# print(f"Critical chart error: {str(e)}")
|
410 |
+
# return {"error": "Internal system error"}
|
411 |
|
412 |
|
413 |
|
|
|
416 |
|
417 |
|
418 |
|
419 |
+
# async def csv_chat(csv_url: str, query: str):
|
420 |
+
# """
|
421 |
+
# Generate a response based on the provided CSV URL and query.
|
422 |
+
# Parameters:
|
423 |
+
# - csv_url (str): The URL of the CSV file.
|
424 |
+
# - query (str): The query for generating the response.
|
425 |
+
# Returns:
|
426 |
+
# - dict: A dictionary containing the generated response.
|
427 |
+
# Example:
|
428 |
+
# - csv_url: "https://example.com/data.csv"
|
429 |
+
# - query: "What is the total sales for the year 2022?"
|
430 |
+
# Returns:
|
431 |
+
# - dict: {"answer": "The total sales for 2022 is $100,000."}.
|
432 |
+
# """
|
433 |
+
# try:
|
434 |
+
# updated_query = f"{query} and Do not show any charts or graphs."
|
435 |
+
|
436 |
+
# # Process with Groq first
|
437 |
+
# try:
|
438 |
+
# groq_answer = await asyncio.to_thread(groq_chat, csv_url, updated_query)
|
439 |
+
# print("groq_answer:", groq_answer)
|
440 |
+
|
441 |
+
# if process_answer(groq_answer) == "Empty response received." or groq_answer == None:
|
442 |
+
# return {"answer": "Sorry, I couldn't find relevant data..."}
|
443 |
+
|
444 |
+
# if process_answer(groq_answer) or groq_answer == None:
|
445 |
+
# raise Exception("Groq response not usable, falling back to LangChain")
|
446 |
+
|
447 |
+
# return {"answer": jsonable_encoder(groq_answer)}
|
448 |
+
|
449 |
+
# except Exception as groq_error:
|
450 |
+
# print(f"Groq error, falling back to LangChain: {str(groq_error)}")
|
451 |
+
|
452 |
+
# # Process with LangChain if Groq fails
|
453 |
+
# try:
|
454 |
+
# lang_answer = await asyncio.to_thread(
|
455 |
+
# langchain_csv_chat, csv_url, query, False
|
456 |
+
# )
|
457 |
+
# if not process_answer(lang_answer):
|
458 |
+
# return {"answer": jsonable_encoder(lang_answer)}
|
459 |
+
# return {"answer": "Sorry, I couldn't find relevant data..."}
|
460 |
+
# except Exception as langchain_error:
|
461 |
+
# print(f"LangChain processing error: {str(langchain_error)}")
|
462 |
+
|
463 |
+
# # last resort: Try with the gemini langchain agent
|
464 |
+
# try:
|
465 |
+
# gemini_answer = await asyncio.to_thread(
|
466 |
+
# langchain_gemini_csv_handler, csv_url, query, False
|
467 |
+
# )
|
468 |
+
# if not process_answer(gemini_answer):
|
469 |
+
# return {"answer": jsonable_encoder(gemini_answer)}
|
470 |
+
# return {"answer": "Sorry, I couldn't find relevant data..."}
|
471 |
+
# except Exception as gemini_error:
|
472 |
+
# print(f"Gemini Langchain processing error: {str(gemini_error)}")
|
473 |
+
# return {"answer": "error"}
|
474 |
+
|
475 |
+
# except Exception as e:
|
476 |
+
# print(f"Error processing request: {str(e)}")
|
477 |
+
# return {"answer": "error"}
|
478 |
+
|
479 |
+
|
480 |
+
|
481 |
+
|
482 |
+
|
483 |
+
|
484 |
+
|
485 |
+
####################################### Start with lc_gemini #######################################
|
486 |
+
|
487 |
+
|
488 |
async def csv_chat(csv_url: str, query: str):
|
489 |
"""
|
490 |
Generate a response based on the provided CSV URL and query.
|
491 |
+
Prioritizes LangChain-Gemini, then LangChain-Groq, and finally raw Groq as fallback.
|
492 |
+
|
493 |
Parameters:
|
494 |
- csv_url (str): The URL of the CSV file.
|
495 |
- query (str): The query for generating the response.
|
496 |
+
|
497 |
Returns:
|
498 |
- dict: A dictionary containing the generated response.
|
499 |
+
|
500 |
Example:
|
501 |
- csv_url: "https://example.com/data.csv"
|
502 |
- query: "What is the total sales for the year 2022?"
|
503 |
Returns:
|
504 |
+
- dict: {"answer": "The total sales for 2022 is $100,000."}
|
505 |
"""
|
506 |
try:
|
507 |
updated_query = f"{query} and Do not show any charts or graphs."
|
508 |
|
509 |
+
# --- 1. First Attempt: LangChain Gemini ---
|
510 |
try:
|
511 |
+
gemini_answer = await asyncio.to_thread(
|
512 |
+
langchain_gemini_csv_handler, csv_url, updated_query, False
|
513 |
+
)
|
514 |
+
print("LangChain-Gemini answer:", gemini_answer)
|
515 |
|
516 |
+
if not process_answer(gemini_answer) or gemini_answer is None:
|
517 |
+
return {"answer": jsonable_encoder(gemini_answer)}
|
518 |
|
519 |
+
raise Exception("LangChain-Gemini response not usable, falling back to LangChain-Groq")
|
|
|
520 |
|
521 |
+
except Exception as gemini_error:
|
522 |
+
print(f"LangChain-Gemini error: {str(gemini_error)}")
|
523 |
|
524 |
+
# --- 2. Second Attempt: LangChain Groq ---
|
|
|
|
|
|
|
525 |
try:
|
526 |
+
lang_groq_answer = await asyncio.to_thread(
|
527 |
+
langchain_csv_chat, csv_url, updated_query, False
|
528 |
)
|
529 |
+
print("LangChain-Groq answer:", lang_groq_answer)
|
530 |
+
|
531 |
+
if not process_answer(lang_groq_answer):
|
532 |
+
return {"answer": jsonable_encoder(lang_groq_answer)}
|
|
|
533 |
|
534 |
+
raise Exception("LangChain-Groq response not usable, falling back to raw Groq")
|
535 |
+
|
536 |
+
except Exception as lang_groq_error:
|
537 |
+
print(f"LangChain-Groq error: {str(lang_groq_error)}")
|
538 |
+
|
539 |
+
# --- 3. Final Attempt: Raw Groq Chat ---
|
540 |
try:
|
541 |
+
raw_groq_answer = await asyncio.to_thread(groq_chat, csv_url, updated_query)
|
542 |
+
print("Raw Groq answer:", raw_groq_answer)
|
543 |
+
|
544 |
+
if process_answer(raw_groq_answer) == "Empty response received." or raw_groq_answer is None:
|
545 |
+
return {"answer": "Sorry, I couldn't find relevant data..."}
|
546 |
+
|
547 |
+
if process_answer(raw_groq_answer):
|
548 |
+
raise Exception("All fallbacks exhausted")
|
549 |
+
|
550 |
+
return {"answer": jsonable_encoder(raw_groq_answer)}
|
551 |
+
|
552 |
+
except Exception as raw_groq_error:
|
553 |
+
print(f"Raw Groq error: {str(raw_groq_error)}")
|
554 |
return {"answer": "error"}
|
555 |
|
556 |
except Exception as e:
|
557 |
+
print(f"Unexpected error: {str(e)}")
|
558 |
+
return {"answer": "error"}
|
559 |
+
|
560 |
+
|
561 |
+
|
562 |
+
|
563 |
+
|
564 |
+
|
565 |
+
|
566 |
+
|
567 |
+
async def csv_chart(csv_url: str, query: str):
|
568 |
+
"""
|
569 |
+
Generate a chart based on the provided CSV URL and query.
|
570 |
+
Prioritizes raw Groq, then LangChain Gemini, and finally LangChain Groq as fallback.
|
571 |
+
|
572 |
+
Parameters:
|
573 |
+
- csv_url (str): The URL of the CSV file.
|
574 |
+
- query (str): The query for generating the chart.
|
575 |
+
|
576 |
+
Returns:
|
577 |
+
- dict: A dictionary containing either:
|
578 |
+
- {"image_url": "https://example.com/chart.png"} on success, or
|
579 |
+
- {"error": "error message"} on failure
|
580 |
+
|
581 |
+
Example:
|
582 |
+
- csv_url: "https://example.com/data.csv"
|
583 |
+
- query: "Show sales trends as a line chart"
|
584 |
+
Returns:
|
585 |
+
- dict: {"image_url": "https://storage.example.com/chart_uuid.png"}
|
586 |
+
"""
|
587 |
+
|
588 |
+
async def upload_and_return(image_path: str) -> dict:
|
589 |
+
"""Helper function to handle image uploads"""
|
590 |
+
unique_name = f'{uuid.uuid4()}.png'
|
591 |
+
public_url = await upload_image_to_supabase(image_path, unique_name)
|
592 |
+
print(f"Uploaded chart: {public_url}")
|
593 |
+
return {"image_url": public_url}
|
594 |
+
|
595 |
+
try:
|
596 |
+
# --- 1. First Attempt: Raw Groq ---
|
597 |
+
try:
|
598 |
+
groq_result = await asyncio.to_thread(groq_chart, csv_url, query)
|
599 |
+
print(f"Raw Groq chart result:", groq_result)
|
600 |
+
|
601 |
+
if groq_result and groq_result != 'Chart not generated':
|
602 |
+
return await upload_and_return(groq_result)
|
603 |
+
|
604 |
+
raise Exception("Raw Groq failed to generate chart")
|
605 |
+
|
606 |
+
except Exception as groq_error:
|
607 |
+
print(f"Raw Groq failed ({str(groq_error)}), trying LangChain Gemini...")
|
608 |
+
|
609 |
+
# --- 2. Second Attempt: LangChain Gemini ---
|
610 |
+
try:
|
611 |
+
gemini_result = await asyncio.to_thread(
|
612 |
+
langchain_gemini_csv_handler, csv_url, query, True
|
613 |
+
)
|
614 |
+
print("LangChain Gemini chart result:", gemini_result)
|
615 |
+
|
616 |
+
if gemini_result:
|
617 |
+
clean_path = gemini_result.strip()
|
618 |
+
return await upload_and_return(clean_path)
|
619 |
+
|
620 |
+
raise Exception("LangChain Gemini returned empty result")
|
621 |
+
|
622 |
+
except Exception as gemini_error:
|
623 |
+
print(f"LangChain Gemini failed ({str(gemini_error)}), trying LangChain Groq...")
|
624 |
+
|
625 |
+
# --- 3. Final Attempt: LangChain Groq ---
|
626 |
+
try:
|
627 |
+
lc_groq_paths = await asyncio.to_thread(
|
628 |
+
langchain_csv_chart, csv_url, query, True
|
629 |
+
)
|
630 |
+
print("LangChain Groq chart result:", lc_groq_paths)
|
631 |
+
|
632 |
+
if isinstance(lc_groq_paths, list) and lc_groq_paths:
|
633 |
+
return await upload_and_return(lc_groq_paths[0])
|
634 |
+
|
635 |
+
return {"error": "All chart generation methods failed"}
|
636 |
+
|
637 |
+
except Exception as lc_groq_error:
|
638 |
+
print(f"LangChain Groq failed: {str(lc_groq_error)}")
|
639 |
+
return {"error": "Could not generate chart"}
|
640 |
+
|
641 |
+
except Exception as e:
|
642 |
+
print(f"Critical error: {str(e)}")
|
643 |
+
return {"error": "Internal system error"}
|