Redis Vector Storage Adapter

Note

(March, 2025): The RedisVectorStorageAdapter is a new and experimental feature introduced as a “beta” feature. Its functionality is subject to change in future releases.

The Redis Vector Storage Adapter allows a ChatterBot instance to store and retrieve text and metadata using a Redis instance configured as a vector database. This adapter supports the use of vectors when filtering queries to search for similar text.

Vectors are a mathematical representation of text that can be used to calculate the similarity between two pieces of text based on the distance between their vectors. This allows for more accurate search results when looking for similar text because the context of the text can be taken into account.

For example, consider the following words:

        (Speaking)
            ●
           / \
          /   \
(Poetry) ●-----● (Rhyming)
          \   /
           \ /
            ●
        (Writing)

The acts of “speaking” and “writing” are both forms of communication, so they are included in the same cluster, but they are somewhat opposite to each other. Both “poetry” and “rhyming” closely related, and in some cases might possibly be used as synonyms within the context of either types of speech or types of writing.

Redis Setup

Before you use the RedisVectorStorageAdapter you will need to install the dependencies required for Redis and generating vectors. This can be done using the chatterbot[redis] extra when installing ChatterBot. For example:

pip install chatterbot[redis]

You will also need to have a Redis server running, with the additional modules installed that enable searching using vectors. And easy way to run one locally is to use Docker:

docker-compose.yml
version: "3.8"

services:
  redis:
    # Use the latest version of the redis-stack image
    image: redis/redis-stack-server:latest
    # Expose the default Redis port
    ports:
      - "6379:6379"
    # Persist the Redis data
    volumes:
      - ./.database/redis/:/data

To start the Redis container, run:

docker compose up -d

Likewise, you can run docker compose ps to review the status of your container, and docker compose down to stop it. For more information on Docker and docker compose, see the Docker Compose documentation.

Redis Configuration

To use the RedisVectorStorageAdapter you will need to provide the following argument when configuring your ChatterBot instance:

from chatterbot import ChatBot

chatbot = ChatBot(
      'Redis Bot',
      storage_adapter='chatterbot.storage.RedisVectorStorageAdapter,
      # Optional: Override the default Redis URI
      # database_uri='redis://localhost:6379/0'
)

Class Attributes

class chatterbot.storage.RedisVectorStorageAdapter(**kwargs)[source]

Warning

BETA feature (Released March, 2025): this storage adapter is new and experimental. Its functionality and default parameters might change in the future and its behavior has not yet been finalized.

The RedisVectorStorageAdapter allows ChatterBot to store conversation data in a redis instance.

All parameters are optional, by default a redis instance on localhost is assumed.

Parameters:

database_uri (str) – eg: redis://localhost:6379/0’, The database_uri can be specified to choose a redis instance.

class RedisMetaDataType[source]

Subclass for redis config metadata type enumerator.

count()[source]

Return the number of entries in the database.

create(text, in_response_to=None, tags=None, **kwargs)[source]

Creates a new statement matching the keyword arguments specified. Returns the created statement.

create_many(statements)[source]

Creates multiple statement entries.

drop()[source]

Remove all existing documents from the database.

filter(page_size=4, **kwargs)[source]

Returns a list of objects from the database. The kwargs parameter can contain any number of attributes. Only objects which contain all listed attributes and in which all values match for all listed attributes will be returned.

kwargs:
  • conversation

  • persona

  • tags

  • in_response_to

  • text

  • exclude_text

  • exclude_text_words

  • persona_not_startswith

  • search_in_response_to_contains

  • order_by

get_random()[source]

Returns a random statement from the database.

get_statement_model()[source]

Return the statement model.

remove(statement)[source]

Removes the statement that matches the input text. Removes any responses from statements where the response text matches the input text.

update(statement)[source]

Modifies an entry in the database. Creates an entry if one does not exist.

More on Vector Databases

For those looking to learn more about vector databases, the following resources can be good starting points:

Vector Database Learning Resources

Article

Link

What is a vector database?

https://www.mongodb.com/resources/basics/databases/vector-databases

Why use a vector database?

https://stackoverflow.blog/2023/09/20/do-you-need-a-specialized-vector-database-to-implement-vector-search-well/

How to choose a vector database?

https://learn.microsoft.com/en-us/azure/cosmos-db/mongodb/vcore/vector-search-ai

Redis as a vector database

https://redis.io/docs/latest/develop/interact/search-and-query/advanced-concepts/vectors/