Skip to content

Commit

Permalink
Falkordb 03 (#36)
Browse files Browse the repository at this point in the history
* feat: add FalkorDB memory integration and tests

* chore: Add FalkorDB memory integration and tests

* Delete test_falkordb_memory.py

* Delete poetry.lock

* refactor: Replace clear method with hybridstore delete in FalkorDB memory integration tests

* Restore notebooks
  • Loading branch information
acazau committed Aug 30, 2024
1 parent 8580136 commit 0eb70f3
Show file tree
Hide file tree
Showing 25 changed files with 3,874 additions and 344 deletions.
29 changes: 17 additions & 12 deletions hybridagi/core/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,8 @@ class InteractionSession(BaseModel):
user: Optional[UserProfile] = Field(description="The user profile", default_factory=UserProfile)
chat: Optional[ChatHistory] = Field(description="The chat history", default_factory=ChatHistory)

def to_dict():
return {"user": self.user.to_dict(), "chat_history": [m.to_dict() for m in self.msgs]}
def to_dict(self):
return {"user": self.user.to_dict(), "chat_history": [m.to_dict() for m in self.chat.msgs]}

class QueryWithSession(BaseModel, dspy.Prediction):
query: Query = Field(description="The input user query", default_factory=Query)
Expand All @@ -285,7 +285,7 @@ def __init__(self, **kwargs):
dspy.Prediction.__init__(self, **kwargs)

def to_dict(self):
return {"query": self.query.query, "session": session.to_dict()}
return {"query": self.query.query, "session": self.session.to_dict()}

class AgentStepType(str, Enum):
Action = "Action"
Expand Down Expand Up @@ -315,40 +315,45 @@ class AgentStepType(str, Enum):

class AgentStep(BaseModel):
id: Union[UUID, str] = Field(description="Unique identifier for a step", default_factory=uuid4)
parent_id: Union[UUID, str] = Field(description="The previous step id if any", default=None)
parent_id: Optional[Union[UUID, str]] = Field(description="The previous step id if any", default=None)
hop: int = Field(description="The step hop", default=0)
step_type: AgentStepType = Field(description="The step type")
weight: float = Field(description="The step weight (between 0.0 and 1.0, default 1.0)", default=1.0)
name: Optional[str] = Field(description="The name of the step", default=None)
description: Optional[str] = Field(description="The description of the step", default=None)
inputs: Optional[Dict[str, Any]] = Field(description="The inputs of the step", default=None)
outputs: Optional[Dict[str, Any]] = Field(description="The outputs of the step", default=None)
vector: Optional[List[float]] = Field(description="Vector representation of the step", default=None)
metadata: Optional[Dict[str, Any]] = Field(description="Additional information about the step", default=None)
created_at: datetime = Field(description="Time when the step was created", default_factory=datetime.now)

def __str__(self):
if self.inputs is None:
self.inputs = {}

if self.step_type == AgentStepType.Action:
return ACTION_TEMPLATE.format(
hop=self.hop,
purpose=self.inputs["purpose"],
purpose=self.inputs.get("purpose", ""),
prediction=json.dumps(self.outputs, indent=2),
)
elif self.step_type == AgentStepType.Decision:
return DECISION_TEMPLATE.format(
hop=self.hop,
purpose=self.inputs["purpose"],
question=self.inputs["question"],
choice=self.outputs["choice"],
purpose=self.inputs.get("purpose", ""),
question=self.inputs.get("question", ""),
choice=self.outputs["choice"] if self.outputs and "choice" in self.outputs else "",
)
elif self.step_type == AgentStepType.ProgramCall:
return CALL_PROGRAM_TEMPLATE.format(
hop=self.hop,
purpose=self.inputs["purpose"],
program=self.inputs["program"],
purpose=self.inputs.get("purpose", ""),
program=self.inputs.get("program", ""),
)
elif self.step_type == AgentStepType.ProgramEnd:
return END_PROGRAM_TEMPLATE.format(
hop=self.hop,
program=self.inputs["program"],
program=self.inputs.get("program", ""),
)
else:
raise ValueError("Invalid type for AgentStep")
Expand Down Expand Up @@ -462,4 +467,4 @@ def __init__(self, **kwargs):
dspy.Prediction.__init__(self, **kwargs)

def to_dict(self):
return {"query": self.query.query, "routines": [p.to_dict() for p in self.progs]}
return {"query": self.query.query, "routines": [p.to_dict() for p in self.progs]}
5 changes: 4 additions & 1 deletion hybridagi/embeddings/sentence_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ def __init__(
self.model.max_seq_length = self.max_seq_length

def embed_text(self, query_or_queries: Union[str, List[str]]) -> np._typing.NDArray:
if isinstance(query_or_queries, str) and query_or_queries == "":
raise ValueError("Input cannot be an empty string.")

text_to_vectorize = [query_or_queries] if not isinstance(query_or_queries, list) else query_or_queries

if self.is_gpu and self.num_devices > 1:
Expand Down Expand Up @@ -70,4 +73,4 @@ def embed_text(self, query_or_queries: Union[str, List[str]]) -> np._typing.NDAr
return emb

def embed_image(self, image_or_images: Union[np._typing.NDArray, List[np._typing.NDArray]]) -> np._typing.NDArray:
raise NotImplementedError("SentenceTransformer does not support image embeddings")
raise NotImplementedError("SentenceTransformer does not support image embeddings")
11 changes: 11 additions & 0 deletions hybridagi/memory/integration/falkordb/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from .falkordb_fact_memory import FalkorDBFactMemory
from .falkordb_program_memory import FalkorDBProgramMemory
from .falkordb_document_memory import FalkorDBDocumentMemory
from .falkordb_trace_memory import FalkorDBTraceMemory

__all__ = [
FalkorDBFactMemory,
FalkorDBDocumentMemory,
FalkorDBProgramMemory,
FalkorDBTraceMemory
]
Loading

0 comments on commit 0eb70f3

Please sign in to comment.