|
| 1 | +import dataclasses as dc |
| 2 | +import json |
| 3 | +from typing import Any, Dict, List, Optional, Union |
| 4 | + |
| 5 | +from chainlit.context import ChainlitContext |
| 6 | +from chainlit.data.sql_alchemy import SQLAlchemyDataLayer |
| 7 | +from chainlit.element import ElementDict |
| 8 | +from chainlit.step import StepDict |
| 9 | +from sqlalchemy import ( |
| 10 | + JSON, |
| 11 | + UUID, |
| 12 | + Boolean, |
| 13 | + Column, |
| 14 | + DateTime, |
| 15 | + ForeignKey, |
| 16 | + Integer, |
| 17 | + MetaData, |
| 18 | + String, |
| 19 | + Table, |
| 20 | +) |
| 21 | +from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession, create_async_engine |
| 22 | +from sqlalchemy.orm import sessionmaker |
| 23 | + |
| 24 | +import chainlit as cl |
| 25 | +from chainlit import logger |
| 26 | + |
| 27 | + |
| 28 | +async def build_db(conninfo="sqlite+aiosqlite:///database.db"): |
| 29 | + engine = create_async_engine(conninfo) |
| 30 | + |
| 31 | + metadata_obj = MetaData() |
| 32 | + |
| 33 | + # Create 'users' table |
| 34 | + Table( |
| 35 | + "users", |
| 36 | + metadata_obj, |
| 37 | + Column("id", UUID(as_uuid=True), primary_key=True), |
| 38 | + Column("identifier", String, nullable=False, unique=True), |
| 39 | + Column("metadata", JSON, nullable=False), |
| 40 | + Column( |
| 41 | + "createdAt", |
| 42 | + String, |
| 43 | + ), |
| 44 | + keep_existing=True, |
| 45 | + ) |
| 46 | + |
| 47 | + # Create 'threads' table |
| 48 | + Table( |
| 49 | + "threads", |
| 50 | + metadata_obj, |
| 51 | + Column("id", UUID(as_uuid=True), primary_key=True), |
| 52 | + Column("createdAt", String), |
| 53 | + Column("name", String), |
| 54 | + Column( |
| 55 | + "userId", UUID(as_uuid=True), ForeignKey("users.id", ondelete="CASCADE") |
| 56 | + ), |
| 57 | + Column("userIdentifier", String), |
| 58 | + Column( |
| 59 | + "tags", String |
| 60 | + ), # Changed from ARRAY(String) as SQLite doesn't support array types |
| 61 | + Column("metadata", JSON), |
| 62 | + keep_existing=True, |
| 63 | + ) |
| 64 | + |
| 65 | + Table( |
| 66 | + "steps", |
| 67 | + metadata_obj, |
| 68 | + Column("id", UUID(as_uuid=True), primary_key=True), |
| 69 | + Column("name", String, nullable=False), |
| 70 | + Column("type", String, nullable=False), |
| 71 | + Column("threadId", UUID(as_uuid=True), nullable=False), |
| 72 | + Column("parentId", UUID(as_uuid=True)), |
| 73 | + Column("disableFeedback", Boolean, nullable=False), |
| 74 | + Column("streaming", Boolean, nullable=False), |
| 75 | + Column("waitForAnswer", Boolean), |
| 76 | + Column("isError", Boolean), |
| 77 | + Column("metadata", JSON), |
| 78 | + Column( |
| 79 | + "tags", String |
| 80 | + ), # Changed from ARRAY(String) as SQLite doesn't support array types |
| 81 | + Column("input", String), |
| 82 | + Column("output", String), |
| 83 | + Column("createdAt", String), |
| 84 | + Column("start", DateTime), |
| 85 | + Column("end", DateTime), |
| 86 | + Column("generation", JSON), |
| 87 | + Column("showInput", String), |
| 88 | + Column("language", String), |
| 89 | + Column("indent", Integer), |
| 90 | + keep_existing=True, |
| 91 | + ) |
| 92 | + Table( |
| 93 | + "elements", |
| 94 | + metadata_obj, |
| 95 | + Column("id", UUID(as_uuid=True), primary_key=True), |
| 96 | + Column("threadId", UUID(as_uuid=True)), |
| 97 | + Column("type", String), |
| 98 | + Column("url", String), |
| 99 | + Column("chainlitKey", String), |
| 100 | + Column("name", String, nullable=False), |
| 101 | + Column("display", String), |
| 102 | + Column("objectKey", String), |
| 103 | + Column("size", String), |
| 104 | + Column("page", Integer), |
| 105 | + Column("language", String), |
| 106 | + Column("forId", UUID(as_uuid=True)), |
| 107 | + Column("mime", String), |
| 108 | + keep_existing=True, |
| 109 | + ) |
| 110 | + Table( |
| 111 | + "feedbacks", |
| 112 | + metadata_obj, |
| 113 | + Column("id", UUID(as_uuid=True), primary_key=True), |
| 114 | + Column("forId", UUID(as_uuid=True), nullable=False), |
| 115 | + Column("threadId", UUID(as_uuid=True), nullable=False), |
| 116 | + Column("value", Integer, nullable=False), |
| 117 | + Column("comment", String), |
| 118 | + keep_existing=True, |
| 119 | + ) |
| 120 | + |
| 121 | + async with engine.begin() as conn: |
| 122 | + await conn.run_sync(metadata_obj.create_all) |
| 123 | + |
| 124 | + |
| 125 | +@dc.dataclass |
| 126 | +class DummyChainlitContext(ChainlitContext): |
| 127 | + user: cl.User |
| 128 | + |
| 129 | + def __post_init__(self): |
| 130 | + pass |
| 131 | + |
| 132 | + @property |
| 133 | + def session(self): |
| 134 | + return self |
| 135 | + |
| 136 | + |
| 137 | +class CustomDataLayer(SQLAlchemyDataLayer): |
| 138 | + def __init__( |
| 139 | + self, |
| 140 | + conninfo: str, |
| 141 | + context: ChainlitContext, |
| 142 | + # ssl_require: bool = False, |
| 143 | + # storage_provider: Optional[BaseStorageClient] = None, |
| 144 | + user_thread_limit: Optional[int] = 1000, |
| 145 | + show_logger: Optional[bool] = False, |
| 146 | + ): |
| 147 | + self._conninfo = conninfo |
| 148 | + self.user_thread_limit = user_thread_limit |
| 149 | + self.show_logger = show_logger |
| 150 | + self._context = context |
| 151 | + ssl_args = {} # type: ignore |
| 152 | + |
| 153 | + self.engine: AsyncEngine = create_async_engine( |
| 154 | + self._conninfo, connect_args=ssl_args |
| 155 | + ) |
| 156 | + self.async_session = sessionmaker(bind=self.engine, expire_on_commit=False, class_=AsyncSession) # type: ignore |
| 157 | + |
| 158 | + @property |
| 159 | + def context(self): |
| 160 | + return self._context |
| 161 | + |
| 162 | + @context.setter |
| 163 | + def context(self, context: ChainlitContext): |
| 164 | + self._context = context |
| 165 | + |
| 166 | + async def execute_sql( |
| 167 | + self, query: str, parameters: dict |
| 168 | + ) -> Union[List[Dict[str, Any]], int, None]: |
| 169 | + require_metadata = "metadata" in query or "*" in query |
| 170 | + |
| 171 | + res = await super().execute_sql(query=query, parameters=parameters) |
| 172 | + if not require_metadata or not isinstance(res, list): |
| 173 | + return res |
| 174 | + |
| 175 | + for r in res: |
| 176 | + for key in r.keys(): |
| 177 | + if "metadata" in key: |
| 178 | + r[key] = json.loads(r[key]) if r[key] is not None else None |
| 179 | + |
| 180 | + return res |
| 181 | + |
| 182 | + async def get_thread_author(self, thread_id: str) -> str: |
| 183 | + if self.show_logger: |
| 184 | + logger.info(f"SQLAlchemy: get_thread_author, thread_id={thread_id}") |
| 185 | + query = """SELECT "userIdentifier" FROM threads WHERE "id" = :id""" |
| 186 | + parameters = {"id": thread_id} |
| 187 | + result = await self.execute_sql(query=query, parameters=parameters) |
| 188 | + if isinstance(result, list) and result: |
| 189 | + author_identifier = result[0].get("userIdentifier") |
| 190 | + if author_identifier is not None: |
| 191 | + return author_identifier |
| 192 | + raise ValueError(f"Author not found for thread_id {thread_id}") |
| 193 | + |
| 194 | + async def create_step(self, step_dict: "StepDict"): |
| 195 | + if "disableFeedback" not in step_dict.keys(): |
| 196 | + step_dict["disableFeedback"] = False # type: ignore |
| 197 | + return await super().create_step(step_dict) |
| 198 | + |
| 199 | + async def get_element( |
| 200 | + self, thread_id: str, element_id: str |
| 201 | + ) -> Optional["ElementDict"]: |
| 202 | + pass |
0 commit comments