Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1808,7 +1808,7 @@ def chatbot_response(message, history):
|
|
| 1808 |
else:
|
| 1809 |
return f"Error: Invalid dataset selection '{selected_dataset_ai}'."
|
| 1810 |
|
| 1811 |
-
# Prepare
|
| 1812 |
top_scoring_statements = "### Top Scoring Statements ###\n\n"
|
| 1813 |
for bucket, statements in trust_data.items():
|
| 1814 |
top_scoring_statements += f"**{bucket}**:\n"
|
|
@@ -1819,17 +1819,20 @@ def chatbot_response(message, history):
|
|
| 1819 |
|
| 1820 |
# Combine predefined prompt, top-scoring statements, and user input
|
| 1821 |
combined_prompt = predefined_prompt
|
| 1822 |
-
|
| 1823 |
-
combined_prompt += "\n\n### Top-Scoring Statements for Integration ###\n" + top_scoring_statements
|
| 1824 |
combined_prompt += "\n\nUser Input:\n" + message
|
| 1825 |
|
| 1826 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1827 |
structured_input = {
|
| 1828 |
"input": combined_prompt,
|
| 1829 |
-
"chat_history":
|
| 1830 |
-
{"role": "user", "content": entry["content"]} if entry["role"] == "user" else
|
| 1831 |
-
{"role": "assistant", "content": entry["content"]} for entry in history
|
| 1832 |
-
]
|
| 1833 |
}
|
| 1834 |
|
| 1835 |
# Generate AI output using the agent pipeline
|
|
@@ -1838,19 +1841,24 @@ def chatbot_response(message, history):
|
|
| 1838 |
# Prepare the final response
|
| 1839 |
response = f"**Selected Dataset: {selected_dataset_ai}**\n\n"
|
| 1840 |
response += f"{top_scoring_statements}\n"
|
| 1841 |
-
response += f"
|
| 1842 |
|
| 1843 |
# Append interaction to history
|
| 1844 |
-
|
| 1845 |
-
|
| 1846 |
|
| 1847 |
return response
|
| 1848 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1849 |
except Exception as e:
|
| 1850 |
-
logger.error(f"
|
| 1851 |
return "Error occurred during response generation."
|
| 1852 |
|
| 1853 |
-
|
| 1854 |
def read_ai_dataset_selection():
|
| 1855 |
global selected_dataset_ai
|
| 1856 |
return selected_dataset_ai
|
|
|
|
| 1808 |
else:
|
| 1809 |
return f"Error: Invalid dataset selection '{selected_dataset_ai}'."
|
| 1810 |
|
| 1811 |
+
# Prepare top-scoring statements
|
| 1812 |
top_scoring_statements = "### Top Scoring Statements ###\n\n"
|
| 1813 |
for bucket, statements in trust_data.items():
|
| 1814 |
top_scoring_statements += f"**{bucket}**:\n"
|
|
|
|
| 1819 |
|
| 1820 |
# Combine predefined prompt, top-scoring statements, and user input
|
| 1821 |
combined_prompt = predefined_prompt
|
| 1822 |
+
combined_prompt += "\n\n### Top-Scoring Statements for Integration ###\n" + top_scoring_statements
|
|
|
|
| 1823 |
combined_prompt += "\n\nUser Input:\n" + message
|
| 1824 |
|
| 1825 |
+
# Validate chat history
|
| 1826 |
+
validated_chat_history = []
|
| 1827 |
+
for entry in history:
|
| 1828 |
+
if isinstance(entry, dict) and "role" in entry and "content" in entry:
|
| 1829 |
+
validated_chat_history.append(entry)
|
| 1830 |
+
else:
|
| 1831 |
+
logger.warning(f"Invalid chat history entry skipped: {entry}")
|
| 1832 |
+
|
| 1833 |
structured_input = {
|
| 1834 |
"input": combined_prompt,
|
| 1835 |
+
"chat_history": validated_chat_history,
|
|
|
|
|
|
|
|
|
|
| 1836 |
}
|
| 1837 |
|
| 1838 |
# Generate AI output using the agent pipeline
|
|
|
|
| 1841 |
# Prepare the final response
|
| 1842 |
response = f"**Selected Dataset: {selected_dataset_ai}**\n\n"
|
| 1843 |
response += f"{top_scoring_statements}\n"
|
| 1844 |
+
response += f"\n{agent_output['output']}"
|
| 1845 |
|
| 1846 |
# Append interaction to history
|
| 1847 |
+
validated_chat_history.append({"role": "user", "content": message})
|
| 1848 |
+
validated_chat_history.append({"role": "assistant", "content": agent_output["output"]})
|
| 1849 |
|
| 1850 |
return response
|
| 1851 |
|
| 1852 |
+
except KeyError as ke:
|
| 1853 |
+
logger.error(f"KeyError encountered: {ke}")
|
| 1854 |
+
return "An unexpected error occurred. Please try again."
|
| 1855 |
+
except ValueError as ve:
|
| 1856 |
+
logger.error(f"ValueError encountered: {ve}")
|
| 1857 |
+
return "An unexpected value was encountered. Please refine your input."
|
| 1858 |
except Exception as e:
|
| 1859 |
+
logger.error(f"Unexpected error: {e}")
|
| 1860 |
return "Error occurred during response generation."
|
| 1861 |
|
|
|
|
| 1862 |
def read_ai_dataset_selection():
|
| 1863 |
global selected_dataset_ai
|
| 1864 |
return selected_dataset_ai
|