From bfd18d686226063d9c69eb5e4eeed4ab61a5a7ae Mon Sep 17 00:00:00 2001 From: daniel Foley Date: Tue, 26 Nov 2024 21:18:02 -0500 Subject: [PATCH] Initial sample chainlit --- Dockerfile | 111 ++++++++++++++++++++ app.py | 265 +++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 9 ++ 3 files changed, 385 insertions(+) create mode 100644 Dockerfile create mode 100644 app.py create mode 100644 requirements.txt diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..fc51977 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,111 @@ +FROM python:3.9 + + + + + + + +# Create a non-root user + + + +RUN useradd -m -u 1000 user + + + +USER user + + + + + + + +# Set PATH to include user's local bin + + + +ENV PATH="/home/user/.local/bin:$PATH" + + + + + + + +# Set working directory + + + +WORKDIR /app + + + + + + + +# Copy requirements file with appropriate ownership + + + +COPY --chown=user ./requirements.txt requirements.txt + + + + + + + +# Install dependencies + + + +RUN pip install --no-cache-dir --upgrade -r requirements.txt + + + + + + + +# Copy application files with appropriate ownership + + + +COPY --chown=user . /app + + + + + + + +# Set environment variables for Chainlit + + + +ENV HOST=0.0.0.0 + + + +ENV PORT=7860 + + + +ENV CHAINLIT_SERVER_PORT=7860 + + + +ENV CHAINLIT_HOST="0.0.0.0" + + + + + + + +# Change the CMD to use chainlit + +CMD ["chainlit", "run", "app.py"] diff --git a/app.py b/app.py new file mode 100644 index 0000000..87e868a --- /dev/null +++ b/app.py @@ -0,0 +1,265 @@ +import chainlit as cl + + + +from typing import Optional + + + +import time + + + + + + + +# Store conversation history + + + +conversation_memory = [] + + + + + + + +@cl.on_chat_start + + + +async def start(): + + + + """Initializes the chat session""" + + + + # Send an initial message + + + + await cl.Message( + + + + content="👋 Hello! I'm your AI assistant. How can I help you today?", + + + + author="Assistant" + + + + ).send() + + + + + + + + # Set some session variables + + + + cl.user_session.set("conversation_started", True) + + + + + + + +@cl.on_message + + + +async def main(message: cl.Message): + + + + """Main message handler""" + + + + + + + + # Simulate some processing time + + + + with cl.Step("Processing...") as step: + + + + time.sleep(1) # Simulated delay + + + + step.output = "Processed message" + + + + + + + + # Store message in conversation history + + + + conversation_memory.append({ + + + + "role": "user", + + + + "content": message.content + + + + }) + + + + + + + + # Create a response + + + + response = f"I received your message: '{message.content}'. This is a demo response." + + + + + + + + # Store response in conversation history + + + + conversation_memory.append({ + + + + "role": "assistant", + + + + "content": response + + + + }) + + + + + + + + # Send response with typing effect + + + + await cl.Message( + + + + content=response, + + + + author="Assistant" + + + + ).send() + + + + + + + +@cl.password_auth_callback + + + +def auth_callback(username: str, password: str) -> Optional[cl.User]: + + + + """Basic authentication handler""" + + + + # This is a simple example - in production, use proper authentication + + + + if username == "demo" and password == "password": + + + + return cl.User(identifier="demo", metadata={"role": "user"}) + + + + return None + + + + + + + +@cl.on_chat_end + + + +async def end(): + + + + """Cleanup when chat ends""" + + + + await cl.Message(content="👋 Thank you for chatting! Goodbye!").send() + + + + + + + +# Custom action handler example + + + +@cl.action_callback("feedback") + + + +async def on_action(action): + + + + """Handles custom feedback action""" + + + + await cl.Message(content=f"Received feedback: {action.value}").send() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..9f1f047 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,9 @@ +fastapi + + + +uvicorn[standard] + + + +chainlit