adding formatting response text function
Browse files
app.py
CHANGED
@@ -241,6 +241,26 @@ class RAGQuestionAnswering:
|
|
241 |
|
242 |
return "\n\n".join(doc.page_content for doc in docs)
|
243 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
244 |
def generate_response(self, input_dict: Dict[str, Any]) -> str:
|
245 |
"""
|
246 |
Parameters
|
@@ -273,10 +293,11 @@ class RAGQuestionAnswering:
|
|
273 |
]
|
274 |
|
275 |
response = self.pipe(messages)
|
276 |
-
|
277 |
-
print(response, type(response))
|
278 |
|
279 |
-
|
|
|
|
|
280 |
|
281 |
def setup_rag_chain(self) -> None:
|
282 |
"""
|
|
|
241 |
|
242 |
return "\n\n".join(doc.page_content for doc in docs)
|
243 |
|
244 |
+
def extract_assistant_content(self, data: Dict[str, Any]) ->:
|
245 |
+
"""
|
246 |
+
Extracts and returns the assistant content text from the provided data structure.
|
247 |
+
|
248 |
+
:param data: A list containing a dictionary with 'generated_text'.
|
249 |
+
:return: The 'content' text of the 'assistant' role, or None if not found.
|
250 |
+
"""
|
251 |
+
try:
|
252 |
+
# Navigate to the assistant content text
|
253 |
+
generated_text = data[0].get('generated_text', [])
|
254 |
+
for item in generated_text:
|
255 |
+
if item.get('role') == 'assistant':
|
256 |
+
return item.get('content')
|
257 |
+
except (IndexError, AttributeError):
|
258 |
+
return None # Return None if the structure is not as expected
|
259 |
+
|
260 |
+
return None # Return None if no assistant content is found
|
261 |
+
|
262 |
+
|
263 |
+
|
264 |
def generate_response(self, input_dict: Dict[str, Any]) -> str:
|
265 |
"""
|
266 |
Parameters
|
|
|
293 |
]
|
294 |
|
295 |
response = self.pipe(messages)
|
296 |
+
output_text = self.extract_assistant_content(response)
|
|
|
297 |
|
298 |
+
if not output_text:
|
299 |
+
return 'Unable to generate output'
|
300 |
+
return output_text
|
301 |
|
302 |
def setup_rag_chain(self) -> None:
|
303 |
"""
|