Skip to content
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

feat: refactor agent memory representation and modify routes for editing blocks #2094

Merged
merged 60 commits into from
Nov 28, 2024

Conversation

sarahwooders
Copy link
Collaborator

@sarahwooders sarahwooders commented Nov 22, 2024

Please describe the purpose of this pull request.
This PR aims to resolve bugs resulting from inconsistent agent state and blocks, as well as simplifying the agent creation REST API route.

Changes to agent state

This PR refactors the schemas related to the agent's state:

  • PersistedAgentState: This is the schema defining what is persisted in the agents table
  • CreateAgent: This is the schema defining the route for agent creation. This PR simplifies is so that memory is created by passing in a list of CreateBlock objects, rather than a Memory object
  • AgentState: This schema contains all the state related to an agent contained in process memory, that is passed into Agent to instantiate an agent. It contains additional objects like the Memory object, sources, tools, and tags - since information for these is also contained in separate tables. We use this to both create the Agent class and also as the return on the client GET for agents.

Update block routes

This PR also cleans up the routes related to blocks:

  • Blocks should primarily be edited via the block_id - the block routes are on /blocks/{block_id}
  • Agent-block routes (e.g. attaching a block to an agent, editing an agen't block) are done via the /agents/blocks/{block_label} route, using the block_label as the reference

Removal of agent caching

We no longer cache agents - agent state must always be read from the DB, and the interface to pass messages to must be passed between functions.

Truncation of error messages

For tool error messages, we add a MAX_ERROR_MESSAGE_CHAR_LIMIT constant to limit the size of the error message passed into the context window.

Is your PR over 500 lines of code?
Yes

Copy link
Collaborator

@mattzh72 mattzh72 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some small bugs left:

FAILED tests/test_memory.py::test_dump_memory_as_json - AttributeError: 'ChatMemory' object has no attribute 'to_dict'
FAILED tests/test_memory.py::test_load_memory_from_json - AttributeError: 'ChatMemory' object has no attribute 'to_dict'
FAILED tests/test_memory.py::test_memory_jinja2_template_load - AttributeError: 'ChatMemory' object has no attribute 'to_dict'
FAILED tests/test_memory.py::test_memory_jinja2_template - AttributeError: 'ChatMemory' object has no attribute 'memory'
FAILED tests/test_memory.py::test_memory_jinja2_set_template - ValueError: Prompt template is not compatible with current memory structure: 'ChatMemory' object has no attribute 'memory'
FAILED tests/test_memory.py::test_link_unlink_block - AttributeError: 'ChatMemory' object has no attribute 'link_block'
FAILED tests/test_memory.py::test_update_block_label - AttributeError: 'ChatMemory' object has no attribute 'update_block_label'. Did you mean: 'update_block_value'?
FAILED tests/test_memory.py::test_update_block_limit - AttributeError: 'ChatMemory' object has no attribute 'update_block_limit'. Did you mean: 'update_block_value'?
FAILED tests/test_summarize.py::test_summarize_messages_inplace - pydantic_core._pydantic_core.ValidationError: 1 validation error for Memory
blocks.0
  Input should be a valid dictionary or object to extract fields from [type=model_attributes_type, input_value=None, input_type=NoneType]
    For further information visit https://errors.pydantic.dev/2.9/v/model_attributes_type
FAILED tests/test_summarize.py::test_auto_summarize - pydantic_core._pydantic_core.ValidationError: 1 validation error for Memory
blocks.0
  Input should be a valid dictionary or object to extract fields from [type=model_attributes_type, input_value=None, input_type=NoneType]
    For further information visit https://errors.pydantic.dev/2.9/v/model_attributes_type

Copy link
Collaborator

@mattzh72 mattzh72 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

overall looks good, some small minor code quality nits, but will probably get cleaned up over time.

@sarahwooders sarahwooders merged commit d30972c into main Nov 28, 2024
27 of 30 checks passed
@sarahwooders sarahwooders deleted the refactor-memory branch November 28, 2024 00:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants