Skip to content

Commit

Permalink
Merge pull request #304 from SylphAI-Inc/main
Browse files Browse the repository at this point in the history
[Release][notebook merges]
  • Loading branch information
Sylph-AI authored Dec 14, 2024
2 parents c857072 + 670011a commit 86072d7
Show file tree
Hide file tree
Showing 63 changed files with 14,175 additions and 5,230 deletions.
8 changes: 7 additions & 1 deletion adalflow/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@

## [0.2.7] - TO Be Released
### Added
- `Memory` is completed with `call` and `add_dialog_turn` methods.
- Integrated `LanceDB` in the `Retriever`
### Improved
- `BedrockAPIClient` added more details on setup, yet it is still in experimental stage.
- `AzureAPIClient` added more details on setup, yet it is still in experimental stage.
## [0.2.6] - 2024-11-25
### Improved
- Add default `max_tokens=512` to the `AnthropicAPIClient` to avoid the error when the user does not provide the `max_tokens` in the prompt.
Expand Down
66 changes: 60 additions & 6 deletions adalflow/adalflow/components/memory/memory.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,77 @@
"""Memory for user-assistant conversations. [Not completed]
"""Memory component for user-assistant conversations.
Memory can include data modeling, in-memory data storage, local file data storage, cloud data persistence, data pipeline, data retriever.
It is itself an LLM application and different use cases can do it differently.
This component handles the storage and retrieval of conversation history between users
and assistants. It provides local memory experience with the ability to format and
return conversation history.
This implementation covers the minimal and local memory experience for the user-assistant conversation.
Attributes:
current_conversation (Conversation): Stores the current active conversation.
turn_db (LocalDB): Database for storing all conversation turns.
conver_db (LocalDB): Database for storing complete conversations.
"""

from uuid import uuid4
from adalflow.core.component import Component
from adalflow.core.db import LocalDB
from adalflow.core.types import (
Conversation,
DialogTurn,
UserQuery,
AssistantResponse,
)

from adalflow.core.db import LocalDB
from adalflow.core.component import Component


class Memory(Component):
def __init__(self, turn_db: LocalDB = None):
"""Initialize the Memory component.
Args:
turn_db (LocalDB, optional): Database for storing conversation turns.
Defaults to None, in which case a new LocalDB is created.
"""
super().__init__()
self.current_convesation = Conversation()
self.current_conversation = Conversation()
self.turn_db = turn_db or LocalDB() # all turns
self.conver_db = LocalDB() # a list of conversations

def call(self) -> str:
"""Returns the current conversation history as a formatted string.
Returns:
str: Formatted conversation history with alternating user and assistant messages.
Returns empty string if no conversation history exists.
"""
if not self.current_conversation.dialog_turns:
return ""

formatted_history = []
for turn in self.current_conversation.dialog_turns.values():
formatted_history.extend(
[
f"User: {turn.user_query.query_str}",
f"Assistant: {turn.assistant_response.response_str}",
]
)
return "\n".join(formatted_history)

def add_dialog_turn(self, user_query: str, assistant_response: str):
"""Add a new dialog turn to the current conversation.
Args:
user_query (str): The user's input message.
assistant_response (str): The assistant's response message.
"""
dialog_turn = DialogTurn(
id=str(uuid4()),
user_query=UserQuery(query_str=user_query),
assistant_response=AssistantResponse(response_str=assistant_response),
)

self.current_conversation.append_dialog_turn(dialog_turn)

self.turn_db.add(
{"user_query": user_query, "assistant_response": assistant_response}
)
2 changes: 1 addition & 1 deletion adalflow/adalflow/components/model_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
)
BedrockAPIClient = LazyImport(
"adalflow.components.model_client.bedrock_client.BedrockAPIClient",
OptionalPackages.BEDROCK,
OptionalPackages.BOTO3,
)
GroqAPIClient = LazyImport(
"adalflow.components.model_client.groq_client.GroqAPIClient",
Expand Down
Loading

0 comments on commit 86072d7

Please sign in to comment.