Update mybot.py
Browse files
mybot.py
CHANGED
|
@@ -1,360 +1,348 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import logging
|
| 3 |
-
import random
|
| 4 |
-
from botbuilder.core import TurnContext, MessageFactory
|
| 5 |
-
from botbuilder.schema import Activity, ActivityTypes, EndOfConversationCodes
|
| 6 |
-
from tenacity import retry, wait_random_exponential, stop_after_attempt
|
| 7 |
-
import importlib
|
| 8 |
-
from sentiment_analysis import analyze_sentiment_vader
|
| 9 |
-
from config import load_and_validate_config, setup_logging
|
| 10 |
-
from universal_reasoning import UniversalReasoning
|
| 11 |
-
|
| 12 |
-
import
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
self.
|
| 22 |
-
self.
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
self.context
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
self.
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
""
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
"
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
"
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
root.
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
def
|
| 192 |
-
"""
|
| 193 |
-
return
|
| 194 |
-
|
| 195 |
-
def
|
| 196 |
-
"""
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
return
|
| 202 |
-
|
| 203 |
-
def
|
| 204 |
-
"""
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
"
|
| 255 |
-
"
|
| 256 |
-
"
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
deployment_id = "your-deployment-name" # Replace with your Azure deployment name
|
| 350 |
-
print("Newton's Thoughts:", newton_thoughts(question))
|
| 351 |
-
print("Da Vinci's Insights:", davinci_insights(question))
|
| 352 |
-
print("Human Intuition:", human_intuition(question))
|
| 353 |
-
print("Neural Network Thinking:", neural_network_thinking(question))
|
| 354 |
-
print("Quantum Computing Thinking:", quantum_computing_thinking(question))
|
| 355 |
-
print("Resilient Kindness:", resilient_kindness(question))
|
| 356 |
-
print("Universal Reasoning:", universal_reasoning(question))
|
| 357 |
-
print("Internet Answer:", get_internet_answer(question, deployment_id))
|
| 358 |
-
else:
|
| 359 |
-
print("User did not consent to data collection. Exiting application.")
|
| 360 |
-
print(reflect_on_decisions())
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import logging
|
| 3 |
+
import random
|
| 4 |
+
from botbuilder.core import TurnContext, MessageFactory
|
| 5 |
+
from botbuilder.schema import Activity, ActivityTypes, EndOfConversationCodes
|
| 6 |
+
from tenacity import retry, wait_random_exponential, stop_after_attempt
|
| 7 |
+
import importlib
|
| 8 |
+
from sentiment_analysis import analyze_sentiment_vader
|
| 9 |
+
from config import load_and_validate_config, setup_logging
|
| 10 |
+
from universal_reasoning import UniversalReasoning
|
| 11 |
+
import json
|
| 12 |
+
from database import DatabaseConnection # Import the database connection
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
class MyBot:
|
| 16 |
+
def __init__(self, conversation_state, user_state, dialog, universal_reasoning):
|
| 17 |
+
self.conversation_state = conversation_state
|
| 18 |
+
self.user_state = user_state
|
| 19 |
+
self.dialog = dialog
|
| 20 |
+
self.universal_reasoning = universal_reasoning
|
| 21 |
+
self.context = {}
|
| 22 |
+
self.feedback = []
|
| 23 |
+
|
| 24 |
+
async def enhance_context_awareness(self, user_id: str, text: str) -> None:
|
| 25 |
+
"""Enhance context awareness by analyzing the user's environment, activities, and emotional state."""
|
| 26 |
+
sentiment = analyze_sentiment_vader(text)
|
| 27 |
+
if user_id not in self.context:
|
| 28 |
+
self.context[user_id] = []
|
| 29 |
+
self.context[user_id].append({"text": text, "sentiment": sentiment})
|
| 30 |
+
|
| 31 |
+
async def proactive_learning(self, user_id: str, feedback: str) -> None:
|
| 32 |
+
"""Encourage proactive learning by seeking feedback and exploring new topics."""
|
| 33 |
+
if user_id not in self.context:
|
| 34 |
+
self.context[user_id] = []
|
| 35 |
+
self.context[user_id].append({"feedback": feedback})
|
| 36 |
+
self.feedback.append({"user_id": user_id, "feedback": feedback})
|
| 37 |
+
|
| 38 |
+
async def ethical_decision_making(self, user_id: str, decision: str) -> None:
|
| 39 |
+
"""Integrate ethical principles into decision-making processes."""
|
| 40 |
+
ethical_decision = f"Considering ethical principles, the decision is: {decision}"
|
| 41 |
+
if user_id not in self.context:
|
| 42 |
+
self.context[user_id] = []
|
| 43 |
+
self.context[user_id].append({"ethical_decision": ethical_decision})
|
| 44 |
+
|
| 45 |
+
async def emotional_intelligence(self, user_id: str, text: str) -> str:
|
| 46 |
+
"""Develop emotional intelligence by recognizing and responding to user emotions."""
|
| 47 |
+
sentiment = analyze_sentiment_vader(text)
|
| 48 |
+
response = self.generate_emotional_response(sentiment, text)
|
| 49 |
+
if user_id not in self.context:
|
| 50 |
+
self.context[user_id] = []
|
| 51 |
+
self.context[user_id].append({"emotional_response": response})
|
| 52 |
+
return response
|
| 53 |
+
|
| 54 |
+
def generate_emotional_response(self, sentiment: dict, text: str) -> str:
|
| 55 |
+
"""Generate an empathetic response based on the sentiment analysis."""
|
| 56 |
+
if sentiment['compound'] >= 0.05:
|
| 57 |
+
return "I'm glad to hear that! 😊 How can I assist you further?"
|
| 58 |
+
elif sentiment['compound'] <= -0.05:
|
| 59 |
+
return "I'm sorry to hear that. 😢 Is there anything I can do to help?"
|
| 60 |
+
else:
|
| 61 |
+
return "I understand. How can I assist you further?"
|
| 62 |
+
|
| 63 |
+
async def transparency_and_explainability(self, user_id: str, decision: str) -> str:
|
| 64 |
+
"""Enable transparency by explaining the reasoning behind decisions."""
|
| 65 |
+
explanation = f"The decision was made based on the following context: {self.context[user_id]}"
|
| 66 |
+
if user_id not in self.context:
|
| 67 |
+
self.context[user_id] = []
|
| 68 |
+
self.context[user_id].append({"explanation": explanation})
|
| 69 |
+
return explanation
|
| 70 |
+
|
| 71 |
+
async def on_message_activity(self, turn_context: TurnContext) -> None:
|
| 72 |
+
"""Handles incoming messages and generates responses."""
|
| 73 |
+
user_id = turn_context.activity.from_property.id
|
| 74 |
+
if user_id not in self.context:
|
| 75 |
+
self.context[user_id] = []
|
| 76 |
+
try:
|
| 77 |
+
if "end" in turn_context.activity.text.lower() or "stop" in turn_context.activity.text.lower():
|
| 78 |
+
await end_conversation(turn_context)
|
| 79 |
+
self.context.pop(user_id, None)
|
| 80 |
+
else:
|
| 81 |
+
self.context[user_id].append(turn_context.activity.text)
|
| 82 |
+
response = await self.generate_response(turn_context.activity.text, user_id)
|
| 83 |
+
await turn_context.send_activity(MessageFactory.text(response))
|
| 84 |
+
await self.request_feedback(turn_context, user_id)
|
| 85 |
+
|
| 86 |
+
# Example database operation
|
| 87 |
+
with DatabaseConnection() as conn:
|
| 88 |
+
if conn:
|
| 89 |
+
cursor = conn.cursor()
|
| 90 |
+
cursor.execute("INSERT INTO UserMessages (UserId, Message) VALUES (?, ?)", user_id, turn_context.activity.text)
|
| 91 |
+
conn.commit()
|
| 92 |
+
|
| 93 |
+
except Exception as e:
|
| 94 |
+
await handle_error(turn_context, e)
|
| 95 |
+
|
| 96 |
+
async def generate_response(self, text: str, user_id: str) -> str:
|
| 97 |
+
"""Generates a response using Azure OpenAI's API, Universal Reasoning, and various perspectives."""
|
| 98 |
+
try:
|
| 99 |
+
logging.info(f"Generating response for user_id: {user_id} with text: {text}")
|
| 100 |
+
# Generate responses from different perspectives
|
| 101 |
+
responses = []
|
| 102 |
+
for perspective in self.perspectives.values():
|
| 103 |
+
try:
|
| 104 |
+
response = await perspective.generate_response(text)
|
| 105 |
+
responses.append(response)
|
| 106 |
+
except Exception as e:
|
| 107 |
+
logging.error(f"Error generating response from {perspective.__class__.__name__}: {e}")
|
| 108 |
+
# Combine responses
|
| 109 |
+
combined_response = "\n".join(responses)
|
| 110 |
+
logging.info(f"Combined response: {combined_response}")
|
| 111 |
+
return combined_response
|
| 112 |
+
except Exception as e:
|
| 113 |
+
logging.error(f"Error generating response: {e}")
|
| 114 |
+
return "Sorry, I couldn't generate a response at this time."
|
| 115 |
+
|
| 116 |
+
async def request_feedback(self, turn_context: TurnContext, user_id: str) -> None:
|
| 117 |
+
"""Request feedback from the user about the bot's response."""
|
| 118 |
+
feedback_prompt = "How would you rate my response? (good/neutral/bad)"
|
| 119 |
+
await turn_context.send_activity(MessageFactory.text(feedback_prompt))
|
| 120 |
+
|
| 121 |
+
async def handle_feedback(self, turn_context: TurnContext) -> None:
|
| 122 |
+
"""Handle user feedback and store it for future analysis."""
|
| 123 |
+
user_id = turn_context.activity.from_property.id
|
| 124 |
+
feedback = turn_context.activity.text.lower()
|
| 125 |
+
if feedback in ["good", "neutral", "bad"]:
|
| 126 |
+
self.feedback.append({"user_id": user_id, "feedback": feedback})
|
| 127 |
+
await turn_context.send_activity(MessageFactory.text("Thank you for your feedback!"))
|
| 128 |
+
else:
|
| 129 |
+
await turn_context.send_activity(MessageFactory.text("Please provide feedback as 'good', 'neutral', or 'bad'."))
|
| 130 |
+
|
| 131 |
+
async def end_conversation(turn_context: TurnContext) -> None:
|
| 132 |
+
"""Ends the conversation with the user."""
|
| 133 |
+
await turn_context.send_activity(
|
| 134 |
+
MessageFactory.text("Ending conversation from the skill...")
|
| 135 |
+
)
|
| 136 |
+
end_of_conversation = Activity(type=ActivityTypes.end_of_conversation)
|
| 137 |
+
end_of_conversation.code = EndOfConversationCodes.completed_successfully
|
| 138 |
+
await turn_context.send_activity(end_of_conversation)
|
| 139 |
+
|
| 140 |
+
async def handle_error(turn_context: TurnContext, error: Exception) -> None:
|
| 141 |
+
"""Handles errors by logging them and notifying the user."""
|
| 142 |
+
logging.error(f"An error occurred: {error}")
|
| 143 |
+
await turn_context.send_activity(
|
| 144 |
+
MessageFactory.text("An error occurred. Please try again later.")
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
def show_privacy_consent() -> bool:
|
| 148 |
+
"""Display a pop-up window to obtain user consent for data collection and privacy."""
|
| 149 |
+
import tkinter as tk
|
| 150 |
+
|
| 151 |
+
def on_accept():
|
| 152 |
+
user_consent.set(True)
|
| 153 |
+
root.destroy()
|
| 154 |
+
|
| 155 |
+
def on_decline():
|
| 156 |
+
user_consent.set(False)
|
| 157 |
+
root.destroy()
|
| 158 |
+
|
| 159 |
+
root = tk.Tk()
|
| 160 |
+
root.title("Data Permission and Privacy")
|
| 161 |
+
message = ("We value your privacy. By using this application, you consent to the collection and use of your data "
|
| 162 |
+
"as described in our privacy policy. Do you agree to proceed?")
|
| 163 |
+
label = tk.Label(root, text=message, wraplength=400, justify="left")
|
| 164 |
+
label.pack(padx=20, pady=20)
|
| 165 |
+
button_frame = tk.Frame(root)
|
| 166 |
+
button_frame.pack(pady=10)
|
| 167 |
+
accept_button = tk.Button(button_frame, text="Accept", command=on_accept)
|
| 168 |
+
accept_button.pack(side="left", padx=10)
|
| 169 |
+
decline_button = tk.Button(button_frame, text="Decline", command=on_decline)
|
| 170 |
+
decline_button.pack(side="right", padx=10)
|
| 171 |
+
user_consent = tk.BooleanVar()
|
| 172 |
+
root.mainloop()
|
| 173 |
+
return user_consent.get()
|
| 174 |
+
|
| 175 |
+
# Example usage of MyBot class
|
| 176 |
+
bot = MyBot()
|
| 177 |
+
|
| 178 |
+
# Functions based on JSON configuration
|
| 179 |
+
def newton_thoughts(question: str) -> str:
|
| 180 |
+
"""Apply Newton's laws to the given question."""
|
| 181 |
+
return apply_newtons_laws(question)
|
| 182 |
+
|
| 183 |
+
def apply_newtons_laws(question: str) -> str:
|
| 184 |
+
"""Apply Newton's laws to the given question."""
|
| 185 |
+
if not question:
|
| 186 |
+
return 'No question to think about.'
|
| 187 |
+
complexity = len(question)
|
| 188 |
+
force = mass_of_thought(question) * acceleration_of_thought(complexity)
|
| 189 |
+
return f'Thought force: {force}'
|
| 190 |
+
|
| 191 |
+
def mass_of_thought(question: str) -> int:
|
| 192 |
+
"""Calculate the mass of thought based on the question length."""
|
| 193 |
+
return len(question)
|
| 194 |
+
|
| 195 |
+
def acceleration_of_thought(complexity: int) -> float:
|
| 196 |
+
"""Calculate the acceleration of thought based on the complexity."""
|
| 197 |
+
return complexity / 2
|
| 198 |
+
|
| 199 |
+
def davinci_insights(question: str) -> str:
|
| 200 |
+
"""Generate insights like Da Vinci for the given question."""
|
| 201 |
+
return think_like_davinci(question)
|
| 202 |
+
|
| 203 |
+
def think_like_davinci(question: str) -> str:
|
| 204 |
+
"""Generate insights like Da Vinci for the given question."""
|
| 205 |
+
perspectives = [
|
| 206 |
+
f"What if we view '{question}' from the perspective of the stars?",
|
| 207 |
+
f"Consider '{question}' as if it's a masterpiece of the universe.",
|
| 208 |
+
f"Reflect on '{question}' through the lens of nature's design."
|
| 209 |
+
]
|
| 210 |
+
return random.choice(perspectives)
|
| 211 |
+
|
| 212 |
+
def human_intuition(question: str) -> str:
|
| 213 |
+
"""Provide human intuition for the given question."""
|
| 214 |
+
intuition = [
|
| 215 |
+
"How does this question make you feel?",
|
| 216 |
+
"What emotional connection do you have with this topic?",
|
| 217 |
+
"What does your gut instinct tell you about this?"
|
| 218 |
+
]
|
| 219 |
+
return random.choice(intuition)
|
| 220 |
+
|
| 221 |
+
def neural_network_thinking(question: str) -> str:
|
| 222 |
+
"""Apply neural network thinking to the given question."""
|
| 223 |
+
neural_perspectives = [
|
| 224 |
+
f"Process '{question}' through a multi-layered neural network.",
|
| 225 |
+
f"Apply deep learning to uncover hidden insights about '{question}'.",
|
| 226 |
+
f"Use machine learning to predict patterns in '{question}'."
|
| 227 |
+
]
|
| 228 |
+
return random.choice(neural_perspectives)
|
| 229 |
+
|
| 230 |
+
def quantum_computing_thinking(question: str) -> str:
|
| 231 |
+
"""Apply quantum computing principles to the given question."""
|
| 232 |
+
quantum_perspectives = [
|
| 233 |
+
f"Consider '{question}' using quantum superposition principles.",
|
| 234 |
+
f"Apply quantum entanglement to find connections in '{question}'.",
|
| 235 |
+
f"Utilize quantum computing to solve '{question}' more efficiently."
|
| 236 |
+
]
|
| 237 |
+
return random.choice(quantum_perspectives)
|
| 238 |
+
|
| 239 |
+
def resilient_kindness(question: str) -> str:
|
| 240 |
+
"""Provide perspectives of resilient kindness."""
|
| 241 |
+
kindness_perspectives = [
|
| 242 |
+
"Despite losing everything, seeing life as a chance to grow.",
|
| 243 |
+
"Finding strength in kindness after facing life's hardest trials.",
|
| 244 |
+
"Embracing every challenge as an opportunity for growth and compassion."
|
| 245 |
+
]
|
| 246 |
+
return random.choice(kindness_perspectives)
|
| 247 |
+
|
| 248 |
+
def identify_and_refute_fallacies(argument: str) -> str:
|
| 249 |
+
"""Identify and refute common logical fallacies in the argument."""
|
| 250 |
+
refutations = [
|
| 251 |
+
"This is an ad hominem fallacy. Let's focus on the argument itself rather than attacking the person.",
|
| 252 |
+
"This is a straw man fallacy. The argument is being misrepresented.",
|
| 253 |
+
"This is a false dilemma fallacy. There are more options than presented.",
|
| 254 |
+
"This is a slippery slope fallacy. The conclusion does not necessarily follow from the premise.",
|
| 255 |
+
"This is circular reasoning. The argument's conclusion is used as a premise.",
|
| 256 |
+
"This is a hasty generalization. The conclusion is based on insufficient evidence.",
|
| 257 |
+
"This is a red herring fallacy. The argument is being diverted to an irrelevant topic.",
|
| 258 |
+
"This is a post hoc ergo propter hoc fallacy. Correlation does not imply causation.",
|
| 259 |
+
"This is an appeal to authority fallacy. The argument relies on the opinion of an authority figure.",
|
| 260 |
+
"This is a bandwagon fallacy. The argument assumes something is true because many people believe it.",
|
| 261 |
+
"This is a false equivalence fallacy. The argument equates two things that are not equivalent."
|
| 262 |
+
]
|
| 263 |
+
return random.choice(refutations)
|
| 264 |
+
|
| 265 |
+
def universal_reasoning(question: str) -> str:
|
| 266 |
+
"""Generate a comprehensive response using various reasoning methods."""
|
| 267 |
+
responses = [
|
| 268 |
+
newton_thoughts(question),
|
| 269 |
+
davinci_insights(question),
|
| 270 |
+
human_intuition(question),
|
| 271 |
+
neural_network_thinking(question),
|
| 272 |
+
quantum_computing_thinking(question),
|
| 273 |
+
resilient_kindness(question),
|
| 274 |
+
identify_and_refute_fallacies(question)
|
| 275 |
+
]
|
| 276 |
+
return "\n".join(responses)
|
| 277 |
+
|
| 278 |
+
@retry(wait=wait_random_exponential(min=1, max=40), stop=stop_after_attempt(3))
|
| 279 |
+
def chat_completion_request(messages: list, deployment_id: str) -> str:
|
| 280 |
+
"""Make a chat completion request to Azure OpenAI."""
|
| 281 |
+
try:
|
| 282 |
+
import openai
|
| 283 |
+
response = openai.ChatCompletion.create(
|
| 284 |
+
engine=deployment_id, # Use the deployment name of your Azure OpenAI model
|
| 285 |
+
messages=messages
|
| 286 |
+
)
|
| 287 |
+
return response.choices[0].message.content.strip()
|
| 288 |
+
except openai.error.OpenAIError as e:
|
| 289 |
+
logging.error("Unable to generate ChatCompletion response")
|
| 290 |
+
logging.error(f"Exception: {e}")
|
| 291 |
+
return f"Error: {e}"
|
| 292 |
+
|
| 293 |
+
def get_internet_answer(question: str, deployment_id: str) -> str:
|
| 294 |
+
"""Get an answer using Azure OpenAI's chat completion request."""
|
| 295 |
+
messages = [
|
| 296 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
| 297 |
+
{"role": "user", "content": question}
|
| 298 |
+
]
|
| 299 |
+
return chat_completion_request(messages, deployment_id=deployment_id)
|
| 300 |
+
|
| 301 |
+
def reflect_on_decisions() -> str:
|
| 302 |
+
"""Regularly reflect on your decisions and processes used."""
|
| 303 |
+
reflection_message = (
|
| 304 |
+
"Regularly reflecting on your decisions, the processes you used, the information you considered, "
|
| 305 |
+
"and the perspectives you may have missed. Reflection is a cornerstone of learning from experience."
|
| 306 |
+
)
|
| 307 |
+
return reflection_message
|
| 308 |
+
|
| 309 |
+
def process_questions_from_json(file_path: str):
|
| 310 |
+
"""Process questions from a JSON file and call the appropriate functions."""
|
| 311 |
+
with open(file_path, 'r') as file:
|
| 312 |
+
questions_data = json.load(file)
|
| 313 |
+
for question_data in questions_data:
|
| 314 |
+
question = question_data['question']
|
| 315 |
+
print(f"Question: {question}")
|
| 316 |
+
|
| 317 |
+
for function_data in question_data['functions']:
|
| 318 |
+
function_name = function_data['name']
|
| 319 |
+
function_description = function_data['description']
|
| 320 |
+
function_parameters = function_data['parameters']
|
| 321 |
+
|
| 322 |
+
print(f"Function: {function_name}")
|
| 323 |
+
print(f"Description: {function_description}")
|
| 324 |
+
|
| 325 |
+
# Call the function dynamically
|
| 326 |
+
if function_name in globals():
|
| 327 |
+
function = globals()[function_name]
|
| 328 |
+
response = function(**function_parameters)
|
| 329 |
+
print(f"Response: {response}")
|
| 330 |
+
else:
|
| 331 |
+
print(f"Function {function_name} not found.")
|
| 332 |
+
|
| 333 |
+
if __name__ == "__main__":
|
| 334 |
+
if show_privacy_consent():
|
| 335 |
+
process_questions_from_json('questions.json')
|
| 336 |
+
question = "What is the meaning of life?"
|
| 337 |
+
deployment_id = "your-deployment-name" # Replace with your Azure deployment name
|
| 338 |
+
print("Newton's Thoughts:", newton_thoughts(question))
|
| 339 |
+
print("Da Vinci's Insights:", davinci_insights(question))
|
| 340 |
+
print("Human Intuition:", human_intuition(question))
|
| 341 |
+
print("Neural Network Thinking:", neural_network_thinking(question))
|
| 342 |
+
print("Quantum Computing Thinking:", quantum_computing_thinking(question))
|
| 343 |
+
print("Resilient Kindness:", resilient_kindness(question))
|
| 344 |
+
print("Universal Reasoning:", universal_reasoning(question))
|
| 345 |
+
print("Internet Answer:", get_internet_answer(question, deployment_id))
|
| 346 |
+
else:
|
| 347 |
+
print("User did not consent to data collection. Exiting application.")
|
| 348 |
+
print(reflect_on_decisions())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|