mirror of
https://github.com/kennethreitz/simplemind.git
synced 2026-06-05 22:50:18 +00:00
Refactor Conversation class to support plugins
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user