Skip to content

Memory

datapizza.memory.memory.Memory

A class for storing the memory of a chat, organized by conversation turns. Each turn can contain multiple blocks (text, function calls, or structured data).

__bool__

__bool__()

Return True if memory contains any turns, False otherwise.

__delitem__

__delitem__(index)

Delete a specific turn.

__eq__

__eq__(other)

Compare two Memory objects based on their content hash. This is more efficient than comparing the full content structure.

__getitem__

__getitem__(index)

Get all blocks from a specific turn.

__hash__

__hash__()

Creates a deterministic hash based on the content of memory turns.

__iter__

__iter__()

Iterate through all blocks in all turns.

__len__

__len__()

Return the total number of turns.

__repr__

__repr__()

Return a detailed string representation of the memory.

__setitem__

__setitem__(index, value)

Set blocks for a specific turn.

__str__

__str__()

Return a string representation of the memory.

add_to_last_turn

add_to_last_turn(block)

Add a block to the most recent turn. Creates a new turn if memory is empty. Args: block (Block): The block to add to the most recent turn.

add_turn

add_turn(blocks, role)

Add a new conversation turn containing one or more blocks.

Parameters:

Name Type Description Default
blocks list[Block] | Block

The blocks to add to the new turn.

required
role ROLE

The role of the new turn.

required

clear

clear()

Clear all memory.

copy

copy()

Deep copy the memory.

iter_blocks

iter_blocks()

Iterate through blocks.

json_dumps

json_dumps()

Serialize the memory to JSON.

Returns:

Name Type Description
str str

The JSON representation of the memory.

json_loads

json_loads(json_str)

Deserialize JSON to the memory.

Parameters:

Name Type Description Default
json_str str

The JSON string to deserialize.

required

new_turn

new_turn(role=ROLE.ASSISTANT)

Add a new conversation turn.

Parameters:

Name Type Description Default
role ROLE

The role of the new turn. Defaults to ROLE.ASSISTANT.

ASSISTANT

to_dict

to_dict()

Convert memory to a dictionary.

Returns:

Type Description
list[dict]

list[dict]: The dictionary representation of the memory.