Skip to content

Commit

Permalink
Conversational RDDDY
Browse files Browse the repository at this point in the history
seanchatmangpt committed Mar 27, 2024
1 parent 2dd6549 commit 8778b7a
Showing 85 changed files with 2,324 additions and 77 deletions.
35 changes: 34 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry] # https://python-poetry.org/docs/pyproject/
name = "dspygen"
version = "2024.3.25"
version = "2024.3.27"
description = "A Ruby on Rails style framework for the DSPy (Demonstrate, Search, Predict) project for Language Models like GPT, BERT, and LLama."
authors = ["Sean Chatman <[email protected]>"]
readme = "README.md"
@@ -50,6 +50,9 @@ sentify = "^0.7.4"
ebooklib = "^0.18"
python-docx = "^1.1.0"
pypdf = "^4.1.0"
sqlmodel = "^0.0.16"
icontract = "^2.6.6"
tzlocal = "^5.2"

[tool.poetry.group.test.dependencies] # https://python-poetry.org/docs/master/managing-dependencies/
coverage = { extras = ["toml"], version = ">=7.2.5" }
13 changes: 13 additions & 0 deletions src/dspygen/experiments/convo_ddd/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from dspygen.experiments.convo_ddd.abstract_command.generate_response_command import GenerateResponseCommand
from dspygen.experiments.convo_ddd.abstract_command.handle_user_query_command import HandleUserQueryCommand
from dspygen.experiments.convo_ddd.abstract_command.recognize_entity_command import RecognizeEntityCommand
from dspygen.experiments.convo_ddd.abstract_command.recognize_intent_command import RecognizeIntentCommand
from dspygen.experiments.convo_ddd.abstract_command.transition_state_command import TransitionStateCommand
from dspygen.experiments.convo_ddd.abstract_command.update_context_command import UpdateContextCommand

from dspygen.experiments.convo_ddd.abstract_event.context_updated_event import ContextUpdatedEvent
from dspygen.experiments.convo_ddd.abstract_event.entity_recognized_event import EntityRecognizedEvent
from dspygen.experiments.convo_ddd.abstract_event.intent_recognized_event import IntentRecognizedEvent
from dspygen.experiments.convo_ddd.abstract_event.response_generated_event import ResponseGeneratedEvent
from dspygen.experiments.convo_ddd.abstract_event.state_transition_event import StateTransitionEvent
from dspygen.experiments.convo_ddd.abstract_event.user_input_received_event import UserInputReceivedEvent
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from munch import Munch

from dspygen.experiments.convo_ddd import (RecognizeIntentCommand,
RecognizeEntityCommand,
UpdateContextCommand,
TransitionStateCommand,
GenerateResponseCommand,
HandleUserQueryCommand,
ContextUpdatedEvent,
EntityRecognizedEvent,
IntentRecognizedEvent,
ResponseGeneratedEvent,
StateTransitionEvent,
UserInputReceivedEvent)

from dspygen.rdddy.abstract_aggregate import AbstractAggregate
from dspygen.modules.gen_pydantic_instance import instance
from dspygen.rdddy.actor_system import ActorSystem


class ConversationAggregate(AbstractAggregate):
def __init__(self, conversation_id: str, actor_system: "ActorSystem"):
super().__init__(actor_system)
self.conversation_id = conversation_id
self.context = Munch({"intent": None, "entity": None})
self.state = "initial"

async def handle_recognize_intent_command(self, command: RecognizeIntentCommand):
# Simulate intent recognition logic
intent = instance(IntentRecognizedEvent, command.user_input) # Assume instance() magically does the job
self.apply_event(intent)

async def handle_recognize_entity_command(self, command: RecognizeEntityCommand):
# Simulate entity recognition logic
entity = instance(EntityRecognizedEvent, command.user_input) # Assume instance() works similarly
self.apply_event(entity)

async def handle_update_context_command(self, command: UpdateContextCommand):
if command.replace:
self.context = command.updates
else:
self.context.update(command.updates)
self.apply_event(ContextUpdatedEvent(content="Context updated"))

async def handle_transition_state_command(self, command: TransitionStateCommand):
self.state = command.new_state
self.apply_event(StateTransitionEvent(content=f"Transitioned to {self.state} state"))

async def handle_generate_response_command(self, command: GenerateResponseCommand):
# Simplified response generation logic based on intent
if self.context.get("intent") == "greeting":
response = "Hello! How can I assist you today?"
elif self.context.get("intent") == "inquiry":
response = "What information are you seeking?"
else:
response = "I'm sorry, could you rephrase that?"

self.apply_event(ResponseGeneratedEvent(content=response))

async def handle_user_query_command(self, command: HandleUserQueryCommand):
# Process user query and potentially invoke other commands based on the query
self.apply_event(UserInputReceivedEvent(content=command.query))
# Example: Recognize intent from the user query
await self.handle_recognize_intent_command(RecognizeIntentCommand(user_input=command.query))
# Generate a response based on recognized intent
await self.handle_generate_response_command(GenerateResponseCommand(intent=self.context.get("intent", "")))

def apply_event(self, event):
if isinstance(event, IntentRecognizedEvent):
self.context['intent'] = event.intent_name
elif isinstance(event, EntityRecognizedEvent):
self.context['entity'] = event.entity_name
# Include handling for other event types as needed
else:
# It's helpful to log or handle the case where an event is not recognized
print(f"Unhandled event type: {type(event)}")
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from dspygen.rdddy.abstract_command import AbstractCommand


class GenerateResponseCommand(AbstractCommand):
"""Generated class for GenerateResponseCommand, inheriting from AbstractCommand."""
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from dspygen.rdddy.abstract_command import AbstractCommand


class HandleUserQueryCommand(AbstractCommand):
"""Generated class for HandleUserQueryCommand, inheriting from AbstractCommand."""

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from dspygen.rdddy.abstract_command import AbstractCommand


class RecognizeEntityCommand(AbstractCommand):
"""Generated class for RecognizeEntityCommand, inheriting from AbstractCommand."""

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from dspygen.rdddy.abstract_command import AbstractCommand


class RecognizeIntentCommand(AbstractCommand):
"""Generated class for RecognizeIntentCommand, inheriting from AbstractCommand."""

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from dspygen.rdddy.abstract_command import AbstractCommand


class TransitionStateCommand(AbstractCommand):
"""Generated class for TransitionStateCommand, inheriting from AbstractCommand."""

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from dspygen.rdddy.abstract_command import AbstractCommand


class UpdateContextCommand(AbstractCommand):
"""Generated class for UpdateContextCommand, inheriting from AbstractCommand."""

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from dspygen.rdddy.abstract_event import AbstractEvent


class ContextUpdatedEvent(AbstractEvent):
"""Generated class for ContextUpdatedEvent, inheriting from AbstractEvent."""

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from dspygen.rdddy.abstract_event import AbstractEvent


class DatabaseQueriedEvent(AbstractEvent):
"""Generated class for DatabaseQueriedEvent, inheriting from AbstractEvent."""

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from dspygen.rdddy.abstract_event import AbstractEvent


class EntityRecognizedEvent(AbstractEvent):
"""Generated class for EntityRecognizedEvent, inheriting from AbstractEvent."""

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from dspygen.rdddy.abstract_event import AbstractEvent


class ExternalAPIResponseEvent(AbstractEvent):
"""Generated class for ExternalAPIResponseEvent, inheriting from AbstractEvent."""

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from dspygen.rdddy.abstract_event import AbstractEvent


class ExternalServiceCalledEvent(AbstractEvent):
"""Generated class for ExternalServiceCalledEvent, inheriting from AbstractEvent."""

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from dspygen.rdddy.abstract_event import AbstractEvent


class IntentRecognizedEvent(AbstractEvent):
"""Generated class for IntentRecognizedEvent, inheriting from AbstractEvent."""

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from dspygen.rdddy.abstract_event import AbstractEvent


class ResponseGeneratedEvent(AbstractEvent):
"""Generated class for ResponseGeneratedEvent, inheriting from AbstractEvent."""

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from dspygen.rdddy.abstract_event import AbstractEvent


class StateTransitionEvent(AbstractEvent):
"""Generated class for StateTransitionEvent, inheriting from AbstractEvent."""

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from dspygen.rdddy.abstract_event import AbstractEvent


class SystemInterruptEvent(AbstractEvent):
"""Generated class for SystemInterruptEvent, inheriting from AbstractEvent."""

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from dspygen.rdddy.abstract_event import AbstractEvent


class SystemMessageDisplayedEvent(AbstractEvent):
"""Generated class for SystemMessageDisplayedEvent, inheriting from AbstractEvent."""

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from dspygen.rdddy.abstract_event import AbstractEvent


class UserInputReceivedEvent(AbstractEvent):
"""Generated class for UserInputReceivedEvent, inheriting from AbstractEvent."""

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from dspygen.rdddy.abstract_event import AbstractEvent


class UserMessageSubmittedEvent(AbstractEvent):
"""Generated class for UserMessageSubmittedEvent, inheriting from AbstractEvent."""

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from dspygen.rdddy.abstract_event import AbstractEvent


class UserQueryHandledEvent(AbstractEvent):
"""Generated class for UserQueryHandledEvent, inheriting from AbstractEvent."""

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from dspygen.rdddy.abstract_policy import AbstractPolicy


class ContextManagementPolicy(AbstractPolicy):
"""Generated class for ContextManagementPolicy, inheriting from AbstractPolicy."""

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from dspygen.rdddy.abstract_policy import AbstractPolicy


class EntityRecognitionPolicy(AbstractPolicy):
"""Generated class for EntityRecognitionPolicy, inheriting from AbstractPolicy."""

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from dspygen.rdddy.abstract_policy import AbstractPolicy


class IntentHandlingPolicy(AbstractPolicy):
"""Generated class for IntentHandlingPolicy, inheriting from AbstractPolicy."""

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from dspygen.rdddy.abstract_policy import AbstractPolicy


class ResponseGenerationPolicy(AbstractPolicy):
"""Generated class for ResponseGenerationPolicy, inheriting from AbstractPolicy."""

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from dspygen.rdddy.abstract_policy import AbstractPolicy


class StateTransitionPolicy(AbstractPolicy):
"""Generated class for StateTransitionPolicy, inheriting from AbstractPolicy."""

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from dspygen.rdddy.abstract_query import AbstractQuery


class GetCurrentContextQuery(AbstractQuery):
"""Generated class for GetCurrentContextQuery, inheriting from AbstractQuery."""

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from dspygen.rdddy.abstract_query import AbstractQuery


class GetCurrentStateQuery(AbstractQuery):
"""Generated class for GetCurrentStateQuery, inheriting from AbstractQuery."""

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from dspygen.rdddy.abstract_query import AbstractQuery


class GetEntityDetailsQuery(AbstractQuery):
"""Generated class for GetEntityDetailsQuery, inheriting from AbstractQuery."""

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from dspygen.rdddy.abstract_query import AbstractQuery


class GetIntentDetailsQuery(AbstractQuery):
"""Generated class for GetIntentDetailsQuery, inheriting from AbstractQuery."""

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from dspygen.rdddy.abstract_read_model import AbstractReadModel


class ContextSnapshotReadModel(AbstractReadModel):
"""Generated class for ContextSnapshotReadModel, inheriting from AbstractReadModel."""

Loading

0 comments on commit 8778b7a

Please sign in to comment.