From 48e6ef2a4350e47e6fb27b715b7267ba25ca0ca1 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Wed, 6 Nov 2024 07:54:11 -0500 Subject: [PATCH] Add InspirationPlugin to examples/inspiration_plugin.py and allow extra fields in BasePlugin's Config --- examples/inspiration_plugin.py | 37 ++++++++++++++++++++++++++++++++++ simplemind/models.py | 3 +++ 2 files changed, 40 insertions(+) create mode 100644 examples/inspiration_plugin.py diff --git a/examples/inspiration_plugin.py b/examples/inspiration_plugin.py new file mode 100644 index 0000000..f41502f --- /dev/null +++ b/examples/inspiration_plugin.py @@ -0,0 +1,37 @@ +import random +from _context import simplemind as sm + + +class InspirationPlugin(sm.BasePlugin): + # Define inspirations as a class variable + inspirations: list[str] = [ + "The only limit to our realization of tomorrow is our doubts of today.", + "Imagine beyond the edges of what you know.", + "What if the stars could speak? What stories would they tell?", + "Creativity is intelligence having fun.", + "Think not only with your mind but with your heart.", + "Let every answer be a doorway to another question.", + "The universe is in constant dialogue with those who listen.", + ] + + def __init__(self): + super().__init__() # Make sure to call parent class's __init__ + + def get_inspiration(self): + # Randomly select an inspirational quote or prompt + return random.choice(self.inspirations) + + def pre_send_hook(self, conversation: sm.Conversation): + # Inject an inspirational message as a system prompt + inspiration = self.get_inspiration() + conversation.add_message(role="system", text=inspiration) + + +# Create a conversation and add the plugin +conversation = sm.create_conversation(llm_model="gpt-4o-mini", llm_provider="openai") +conversation.add_plugin(InspirationPlugin()) + +# Add a user message and send the conversation +conversation.add_message(role="user", text="Tell me something inspiring.") +response = conversation.send() +print(response) diff --git a/simplemind/models.py b/simplemind/models.py index e2e22ab..e7dd937 100644 --- a/simplemind/models.py +++ b/simplemind/models.py @@ -28,6 +28,9 @@ class BasePlugin(SMBaseModel): # Plugin metadata. meta: Dict[str, Any] = {} + class Config: + extra = "allow" + def initialize_hook(self, conversation: "Conversation") -> Any: """Initialize a hook for the plugin.""" raise NotImplementedError