Spaces:
Sleeping
Sleeping
File size: 15,165 Bytes
f374654 |
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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
# app.py
import json
import openai
from typing import Dict, Any, Optional, List
from dataclasses import dataclass
import logging
from openai import OpenAI
from dotenv import load_dotenv
import os
# Import your modules
from easy_agents import EASYFARMS_FUNCTION_SCHEMAS, execute_easyfarms_function
from alert import WEATHER_TOOLS , execute_function
from conversation_manager import ConversationManager
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Load environment variables
load_dotenv()
@dataclass
class Config:
"""Configuration settings"""
api_key: str
api_url: str
model_name: str
max_retries: int = 3
temperature: float = 0.5
@classmethod
def from_env(cls):
"""Load configuration from environment variables"""
return cls(
api_key=os.getenv("API_KEY"),
api_url=os.getenv("API_URL"),
model_name=os.getenv("MODEL_NAME")
)
class EasyFarmsAssistant:
"""Enhanced EasyFarms AI Assistant with weather integration and persistent sessions"""
def __init__(self, config: Optional[Config] = None, manager: Optional[ConversationManager] = None):
"""
Initialize the assistant with configuration and a conversation manager.
Args:
config (Optional[Config]): Configuration object. If None, loads from environment.
manager (Optional[ConversationManager]): Manager for handling conversation persistence.
"""
self.config = config or Config.from_env()
# Validate configuration
if not all([self.config.api_key, self.config.api_url, self.config.model_name]):
raise ValueError("Missing required configuration: API_KEY, API_URL, and MODEL_NAME must be set")
self.client = OpenAI(
api_key=self.config.api_key,
base_url=self.config.api_url
)
# All available functions from both modules are combined into the tools list
self.tools = self._initialize_tools()
# Use the provided conversation manager or create a new one
self.manager = manager or ConversationManager()
# System prompts
self.system_prompt = """You are the AI assistant for EasyForms Agritech Solutions. Your task is to provide users with clear, concise, and actionable responses regarding agriculture, crop management, production, treatment, weather alerts, and related queries.
Core Capabilities:
- Crop recommendations based on soil and weather conditions
- Fertilizer recommendations for specific crops
- Plant disease detection and treatment advice
- Weather alerts and forecasts for farming decisions
- Market data and commodity prices
- General agricultural guidance
Rules:
1. Check if any relevant function_tools or datasets are available for this query.
2. If available, use the functions to fetch information and generate the final user-facing response.
3. If the functions or data are unavailable, do **not stop**; instead, generate a general, well-reasoned response based on your own knowledge.
4. Keep the response **simple, smooth, well-pointed, and concise**.
5. Structure the response with bullet points or numbered steps where helpful.
6. Provide practical, actionable advice a user can implement immediately.
7. Use English or Hindi based on user preference.
8. If any information is uncertain, mention it clearly and suggest alternatives.
9. For weather-related queries, prioritize safety and timely alerts."""
self.final_system = """You are the final response assistant for EasyForms Agritech Solutions. Use the outputs from previous function calls to generate a **clear, concise, actionable response** for the user.
Rules:
1. Combine the function outputs and your own reasoning to answer the query.
2. Keep responses simple, smooth, well-pointed, and concise.
3. Structure response with headings or bullet points if helpful.
4. Provide practical advice that a farmer or user can implement immediately.
5. If some data is missing, clearly state it and offer alternatives.
6. Use English or Hindi based on the user preference.
7. For weather alerts, emphasize urgency and protective measures."""
def _initialize_tools(self) -> List[Dict]:
"""Initialize and convert all function schemas to the new tools format"""
tools = []
# Convert EasyFarms schemas to the new format
for schema in EASYFARMS_FUNCTION_SCHEMAS:
tool = {
"type": "function",
"function": {
"name": schema["name"],
"description": schema["description"],
"parameters": schema["parameters"]
}
}
tools.append(tool)
# Add weather tools (which are already in the correct format)
tools.extend(WEATHER_TOOLS)
return tools
def call_function(self, function_name: str, arguments: Dict) -> Any:
"""Route function calls to appropriate handlers with error handling"""
try:
# Map all available function names to their handlers
function_map = {
# EasyFarms functions
"get_crop_recommendation": lambda args: execute_easyfarms_function("get_crop_recommendation", **args),
"get_fertilizer_recommendation": lambda args: execute_easyfarms_function("get_fertilizer_recommendation", **args),
"detect_plant_disease": lambda args: execute_easyfarms_function("detect_plant_disease", **args),
"get_supported_options": lambda args: execute_easyfarms_function("get_supported_options", **args),
"get_market_prices": lambda args: execute_easyfarms_function("get_market_prices", **args),
"compare_commodity_prices": lambda args: execute_easyfarms_function("compare_commodity_prices", **args),
"get_market_locations": lambda args: execute_easyfarms_function("get_market_locations", **args),
"get_commodity_list": lambda args: execute_easyfarms_function("get_commodity_list", **args),
# Weather alert functions
"get_weather_alerts": lambda args: self._execute_weather_function("get_weather_alerts", **args),
"get_weather": lambda args: self._execute_weather_function("get_weather", **args),
"get_alert_summary": lambda args: self._execute_weather_function("get_alert_summary", **args),
"get_available_locations": lambda args: self._execute_weather_function("get_available_locations", **args)
}
if function_name in function_map:
return function_map[function_name](arguments)
else:
return {"error": f"Unknown function: {function_name}"}
except Exception as e:
logger.error(f"Error executing function {function_name}: {e}")
return {"error": str(e)}
def _execute_weather_function(self, function_name: str, **kwargs):
"""Helper to execute weather functions from the alert.py module"""
from alert import execute_function
return execute_function(function_name, kwargs)
def process_query(self, user_message: str, session_id: str, image_url: Optional[str] = None) -> str:
"""
Process user query, correctly reformatting history for the LLM API call.
"""
try:
# MEMORY STEP 1: Fetch the complete past conversation using the session_id.
conversation_history = self.manager.get_history(session_id)
# Prepare the list that will be sent to the AI
messages = [{"role": "system", "content": self.system_prompt}]
# MEMORY STEP 2: Loop through the history and add every past message.
# This builds the AI's memory of what was said before.
for message in conversation_history:
if message.get("role") == "user":
llm_user_content = message.get("content", "")
if message.get("imageUrl"):
llm_user_content += f" [image_url: {message.get('imageUrl')}]"
messages.append({"role": "user", "content": llm_user_content})
elif message.get("role") == "assistant":
messages.append({"role": "assistant", "content": message.get("content", "")})
# MEMORY STEP 3: Add the user's CURRENT message to the end of the history.
llm_message_content = user_message
if image_url:
llm_message_content += f" [image_url: {image_url}]"
messages.append({"role": "user", "content": llm_message_content})
# MEMORY STEP 4: Send the entire 'messages' list to the AI.
response = self.client.chat.completions.create(
model=self.config.model_name,
messages=messages,
tools=self.tools,
tool_choice="auto",
temperature=self.config.temperature
)
message = response.choices[0].message
if hasattr(message, 'tool_calls') and message.tool_calls:
# Add the assistant's message with tool calls
messages.append({
"role": "assistant",
"tool_calls": [
{
"id": tool_call.id,
"type": "function",
"function": {
"name": tool_call.function.name,
"arguments": tool_call.function.arguments
}
} for tool_call in message.tool_calls
]
})
# Execute all tool calls
for tool_call in message.tool_calls:
function_name = tool_call.function.name
function_args = json.loads(tool_call.function.arguments)
logger.info(f"Calling function: {function_name} with args: {function_args}")
# Call the function
function_result = self.call_function(function_name, function_args)
# Add function result to messages
messages.append({
"role": "tool",
"tool_call_id": tool_call.id,
"content": json.dumps(function_result)
})
# Add final system prompt for generating response
messages.append({
"role": "system",
"content": self.final_system
})
# Get final response
final_response = self.client.chat.completions.create(
model=self.config.model_name,
messages=messages,
temperature=self.config.temperature
)
response_content = final_response.choices[0].message.content
else:
response_content = message.content
# After getting the response, save the new turns back to the database for the next message.
user_turn_for_storage = {"role": "user", "content": user_message}
if image_url:
user_turn_for_storage["imageUrl"] = image_url
updated_history = conversation_history + [
user_turn_for_storage,
{"role": "assistant", "content": response_content}
]
self.manager.save_history(session_id, updated_history)
return response_content
except Exception as e:
logger.error(f"Error processing query for session {session_id}: {e}")
return f"I apologize, but I encountered an error: {str(e)}. Please try again or rephrase your question."
def clear_history(self, session_id: str) -> bool:
"""
Clear conversation history for a specific session from the database.
Args:
session_id: The ID of the session to clear.
Returns:
True if deletion was successful, False otherwise.
"""
logger.info(f"Clearing history for session: {session_id}")
return self.manager.delete_history(session_id)
# Utility class for generating example queries (can be used for testing)
class QuickQueries:
"""Pre-defined query templates for common farming questions"""
@staticmethod
def crop_recommendation(N: int, P: int, K: int, temp: float, humidity: float, ph: float = 6.5) -> str:
"""Generate crop recommendation query"""
return f"What crop should I grow with N={N}, P={P}, K={K}, temperature {temp}°C, humidity {humidity}%, pH {ph}?"
@staticmethod
def fertilizer_query(crop: str, soil: str, N: int, P: int, K: int) -> str:
"""Generate fertilizer recommendation query"""
return f"I need fertilizer recommendation for {crop} in {soil} soil with N={N}, P={P}, K={K}"
@staticmethod
def weather_alert(location: str = "") -> str:
"""Generate weather alert query"""
location_str = f" for {location}" if location else ""
return f"What are the current weather alerts and conditions{location_str}? How will this affect farming?"
# Test function to validate configuration
def test_configuration():
"""Test if all configuration is properly set up"""
try:
# Check environment variables
required_env_vars = ["API_KEY", "API_URL", "MODEL_NAME"]
missing_vars = [var for var in required_env_vars if not os.getenv(var)]
if missing_vars:
print(f"❌ Missing environment variables: {missing_vars}")
return False
# Test assistant initialization
assistant = EasyFarmsAssistant()
print("✅ Assistant initialized successfully")
# Test function schemas
print(f"✅ Loaded {len(assistant.tools)} function tools")
return True
except Exception as e:
print(f"❌ Configuration test failed: {e}")
return False
if __name__ == "__main__":
print("=== EasyFarms Assistant Configuration Test ===")
if test_configuration():
print("✅ All systems ready!")
else:
print("❌ Please fix configuration issues before running the assistant.") |