Spaces:
Running
Running
Create pandasai_setup.py
Browse files
insight_and_tasks/utils/pandasai_setup.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# utils/pandasai_setup.py
|
2 |
+
import os
|
3 |
+
import logging
|
4 |
+
import pandasai as pai
|
5 |
+
from pandasai_litellm import LiteLLM # Ensure this import matches your installed library
|
6 |
+
|
7 |
+
# Configure logger for this module
|
8 |
+
logger = logging.getLogger(__name__)
|
9 |
+
|
10 |
+
# It's good practice to define constants at the top or in a config file
|
11 |
+
DEFAULT_PANDASAI_MODEL = "gemini/gemini-2.5-flash-preview-05-20" # Using a common default
|
12 |
+
|
13 |
+
def configure_pandasai(api_key: str, model_name: str = None):
|
14 |
+
"""
|
15 |
+
Configures PandasAI with LiteLLM using the provided API key and model.
|
16 |
+
|
17 |
+
Args:
|
18 |
+
api_key: The Google API key.
|
19 |
+
model_name: The specific model to use (e.g., "gemini/gemini-1.5-flash-latest").
|
20 |
+
If None, uses DEFAULT_PANDASAI_MODEL.
|
21 |
+
"""
|
22 |
+
if not api_key:
|
23 |
+
logger.error("PandasAI Configuration Error: API key is missing.")
|
24 |
+
# Depending on strictness, you might raise an error or just log
|
25 |
+
# raise ValueError("API key must be provided for PandasAI configuration")
|
26 |
+
return
|
27 |
+
|
28 |
+
selected_model = model_name if model_name else DEFAULT_PANDASAI_MODEL
|
29 |
+
|
30 |
+
try:
|
31 |
+
llm = LiteLLM(
|
32 |
+
model=selected_model, # Use the selected model
|
33 |
+
api_key=api_key
|
34 |
+
# You might need to add other parameters for LiteLLM depending on the provider
|
35 |
+
# e.g., if not using a Google model directly via gemini provider in LiteLLM
|
36 |
+
)
|
37 |
+
|
38 |
+
# PandasAI configuration
|
39 |
+
pai.config.set({
|
40 |
+
"llm": llm,
|
41 |
+
"verbose": os.environ.get("PANDASAI_VERBOSE", "False").lower() == "true",
|
42 |
+
"enable_cache": True,
|
43 |
+
"enforce_privacy": False, # Be cautious with this in production
|
44 |
+
"save_charts": False, # Set to True if you want to save charts locally
|
45 |
+
# "save_charts_path": "charts_output", # Define path if saving charts
|
46 |
+
"custom_whitelisted_dependencies": [], # Add any custom dependencies if needed
|
47 |
+
"max_retries": 3, # Default retries for PandasAI operations
|
48 |
+
"temperature": 0.3, # Lower temperature for more deterministic/factual outputs
|
49 |
+
# "open_charts": False # Whether to automatically open charts
|
50 |
+
})
|
51 |
+
logger.info(f"PandasAI configured successfully with model: {selected_model}")
|
52 |
+
logger.info(f"PandasAI LLM object: {pai.config.llm}")
|
53 |
+
|
54 |
+
|
55 |
+
except ImportError:
|
56 |
+
logger.error("PandasAI or pandasai_litellm is not installed. Please install the required packages.")
|
57 |
+
except Exception as e:
|
58 |
+
logger.error(f"Error configuring PandasAI: {e}", exc_info=True)
|
59 |
+
|