Refactor Conversation class to support plugins

This commit is contained in:
2024-10-28 19:35:34 -04:00
parent 1a8782d562
commit 20f2bedcc2
2 changed files with 26 additions and 1 deletions
+8
View File
@@ -41,6 +41,7 @@ class Conversation(SMBaseModel):
messages: List[Message] = []
llm_model: Optional[str] = None
llm_provider: Optional[str] = None
plugins: List[Any] = []
def __str__(self):
return f"<Conversation id={self.id!r}>"
@@ -53,8 +54,15 @@ class Conversation(SMBaseModel):
self, llm_model: Optional[str] = None, llm_provider: Optional[str] = None
) -> Message:
"""Send the conversation to the LLM."""
for plugin in self.plugins:
plugin.send_hook(self)
provider = find_provider(llm_provider or self.llm_provider)
response = provider.send_conversation(self)
self.add_message(role="assistant", text=response.text, meta=response.meta)
return response
def add_plugin(self, plugin: Any):
"""Add a plugin to the conversation."""
self.plugins.append(plugin)
+18 -1
View File
@@ -1,11 +1,28 @@
import simplemind as sm
class SimpleMemoryPlugin:
def __init__(self):
self.memories = [
"the earth has fictionally beeen destroyed.",
"the moon is made of cheese.",
]
def yield_memories(self):
return (m for m in self.memories)
def send_hook(self, conversation: sm.Conversation):
for m in self.yield_memories():
conversation.add_message(role="system", text=m)
conversation = sm.create_conversation(llm_model="grok-beta", llm_provider="xai")
conversation.add_plugin(SimpleMemoryPlugin())
conversation.add_message(
role="user",
text="Write a poem about working at dominoes",
text="Write a poem about the moon",
)
r = conversation.send()