Eng-Musa commited on
Commit
73dad05
·
1 Parent(s): f1fe51a

use ip for user sessison tracnking for suggestions

Browse files
__pycache__/main.cpython-312.pyc CHANGED
Binary files a/__pycache__/main.cpython-312.pyc and b/__pycache__/main.cpython-312.pyc differ
 
clients/__pycache__/groq_client.cpython-312.pyc CHANGED
Binary files a/clients/__pycache__/groq_client.cpython-312.pyc and b/clients/__pycache__/groq_client.cpython-312.pyc differ
 
clients/groq_client.py CHANGED
@@ -12,6 +12,7 @@ import random
12
  import shutil
13
  from optimized_quiz import OPTIMIZED_QUESTIONS
14
  from chat_resources import *
 
15
 
16
  # Load main env
17
  load_dotenv(dotenv_path=".env")
@@ -36,6 +37,8 @@ LAMBDA_MULT = float(os.getenv("LAMBDA_MULT", 0.5))
36
 
37
  class GroqClient:
38
  def __init__(self):
 
 
39
 
40
  self.documents = self.load_json_data(DATA_PATH)
41
  if not self.documents:
@@ -133,19 +136,41 @@ class GroqClient:
133
 
134
  # def get_next_questions(self):
135
  # return random.sample(OPTIMIZED_QUESTIONS, 3)
 
 
 
 
 
 
 
 
 
 
136
 
137
- _shown = set()
138
-
139
- def get_next_questions(self):
140
  """Return 3 non-repeated random questions within the session."""
141
- remaining = [q for q in OPTIMIZED_QUESTIONS if q not in self._shown]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
 
143
  if len(remaining) < 3:
144
- self._shown.clear()
145
  remaining = OPTIMIZED_QUESTIONS[:]
146
 
147
  selected = random.sample(remaining, 3)
148
- self._shown.update(selected)
149
  return selected
150
 
151
  # ---------------MAIN-----------------
@@ -186,7 +211,6 @@ class GroqClient:
186
  ]
187
 
188
  # Try multiple models with fallback
189
-
190
  models_to_try = [
191
  "openai/gpt-oss-120b",
192
  "openai/gpt-oss-20b",
 
12
  import shutil
13
  from optimized_quiz import OPTIMIZED_QUESTIONS
14
  from chat_resources import *
15
+ from datetime import datetime, timedelta
16
 
17
  # Load main env
18
  load_dotenv(dotenv_path=".env")
 
37
 
38
  class GroqClient:
39
  def __init__(self):
40
+ self._sessions = {} # {ip: {'shown': set(), 'last_activity': datetime}}
41
+ self.SESSION_TIMEOUT = timedelta(minutes=30)
42
 
43
  self.documents = self.load_json_data(DATA_PATH)
44
  if not self.documents:
 
136
 
137
  # def get_next_questions(self):
138
  # return random.sample(OPTIMIZED_QUESTIONS, 3)
139
+ def cleanup_expired_sessions(self):
140
+ """Remove expired sessions to avoid memory overload."""
141
+ current_time = datetime.now()
142
+ expired_ips = [
143
+ ip
144
+ for ip, data in self._sessions.items()
145
+ if current_time - data["last_activity"] > self.SESSION_TIMEOUT
146
+ ]
147
+ for ip in expired_ips:
148
+ del self._sessions[ip]
149
 
150
+ def get_next_questions(self, ip: str):
 
 
151
  """Return 3 non-repeated random questions within the session."""
152
+ self.cleanup_expired_sessions()
153
+
154
+ current_time = datetime.now()
155
+ if ip not in self._sessions:
156
+ self._sessions[ip] = {"shown": set(), "last_activity": current_time}
157
+ else:
158
+ if (
159
+ current_time - self._sessions[ip]["last_activity"]
160
+ > self.SESSION_TIMEOUT
161
+ ):
162
+ self._sessions[ip]["shown"].clear()
163
+ self._sessions[ip]["last_activity"] = current_time
164
+
165
+ shown = self._sessions[ip]["shown"]
166
+ remaining = [q for q in OPTIMIZED_QUESTIONS if q not in shown]
167
 
168
  if len(remaining) < 3:
169
+ shown.clear()
170
  remaining = OPTIMIZED_QUESTIONS[:]
171
 
172
  selected = random.sample(remaining, 3)
173
+ shown.update(selected)
174
  return selected
175
 
176
  # ---------------MAIN-----------------
 
211
  ]
212
 
213
  # Try multiple models with fallback
 
214
  models_to_try = [
215
  "openai/gpt-oss-120b",
216
  "openai/gpt-oss-20b",
main.py CHANGED
@@ -26,6 +26,7 @@ app.add_middleware(
26
 
27
  class QueryRequest(BaseModel):
28
  query: str
 
29
 
30
  # Lazy singleton for GroqClient
31
  @lru_cache(maxsize=1)
@@ -42,7 +43,7 @@ async def chat_endpoint(request: QueryRequest):
42
  return {
43
  "status_code": 200,
44
  "response": response,
45
- "next_questions": groq_client.get_next_questions(),
46
  }
47
  except Exception as e:
48
  return {"status_code": 500, "response": f"Internal error: {e}", "next_questions": []}
 
26
 
27
  class QueryRequest(BaseModel):
28
  query: str
29
+ ip: str
30
 
31
  # Lazy singleton for GroqClient
32
  @lru_cache(maxsize=1)
 
43
  return {
44
  "status_code": 200,
45
  "response": response,
46
+ "next_questions": groq_client.get_next_questions(request.ip),
47
  }
48
  except Exception as e:
49
  return {"status_code": 500, "response": f"Internal error: {e}", "next_questions": []}