-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
daniel Foley
committed
Nov 27, 2024
1 parent
848af7c
commit bfd18d6
Showing
3 changed files
with
385 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
fastapi | ||
|
||
|
||
|
||
uvicorn[standard] | ||
|
||
|
||
|
||
chainlit |