Create botlogic.py
Browse filesA little extra logic to help your coder out from my coder :)
- botlogic.py +123 -0
botlogic.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
from botbuilder.core import TurnContext, MessageFactory, ConversationState, UserState
|
| 3 |
+
from botbuilder.schema import Activity, ActivityTypes, EndOfConversationCodes
|
| 4 |
+
from UniversalReasoning import UniversalReasoning # Ensure correct import
|
| 5 |
+
import os
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
from dialog_bot import DialogBot
|
| 8 |
+
from main_dialog import MainDialog
|
| 9 |
+
|
| 10 |
+
# Load environment variables from .env file
|
| 11 |
+
load_dotenv()
|
| 12 |
+
|
| 13 |
+
class MyBot(DialogBot):
|
| 14 |
+
def __init__(self, conversation_state: ConversationState, user_state: UserState, dialog: MainDialog):
|
| 15 |
+
super(MyBot, self).__init__(conversation_state, user_state, dialog)
|
| 16 |
+
self.context = {}
|
| 17 |
+
self.feedback = []
|
| 18 |
+
config = load_and_validate_config('config.json', 'config_schema.json')
|
| 19 |
+
# Add Azure OpenAI and LUIS configurations to the config
|
| 20 |
+
config['azure_openai_api_key'] = os.getenv('AZURE_OPENAI_API_KEY')
|
| 21 |
+
config['azure_openai_endpoint'] = os.getenv('AZURE_OPENAI_ENDPOINT')
|
| 22 |
+
config['luis_endpoint'] = os.getenv('LUIS_ENDPOINT')
|
| 23 |
+
config['luis_api_version'] = os.getenv('LUIS_API_VERSION')
|
| 24 |
+
config['luis_api_key'] = os.getenv('LUIS_API_KEY')
|
| 25 |
+
setup_logging(config)
|
| 26 |
+
self.universal_reasoning = UniversalReasoning(config)
|
| 27 |
+
|
| 28 |
+
async def enhance_context_awareness(self, user_id: str, text: str) -> None:
|
| 29 |
+
"""Enhance context awareness by analyzing the user's environment, activities, and emotional state."""
|
| 30 |
+
sentiment = analyze_sentiment_vader(text)
|
| 31 |
+
self.context[user_id].append({"text": text, "sentiment": sentiment})
|
| 32 |
+
|
| 33 |
+
async def proactive_learning(self, user_id: str, feedback: str) -> None:
|
| 34 |
+
"""Encourage proactive learning by seeking feedback and exploring new topics."""
|
| 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 |
+
self.context[user_id].append({"ethical_decision": ethical_decision})
|
| 42 |
+
|
| 43 |
+
async def emotional_intelligence(self, user_id: str, text: str) -> str:
|
| 44 |
+
"""Develop emotional intelligence by recognizing and responding to user emotions."""
|
| 45 |
+
sentiment = analyze_sentiment_vader(text)
|
| 46 |
+
response = self.generate_emotional_response(sentiment, text)
|
| 47 |
+
self.context[user_id].append({"emotional_response": response})
|
| 48 |
+
return response
|
| 49 |
+
|
| 50 |
+
def generate_emotional_response(self, sentiment: dict, text: str) -> str:
|
| 51 |
+
"""Generate an empathetic response based on the sentiment analysis."""
|
| 52 |
+
if sentiment['compound'] >= 0.05:
|
| 53 |
+
return "I'm glad to hear that! 😊 How can I assist you further?"
|
| 54 |
+
elif sentiment['compound'] <= -0.05:
|
| 55 |
+
return "I'm sorry to hear that. 😢 Is there anything I can do to help?"
|
| 56 |
+
else:
|
| 57 |
+
return "I understand. How can I assist you further?"
|
| 58 |
+
|
| 59 |
+
async def transparency_and_explainability(self, user_id: str, decision: str) -> str:
|
| 60 |
+
"""Enable transparency by explaining the reasoning behind decisions."""
|
| 61 |
+
explanation = f"The decision was made based on the following context: {self.context[user_id]}"
|
| 62 |
+
self.context[user_id].append({"explanation": explanation})
|
| 63 |
+
return explanation
|
| 64 |
+
|
| 65 |
+
async def on_message_activity(self, turn_context: TurnContext) -> None:
|
| 66 |
+
"""Handles incoming messages and generates responses."""
|
| 67 |
+
user_id = turn_context.activity.from_property.id
|
| 68 |
+
if user_id not in self.context:
|
| 69 |
+
self.context[user_id] = []
|
| 70 |
+
try:
|
| 71 |
+
message_text = turn_context.activity.text.strip().lower()
|
| 72 |
+
if "end" in message_text or "stop" in message_text:
|
| 73 |
+
await end_conversation(turn_context)
|
| 74 |
+
self.context.pop(user_id, None)
|
| 75 |
+
else:
|
| 76 |
+
self.context[user_id].append(turn_context.activity.text)
|
| 77 |
+
response = await self.generate_response(turn_context.activity.text, user_id)
|
| 78 |
+
await turn_context.send_activity(MessageFactory.text(response))
|
| 79 |
+
await self.request_feedback(turn_context, user_id)
|
| 80 |
+
except Exception as e:
|
| 81 |
+
await handle_error(turn_context, e)
|
| 82 |
+
|
| 83 |
+
async def generate_response(self, text: str, user_id: str) -> str:
|
| 84 |
+
"""Generates a response using UniversalReasoning."""
|
| 85 |
+
try:
|
| 86 |
+
logging.info(f"Generating response for user_id: {user_id} with text: {text}")
|
| 87 |
+
response = self.universal_reasoning.generate_response(text)
|
| 88 |
+
logging.info(f"Generated response: {response}")
|
| 89 |
+
return response
|
| 90 |
+
except Exception as e:
|
| 91 |
+
logging.error(f"Error generating response: {e}")
|
| 92 |
+
return "Sorry, I couldn't generate a response at this time."
|
| 93 |
+
|
| 94 |
+
async def request_feedback(self, turn_context: TurnContext, user_id: str) -> None:
|
| 95 |
+
"""Request feedback from the user about the bot's response."""
|
| 96 |
+
feedback_prompt = "How would you rate my response? (good/neutral/bad)"
|
| 97 |
+
await turn_context.send_activity(MessageFactory.text(feedback_prompt))
|
| 98 |
+
|
| 99 |
+
async def handle_feedback(self, turn_context: TurnContext) -> None:
|
| 100 |
+
"""Handle user feedback and store it for future analysis."""
|
| 101 |
+
user_id = turn_context.activity.from_property.id
|
| 102 |
+
feedback = turn_context.activity.text.lower()
|
| 103 |
+
if feedback in ["good", "neutral", "bad"]:
|
| 104 |
+
self.feedback.append({"user_id": user_id, "feedback": feedback})
|
| 105 |
+
await turn_context.send_activity(MessageFactory.text("Thank you for your feedback!"))
|
| 106 |
+
else:
|
| 107 |
+
await turn_context.send_activity(MessageFactory.text("Please provide feedback as 'good', 'neutral', or 'bad'."))
|
| 108 |
+
|
| 109 |
+
async def end_conversation(turn_context: TurnContext) -> None:
|
| 110 |
+
"""Ends the conversation with the user."""
|
| 111 |
+
await turn_context.send_activity(
|
| 112 |
+
MessageFactory.text("Ending conversation from the skill...")
|
| 113 |
+
)
|
| 114 |
+
end_of_conversation = Activity(type=ActivityTypes.end_of_conversation)
|
| 115 |
+
end_of_conversation.code = EndOfConversationCodes.completed_successfully
|
| 116 |
+
await turn_context.send_activity(end_of_conversation)
|
| 117 |
+
|
| 118 |
+
async def handle_error(turn_context: TurnContext, error: Exception) -> None:
|
| 119 |
+
"""Handles errors by logging them and notifying the user."""
|
| 120 |
+
logging.error(f"An error occurred: {error}")
|
| 121 |
+
await turn_context.send_activity(
|
| 122 |
+
MessageFactory.text("An error occurred. Please try again later.")
|
| 123 |
+
)
|