trying groq kimi
Browse files- groq_kimi_csv_agent.py +0 -0
- groq_kimi_instance_provider.py +0 -0
- groq_kimi_report_generator.py +0 -0
- orchestrator_functions.py +51 -28
groq_kimi_csv_agent.py
ADDED
|
File without changes
|
groq_kimi_instance_provider.py
ADDED
|
File without changes
|
groq_kimi_report_generator.py
ADDED
|
File without changes
|
orchestrator_functions.py
CHANGED
|
@@ -342,8 +342,10 @@ def langchain_csv_chart(csv_url: str, question: str, chart_required: bool):
|
|
| 342 |
logger.info("All API keys exhausted for chart generation")
|
| 343 |
return None
|
| 344 |
|
| 345 |
-
####################################### Orchestrator Function for Chat #######################################
|
| 346 |
|
|
|
|
|
|
|
|
|
|
| 347 |
async def csv_chat(csv_url: str, query: str) -> dict:
|
| 348 |
"""
|
| 349 |
Generate a response based on the provided CSV URL and query.
|
|
@@ -398,15 +400,32 @@ def is_valid_response(response) -> bool:
|
|
| 398 |
return True
|
| 399 |
|
| 400 |
|
|
|
|
|
|
|
| 401 |
####################################### Orchestrator Function for Chart #######################################
|
| 402 |
|
|
|
|
| 403 |
async def csv_chart(csv_url: str, query: str, chat_id: str) -> Dict[str, str]:
|
| 404 |
"""
|
| 405 |
Generate a chart based on the provided CSV URL and query.
|
| 406 |
-
Prioritizes LangChain-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 407 |
"""
|
| 408 |
error_messages = []
|
| 409 |
-
|
| 410 |
async def upload_and_return(image_path: str) -> Dict[str, str]:
|
| 411 |
"""Handle image upload and return public URL"""
|
| 412 |
unique_name = f'{uuid.uuid4()}.png'
|
|
@@ -418,50 +437,54 @@ async def csv_chart(csv_url: str, query: str, chat_id: str) -> Dict[str, str]:
|
|
| 418 |
logger.warning(f"Could not delete temp file {image_path}: {str(e)}")
|
| 419 |
return {"image_url": public_url}
|
| 420 |
|
| 421 |
-
# --- 1. First Attempt: LangChain
|
| 422 |
-
try:
|
| 423 |
-
lc_groq_paths = await asyncio.to_thread(
|
| 424 |
-
langchain_csv_chart, csv_url, query, True
|
| 425 |
-
)
|
| 426 |
-
logger.info(f"LangChain-Groq chart result: {lc_groq_paths}")
|
| 427 |
-
|
| 428 |
-
if lc_groq_paths:
|
| 429 |
-
if isinstance(lc_groq_paths, list) and lc_groq_paths:
|
| 430 |
-
first_path = lc_groq_paths[0]
|
| 431 |
-
if os.path.exists(first_path):
|
| 432 |
-
return await upload_and_return(first_path)
|
| 433 |
-
|
| 434 |
-
if isinstance(lc_groq_paths, str) and os.path.exists(lc_groq_paths):
|
| 435 |
-
return await upload_and_return(lc_groq_paths)
|
| 436 |
-
|
| 437 |
-
error_messages.append("LangChain-Groq returned invalid result")
|
| 438 |
-
except Exception as lc_groq_error:
|
| 439 |
-
error_messages.append(f"LangChain-Groq error: {str(lc_groq_error)}")
|
| 440 |
-
logger.error(f"Groq chart failed: {str(lc_groq_error)}")
|
| 441 |
-
|
| 442 |
-
# --- 2. Fallback Attempt: LangChain Gemini ---
|
| 443 |
try:
|
| 444 |
gemini_result = await asyncio.to_thread(
|
| 445 |
langchain_gemini_csv_handler, csv_url, query, True
|
| 446 |
)
|
| 447 |
logger.info(f"LangChain-Gemini chart result: {gemini_result}")
|
| 448 |
-
|
| 449 |
if gemini_result:
|
|
|
|
| 450 |
if isinstance(gemini_result, str):
|
| 451 |
clean_path = gemini_result.strip()
|
| 452 |
if os.path.exists(clean_path):
|
| 453 |
return await upload_and_return(clean_path)
|
| 454 |
-
|
| 455 |
if isinstance(gemini_result, list) and gemini_result:
|
| 456 |
first_path = gemini_result[0]
|
| 457 |
if os.path.exists(first_path):
|
| 458 |
return await upload_and_return(first_path)
|
| 459 |
-
|
| 460 |
error_messages.append("LangChain-Gemini returned invalid result")
|
| 461 |
except Exception as gemini_error:
|
| 462 |
error_messages.append(f"LangChain-Gemini error: {str(gemini_error)}")
|
| 463 |
logger.error(f"Gemini chart failed: {str(gemini_error)}")
|
| 464 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 465 |
# --- Final Error Handling ---
|
| 466 |
logger.error(f"All chart generation failed. Errors: {'; '.join(error_messages)}")
|
| 467 |
return {"error": "Could not generate chart from the provided data"}
|
|
|
|
|
|
|
|
|
|
|
|
| 342 |
logger.info("All API keys exhausted for chart generation")
|
| 343 |
return None
|
| 344 |
|
|
|
|
| 345 |
|
| 346 |
+
|
| 347 |
+
|
| 348 |
+
####################################### Orchestrator Function for Chat #######################################
|
| 349 |
async def csv_chat(csv_url: str, query: str) -> dict:
|
| 350 |
"""
|
| 351 |
Generate a response based on the provided CSV URL and query.
|
|
|
|
| 400 |
return True
|
| 401 |
|
| 402 |
|
| 403 |
+
|
| 404 |
+
|
| 405 |
####################################### Orchestrator Function for Chart #######################################
|
| 406 |
|
| 407 |
+
|
| 408 |
async def csv_chart(csv_url: str, query: str, chat_id: str) -> Dict[str, str]:
|
| 409 |
"""
|
| 410 |
Generate a chart based on the provided CSV URL and query.
|
| 411 |
+
Prioritizes LangChain-Gemini first, then falls back to LangChain-Groq.
|
| 412 |
+
|
| 413 |
+
Parameters:
|
| 414 |
+
- csv_url (str): URL of the CSV file
|
| 415 |
+
- query (str): Query for generating the chart
|
| 416 |
+
- chat_id (str): Chat session ID for file storage
|
| 417 |
+
|
| 418 |
+
Returns:
|
| 419 |
+
- dict: Either {"image_url": "url"} on success or {"error": "message"} on failure
|
| 420 |
+
|
| 421 |
+
Example:
|
| 422 |
+
- csv_url: "https://example.com/data.csv"
|
| 423 |
+
- query: "Show sales trends as line chart"
|
| 424 |
+
Returns:
|
| 425 |
+
- dict: {"image_url": "https://storage.example.com/chart_uuid.png"}
|
| 426 |
"""
|
| 427 |
error_messages = []
|
| 428 |
+
|
| 429 |
async def upload_and_return(image_path: str) -> Dict[str, str]:
|
| 430 |
"""Handle image upload and return public URL"""
|
| 431 |
unique_name = f'{uuid.uuid4()}.png'
|
|
|
|
| 437 |
logger.warning(f"Could not delete temp file {image_path}: {str(e)}")
|
| 438 |
return {"image_url": public_url}
|
| 439 |
|
| 440 |
+
# --- 1. First Attempt: LangChain Gemini ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 441 |
try:
|
| 442 |
gemini_result = await asyncio.to_thread(
|
| 443 |
langchain_gemini_csv_handler, csv_url, query, True
|
| 444 |
)
|
| 445 |
logger.info(f"LangChain-Gemini chart result: {gemini_result}")
|
| 446 |
+
|
| 447 |
if gemini_result:
|
| 448 |
+
# Handle string or list response
|
| 449 |
if isinstance(gemini_result, str):
|
| 450 |
clean_path = gemini_result.strip()
|
| 451 |
if os.path.exists(clean_path):
|
| 452 |
return await upload_and_return(clean_path)
|
| 453 |
+
|
| 454 |
if isinstance(gemini_result, list) and gemini_result:
|
| 455 |
first_path = gemini_result[0]
|
| 456 |
if os.path.exists(first_path):
|
| 457 |
return await upload_and_return(first_path)
|
| 458 |
+
|
| 459 |
error_messages.append("LangChain-Gemini returned invalid result")
|
| 460 |
except Exception as gemini_error:
|
| 461 |
error_messages.append(f"LangChain-Gemini error: {str(gemini_error)}")
|
| 462 |
logger.error(f"Gemini chart failed: {str(gemini_error)}")
|
| 463 |
|
| 464 |
+
# --- 2. Fallback Attempt: LangChain Groq ---
|
| 465 |
+
try:
|
| 466 |
+
lc_groq_paths = await asyncio.to_thread(
|
| 467 |
+
langchain_csv_chart, csv_url, query, True
|
| 468 |
+
)
|
| 469 |
+
logger.info(f"LangChain-Groq chart result: {lc_groq_paths}")
|
| 470 |
+
|
| 471 |
+
if lc_groq_paths:
|
| 472 |
+
if isinstance(lc_groq_paths, list) and lc_groq_paths:
|
| 473 |
+
first_path = lc_groq_paths[0]
|
| 474 |
+
if os.path.exists(first_path):
|
| 475 |
+
return await upload_and_return(first_path)
|
| 476 |
+
|
| 477 |
+
if isinstance(lc_groq_paths, str) and os.path.exists(lc_groq_paths):
|
| 478 |
+
return await upload_and_return(lc_groq_paths)
|
| 479 |
+
|
| 480 |
+
error_messages.append("LangChain-Groq returned invalid result")
|
| 481 |
+
except Exception as lc_groq_error:
|
| 482 |
+
error_messages.append(f"LangChain-Groq error: {str(lc_groq_error)}")
|
| 483 |
+
logger.error(f"Groq chart failed: {str(lc_groq_error)}")
|
| 484 |
+
|
| 485 |
# --- Final Error Handling ---
|
| 486 |
logger.error(f"All chart generation failed. Errors: {'; '.join(error_messages)}")
|
| 487 |
return {"error": "Could not generate chart from the provided data"}
|
| 488 |
+
|
| 489 |
+
|
| 490 |
+
|