Skip to content

Commit

Permalink
Initial sample chainlit
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel Foley committed Nov 27, 2024
1 parent 848af7c commit bfd18d6
Show file tree
Hide file tree
Showing 3 changed files with 385 additions and 0 deletions.
111 changes: 111 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
265 changes: 265 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -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()
9 changes: 9 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
fastapi



uvicorn[standard]



chainlit

0 comments on commit bfd18d6

Please sign in to comment.