cryogenic22 commited on
Commit
036a620
·
verified ·
1 Parent(s): 3310baa

Update utils/legal_notebook_interface.py

Browse files
Files changed (1) hide show
  1. utils/legal_notebook_interface.py +56 -46
utils/legal_notebook_interface.py CHANGED
@@ -116,63 +116,73 @@ class LegalNotebookInterface:
116
  self._handle_chat_input(prompt)
117
 
118
  def _handle_chat_input(self, prompt: str):
119
- """Process user input and generate response."""
120
- if not st.session_state.current_case:
121
- st.error("Please select a case first.")
122
- return
123
-
124
- # Add user message
125
- st.session_state.chat_history.append({
126
- "role": "user",
127
- "content": prompt,
128
- "timestamp": datetime.datetime.now().isoformat()
129
- })
130
-
 
131
  # Get documents from current case
132
  documents = self.case_manager.list_documents(st.session_state.current_case)
133
  if not documents:
134
  st.error("No documents available for analysis.")
135
  return
136
 
 
137
  try:
138
- # Get relevant chunks from documents
139
- chunks = []
140
- for doc in documents:
141
- doc_chunks = self.vector_store.similarity_search(
142
- prompt,
143
- filter_criteria={"doc_id": doc['id']},
144
- k=2
145
- )
146
- if doc_chunks:
147
- chunks.extend(doc_chunks)
148
-
149
- # Generate response
150
- response = {
151
  "role": "assistant",
152
- "content": self._generate_response(prompt, chunks),
153
  "timestamp": datetime.datetime.now().isoformat()
154
- }
155
-
156
- # Add response to chat history
157
- st.session_state.chat_history.append(response)
158
 
159
  except Exception as e:
160
- st.error(f"Error analyzing documents: {str(e)}")
161
-
162
- def _generate_response(self, prompt: str, chunks: List[Dict]) -> str:
163
- """Generate response based on document chunks."""
164
- if not chunks:
165
- return "I couldn't find any relevant information in the documents. Please try rephrasing your question."
166
-
167
- # Combine context from chunks
168
- context = "\n\n".join([chunk['text'] for chunk in chunks])
169
-
170
- # Generate a simple response (replace with your actual response generation)
171
- response = f"Based on the documents, I found {len(chunks)} relevant sections:\n\n"
172
- for i, chunk in enumerate(chunks, 1):
173
- response += f"{i}. {chunk['text'][:200]}...\n\n"
174
-
175
- return response
 
 
 
 
 
 
 
 
 
 
 
176
 
177
  def _show_document_details(self, doc: Dict):
178
  """Show detailed document information."""
 
116
  self._handle_chat_input(prompt)
117
 
118
  def _handle_chat_input(self, prompt: str):
119
+ """Process user input and generate response."""
120
+ if not st.session_state.current_case:
121
+ st.error("Please select a case first.")
122
+ return
123
+
124
+ # Add user message
125
+ st.session_state.chat_history.append({
126
+ "role": "user",
127
+ "content": prompt,
128
+ "timestamp": datetime.datetime.now().isoformat()
129
+ })
130
+
131
+ try:
132
  # Get documents from current case
133
  documents = self.case_manager.list_documents(st.session_state.current_case)
134
  if not documents:
135
  st.error("No documents available for analysis.")
136
  return
137
 
138
+ # Get relevant chunks
139
  try:
140
+ results = self.vector_store.similarity_search(
141
+ query=prompt,
142
+ k=3 # Number of chunks to retrieve
143
+ )
144
+
145
+ if not results:
146
+ response = "I couldn't find relevant information in the documents. Please try rephrasing your question."
147
+ else:
148
+ # Generate response from results
149
+ response = self._generate_response(prompt, results)
150
+
151
+ # Add response to chat history
152
+ st.session_state.chat_history.append({
153
  "role": "assistant",
154
+ "content": response,
155
  "timestamp": datetime.datetime.now().isoformat()
156
+ })
 
 
 
157
 
158
  except Exception as e:
159
+ st.error(f"Error searching documents: {str(e)}")
160
+ st.session_state.chat_history.append({
161
+ "role": "assistant",
162
+ "content": "I encountered an error while searching the documents. Please try again.",
163
+ "timestamp": datetime.datetime.now().isoformat()
164
+ })
165
+
166
+ except Exception as e:
167
+ st.error(f"Error analyzing documents: {str(e)}")
168
+
169
+ def _generate_response(self, prompt: str, results: List[Dict]) -> str:
170
+ """Generate response based on search results."""
171
+ if not results:
172
+ return "I couldn't find relevant information in the documents."
173
+
174
+ # Format the response with chunks
175
+ response = "Based on the documents, here's what I found:\n\n"
176
+
177
+ for i, result in enumerate(results, 1):
178
+ response += f"**Excerpt {i}**\n"
179
+ response += f"{result.get('text', '')[:200]}...\n\n"
180
+ if result.get('metadata', {}).get('title'):
181
+ response += f"*Source: {result['metadata']['title']}*\n\n"
182
+
183
+ response += "\nWould you like me to elaborate on any of these points?"
184
+
185
+ return response
186
 
187
  def _show_document_details(self, doc: Dict):
188
  """Show detailed document information."""