camel.memories.blocks package#

Submodules#

camel.memories.blocks.chat_history_block module#

class camel.memories.blocks.chat_history_block.ChatHistoryBlock(storage: BaseKeyValueStorage | None = None, keep_rate: float = 0.9)[source]#

Bases: MemoryBlock

An implementation of the MemoryBlock abstract base class for maintaining a record of chat histories.

This memory block helps manage conversation histories with a key-value storage backend, either provided by the user or using a default in-memory storage. It offers a windowed approach to retrieving chat histories, allowing users to specify how many recent messages they’d like to fetch.

Parameters:
  • storage (BaseKeyValueStorage, optional) – A storage mechanism for storing chat history. If None, an InMemoryKeyValueStorage will be used. (default: None)

  • keep_rate (float, optional) – In historical messages, the score of the last message is 1.0, and with each step taken backward, the score of the message is multiplied by the keep_rate. Higher keep_rate leads to high possiblity to keep history messages during context creation.

clear() None[source]#

Clears all chat messages from the memory.

retrieve(window_size: int | None = None) List[ContextRecord][source]#

Retrieves records with a proper size for the agent from the memory based on the window size or fetches the entire chat history if no window size is specified.

Parameters:

window_size (int, optional) – Specifies the number of recent chat messages to retrieve. If not provided, the entire chat history will be retrieved. (default: None)

Returns:

A list of retrieved records.

Return type:

List[ContextRecord]

write_records(records: List[MemoryRecord]) None[source]#

Writes memory records to the memory. Additionally, performs validation checks on the messages.

Parameters:

records (List[MemoryRecord]) – Memory records to be added to the memory.

camel.memories.blocks.vectordb_block module#

class camel.memories.blocks.vectordb_block.VectorDBBlock(storage: BaseVectorStorage | None = None, embedding: BaseEmbedding | None = None)[source]#

Bases: MemoryBlock

An implementation of the MemoryBlock abstract base class for maintaining and retrieving information using vector embeddings within a vector database.

Parameters:
  • storage (Optional[BaseVectorStorage], optional) – The storage mechanism for the vector database. Defaults to in-memory Qdrant if not provided. (default: None)

  • embedding (Optional[BaseEmbedding], optional) – Embedding mechanism to convert chat messages into vector representations. Defaults to OpenAiEmbedding if not provided. (default: None)

clear() None[source]#

Removes all records from the vector database memory.

retrieve(keyword: str, limit: int = 3) List[ContextRecord][source]#

Retrieves similar records from the vector database based on the content of the keyword.

Parameters:
  • keyword (str) – This string will be converted into a vector representation to query the database.

  • limit (int, optional) – The maximum number of similar messages to retrieve. (default: 3).

Returns:

A list of memory records retrieved from the

vector database based on similarity to current_state.

Return type:

List[ContextRecord]

write_records(records: List[MemoryRecord]) None[source]#

Converts the provided chat messages into vector representations and writes them to the vector database.

Parameters:

records (List[MemoryRecord]) – Memory records to be added to the memory.

Module contents#

class camel.memories.blocks.ChatHistoryBlock(storage: BaseKeyValueStorage | None = None, keep_rate: float = 0.9)[source]#

Bases: MemoryBlock

An implementation of the MemoryBlock abstract base class for maintaining a record of chat histories.

This memory block helps manage conversation histories with a key-value storage backend, either provided by the user or using a default in-memory storage. It offers a windowed approach to retrieving chat histories, allowing users to specify how many recent messages they’d like to fetch.

Parameters:
  • storage (BaseKeyValueStorage, optional) – A storage mechanism for storing chat history. If None, an InMemoryKeyValueStorage will be used. (default: None)

  • keep_rate (float, optional) – In historical messages, the score of the last message is 1.0, and with each step taken backward, the score of the message is multiplied by the keep_rate. Higher keep_rate leads to high possiblity to keep history messages during context creation.

clear() None[source]#

Clears all chat messages from the memory.

retrieve(window_size: int | None = None) List[ContextRecord][source]#

Retrieves records with a proper size for the agent from the memory based on the window size or fetches the entire chat history if no window size is specified.

Parameters:

window_size (int, optional) – Specifies the number of recent chat messages to retrieve. If not provided, the entire chat history will be retrieved. (default: None)

Returns:

A list of retrieved records.

Return type:

List[ContextRecord]

write_records(records: List[MemoryRecord]) None[source]#

Writes memory records to the memory. Additionally, performs validation checks on the messages.

Parameters:

records (List[MemoryRecord]) – Memory records to be added to the memory.

class camel.memories.blocks.VectorDBBlock(storage: BaseVectorStorage | None = None, embedding: BaseEmbedding | None = None)[source]#

Bases: MemoryBlock

An implementation of the MemoryBlock abstract base class for maintaining and retrieving information using vector embeddings within a vector database.

Parameters:
  • storage (Optional[BaseVectorStorage], optional) – The storage mechanism for the vector database. Defaults to in-memory Qdrant if not provided. (default: None)

  • embedding (Optional[BaseEmbedding], optional) – Embedding mechanism to convert chat messages into vector representations. Defaults to OpenAiEmbedding if not provided. (default: None)

clear() None[source]#

Removes all records from the vector database memory.

retrieve(keyword: str, limit: int = 3) List[ContextRecord][source]#

Retrieves similar records from the vector database based on the content of the keyword.

Parameters:
  • keyword (str) – This string will be converted into a vector representation to query the database.

  • limit (int, optional) – The maximum number of similar messages to retrieve. (default: 3).

Returns:

A list of memory records retrieved from the

vector database based on similarity to current_state.

Return type:

List[ContextRecord]

write_records(records: List[MemoryRecord]) None[source]#

Converts the provided chat messages into vector representations and writes them to the vector database.

Parameters:

records (List[MemoryRecord]) – Memory records to be added to the memory.