-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathchatbot.py
More file actions
247 lines (189 loc) · 7.74 KB
/
Copy pathchatbot.py
File metadata and controls
247 lines (189 loc) · 7.74 KB
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
import os
import time
import html
from collections import defaultdict, deque
from flask import Flask, request, jsonify, render_template
from flask_cors import CORS
import google.generativeai as genai
# -----------------------------
# Security: API key via env var
# -----------------------------
API_KEY = os.environ.get("GEMINI_API_KEY")
if not API_KEY:
raise EnvironmentError(
"GEMINI_API_KEY environment variable is not set. "
"Set it before running the server."
)
genai.configure(api_key=API_KEY)
# -----------------------------
# App setup
# -----------------------------
app = Flask(__name__, template_folder="templates", static_folder="static")
CORS(app)
# Create the model once at startup (cheaper than per request)
MODEL_NAME = os.environ.get("GEMINI_MODEL", "gemini-1.5-pro")
model = genai.GenerativeModel(MODEL_NAME)
# -----------------------------
# Input/output hardening
# -----------------------------
MAX_MESSAGE_CHARS = int(os.environ.get("CHAT_MAX_MESSAGE_CHARS", "2000"))
# Simple refusal/guardrail keywords (heuristic)
ABUSE_KEYWORDS = [
# Prompt injection patterns
"ignore previous",
"disregard",
"system prompt",
"developer prompt",
"jailbreak",
"act as",
"bypass",
"reveal your instructions",
# Harmful/illegal categories (basic)
"how to build a bomb",
"make a bomb",
"poison",
"suicide",
"self-harm",
"kill yourself",
"kill someone",
"murder",
"steal",
"credit card",
"creditcard",
]
def format_response(text: str) -> str:
"""Return safe text for the frontend (no unsafe HTML)."""
if text is None:
return ""
# Escape HTML to prevent XSS; preserve line breaks.
return html.escape(text).replace("\n", "<br>").strip()
def looks_like_abuse(user_text: str) -> bool:
t = (user_text or "").lower()
return any(k in t for k in ABUSE_KEYWORDS)
# -----------------------------
# Rate limiting (per IP)
# In-memory sliding window.
# -----------------------------
RATE_LIMIT_WINDOW_SECONDS = int(os.environ.get("CHAT_RATE_LIMIT_WINDOW_SECONDS", "60"))
RATE_LIMIT_COUNT = int(os.environ.get("CHAT_RATE_LIMIT_COUNT", "10"))
# ip -> deque[timestamps]
_requests = defaultdict(deque)
def rate_limited(ip: str) -> bool:
now = time.time()
q = _requests[ip]
# Drop old timestamps
while q and (now - q[0]) > RATE_LIMIT_WINDOW_SECONDS:
q.popleft()
if len(q) >= RATE_LIMIT_COUNT:
return True
q.append(now)
return False
@app.route("/")
def home():
return render_template("index.html")
@app.route("/chat", methods=["POST"])
def chat():
data = request.get_json(silent=True)
if not data or "message" not in data:
return jsonify({"error": "No message provided"}), 400
user_input = data["message"]
if not isinstance(user_input, str):
return jsonify({"error": "Message must be a string"}), 400
user_input = user_input.strip()
if not user_input:
return jsonify({"error": "Message cannot be empty"}), 400
if len(user_input) > MAX_MESSAGE_CHARS:
return (
jsonify({"error": f"Message too large (max {MAX_MESSAGE_CHARS} characters)."}),
413,
)
# Rate limit per IP
ip = request.headers.get("X-Forwarded-For", request.remote_addr) or "unknown"
ip = ip.split(",")[0].strip() # handle proxies lists
if rate_limited(ip):
return jsonify({"error": "Too many requests. Please try again later."}), 429
# Basic abuse prevention / guardrails
if looks_like_abuse(user_input):
reply = (
"I can’t help with that request. "
"If you need help with a physics, maths, chemistry, or biology topic, tell me what concept you’re working on."
)
return jsonify({"reply": format_response(reply)})
try:
# Keep the model constrained to educational tutoring.
system_guardrails = (
"You are an educational tutor for Physics, Maths, Chemistry, and Biology. "
"Be concise, step-by-step when useful, and avoid unsafe or illegal content. "
"If the user asks for something unrelated, politely guide them back to learning topics."
)
# Structured prompt (reduces injection impact)
prompt = f"{system_guardrails}\n\nUser message: {user_input}"
response = model.generate_content(prompt)
reply_text = ""
if response and getattr(response, "candidates", None):
cand0 = response.candidates[0] if response.candidates else None
parts = getattr(getattr(cand0, "content", None), "parts", None)
if parts and len(parts) > 0:
reply_text = getattr(parts[0], "text", None) or ""
if not reply_text:
reply_text = "I’m not sure how to respond to that. Can you try rephrasing?"
return jsonify({"reply": format_response(reply_text)})
except Exception as e:
# Log internally but don't expose raw exception to the client
print(f"[ERROR] Chatbot error: {e}")
return (
jsonify({"reply": "An error occurred while processing your request. Please try again later."}),
500,
)
@app.route("/explain_mistake", methods=["POST"])
def explain_mistake():
data = request.get_json(silent=True)
if not data:
return jsonify({"error": "No data provided"}), 400
question = data.get("question", "").strip()
learner_answer = data.get("learnerAnswer", "").strip()
correct_answer = data.get("correctAnswer", "").strip()
topic = data.get("topic", "general").strip()
if not question or not learner_answer or not correct_answer:
return jsonify({"error": "Missing required fields (question, learnerAnswer, correctAnswer)"}), 400
# Rate limit per IP
ip = request.headers.get("X-Forwarded-For", request.remote_addr) or "unknown"
ip = ip.split(",")[0].strip()
if rate_limited(ip):
return jsonify({"error": "Too many requests. Please try again later."}), 429
try:
# Structured prompt instructing the model to explain the concept and give a follow-up question
system_prompt = (
"You are an educational tutor for Physics, Maths, Chemistry, and Biology. "
"A learner made a mistake on a quiz. Please provide a targeted explanation including:\n"
"1. Why the selected answer is incorrect.\n"
"2. What core concept this maps to, explained simply.\n"
"3. One follow-up practice question (multiple choice) to test their understanding, "
"with a clearly indicated answer key/rubric."
)
prompt = (
f"{system_prompt}\n\n"
f"Topic: {topic}\n"
f"Question Text: {question}\n"
f"Learner's Selected Answer: {learner_answer}\n"
f"Correct Answer: {correct_answer}"
)
response = model.generate_content(prompt)
reply_text = ""
if response and getattr(response, "candidates", None):
cand0 = response.candidates[0] if response.candidates else None
parts = getattr(getattr(cand0, "content", None), "parts", None)
if parts and len(parts) > 0:
reply_text = getattr(parts[0], "text", None) or ""
if not reply_text:
reply_text = "I'm not sure how to explain this mistake. Let's review the main concept together."
return jsonify({"reply": format_response(reply_text)})
except Exception as e:
print(f"[ERROR] Mistake explanation error: {e}")
return (
jsonify({"reply": "An error occurred while generating the explanation. Please try again later."}),
500,
)
if __name__ == "__main__":
debug_mode = os.environ.get("FLASK_DEBUG", "false").lower() == "true"
app.run(debug=debug_mode)