|
| 1 | +--- |
| 2 | +title: DynamoDB Data Layer |
| 3 | +--- |
| 4 | + |
| 5 | +This data layer also supports the `BaseStorageClient` that enables you to store your elements into AWS S3 or Azure Blob Storage. |
| 6 | + |
| 7 | +## Example |
| 8 | +Here is an example of setting up this data layer. First install boto3: |
| 9 | +```bash |
| 10 | +pip install boto3 |
| 11 | +``` |
| 12 | + |
| 13 | +Import the custom data layer and storage client, and set the `cl_data._data_layer` variable at the beginning of your Chainlit app. |
| 14 | + |
| 15 | +```python |
| 16 | +import chainlit.data as cl_data |
| 17 | +from chainlit.data.dynamodb import DynamoDBDataLayer |
| 18 | +from chainlit.data.storage_clients import S3StorageClient |
| 19 | + |
| 20 | +storage_client = S3StorageClient(bucket="<Your Bucket>") |
| 21 | + |
| 22 | +cl_data._data_layer = DynamoDBDataLayer(table_name="<Your Table>", storage_provider=storage_client) |
| 23 | +``` |
| 24 | + |
| 25 | +## Table structure |
| 26 | + |
| 27 | +Here is the Cloudformation used to create the dynamo table: |
| 28 | +```json |
| 29 | +{ |
| 30 | + "AWSTemplateFormatVersion": "2010-09-09", |
| 31 | + "Resources": { |
| 32 | + "DynamoDBTable": { |
| 33 | + "Type": "AWS::DynamoDB::Table", |
| 34 | + "Properties": { |
| 35 | + "TableName": "<YOUR-TABLE-NAME>", |
| 36 | + "AttributeDefinitions": [ |
| 37 | + { |
| 38 | + "AttributeName": "PK", |
| 39 | + "AttributeType": "S" |
| 40 | + }, |
| 41 | + { |
| 42 | + "AttributeName": "SK", |
| 43 | + "AttributeType": "S" |
| 44 | + }, |
| 45 | + { |
| 46 | + "AttributeName": "UserThreadPK", |
| 47 | + "AttributeType": "S" |
| 48 | + }, |
| 49 | + { |
| 50 | + "AttributeName": "UserThreadSK", |
| 51 | + "AttributeType": "S" |
| 52 | + } |
| 53 | + ], |
| 54 | + "KeySchema": [ |
| 55 | + { |
| 56 | + "AttributeName": "PK", |
| 57 | + "KeyType": "HASH" |
| 58 | + }, |
| 59 | + { |
| 60 | + "AttributeName": "SK", |
| 61 | + "KeyType": "RANGE" |
| 62 | + } |
| 63 | + ], |
| 64 | + "GlobalSecondaryIndexes": [ |
| 65 | + { |
| 66 | + "IndexName": "UserThread", |
| 67 | + "KeySchema": [ |
| 68 | + { |
| 69 | + "AttributeName": "UserThreadPK", |
| 70 | + "KeyType": "HASH" |
| 71 | + }, |
| 72 | + { |
| 73 | + "AttributeName": "UserThreadSK", |
| 74 | + "KeyType": "RANGE" |
| 75 | + } |
| 76 | + ], |
| 77 | + "Projection": { |
| 78 | + "ProjectionType": "INCLUDE", |
| 79 | + "NonKeyAttributes": ["id", "name"] |
| 80 | + } |
| 81 | + } |
| 82 | + ], |
| 83 | + "BillingMode": "PAY_PER_REQUEST" |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +## Logging |
| 91 | + |
| 92 | +DynamoDB data layer defines a child of chainlit logger. |
| 93 | + |
| 94 | +```python |
| 95 | +import logging |
| 96 | +from chainlit import logger |
| 97 | + |
| 98 | +logger.getChild("DynamoDB").setLevel(logging.DEBUG) |
| 99 | +``` |
| 100 | + |
| 101 | +## Limitations |
| 102 | +Filtering by positive/negative feedback is not supported. |
| 103 | + |
| 104 | +The data layer methods are not async. Boto3 is not async and therefore the data layer uses non-async blocking io. |
| 105 | + |
| 106 | +## Design |
| 107 | + |
| 108 | +This implementation uses Single Table Design. There are 4 different entity types in one table identified by the prefixes in PK & SK. |
| 109 | + |
| 110 | +Here are the entity types: |
| 111 | +```ts |
| 112 | +type User = { |
| 113 | + PK: "USER#{user.identifier}" |
| 114 | + SK: "USER" |
| 115 | + // ...PersistedUser |
| 116 | +} |
| 117 | + |
| 118 | +type Thread = { |
| 119 | + PK: f"THREAD#{thread_id}" |
| 120 | + SK: "THREAD" |
| 121 | + // GSI: UserThread for querying in list_threads |
| 122 | + UserThreadPK: f"USER#{user_id}" |
| 123 | + UserThreadSK: f"TS#{ts}" |
| 124 | + // ...ThreadDict |
| 125 | +} |
| 126 | + |
| 127 | +type Step = { |
| 128 | + PK: f"THREAD#{threadId}" |
| 129 | + SK: f"STEP#{stepId}" |
| 130 | + // ...StepDict |
| 131 | + |
| 132 | + // feedback is stored as part of step. |
| 133 | + // NOTE: feedback.value is stored as Decimal in dynamo which is not json serializable |
| 134 | + feedback?: Feedback |
| 135 | +} |
| 136 | + |
| 137 | +type Element = { |
| 138 | + "PK": f"THREAD#{threadId}" |
| 139 | + "SK": f"ELEMENT#{element.id}" |
| 140 | + // ...ElementDict |
| 141 | +} |
| 142 | +``` |
0 commit comments