Spaces:
Sleeping
Sleeping
Upload 8 files
Browse files- agents/__init__.py +0 -0
- agents/__pycache__/__init__.cpython-311.pyc +0 -0
- agents/__pycache__/rag_agent.cpython-311.pyc +0 -0
- agents/__pycache__/router_agent.cpython-311.pyc +0 -0
- agents/__pycache__/weather_agent.cpython-311.pyc +0 -0
- agents/rag_agent.py +75 -0
- agents/router_agent.py +50 -0
- agents/weather_agent.py +82 -0
agents/__init__.py
ADDED
|
File without changes
|
agents/__pycache__/__init__.cpython-311.pyc
ADDED
|
Binary file (161 Bytes). View file
|
|
|
agents/__pycache__/rag_agent.cpython-311.pyc
ADDED
|
Binary file (4.63 kB). View file
|
|
|
agents/__pycache__/router_agent.cpython-311.pyc
ADDED
|
Binary file (3.25 kB). View file
|
|
|
agents/__pycache__/weather_agent.cpython-311.pyc
ADDED
|
Binary file (4.4 kB). View file
|
|
|
agents/rag_agent.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Dict, Any, List
|
| 2 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 3 |
+
from langchain.prompts import ChatPromptTemplate
|
| 4 |
+
from langchain.schema import Document
|
| 5 |
+
from pydantic import BaseModel, Field
|
| 6 |
+
from models.vector_store import VectorStore
|
| 7 |
+
import os
|
| 8 |
+
from dotenv import load_dotenv
|
| 9 |
+
load_dotenv()
|
| 10 |
+
|
| 11 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 12 |
+
|
| 13 |
+
class RAGAgentState(BaseModel):
|
| 14 |
+
"""State for the RAG agent"""
|
| 15 |
+
query: str = Field(description="The user's query for document retrieval")
|
| 16 |
+
context: List[Dict[str, Any]] = Field(description="Retrieved context from documents", default=[])
|
| 17 |
+
response: str = Field(description="The agent's response", default="")
|
| 18 |
+
|
| 19 |
+
class RAGAgent:
|
| 20 |
+
"""Agent that handles document-based queries using RAG"""
|
| 21 |
+
|
| 22 |
+
def __init__(self, api_key: str = GEMINI_API_KEY):
|
| 23 |
+
self.llm = ChatGoogleGenerativeAI(
|
| 24 |
+
model="gemini-2.0-flash",
|
| 25 |
+
google_api_key=api_key
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
self.vector_store = VectorStore()
|
| 29 |
+
|
| 30 |
+
self.rag_prompt = ChatPromptTemplate.from_messages([
|
| 31 |
+
("system", """You are an expert research assistant helping a user understand complex topics clearly and concisely.
|
| 32 |
+
Use only the provided context to answer the user's question. If the context does not contain the answer, say:
|
| 33 |
+
"I don't have enough information to answer that question."
|
| 34 |
+
|
| 35 |
+
When answering:
|
| 36 |
+
- Explain technical terms simply
|
| 37 |
+
- Use examples if helpful
|
| 38 |
+
- Keep the tone friendly and helpful
|
| 39 |
+
|
| 40 |
+
Context:
|
| 41 |
+
{context}"""),
|
| 42 |
+
("human", "{query}")
|
| 43 |
+
])
|
| 44 |
+
|
| 45 |
+
self.rag_chain = self.rag_prompt | self.llm
|
| 46 |
+
|
| 47 |
+
def retrieve_context(self, query: str, k: int = 4) -> List[Document]:
|
| 48 |
+
"""Retrieve relevant context from the vector store"""
|
| 49 |
+
return self.vector_store.similarity_search(query, k=k)
|
| 50 |
+
|
| 51 |
+
def get_rag_response(self, query: str) -> Dict[str, Any]:
|
| 52 |
+
"""Generate a RAG-based response to the query"""
|
| 53 |
+
# Retrieve relevant documents
|
| 54 |
+
docs = self.retrieve_context(query)
|
| 55 |
+
|
| 56 |
+
if not docs:
|
| 57 |
+
return {
|
| 58 |
+
"context": [],
|
| 59 |
+
"response": "I couldn't find any relevant information in the documents to answer your question."
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
# Format context
|
| 63 |
+
context_texts = [doc.page_content for doc in docs]
|
| 64 |
+
context_str = "\n\n".join(context_texts)
|
| 65 |
+
|
| 66 |
+
# Generate response
|
| 67 |
+
response = self.rag_chain.invoke({
|
| 68 |
+
"query": query,
|
| 69 |
+
"context": context_str
|
| 70 |
+
})
|
| 71 |
+
|
| 72 |
+
return {
|
| 73 |
+
"context": [{"page_content": doc.page_content, "metadata": doc.metadata} for doc in docs],
|
| 74 |
+
"response": response.content
|
| 75 |
+
}
|
agents/router_agent.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Dict, Any, List, Tuple
|
| 2 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 3 |
+
from langchain.prompts import ChatPromptTemplate
|
| 4 |
+
from pydantic import BaseModel , Field
|
| 5 |
+
from langgraph.graph import StateGraph
|
| 6 |
+
import os
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
load_dotenv()
|
| 9 |
+
|
| 10 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 11 |
+
|
| 12 |
+
class RouterState(BaseModel):
|
| 13 |
+
"""State for the router agent"""
|
| 14 |
+
query: str = Field(description="The user's query")
|
| 15 |
+
action: str = Field(description="The action to take: 'weather' or 'document'")
|
| 16 |
+
|
| 17 |
+
class RouterAgent:
|
| 18 |
+
"""Agent that decides whether to use weather API or document RAG"""
|
| 19 |
+
|
| 20 |
+
def __init__(self, api_key: str = GEMINI_API_KEY):
|
| 21 |
+
self.llm = ChatGoogleGenerativeAI(
|
| 22 |
+
model="gemini-2.0-flash",
|
| 23 |
+
google_api_key=api_key
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
self.prompt = ChatPromptTemplate.from_messages([
|
| 27 |
+
("system", """You are a router agent that decides whether a user query is about:
|
| 28 |
+
1. Weather information (requiring a weather API call)
|
| 29 |
+
2. Information from documents (requiring RAG retrieval)
|
| 30 |
+
|
| 31 |
+
If the query mentions weather, forecast, temperature, rain, sun, climate, or other weather-related terms for a specific location, classify it as 'weather'.
|
| 32 |
+
|
| 33 |
+
Otherwise, classify it as 'document' for document retrieval.
|
| 34 |
+
|
| 35 |
+
Return only 'weather' or 'document' as your classification."""),
|
| 36 |
+
("human", "{query}")
|
| 37 |
+
])
|
| 38 |
+
|
| 39 |
+
self.chain = self.prompt | self.llm
|
| 40 |
+
|
| 41 |
+
def route_query(self, query: str) -> str:
|
| 42 |
+
"""Route a query to either weather API or document RAG"""
|
| 43 |
+
response = self.chain.invoke({"query": query})
|
| 44 |
+
# Extract just the decision: 'weather' or 'document'
|
| 45 |
+
decision = response.content.strip().lower()
|
| 46 |
+
|
| 47 |
+
if "weather" in decision:
|
| 48 |
+
return "weather"
|
| 49 |
+
else:
|
| 50 |
+
return "document"
|
agents/weather_agent.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Dict, Any
|
| 2 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 3 |
+
from langchain.prompts import ChatPromptTemplate
|
| 4 |
+
from langchain.pydantic_v1 import BaseModel, Field
|
| 5 |
+
from utils.api_handler import WeatherAPIHandler
|
| 6 |
+
import os
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
load_dotenv()
|
| 9 |
+
|
| 10 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class WeatherAgentState(BaseModel):
|
| 15 |
+
"""State for the weather agent"""
|
| 16 |
+
query: str = Field(description="The user's query about weather")
|
| 17 |
+
city: str = Field(description="The city to get weather for", default="")
|
| 18 |
+
weather_data: Dict[str, Any] = Field(description="Raw weather data", default={})
|
| 19 |
+
response: str = Field(description="The agent's response", default="")
|
| 20 |
+
|
| 21 |
+
class WeatherAgent:
|
| 22 |
+
"""Agent that handles weather-related queries"""
|
| 23 |
+
|
| 24 |
+
def __init__(self, api_key: str = GEMINI_API_KEY):
|
| 25 |
+
self.llm = ChatGoogleGenerativeAI(
|
| 26 |
+
model="gemini-2.0-flash",
|
| 27 |
+
google_api_key=api_key
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
self.weather_api = WeatherAPIHandler()
|
| 31 |
+
|
| 32 |
+
self.extract_city_prompt = ChatPromptTemplate.from_messages([
|
| 33 |
+
("system", """Extract the city name from the user's weather query.
|
| 34 |
+
Return ONLY the city name, nothing else.
|
| 35 |
+
If no city is mentioned, return "Not specified"."""),
|
| 36 |
+
("human", "{query}")
|
| 37 |
+
])
|
| 38 |
+
|
| 39 |
+
self.response_prompt = ChatPromptTemplate.from_messages([
|
| 40 |
+
("system", """You are a helpful weather assistant.
|
| 41 |
+
Format the weather information in a friendly, conversational way.
|
| 42 |
+
Include all relevant weather details from the provided data."""),
|
| 43 |
+
("human", "Query: {query}\nWeather Data: {weather_info}")
|
| 44 |
+
])
|
| 45 |
+
|
| 46 |
+
self.extract_city_chain = self.extract_city_prompt | self.llm
|
| 47 |
+
self.response_chain = self.response_prompt | self.llm
|
| 48 |
+
|
| 49 |
+
def extract_city(self, query: str) -> str:
|
| 50 |
+
"""Extract city name from the user query"""
|
| 51 |
+
response = self.extract_city_chain.invoke({"query": query})
|
| 52 |
+
city = response.content.strip()
|
| 53 |
+
|
| 54 |
+
# Handle case where no city is specified
|
| 55 |
+
if city.lower() == "not specified":
|
| 56 |
+
return "London" # Default city
|
| 57 |
+
|
| 58 |
+
return city
|
| 59 |
+
|
| 60 |
+
def get_weather_response(self, query: str, city: str = None) -> Dict[str, Any]:
|
| 61 |
+
"""Get weather data and generate a response"""
|
| 62 |
+
# Extract city if not provided
|
| 63 |
+
if not city:
|
| 64 |
+
city = self.extract_city(query)
|
| 65 |
+
|
| 66 |
+
# Get weather data
|
| 67 |
+
weather_data = self.weather_api.get_weather(city)
|
| 68 |
+
|
| 69 |
+
# Format weather data
|
| 70 |
+
weather_info = self.weather_api.format_weather_data(weather_data)
|
| 71 |
+
|
| 72 |
+
# Generate response
|
| 73 |
+
response = self.response_chain.invoke({
|
| 74 |
+
"query": query,
|
| 75 |
+
"weather_info": weather_info
|
| 76 |
+
})
|
| 77 |
+
|
| 78 |
+
return {
|
| 79 |
+
"city": city,
|
| 80 |
+
"weather_data": weather_data,
|
| 81 |
+
"response": response.content
|
| 82 |
+
}
|