Custom Data Layer
Literal AI provides the simplest way to persist, analyze and monitor your data.
If you’re considering implementing a custom data layer, check out this example here for some inspiration.
Also, we would absolutely love to see a community-led open source data layer implementation and list it here. If you’re interested in contributing, please reach out to us on Discord.
You need to import you custom data layer in your chainlit app, and assign it to the data layer variable, like so:
SQL alchemy data layer
This custom layer has been tested for PostgreSQL, however it should support more SQL databases thanks to the use of the SQL Alchemy database.
This data layer also supports the BaseStorageClient
that enables you to store your elements into Azure Blob Storage or AWS S3.
Here is the SQL used to create the schema for this data layer:
Example
Here is an example of setting up this data layer on a PostgreSQL database with an Azure storage client. First install the required dependencies:
Import the custom data layer and storage client, and set the cl_data._data_layer
variable at the beginning of your Chainlit app.
Note that you need to add +asyncpg
to the protocol in the conninfo
string so that it uses the asyncpg library.
DynamoDB data layer
This data layer also supports the BaseStorageClient
that enables you to store your elements into AWS S3 or Azure Blob Storage.
Example
Here is an example of setting up this data layer. First install boto3:
Import the custom data layer and storage client, and set the cl_data._data_layer
variable at the beginning of your Chainlit app.
Table structure
Here is the Cloudformation used to create the dynamo table:
Logging
DynamoDB data layer defines a child of chainlit logger.
Limitations
Filtering by positive/negative feedback is not supported.
The data layer methods are not async. Boto3 is not async and therefore the data layer uses non-async blocking io.
Design
This implementation uses Single Table Design. There are 4 different entity types in one table identified by the prefixes in PK & SK.
Here are the entity types:
How to implement a custom data layer?
Follow the reference for an exhaustive list of the methods your custom data layer needs to implement.
Was this page helpful?