-
Notifications
You must be signed in to change notification settings - Fork 7
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 function to retrieve actions given context for general agent #616
base: main
Are you sure you want to change the base?
Conversation
WalkthroughThis pull request introduces significant enhancements to the memory management system in the prediction market agent. The changes focus on improving memory retrieval capabilities by renaming the existing Changes
Sequence DiagramsequenceDiagram
participant Agent
participant MemoryFunctions
participant ChromaVectorStore
participant OpenAIEmbeddings
Agent->>MemoryFunctions: Request past actions
MemoryFunctions->>ChromaVectorStore: Perform similarity search
ChromaVectorStore->>OpenAIEmbeddings: Generate embeddings
OpenAIEmbeddings-->>ChromaVectorStore: Return embeddings
ChromaVectorStore-->>MemoryFunctions: Return relevant memories
MemoryFunctions-->>Agent: Provide context-specific memories
Possibly related PRs
Suggested reviewers
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
prediction_market_agent/agents/microchain_agent/memory_functions.py
(2 hunks)prediction_market_agent/agents/microchain_agent/microchain_agent.py
(2 hunks)prediction_market_agent/agents/utils.py
(1 hunks)tests/agents/microchain/test_functions.py
(3 hunks)
🔇 Additional comments (8)
prediction_market_agent/agents/microchain_agent/memory_functions.py (2)
19-27
: 'LongTermMemoryBasedFunction' class is correctly implemented
The base class initializes long_term_memory
and model
attributes appropriately and calls the superclass __init__
method.
Line range hint 28-53
: 'LookAtPastActionsFromLastDay' retrieves past actions effectively
The implementation correctly fetches memories from the past 25 hours and processes them into learnings.
prediction_market_agent/agents/utils.py (2)
35-35
: Clarify terminology by changing 'Trading Session' to 'Session'
The modification in the template enhances clarity and aligns terminology with the context of the memories.
Line range hint 167-175
: Improved error handling in 'get_event_date_from_question'
Adding a try-except
block to handle ValueError
and logging a warning enhances the robustness of the function.
tests/agents/microchain/test_functions.py (2)
Line range hint 167-179
: Update test function to use 'LookAtPastActionsFromLastDay'
The test now accurately reflects the updated class name and ensures continued test coverage.
186-210
: Add new test for 'CheckAllPastActionsGivenContext'
The added test function effectively verifies the functionality of retrieving past actions based on context.
prediction_market_agent/agents/microchain_agent/microchain_agent.py (2)
45-45
: Import 'MEMORY_FUNCTIONS' for modular memory management
Replacing individual memory function imports with 'MEMORY_FUNCTIONS' enhances modularity and maintainability.
174-175
: Integrate memory functions dynamically in 'build_agent_functions'
Extending the functions
list with instances from 'MEMORY_FUNCTIONS' allows for flexible inclusion of memory-related functionalities.
class CheckAllPastActionsGivenContext(LongTermMemoryBasedFunction): | ||
@property | ||
def description(self) -> str: | ||
return ( | ||
"Use this function to fetch information about the actions you executed with respect to a specific context. " | ||
"For example, you can use this function to look into all your past actions if you ever did form a coalition with another agent." | ||
) | ||
|
||
@property | ||
def example_args(self) -> list[str]: | ||
return ["What coalitions did I form?"] | ||
|
||
def __call__(self, context: str) -> str: | ||
keys = MicrochainAgentKeys() | ||
all_memories = self.long_term_memory.search() | ||
|
||
collection = Chroma( | ||
embedding_function=OpenAIEmbeddings( | ||
api_key=keys.openai_api_key_secretstr_v1 | ||
) | ||
) | ||
collection.add_texts( | ||
texts=[ | ||
f"From: {check_not_none(x.metadata_dict)['role']} Content: {check_not_none(x.metadata_dict)['content']}" | ||
for x in all_memories | ||
], | ||
metadatas=[{"json": x.model_dump_json()} for x in all_memories], | ||
) | ||
|
||
top_k_per_query_results = collection.similarity_search(context, k=50) | ||
results = [ | ||
DatedChatMessage.from_long_term_memory( | ||
LongTermMemories.model_validate_json(x.metadata["json"]) | ||
) | ||
for x in top_k_per_query_results | ||
] | ||
|
||
return memories_to_learnings(memories=results, model=self.model) | ||
|
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.
🛠️ Refactor suggestion
Add error handling for potential missing metadata keys
In the __call__
method of CheckAllPastActionsGivenContext
, accessing x.metadata_dict['role']
, x.metadata_dict['content']
, and x.metadata['json']
may raise exceptions if these keys are missing or if the metadata is not as expected. To prevent runtime errors, consider adding checks or exception handling to ensure these keys exist before accessing them.
No description provided.