-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
✨ Add fallback memory #736
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
35 changes: 35 additions & 0 deletions
35
platform/reworkd_platform/tests/memory/memory_with_fallback_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from unittest.mock import MagicMock | ||
|
||
import pytest | ||
|
||
from reworkd_platform.web.api.memory.memory_with_fallback import MemoryWithFallback | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"method_name, args", | ||
[ | ||
("__enter__", ()), | ||
("__exit__", (None, None, None)), | ||
("add_tasks", (["task1", "task2"],)), | ||
("get_similar_tasks", ("task1", 0.8)), | ||
("reset_class", ()), | ||
], | ||
) | ||
def test_memory_with_fallback(method_name: str, args) -> None: | ||
primary = MagicMock() | ||
secondary = MagicMock() | ||
memory_with_fallback = MemoryWithFallback(primary, secondary) | ||
|
||
# Use getattr() to call the method on the object with args | ||
getattr(memory_with_fallback, method_name)(*args) | ||
getattr(primary, method_name).assert_called_once_with(*args) | ||
getattr(secondary, method_name).assert_not_called() | ||
|
||
# Reset mock and make primary raise an exception | ||
getattr(primary, method_name).reset_mock() | ||
getattr(primary, method_name).side_effect = Exception("Primary Failed") | ||
|
||
# Call the method again, this time it should fall back to secondary | ||
getattr(memory_with_fallback, method_name)(*args) | ||
getattr(primary, method_name).assert_called_once_with(*args) | ||
getattr(secondary, method_name).assert_called_once_with(*args) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
platform/reworkd_platform/web/api/memory/memory_with_fallback.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from __future__ import annotations | ||
|
||
from typing import List | ||
|
||
from loguru import logger | ||
|
||
from reworkd_platform.web.api.memory.memory import AgentMemory | ||
|
||
|
||
class MemoryWithFallback(AgentMemory): | ||
""" | ||
Wrap a primary AgentMemory provider and use a fallback in the case that it fails | ||
We do this because we've had issues with Weaviate crashing and causing memory to randomly fail | ||
""" | ||
|
||
def __init__(self, primary: AgentMemory, secondary: AgentMemory): | ||
self.primary = primary | ||
self.secondary = secondary | ||
|
||
def __enter__(self) -> AgentMemory: | ||
try: | ||
return self.primary.__enter__() | ||
except Exception as e: | ||
logger.exception(e) | ||
return self.secondary.__enter__() | ||
|
||
def __exit__(self, exc_type, exc_value, traceback) -> None: | ||
try: | ||
self.primary.__exit__(exc_type, exc_value, traceback) | ||
except Exception as e: | ||
logger.exception(e) | ||
self.secondary.__exit__(exc_type, exc_value, traceback) | ||
|
||
def add_tasks(self, tasks: List[str]) -> List[str]: | ||
try: | ||
return self.primary.add_tasks(tasks) | ||
except Exception as e: | ||
logger.exception(e) | ||
return self.secondary.add_tasks(tasks) | ||
|
||
def get_similar_tasks(self, query: str, score_threshold) -> List[str]: | ||
try: | ||
return self.primary.get_similar_tasks(query, score_threshold) | ||
except Exception as e: | ||
logger.exception(e) | ||
return self.secondary.get_similar_tasks(query, score_threshold) | ||
|
||
def reset_class(self) -> None: | ||
try: | ||
self.primary.reset_class() | ||
except Exception as e: | ||
logger.exception(e) | ||
self.secondary.reset_class() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import weaviate | ||
|
||
auth = ( | ||
weaviate.auth.AuthApiKey(api_key="KNaObeDhRVRaI488QkEoprZ3LriotjRIo6Rg") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ... you get the gorilla There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. deleted 💥 |
||
) | ||
|
||
client = weaviate.Client("https://zgjbgueysdoxesgb7f8esa.gcp-d.weaviate.cloud", auth) | ||
|
||
|
||
def _default_schema(index_name: str, text_key: str): | ||
return { | ||
"class": index_name, | ||
"properties": [ | ||
{ | ||
"name": text_key, | ||
"dataType": ["text"], | ||
} | ||
], | ||
} | ||
|
||
|
||
schema = _default_schema("testytest", "testytest") | ||
client.schema.create_class(schema) | ||
|
||
schema = client.schema.get() | ||
print(schema) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤯