kaeizen commited on
Commit
345cfbe
·
1 Parent(s): 0b958ce

fix errors, use system prompt

Browse files
Files changed (2) hide show
  1. agent_manager/__init__.py +25 -17
  2. api/views.py +7 -2
agent_manager/__init__.py CHANGED
@@ -169,6 +169,7 @@ STRUCTURED_CHAT = StructuredChatWrapper(CHAT)
169
 
170
 
171
  SESSION_AGENTS = {}
 
172
 
173
  def set_session_agent(session_key):
174
  memory = InMemorySaver()
@@ -178,24 +179,28 @@ def set_session_agent(session_key):
178
  checkpointer=memory,
179
  )
180
  SESSION_AGENTS[session_key] = agent
 
 
 
 
 
 
 
181
 
182
  def get_or_create_agent(cookie_session, chat_session):
183
  """Get or create an agent keyed by the provided cookie_session token."""
184
  # Normalize to string to avoid type-mismatch keys
185
  session_key = str(cookie_session) if cookie_session else None
186
 
187
- print("start", session_key, chat_session)
188
  if not session_key or chat_session == 0:
189
  if session_key and session_key in SESSION_AGENTS:
190
- del SESSION_AGENTS[session_key]
191
- cache.delete(f"chat_session_{session_key}")
192
  session_key = str(uuid.uuid4())
193
 
194
  if session_key not in SESSION_AGENTS:
195
  set_session_agent(session_key)
196
  cache.set(f"chat_session_{session_key}", True)
197
 
198
- print("end", session_key, chat_session)
199
  return SESSION_AGENTS.get(session_key), session_key
200
 
201
 
@@ -207,14 +212,13 @@ def end_session(cookie_session):
207
  """Delete an agent session to free memory."""
208
  session_key = str(cookie_session) if cookie_session is not None else None
209
  if session_key and session_key in SESSION_AGENTS:
210
- del SESSION_AGENTS[session_key]
211
- cache.delete(f"chat_session_{session_key}")
212
  return True
213
  return False
214
 
215
  def get_message_list(mode, tone, message):
216
  messages = []
217
- content = ''
218
 
219
  if mode == 'default' and tone == 'default':
220
  messages = [{
@@ -223,21 +227,25 @@ def get_message_list(mode, tone, message):
223
  }]
224
  return messages
225
 
 
226
  if mode == 'grammar':
227
- content = f"""Carefully review the following text (inside triple backticks) for grammar, spelling, and punctuation mistakes. Correct any errors you find and provide suggestions for improvement if appropriate.
228
-
229
- ```{message}```
230
- """
231
- else:
232
- content = f"{message}\n"
233
 
234
  if tone != 'default':
235
- content += f"Please use a {tone} tone while preserving its original meaning and clarity."
 
 
 
236
 
 
 
237
 
238
- messages = [{
239
  "role": "user",
240
  "content": content
241
- }]
242
  return messages
243
-
 
169
 
170
 
171
  SESSION_AGENTS = {}
172
+ SESSION_MEMORY = {}
173
 
174
  def set_session_agent(session_key):
175
  memory = InMemorySaver()
 
179
  checkpointer=memory,
180
  )
181
  SESSION_AGENTS[session_key] = agent
182
+ SESSION_MEMORY[session_key] = memory
183
+
184
+ def maybe_delete_session_agent(session_key):
185
+ if session_key and session_key in SESSION_AGENTS:
186
+ del SESSION_AGENTS[session_key]
187
+ del SESSION_MEMORY[session_key]
188
+ cache.delete(f"chat_session_{session_key}")
189
 
190
  def get_or_create_agent(cookie_session, chat_session):
191
  """Get or create an agent keyed by the provided cookie_session token."""
192
  # Normalize to string to avoid type-mismatch keys
193
  session_key = str(cookie_session) if cookie_session else None
194
 
 
195
  if not session_key or chat_session == 0:
196
  if session_key and session_key in SESSION_AGENTS:
197
+ maybe_delete_session_agent(session_key)
 
198
  session_key = str(uuid.uuid4())
199
 
200
  if session_key not in SESSION_AGENTS:
201
  set_session_agent(session_key)
202
  cache.set(f"chat_session_{session_key}", True)
203
 
 
204
  return SESSION_AGENTS.get(session_key), session_key
205
 
206
 
 
212
  """Delete an agent session to free memory."""
213
  session_key = str(cookie_session) if cookie_session is not None else None
214
  if session_key and session_key in SESSION_AGENTS:
215
+ maybe_delete_session_agent(session_key)
 
216
  return True
217
  return False
218
 
219
  def get_message_list(mode, tone, message):
220
  messages = []
221
+ content = message
222
 
223
  if mode == 'default' and tone == 'default':
224
  messages = [{
 
227
  }]
228
  return messages
229
 
230
+ add_backticks = False
231
  if mode == 'grammar':
232
+ messages.append({
233
+ "role": "system",
234
+ "content": "Carefully review the following text (inside triple backticks) for grammar, spelling, and punctuation mistakes. Correct any errors you find and provide suggestions for improvement if appropriate."
235
+ })
236
+ add_backticks = True
 
237
 
238
  if tone != 'default':
239
+ messages.append({
240
+ "role": "system",
241
+ "content": f"Ensure that your translation or corrected grammar is expressed in a {tone} tone, while strictly preserving the sentence's original meaning and clarity.",
242
+ })
243
 
244
+ if add_backticks:
245
+ content = f"```{message}```"
246
 
247
+ messages.append({
248
  "role": "user",
249
  "content": content
250
+ })
251
  return messages
 
api/views.py CHANGED
@@ -3,7 +3,7 @@ from rest_framework.decorators import api_view, permission_classes
3
  from rest_framework.permissions import AllowAny
4
  from rest_framework.response import Response
5
  from rest_framework import status
6
- from agent_manager import get_or_create_agent, end_session, get_message_list
7
  from django.conf import settings
8
 
9
  @csrf_exempt
@@ -19,7 +19,12 @@ def chat(request):
19
  """Start or continue an existing chat session."""
20
  # Prefer secure HttpOnly cookie for session tracking
21
  cookie_session = request.COOKIES.get("gm_session")
22
- chat_session = request.data.get("chatSession", 0)
 
 
 
 
 
23
  message = request.data.get("message")
24
 
25
  if not message:
 
3
  from rest_framework.permissions import AllowAny
4
  from rest_framework.response import Response
5
  from rest_framework import status
6
+ from agent_manager import get_or_create_agent, end_session, get_message_list, maybe_delete_session_agent
7
  from django.conf import settings
8
 
9
  @csrf_exempt
 
19
  """Start or continue an existing chat session."""
20
  # Prefer secure HttpOnly cookie for session tracking
21
  cookie_session = request.COOKIES.get("gm_session")
22
+
23
+ chat_session = int(request.data.get("chat_session", 0))
24
+ if chat_session == 0:
25
+ maybe_delete_session_agent(cookie_session)
26
+ cookie_session = None
27
+
28
  message = request.data.get("message")
29
 
30
  if not message: