Spaces:
Build error
Build error
Update utils/legal_notebook_interface.py
Browse files
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 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 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 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
#
|
| 150 |
-
|
| 151 |
"role": "assistant",
|
| 152 |
-
"content":
|
| 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
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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."""
|