fix: add google-generativeai to requirements, lazy-import agent in CI
Browse files- backend/api/main.py +22 -8
- backend/services/langgraph_agent.py +8 -1
- requirements.txt +2 -0
backend/api/main.py
CHANGED
|
@@ -392,18 +392,32 @@ app.include_router(v2_router)
|
|
| 392 |
# ---------------------------------------------------------------------------
|
| 393 |
from fastapi import Request
|
| 394 |
from fastapi.responses import StreamingResponse
|
| 395 |
-
from backend.services.langgraph_agent import run_agent_stream
|
| 396 |
-
from ml_core.demand_forecaster import TobitRegressor
|
| 397 |
-
from ml_core.demand_simulation import run_simulation as run_demand_sim
|
| 398 |
from ml_core.fraud_guard import FraudGuard
|
| 399 |
-
from ml_core.fraud_simulation import generate_fraud_events
|
| 400 |
-
from ml_core.dispatch_batcher import DispatchBatcher
|
| 401 |
-
from ml_core.eta_smoother import ETASmoother
|
| 402 |
import numpy as np
|
| 403 |
|
| 404 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 405 |
_fraud_guard = FraudGuard()
|
| 406 |
-
_dispatch_batcher = DispatchBatcher() if hasattr(__import__("ml_core.dispatch_batcher", fromlist=["DispatchBatcher"]), "DispatchBatcher") else None
|
| 407 |
|
| 408 |
|
| 409 |
class AgentChatRequest(BaseModel):
|
|
|
|
| 392 |
# ---------------------------------------------------------------------------
|
| 393 |
from fastapi import Request
|
| 394 |
from fastapi.responses import StreamingResponse
|
|
|
|
|
|
|
|
|
|
| 395 |
from ml_core.fraud_guard import FraudGuard
|
|
|
|
|
|
|
|
|
|
| 396 |
import numpy as np
|
| 397 |
|
| 398 |
+
# Optional: LangGraph agent requires google-generativeai.
|
| 399 |
+
# Import lazily so tests and CI pass even if the package is absent.
|
| 400 |
+
try:
|
| 401 |
+
from backend.services.langgraph_agent import run_agent_stream
|
| 402 |
+
_agent_available = True
|
| 403 |
+
except ImportError:
|
| 404 |
+
_agent_available = False
|
| 405 |
+
async def run_agent_stream(message, history):
|
| 406 |
+
yield 'data: {"type": "error", "message": "google-generativeai not installed"}\n\n'
|
| 407 |
+
yield 'data: {"type": "done"}\n\n'
|
| 408 |
+
|
| 409 |
+
# Optional ML core imports — fail gracefully if modules missing
|
| 410 |
+
try:
|
| 411 |
+
from ml_core.demand_forecaster import TobitRegressor
|
| 412 |
+
from ml_core.dispatch_batcher import DispatchBatcher
|
| 413 |
+
from ml_core.eta_smoother import ETASmoother
|
| 414 |
+
except ImportError:
|
| 415 |
+
TobitRegressor = None
|
| 416 |
+
DispatchBatcher = None
|
| 417 |
+
ETASmoother = None
|
| 418 |
+
|
| 419 |
+
# Singletons
|
| 420 |
_fraud_guard = FraudGuard()
|
|
|
|
| 421 |
|
| 422 |
|
| 423 |
class AgentChatRequest(BaseModel):
|
backend/services/langgraph_agent.py
CHANGED
|
@@ -12,7 +12,14 @@ from dotenv import load_dotenv
|
|
| 12 |
|
| 13 |
load_dotenv()
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
from backend.api.utils import call_swiggy_mcp_sync
|
| 17 |
|
| 18 |
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
|
|
|
| 12 |
|
| 13 |
load_dotenv()
|
| 14 |
|
| 15 |
+
try:
|
| 16 |
+
import google.generativeai as genai
|
| 17 |
+
except ModuleNotFoundError as e:
|
| 18 |
+
raise ImportError(
|
| 19 |
+
"google-generativeai is required for the AI Commerce Agent. "
|
| 20 |
+
"Install it with: pip install google-generativeai>=0.8.0"
|
| 21 |
+
) from e
|
| 22 |
+
|
| 23 |
from backend.api.utils import call_swiggy_mcp_sync
|
| 24 |
|
| 25 |
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
requirements.txt
CHANGED
|
@@ -15,3 +15,5 @@ pytest>=8.0.0
|
|
| 15 |
pytest-asyncio>=0.23.0
|
| 16 |
opentelemetry-api>=1.22.0
|
| 17 |
opentelemetry-sdk>=1.22.0
|
|
|
|
|
|
|
|
|
| 15 |
pytest-asyncio>=0.23.0
|
| 16 |
opentelemetry-api>=1.22.0
|
| 17 |
opentelemetry-sdk>=1.22.0
|
| 18 |
+
google-generativeai>=0.8.0
|
| 19 |
+
pydantic>=2.0.0
|