-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,31 @@ | ||
from datetime import timedelta | ||
|
||
from langchain.vectorstores.chroma import Chroma | ||
from langchain_openai import OpenAIEmbeddings | ||
from microchain import Function | ||
from prediction_market_agent_tooling.tools.utils import utcnow | ||
from prediction_market_agent_tooling.tools.utils import check_not_none, utcnow | ||
|
||
from prediction_market_agent.agents.microchain_agent.memory import DatedChatMessage | ||
from prediction_market_agent.agents.microchain_agent.microchain_agent_keys import ( | ||
MicrochainAgentKeys, | ||
) | ||
from prediction_market_agent.agents.utils import memories_to_learnings | ||
from prediction_market_agent.db.long_term_memory_table_handler import ( | ||
LongTermMemories, | ||
LongTermMemoryTableHandler, | ||
) | ||
|
||
|
||
class LookAtPastActions(Function): | ||
class LongTermMemoryBasedFunction(Function): | ||
def __init__( | ||
self, long_term_memory: LongTermMemoryTableHandler, model: str | ||
) -> None: | ||
self.long_term_memory = long_term_memory | ||
self.model = model | ||
super().__init__() | ||
|
||
|
||
class LookAtPastActionsFromLastDay(LongTermMemoryBasedFunction): | ||
@property | ||
def description(self) -> str: | ||
return ( | ||
|
@@ -42,3 +50,49 @@ def __call__(self) -> str: | |
DatedChatMessage.from_long_term_memory(ltm) for ltm in memories | ||
] | ||
return memories_to_learnings(memories=simple_memories, model=self.model) | ||
|
||
|
||
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) | ||
|
||
Comment on lines
+55
to
+93
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. 🛠️ Refactor suggestion Add error handling for potential missing metadata keys In the |
||
|
||
MEMORY_FUNCTIONS: list[type[LongTermMemoryBasedFunction]] = [ | ||
LookAtPastActionsFromLastDay, | ||
CheckAllPastActionsGivenContext, | ||
] |
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.
Those kinds of things are obtained for free if a more mature framework (Autogen, crewAI, etc) are used. This is basically a memory solution, for which there are specialized solutions (like Mem0 - https://docs.mem0.ai/features/selective-memory), integrated already with those other tools.
This is the correct approach (that you implemented), but leaving this comment here if we decide to refactor at some point.
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.
I never used Mem0, but in that link, it's doing the exact opposite of what's required here -- storing only certain stuff according to prompt instead of retrieving only certain stuff according to the prompt (we need to save everything).
When I started in development, many courses warned people that
"grass is always greener on the neighborhood's side"
. I think that's especially true right now in this field -- there is always yet another tool that could be used and seems to solve all problems "for free". But then there is a ton of other problems to solve.So here I just took
Chroma
that we already use on other places. It's also described as"Chroma is the open-source AI application database. Chroma makes it easy to build LLM apps by making knowledge, facts, and skills pluggable for LLMs."
But that being said, I'm in for testing out new stuff if one wants, as long as it doesn't require re-writing or ditching of big parts of what we already have and it's working 😄