Skip to content

Commit

Permalink
Fix history from long term memory (#644)
Browse files Browse the repository at this point in the history
  • Loading branch information
kongzii authored Jan 17, 2025
1 parent 43e22f4 commit c78d4da
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
29 changes: 26 additions & 3 deletions prediction_market_agent/agents/microchain_agent/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ def build_agent(self, market_type: MarketType) -> Agent:
unformatted_system_prompt=unformatted_system_prompt,
allow_stop=True,
long_term_memory=self.long_term_memory,
import_actions_from_memory=self.import_actions_from_memory,
keys=APIKeys(),
functions_config=self.functions_config,
enable_langfuse=self.enable_langfuse,
Expand Down Expand Up @@ -107,6 +106,27 @@ def run(
"""
self.run_general_agent(market_type=market_type)

def initialise_agent(self) -> None:
self.agent.reset()
self.agent.build_initial_messages()

# Inject past history if wanted.
if self.import_actions_from_memory:
latest_saved_memories = check_not_none(
self.long_term_memory,
"long_term_memory is needed for this functionality.",
).search(limit=self.import_actions_from_memory)
messages_to_insert = [
m.metadata_dict
for m in latest_saved_memories[
::-1
] # Revert the list to have the oldest messages first, as they were in the history.
if check_not_none(m.metadata_dict)["role"]
!= "system" # Do not include system message as that one is automatically in the beginning of the history.
]
# Inject them after the system message.
self.agent.history[1:1] = messages_to_insert

@observe()
def run_general_agent(self, market_type: MarketType) -> None:
if market_type != MarketType.OMEN:
Expand All @@ -118,6 +138,9 @@ def run_general_agent(self, market_type: MarketType) -> None:
if goal:
self.agent.prompt = goal.to_prompt()

# Initialise the agent before our working loop.
self.initialise_agent()

# Save formatted system prompt
initial_formatted_system_prompt = self.agent.system_prompt

Expand All @@ -130,8 +153,8 @@ def run_general_agent(self, market_type: MarketType) -> None:

starting_history_length = len(self.agent.history)
try:
# After the first iteration, resume=True to not re-initialize the agent.
self.agent.run(iterations=1, resume=iteration > 0)
# We initialise agent manually because of inserting past history, so force resume=True, to not re-initialise it which would remove the history.
self.agent.run(iterations=1, resume=True)
except Exception as e:
logger.error(f"Error while running microchain agent: {e}")
raise e
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
)
from microchain.functions import Reasoning, Stop
from prediction_market_agent_tooling.markets.markets import MarketType
from prediction_market_agent_tooling.tools.utils import (
check_not_none,
should_not_happen,
)
from prediction_market_agent_tooling.tools.utils import should_not_happen

from prediction_market_agent.agents.microchain_agent.agent_functions import (
AGENT_FUNCTIONS,
Expand Down Expand Up @@ -191,7 +188,6 @@ def build_agent(
enable_langfuse: bool,
api_base: str = "https://api.openai.com/v1",
long_term_memory: LongTermMemoryTableHandler | None = None,
import_actions_from_memory: int = 0,
max_tokens: int = 8196,
allow_stop: bool = True,
bootstrap: str | None = None,
Expand Down Expand Up @@ -240,19 +236,6 @@ def step_end_callback(agent: Agent, step_output: StepOutput) -> None:
enable_langfuse=enable_langfuse,
)

if import_actions_from_memory:
latest_saved_memories = check_not_none(
long_term_memory, "long_term_memory is needed for this functionality."
).search(limit=import_actions_from_memory)
agent.history.extend(
m.metadata_dict
for m in latest_saved_memories[
::-1
] # Revert the list to have the oldest messages first, as they were in the history.
if check_not_none(m.metadata_dict)["role"]
!= "system" # Do not include system message as that one is automatically in the beginning of the history.
)

for f in build_agent_functions(
agent=agent,
market_type=market_type,
Expand Down

0 comments on commit c78d4da

Please sign in to comment.