Lasdw commited on
Commit
fac15c0
·
1 Parent(s): 8f679bf

updated session memory

Browse files
Files changed (2) hide show
  1. app.py +16 -12
  2. static/favicon.ico +0 -0
app.py CHANGED
@@ -58,6 +58,10 @@ def chat_with_agent(question: str, file_uploads, history: list) -> tuple:
58
  if session_id not in session_histories:
59
  session_histories[session_id] = []
60
 
 
 
 
 
61
  # Check rate limit
62
  if not query_limiter.is_allowed(session_id):
63
  remaining_time = query_limiter.get_time_until_reset(session_id)
@@ -65,9 +69,8 @@ def chat_with_agent(question: str, file_uploads, history: list) -> tuple:
65
  f"Rate limit exceeded. You can make {query_limiter.max_queries} queries per hour. Think of my bank account🙏. "
66
  f"Please wait {int(remaining_time)} seconds before trying again."
67
  )
68
- history.append({"role": "user", "content": question})
69
  history.append({"role": "assistant", "content": error_message})
70
- session_histories[session_id].extend([{"role": "user", "content": question}, {"role": "assistant", "content": error_message}])
71
  return history, "", f"Remaining queries this hour: 0/{query_limiter.max_queries}"
72
 
73
  # Initialize agent
@@ -102,15 +105,18 @@ def chat_with_agent(question: str, file_uploads, history: list) -> tuple:
102
  # Format the session-specific conversation history
103
  conversation_history = format_history_for_agent(session_histories[session_id])
104
 
105
- # Add context about the conversation history
106
- if conversation_history:
107
- question = f"Previous conversation in this session:\n{conversation_history}\n\nCurrent question: {question}"
 
 
 
108
 
109
  # Get response from agent with attachments if available
110
  if attachments:
111
- response = agent(question, attachments)
112
  else:
113
- response = agent(question)
114
 
115
  # Format the response to show thought process
116
  formatted_response = ""
@@ -142,18 +148,16 @@ def chat_with_agent(question: str, file_uploads, history: list) -> tuple:
142
  # Add remaining queries info
143
  remaining_queries = query_limiter.get_remaining_queries(session_id)
144
 
145
- # Add question and response to both the current history and session history
146
- history.append({"role": "user", "content": question})
147
  history.append({"role": "assistant", "content": formatted_response})
148
- session_histories[session_id].extend([{"role": "user", "content": question}, {"role": "assistant", "content": formatted_response}])
149
 
150
  return history, "", f"Remaining queries this hour: {remaining_queries}/{query_limiter.max_queries}"
151
  except Exception as e:
152
  error_message = f"Error: {str(e)}"
153
- history.append({"role": "user", "content": question})
154
  history.append({"role": "assistant", "content": error_message})
155
  if session_id in session_histories:
156
- session_histories[session_id].extend([{"role": "user", "content": question}, {"role": "assistant", "content": error_message}])
157
  return history, "", "Remaining queries this hour: 5/5"
158
 
159
  def clear_chat():
 
58
  if session_id not in session_histories:
59
  session_histories[session_id] = []
60
 
61
+ # Add the question to both histories immediately
62
+ history.append({"role": "user", "content": question})
63
+ session_histories[session_id].append({"role": "user", "content": question})
64
+
65
  # Check rate limit
66
  if not query_limiter.is_allowed(session_id):
67
  remaining_time = query_limiter.get_time_until_reset(session_id)
 
69
  f"Rate limit exceeded. You can make {query_limiter.max_queries} queries per hour. Think of my bank account🙏. "
70
  f"Please wait {int(remaining_time)} seconds before trying again."
71
  )
 
72
  history.append({"role": "assistant", "content": error_message})
73
+ session_histories[session_id].append({"role": "assistant", "content": error_message})
74
  return history, "", f"Remaining queries this hour: 0/{query_limiter.max_queries}"
75
 
76
  # Initialize agent
 
105
  # Format the session-specific conversation history
106
  conversation_history = format_history_for_agent(session_histories[session_id])
107
 
108
+ # Prepare the full context for the agent
109
+ full_context = {
110
+ "question": question,
111
+ "conversation_history": conversation_history,
112
+ "session_id": session_id
113
+ }
114
 
115
  # Get response from agent with attachments if available
116
  if attachments:
117
+ response = agent(full_context, attachments)
118
  else:
119
+ response = agent(full_context)
120
 
121
  # Format the response to show thought process
122
  formatted_response = ""
 
148
  # Add remaining queries info
149
  remaining_queries = query_limiter.get_remaining_queries(session_id)
150
 
151
+ # Add response to both histories
 
152
  history.append({"role": "assistant", "content": formatted_response})
153
+ session_histories[session_id].append({"role": "assistant", "content": formatted_response})
154
 
155
  return history, "", f"Remaining queries this hour: {remaining_queries}/{query_limiter.max_queries}"
156
  except Exception as e:
157
  error_message = f"Error: {str(e)}"
 
158
  history.append({"role": "assistant", "content": error_message})
159
  if session_id in session_histories:
160
+ session_histories[session_id].append({"role": "assistant", "content": error_message})
161
  return history, "", "Remaining queries this hour: 5/5"
162
 
163
  def clear_chat():
static/favicon.ico ADDED