Custom Data Layer
The BaseDataLayer
class serves as an abstract foundation for data persistence operations within the Chainlit framework. This class outlines methods for managing users, feedback, elements, steps, and threads in a chatbot application.
Methods
Fetches a user by their identifier. Return type is optionally a PersistedUser
.
Creates a new user based on the User
instance provided. Return type is optionally a PersistedUser
.
Inserts or updates feedback. Accepts a Feedback
instance and returns a string as an identifier of the persisted feedback.
Deletes a feedback by feedback_id
. Return True
if it was successful.
Adds a new element to the data layer. Accepts ElementDict
as an argument.
Retrieves an element by thread_id
and element_id
. Return type is optionally an ElementDict
.
Deletes an element given its identifier element_id
.
Creates a new step in the data layer. Accepts StepDict
as an argument.
Updates an existing step. Accepts StepDict
as an argument.
Deletes a step given its identifier step_id
.
Fetches the author of a given thread by thread_id
. Returns a string representing the author identifier.
Deletes a thread given its identifier thread_id
.
Lists threads based on pagination
and filters
arguments. Returns a PaginatedResponse[ThreadDict]
.
Retrieves a thread by its identifier thread_id
. Return type is optionally a ThreadDict
.
Updates a thread’s details like name, user_id, metadata, and tags. Arguments are mostly optional.
Deletes a user session given its identifier id
. Returns a boolean value indicating success.
Decorators
Queues certain methods to execute only after the first user message is received, especially useful for WebsocketSessions
.
Example
Due to the abstract nature of BaseDataLayer
, direct instantiation and usage are not practical without subclassing and implementing the abstract methods.
You can refer to the guide for custom data layer implementation.