From 7a84ade5a49748ec839888446ae1a621994a3163 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Wed, 6 Nov 2024 08:00:09 -0500 Subject: [PATCH] Add MoodDetectorPlugin to examples/mood_detector_plugin.py and update requirements.txt --- examples/mood_detector_plugin.py | 71 ++++++++++++++++++++++++++++++++ examples/requirements.txt | 1 + simplemind/models.py | 1 + 3 files changed, 73 insertions(+) create mode 100644 examples/mood_detector_plugin.py diff --git a/examples/mood_detector_plugin.py b/examples/mood_detector_plugin.py new file mode 100644 index 0000000..a287042 --- /dev/null +++ b/examples/mood_detector_plugin.py @@ -0,0 +1,71 @@ +import nltk +from nltk.sentiment import SentimentIntensityAnalyzer +from rich.console import Console +from rich import print as rprint +from _context import simplemind as sm + +nltk.download("vader_lexicon") + +console = Console() + + +class MoodDetectorPlugin(sm.BasePlugin): + model_config = {"arbitrary_types_allowed": True} + analyzer: SentimentIntensityAnalyzer = None + + def __init__(self): + super().__init__() + # Initialize sentiment analyzer from nltk + self.analyzer = SentimentIntensityAnalyzer() + + def detect_mood(self, text): + # Analyze the sentiment of the given text + scores = self.analyzer.polarity_scores(text) + + # Print sentiment analysis details with colors + console.print("\n[bold]Sentiment Analysis:[/bold]") + console.print(f"Text: [italic]{text}[/italic]") + console.print("\nScores:") + console.print(f"🟢 Positive: [green]{scores['pos']:.3f}[/green]") + console.print(f"🔴 Negative: [red]{scores['neg']:.3f}[/red]") + console.print(f"⚪ Neutral: [blue]{scores['neu']:.3f}[/blue]") + console.print(f"📊 Compound: [yellow]{scores['compound']:.3f}[/yellow]\n") + + if scores["compound"] >= 0.5: + console.print("Overall Mood: [green]positive[/green] 😊") + return "positive" + elif scores["compound"] <= -0.5: + console.print("Overall Mood: [red]negative[/red] 😢") + return "negative" + else: + console.print("Overall Mood: [blue]neutral[/blue] 😐") + return "neutral" + + def pre_send_hook(self, conversation: sm.Conversation): + # Get the last user message to analyze its mood + last_message = conversation.get_last_message(role="user") + if last_message: + mood = self.detect_mood(last_message.text) + # Adjust AI response style based on the detected mood + if mood == "positive": + tone_message = ( + "The user seems cheerful. Respond with enthusiasm and positivity." + ) + elif mood == "negative": + tone_message = "The user seems to be in a low mood. Respond with empathy and warmth." + else: + tone_message = "The user seems neutral. Respond with a balanced tone." + + # Inject the tone adjustment message as a system prompt + conversation.add_message(role="system", text=tone_message) + + +# Create a conversation and add the plugin +conversation = sm.create_conversation(llm_model="gpt-4o-mini", llm_provider="openai") +conversation.add_plugin(MoodDetectorPlugin()) + +# Add a user message and send the conversation +conversation.add_message(role="user", text="I'm having a really rough day.") +response = conversation.send() + +console.print(f"*{ response.text }*") diff --git a/examples/requirements.txt b/examples/requirements.txt index a1348ca..476de75 100644 --- a/examples/requirements.txt +++ b/examples/requirements.txt @@ -3,3 +3,4 @@ openai pydantic faiss-cpu rich +nltk diff --git a/simplemind/models.py b/simplemind/models.py index e7dd937..5ba0a2b 100644 --- a/simplemind/models.py +++ b/simplemind/models.py @@ -30,6 +30,7 @@ class BasePlugin(SMBaseModel): class Config: extra = "allow" + # allow_arbitrary_types = True def initialize_hook(self, conversation: "Conversation") -> Any: """Initialize a hook for the plugin."""