Skip to content

Commit 181aca9

Browse files
committed
Adds session management for openweb ui
1 parent 16f813a commit 181aca9

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

‎docker-compose.dev-container.yml‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,21 @@ services:
3939
- PGPASSWORD=talkdai
4040
env_file:
4141
- .env
42+
openwebui:
43+
image: ghcr.io/open-webui/open-webui:main
44+
ports:
45+
- '3000:8080'
46+
environment:
47+
- OPENAI_API_KEYS=FAKE-KEY;
48+
- OPENAI_API_BASE_URLS=http://dialog:8000/openai;
49+
- ENABLE_OPENAI_API=true
50+
volumes:
51+
- open-webui:/app/backend/data
52+
depends_on:
53+
db:
54+
condition: service_healthy
55+
dialog:
56+
condition: service_started
57+
58+
volumes:
59+
open-webui:

‎src/dialog/routers/openai.py‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# *-* coding: utf-8 *-*
2-
from uuid import uuid4
3-
import datetime
2+
import os
43
import logging
4+
import datetime
5+
from uuid import uuid4
56

67
from dialog.db import engine, get_session
78
from dialog_lib.db.models import Chat as ChatEntity, ChatMessages
@@ -44,11 +45,20 @@ async def ask_question_to_llm(message: OpenAIChat, session: Session = Depends(ge
4445
"""
4546
This posts a message to the LLM and returns the response in the OpenAI format.
4647
"""
48+
4749
start_time = datetime.datetime.now()
48-
new_chat = ChatEntity(
49-
session_id = f"openai-{str(uuid4())}",
50-
)
51-
session.add(new_chat)
50+
chat_entity = session.query(ChatEntity).filter(ChatEntity.session_id == Settings().OPENWEB_UI_SESSION).first()
51+
52+
if not chat_entity:
53+
logging.info("Creating new chat entity")
54+
new_chat = ChatEntity(
55+
session_id = Settings().OPENWEB_UI_SESSION,
56+
)
57+
session.add(new_chat)
58+
session.flush()
59+
else:
60+
logging.info("Using old chat entity")
61+
new_chat = chat_entity
5262

5363
non_empty_messages = []
5464

‎src/dialog/settings.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import tomllib
22
import logging
3+
from uuid import uuid4
34
from pathlib import Path
45
from decouple import Csv, Config
56

67
config = Config(".env")
78

89
logger = logging.getLogger(__name__)
910

11+
openweb_ui_session_id_fallback = uuid4()
12+
1013
class Settings:
1114

1215
@property
@@ -113,4 +116,8 @@ def CORS_ALLOW_METHODS(self):
113116

114117
@property
115118
def CORS_ALLOW_HEADERS(self):
116-
return config.get("CORS_ALLOW_HEADERS", cast=Csv(), default="*")
119+
return config.get("CORS_ALLOW_HEADERS", cast=Csv(), default="*")
120+
121+
@property
122+
def OPENWEB_UI_SESSION(self):
123+
return config.get("OPENWEB_UI_SESSION", f"dialog-openweb-ui")

0 commit comments

Comments
 (0)