chainlit 2.0 试用

就是对于chanlit 2.0 的一个试用,测试下对于sqlite 的支持

环境准备

  • docker-compose

主要是部署minio s3,可选的

services:
   minio:
      image: minio/minio
      command: server /data --console-address ":19000"
      ports:
        - "9000:9000"
        - "19000:19000"
      environment:
      - MINIO_ACCESS_KEY=minio
      - MINIO_SECRET_KEY=minio123
  • app.py
import chainlit as cl

from chainlit.data.sql_alchemy import SQLAlchemyDataLayer
from chainlit.data.storage_clients.s3 import S3StorageClient

@cl.data_layer
def get_data_layer():
    return SQLAlchemyDataLayer(conninfo="sqlite+aiosqlite:///database.db",storage_provider=S3StorageClient("demo"))


@cl.set_chat_profiles
async def chat_profile(current_user: cl.User):
    if current_user.metadata["role"] != "ADMIN":
        return None

    return [
        cl.ChatProfile(
            name="My Chat Profile",
            icon="https://picsum.photos/250",
            markdown_description="The underlying LLM model is **GPT-3.5**, a *175B parameter model* trained on 410GB of text data.",
            starters=[
                cl.Starter(
                    label="Morning routine ideation",
                    message="Can you help me create a personalized morning routine that would help increase my productivity throughout the day? Start by asking me about my current habits and what activities energize me in the morning.",
                    icon="/public/logo_light.png",
                ),
                cl.Starter(
                    label="Explain superconductors",
                    message="Explain superconductors like I'm five years old.",
                    icon="/public/logo_light.png",
                ),
            ],
        )
    ]

@cl.set_starters
async def set_starters():
    return [
        cl.Starter(
            label="Morning routine ideation",
            message="Can you help me create a personalized morning routine that would help increase my productivity throughout the day? Start by asking me about my current habits and what activities energize me in the morning.",
            icon="/public/logo_light.png",
            ),

        cl.Starter(
            label="Explain superconductors",
            message="Explain superconductors like I'm five years old.",
            icon="/public/logo_light.png",
            ),
        cl.Starter(
            label="Python script for daily email reports",
            message="Write a script to automate sending daily email reports in Python, and walk me through how I would set it up.",
            icon="/public/logo_light.png",
            ),
        cl.Starter(
            label="Text inviting friend to wedding",
            message="Write a text asking a friend to be my plus-one at a wedding next month. I want to keep it super short and casual, and offer an out.",
            icon="/public/logo_light.png",
            )
        ]

@cl.password_auth_callback
def auth_callback(username: str, password: str):
    if (username, password) == ("admin", "admin"):
        return cl.User(
            identifier="admin", metadata={"role": "admin", "provider": "credentials"}
        )
    else:
        return None
    
@cl.on_message
async def main(message: cl.Message):
    await cl.Message(
        content=f"Received: {message.content}",
    ).send()
  • .evn 配置

主要是s3 以及 secret key 的,结合自己的实际环境调整

export AWS_ENDPOINT_URL=http://localhost:9000
export AWS_ACCESS_KEY_ID=minio
export AWS_SECRET_ACCESS_KEY=minio123
CHAINLIT_AUTH_SECRET="ua0S.rlmAdizHHVG.NaRA@yW3:yO6LT3520PDMqNl4EQR$GzqS9bi$,9NMU_w,w8"
  • 效果

说明

支持sqlite 的集成是很方便的功能, 可以简化对于外部db的依赖,同时目前新版本也解决了以前sql ddl 的问题

参考资料

https://github.com/Chainlit/chainlit

https://github.com/rongfengliang/chainlit-learning

posted on 2025-04-25 08:00  荣锋亮  阅读(97)  评论(0)    收藏  举报

导航