Skip to content

Commit 26ea235

Browse files
committed
updated image of streamlit and docker compose
1 parent b43050d commit 26ea235

File tree

8 files changed

+72
-16
lines changed

8 files changed

+72
-16
lines changed

create_fastapi_project/templates/langchain_basic/Makefile

-3
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@ run-prod:
5151
stop-prod:
5252
docker compose down
5353

54-
run-streamlit:
55-
docker compose -f streamlit.yml up --force-recreate
56-
5754
formatter:
5855
cd app && \
5956
poetry run black app

create_fastapi_project/templates/langchain_basic/backend/app/app/api/v1/endpoints/chat.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
from app.core.config import settings
12
from app.schemas.message_schema import (
23
IChatResponse,
3-
IUserMessage,
44
)
55
import logging
66
from app.utils.adaptive_cards.cards import create_adaptive_card
@@ -15,7 +15,7 @@
1515
YoutubeSearchTool,
1616
GeneralWeatherTool,
1717
)
18-
from fastapi import APIRouter, WebSocket
18+
from fastapi import APIRouter, WebSocket, WebSocketDisconnect
1919
from app.utils.uuid6 import uuid7
2020
from langchain.chat_models import ChatOpenAI
2121
from langchain.schema import SystemMessage
@@ -37,6 +37,10 @@
3737
@router.websocket("")
3838
async def websocket_endpoint(websocket: WebSocket):
3939
await websocket.accept()
40+
if not settings.OPENAI_API_KEY.startswith("sk-"):
41+
await websocket.send_json({"error": "OPENAI_API_KEY is not set"})
42+
return
43+
4044
while True:
4145
data = await websocket.receive_json()
4246
user_message = data["message"]
@@ -86,6 +90,10 @@ async def websocket_endpoint(websocket: WebSocket):
8690
async def websocket_endpoint(websocket: WebSocket):
8791
await websocket.accept()
8892

93+
if not settings.OPENAI_API_KEY.startswith("sk-"):
94+
await websocket.send_json({"error": "OPENAI_API_KEY is not set"})
95+
return
96+
8997
while True:
9098
try:
9199
data = await websocket.receive_json()

create_fastapi_project/templates/langchain_basic/backend/app/app/main.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
from fastapi import (
2-
FastAPI,
3-
)
1+
from fastapi import FastAPI, HTTPException
42
from fastapi.responses import HTMLResponse
53
from app.api.v1.api import api_router as api_router_v1
64
from app.core.config import settings
@@ -37,6 +35,11 @@ async def root():
3735

3836
@app.get("/chat", response_class=HTMLResponse)
3937
async def chat():
38+
if not settings.OPENAI_API_KEY.startswith("sk-"):
39+
raise HTTPException(
40+
status_code=500, detail="OPENAI_API_KEY is not set or not start with sk-"
41+
)
42+
4043
return chat_html
4144

4245

create_fastapi_project/templates/langchain_basic/caddy/Caddyfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
}
44

5-
fastapi.{$EXT_ENDPOINT1}:80, fastapi.{$LOCAL_1}:80, fastapi.{$LOCAL_2}:80, :80 {
5+
fastapi.{$EXT_ENDPOINT1}:80, fastapi.{$LOCAL_1}:80, fastapi.{$LOCAL_2}:80 {
66
reverse_proxy fastapi_server:8000
77
}
88

create_fastapi_project/templates/langchain_basic/docker-compose-dev.yml

+11-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,17 @@ services:
1010
- ./backend/app:/code
1111
expose:
1212
- 8000
13-
ports:
14-
- 8000:8000
13+
env_file: ".env"
14+
15+
streamlit_frontend:
16+
container_name: streamlit_frontend
17+
build: ./frontend
18+
restart: always
19+
volumes:
20+
- ./frontend/app:/code
21+
expose:
22+
- 8501
23+
1524
env_file: ".env"
1625

1726
caddy_reverse_proxy:

create_fastapi_project/templates/langchain_basic/docker-compose.yml

+33-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,41 @@ services:
66
restart: always
77
command: "sh -c 'gunicorn -w 3 -k uvicorn.workers.UvicornWorker app.main:app --bind 0.0.0.0:8000 --preload --log-level=debug --timeout 120'"
88
volumes:
9-
- ./app:/code
9+
- ./backend/app:/code
1010
expose:
1111
- 8000
1212
ports:
1313
- 8000:8000
1414
env_file: ".env"
15+
16+
streamlit_frontend:
17+
container_name: streamlit_frontend
18+
build: ./frontend
19+
restart: always
20+
volumes:
21+
- ./frontend/app:/code
22+
expose:
23+
- 8501
24+
25+
env_file: ".env"
26+
27+
caddy_reverse_proxy:
28+
container_name: caddy_reverse_proxy
29+
image: caddy:alpine
30+
restart: always
31+
ports:
32+
- 80:80
33+
- 443:443
34+
environment:
35+
- EXT_ENDPOINT1=${EXT_ENDPOINT1}
36+
- LOCAL_1=${LOCAL_1}
37+
- LOCAL_2=${LOCAL_2}
38+
volumes:
39+
- ./caddy/Caddyfile:/etc/caddy/Caddyfile
40+
- ./static:/code/static
41+
- caddy_data:/data
42+
- caddy_config:/config
43+
44+
volumes:
45+
caddy_data:
46+
caddy_config:
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
FROM python:3.10-slim
22
ENV PYTHONUNBUFFERED=1
3-
WORKDIR /app
3+
WORKDIR /code
44
# Install Poetry
55
RUN apt clean && apt update && apt install curl -y
66
RUN curl -sSL https://install.python-poetry.org | POETRY_HOME=/opt/poetry python && \
@@ -9,10 +9,13 @@ RUN curl -sSL https://install.python-poetry.org | POETRY_HOME=/opt/poetry python
99
poetry config virtualenvs.create false
1010

1111
# Copy poetry.lock* in case it doesn't exist in the repo
12-
COPY app/pyproject.toml app/poetry.lock* /app/
12+
COPY app/pyproject.toml app/poetry.lock* /code/
1313

14+
ENV PYTHONPATH=/code
1415
EXPOSE 8501
1516

16-
HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health
17+
ARG INSTALL_DEV=false
18+
RUN bash -c "if [ $INSTALL_DEV == 'true' ] ; then poetry install --no-root ; else poetry install --no-root --no-dev ; fi"
1719

18-
ENTRYPOINT ["streamlit", "run", "streamlit_app.py", "--server.port=8501", "--server.address=0.0.0.0"]
20+
21+
ENTRYPOINT ["streamlit", "run", "main.py", "--server.port=8501", "--server.address=0.0.0.0"]

create_fastapi_project/templates/langchain_basic/frontend/app/streamlit_app.py renamed to create_fastapi_project/templates/langchain_basic/frontend/app/main.py

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ async def retrieve_bot_response(text):
2121
response = await asyncio.wait_for(websocket.recv(), timeout=20)
2222
response = json.loads(response)
2323

24+
if "error" in response:
25+
stream_data = response["error"]
26+
break
27+
2428
if response["sender"] == "bot":
2529
stream_data = (
2630
response["message"]["body"][0]["items"][0]["text"]

0 commit comments

Comments
 (0)