Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance error handling and logging for API key loading and request pr… #735

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions agrotech-ai-chatbot/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,41 @@
from flask_cors import CORS
import os
import json
import logging
from groq import Groq
import time

app = Flask(__name__)
CORS(app) # Enable CORS for all routes

# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Load API key
working_dir = os.path.dirname(os.path.abspath(__file__))
config_data = json.load(open(f"{working_dir}/config.json"))
GROQ_API_KEY = config_data["GROQ_API_KEY"]
os.environ["GROQ_API_KEY"] = GROQ_API_KEY
try:
config_data = json.load(open(f"{working_dir}/config.json"))
GROQ_API_KEY = config_data["GROQ_API_KEY"]
os.environ["GROQ_API_KEY"] = GROQ_API_KEY
logger.info("API key loaded successfully.")
except FileNotFoundError:
logger.error("config.json file not found. Please add your API key in config.json.")
raise
except KeyError:
logger.error("GROQ_API_KEY is missing in config.json.")
raise
except Exception as e:
logger.error(f"Failed to load API key: {e}")
raise

client = Groq()

# Rate limiting variables
RATE_LIMIT = 20 # Maximum 20 requests per minute
rate_limit_store = {} # Store to track user requests by IP


# Premade requests
# Predefined responses for common questions
premade_requests = {
"What is AgroTech AI?": (
"AgroTech AI is a cutting-edge platform that uses artificial intelligence to improve farming practices. "
Expand Down Expand Up @@ -66,7 +81,6 @@
)
}


def is_rate_limited(ip):
current_time = time.time()
if ip not in rate_limit_store:
Expand All @@ -77,6 +91,7 @@ def is_rate_limited(ip):

# Check if the number of requests exceeds the rate limit
if len(rate_limit_store[ip]) >= RATE_LIMIT:
logger.warning(f"Rate limit exceeded for IP: {ip}")
return True
rate_limit_store[ip].append(current_time)
return False
Expand All @@ -91,8 +106,8 @@ def chat():
user_prompt = data.get('prompt')

# Check if the prompt matches a pre-made prompt

if premade_requests.get(user_prompt):
logger.info(f"Premade response provided for IP {ip}: {user_prompt}")
return jsonify({"response": premade_requests[user_prompt]})

# If not a pre-made prompt, use the LLM
Expand All @@ -107,9 +122,11 @@ def chat():
messages=messages
)
assistant_response = response.choices[0].message.content
logger.info(f"Response generated for IP {ip}")
return jsonify({"response": assistant_response})
except Exception as e:
return jsonify({"error": str(e)}), 500
logger.error(f"Error generating response for IP {ip}: {e}")
return jsonify({"error": "There was an error processing your request. Please try again later."}), 500

if __name__ == '__main__':
app.run()
app.run()
Loading