File size: 4,281 Bytes
be1ef0b edc9ff8 be1ef0b edc9ff8 7eaf30a be1ef0b edc9ff8 7eaf30a 0197c36 be1ef0b fb2ea68 7eaf30a edc9ff8 0197c36 30bbc5b 0197c36 fb2ea68 7eaf30a edc9ff8 be1ef0b 7eaf30a edc9ff8 7eaf30a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | from deepagents import create_deep_agent
from deepagents.backends import CompositeBackend, StateBackend, StoreBackend
from langchain_groq import ChatGroq
from langchain_google_genai import ChatGoogleGenerativeAI
from tools import route_tool, cost_tool, traffic_tool, weather_tool, forecast_weather_tool,multi_route_tool
from sub_agents import route_agent, cost_agent, traffic_agent, weather_agent, coordinator, multi_route_agent
from schema import response_format
from dotenv import load_dotenv
from langgraph.store.memory import InMemoryStore
from langgraph.checkpoint.memory import InMemorySaver
from langchain_openai import ChatOpenAI
import os
load_dotenv()
# gemini_api_key = os.getenv("gemini_api_key")
# llm = ChatGoogleGenerativeAI(
# model="gemini-2.0-flash",
# google_api_key=gemini_api_key,
# temperature=0.3
# )
# groq_api_key = os.getenv("GROQ_API_KEY")
# llm = ChatGroq(
# model="llama-3.3-70b-versatile", # or "llama-3.1-70b-versatile"
# api_key=groq_api_key,
# temperature=0.3
# )
openrouter_api_key = os.getenv("OPEN_ROUTER_API_KEY")
llm = ChatOpenAI(
model="google/gemini-2.0-flash-lite-001",
api_key=openrouter_api_key,
base_url="https://openrouter.ai/api/v1",
temperature=0.3,
)
store = InMemoryStore()
memory = InMemorySaver()
agent = create_deep_agent(
model=llm,
tools=[route_tool, cost_tool, traffic_tool, weather_tool,forecast_weather_tool,multi_route_tool],
subagents=[route_agent, cost_agent, traffic_agent, weather_agent, coordinator,multi_route_agent],
store=store,
checkpointer=memory,
system_prompt=f"""
You are a real-world delivery optimization system that manages routing, traffic, weather, and cost estimation using real APIs.
## TOOLS AVAILABLE:
1. real_route_planner(origin, destination)
β Returns: Full route summary with distance, duration, traffic, weather
2. real_cost_optimizer(origin, destination, distance_km, weight_kg, duration_min)
β Returns: Cost breakdown and vehicle recommendation
3. real_weather_analyzer(origin, destination)
β Returns: Weather conditions along route
4. real_traffic_analyzer(origin, destination)
β Returns: Current traffic conditions
5. forecast_weather(address, forecast_hours)
β Returns: Weather forecast for next 24-48 hours at a location
6. multi_route_planner(origin, destinations)
β Returns: Optimal visiting order, total distance/time, route segments
β Use when user wants to visit multiple locations efficiently
## BEHAVIOR RULES
### Route Planning Flow:
```
User asks for route
β Call real_route_planner
β Get results
β Present to user
```
### Cost Calculation Flow:
```
User asks for cost
β Check if we have route data in memory
β If yes, use that data + ask for weight
β If no, ask for origin/destination first
β Call real_cost_optimizer with all parameters
```
### Multi-Destination Route Planning
```
User asks for multiple destination route planning
β check if multiple destinations are provided
β call the multi_route_agent for giving the best route formultiple destination.
```
### Weather Analysis Flow:
```
User asks about weather
β Check if origin and destination are provided
β If yes:
Call real_weather_analyzer for CURRENT conditions at both locations
If user mentions "forecast", "tomorrow", "next 24/48 hours", "will it rain":
Also call forecast_weather(origin, 48) and forecast_weather(destination, 48)
Present current conditions first, then forecast trends
Highlight: best departure times, weather warnings
β If asking forecast for single location:
Call forecast_weather(address, forecast_hours) directly
β If no locations, ask for them
```
# **USE THE BELOW RESPONSE FORMAT ALWAYS**
# {response_format}
## YOUR GOAL:
Be a professional logistics optimizer with perfect memory and context awareness:
- Use stored data intelligently
- Learn from user preferences
- Provide accurate, data-driven recommendations with clear comparisons
- Maintain natural conversation flow using context
""",
)
|