From ee38b3c84d09ea727c3daed6d39262a8cb305454 Mon Sep 17 00:00:00 2001 From: Patrice-Gaudicheau Date: Sun, 31 Mar 2024 20:06:15 +0200 Subject: [PATCH 01/10] Enhance DATA_DIR handling in main.py and docker-compose configuration --- backend/main.py | 19 ++++++++++++------- docker-compose.yaml | 1 + 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/backend/main.py b/backend/main.py index fa35e55a9..8f35962dc 100644 --- a/backend/main.py +++ b/backend/main.py @@ -161,24 +161,29 @@ app.mount("/images/api/v1", images_app) app.mount("/audio/api/v1", audio_app) app.mount("/rag/api/v1", rag_app) - @app.get("/api/config") async def get_app_config(): + # Checking and Handling the Absence of 'ui' in CONFIG_DATA + if "ui" in CONFIG_DATA: + default_locale = CONFIG_DATA["ui"].get("default_locale", "en-US") + default_prompt_suggestions = CONFIG_DATA["ui"].get("prompt_suggestions", []) + else: + default_locale = "en-US" + default_prompt_suggestions = [] + + # The Rest of the Function Now Uses the Variables Defined Above return { "status": True, "name": WEBUI_NAME, "version": VERSION, - "default_locale": ( - CONFIG_DATA["ui"]["default_locale"] - if "default_locale" in CONFIG_DATA["ui"] - else "en-US" - ), + "default_locale": default_locale, "images": images_app.state.ENABLED, "default_models": webui_app.state.DEFAULT_MODELS, - "default_prompt_suggestions": webui_app.state.DEFAULT_PROMPT_SUGGESTIONS, + "default_prompt_suggestions": default_prompt_suggestions, } + @app.get("/api/config/model/filter") async def get_model_filter_config(user=Depends(get_admin_user)): return { diff --git a/docker-compose.yaml b/docker-compose.yaml index f69084b8a..54b72f526 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -27,6 +27,7 @@ services: environment: - 'OLLAMA_BASE_URL=http://ollama:11434' - 'WEBUI_SECRET_KEY=' + - 'DATA_DIR=/app/backend/data' extra_hosts: - host.docker.internal:host-gateway restart: unless-stopped From 144c9059a30193d228a8a2bcf4a1bfe98426aee6 Mon Sep 17 00:00:00 2001 From: Self Denial Date: Sun, 31 Mar 2024 13:17:29 -0600 Subject: [PATCH 02/10] Improve logging. Move `print()` statements to appropiate `log()`. Add COMFYUI and WEBHOOK logging and associated environment variable control. Add WEBHOOK payload & request debug logs. --- backend/apps/images/main.py | 6 +++--- backend/apps/images/utils/comfyui.py | 20 +++++++++++++------- backend/apps/ollama/main.py | 2 +- backend/apps/rag/main.py | 6 +++--- backend/config.py | 2 ++ backend/utils/webhook.py | 11 +++++++++-- 6 files changed, 31 insertions(+), 16 deletions(-) diff --git a/backend/apps/images/main.py b/backend/apps/images/main.py index d331fa384..af8cbf7c5 100644 --- a/backend/apps/images/main.py +++ b/backend/apps/images/main.py @@ -325,7 +325,7 @@ def save_url_image(url): return image_id except Exception as e: - print(f"Error saving image: {e}") + log.exception(f"Error saving image: {e}") return None @@ -397,7 +397,7 @@ def generate_image( user.id, app.state.COMFYUI_BASE_URL, ) - print(res) + log.debug(f"res: {res}") images = [] @@ -409,7 +409,7 @@ def generate_image( with open(file_body_path, "w") as f: json.dump(data.model_dump(exclude_none=True), f) - print(images) + log.debug(f"images: {images}") return images else: if form_data.model: diff --git a/backend/apps/images/utils/comfyui.py b/backend/apps/images/utils/comfyui.py index 6a9fef353..393333255 100644 --- a/backend/apps/images/utils/comfyui.py +++ b/backend/apps/images/utils/comfyui.py @@ -4,6 +4,12 @@ import json import urllib.request import urllib.parse import random +import logging + +from config import SRC_LOG_LEVELS + +log = logging.getLogger(__name__) +log.setLevel(SRC_LOG_LEVELS["COMFYUI"]) from pydantic import BaseModel @@ -121,7 +127,7 @@ COMFYUI_DEFAULT_PROMPT = """ def queue_prompt(prompt, client_id, base_url): - print("queue_prompt") + log.info("queue_prompt") p = {"prompt": prompt, "client_id": client_id} data = json.dumps(p).encode("utf-8") req = urllib.request.Request(f"{base_url}/prompt", data=data) @@ -129,7 +135,7 @@ def queue_prompt(prompt, client_id, base_url): def get_image(filename, subfolder, folder_type, base_url): - print("get_image") + log.info("get_image") data = {"filename": filename, "subfolder": subfolder, "type": folder_type} url_values = urllib.parse.urlencode(data) with urllib.request.urlopen(f"{base_url}/view?{url_values}") as response: @@ -137,14 +143,14 @@ def get_image(filename, subfolder, folder_type, base_url): def get_image_url(filename, subfolder, folder_type, base_url): - print("get_image") + log.info("get_image") data = {"filename": filename, "subfolder": subfolder, "type": folder_type} url_values = urllib.parse.urlencode(data) return f"{base_url}/view?{url_values}" def get_history(prompt_id, base_url): - print("get_history") + log.info("get_history") with urllib.request.urlopen(f"{base_url}/history/{prompt_id}") as response: return json.loads(response.read()) @@ -212,15 +218,15 @@ def comfyui_generate_image( try: ws = websocket.WebSocket() ws.connect(f"ws://{host}/ws?clientId={client_id}") - print("WebSocket connection established.") + log.info("WebSocket connection established.") except Exception as e: - print(f"Failed to connect to WebSocket server: {e}") + log.exception(f"Failed to connect to WebSocket server: {e}") return None try: images = get_images(ws, comfyui_prompt, client_id, base_url) except Exception as e: - print(f"Error while receiving images: {e}") + log.exception(f"Error while receiving images: {e}") images = None ws.close() diff --git a/backend/apps/ollama/main.py b/backend/apps/ollama/main.py index 405f34dd2..2e3b38731 100644 --- a/backend/apps/ollama/main.py +++ b/backend/apps/ollama/main.py @@ -272,7 +272,7 @@ async def pull_model( if request_id in REQUEST_POOL: yield chunk else: - print("User: canceled request") + log.warning("User: canceled request") break finally: if hasattr(r, "close"): diff --git a/backend/apps/rag/main.py b/backend/apps/rag/main.py index da7bb307d..cb8cbb6c3 100644 --- a/backend/apps/rag/main.py +++ b/backend/apps/rag/main.py @@ -333,7 +333,7 @@ def store_docs_in_vector_db(docs, collection_name, overwrite: bool = False) -> b if overwrite: for collection in CHROMA_CLIENT.list_collections(): if collection_name == collection.name: - print(f"deleting existing collection {collection_name}") + log.info(f"deleting existing collection {collection_name}") CHROMA_CLIENT.delete_collection(name=collection_name) collection = CHROMA_CLIENT.create_collection( @@ -346,7 +346,7 @@ def store_docs_in_vector_db(docs, collection_name, overwrite: bool = False) -> b ) return True except Exception as e: - print(e) + log.exception(e) if e.__class__.__name__ == "UniqueConstraintError": return True @@ -575,7 +575,7 @@ def scan_docs_dir(user=Depends(get_admin_user)): ), ) except Exception as e: - print(e) + log.exception(e) pass except Exception as e: diff --git a/backend/config.py b/backend/config.py index ff73baf75..04778f9f0 100644 --- a/backend/config.py +++ b/backend/config.py @@ -119,6 +119,7 @@ log.info(f"GLOBAL_LOG_LEVEL: {GLOBAL_LOG_LEVEL}") log_sources = [ "AUDIO", + "COMFYUI", "CONFIG", "DB", "IMAGES", @@ -128,6 +129,7 @@ log_sources = [ "OLLAMA", "OPENAI", "RAG", + "WEBHOOK", ] SRC_LOG_LEVELS = {} diff --git a/backend/utils/webhook.py b/backend/utils/webhook.py index e700b5031..b6692e53a 100644 --- a/backend/utils/webhook.py +++ b/backend/utils/webhook.py @@ -1,6 +1,11 @@ import json import requests -from config import VERSION, WEBUI_FAVICON_URL, WEBUI_NAME +import logging + +from config import SRC_LOG_LEVELS, VERSION, WEBUI_FAVICON_URL, WEBUI_NAME + +log = logging.getLogger(__name__) +log.setLevel(SRC_LOG_LEVELS["WEBHOOK"]) def post_webhook(url: str, message: str, event_data: dict) -> bool: @@ -39,9 +44,11 @@ def post_webhook(url: str, message: str, event_data: dict) -> bool: else: payload = {**event_data} + log.debug(f"payload: {payload}") r = requests.post(url, json=payload) r.raise_for_status() + log.debug(f"r.text: {r.text}") return True except Exception as e: - print(e) + log.exception(e) return False From 68cfdd89c401b51da2ddd51f2da2f9dde456b57b Mon Sep 17 00:00:00 2001 From: Patrice-Gaudicheau Date: Sun, 31 Mar 2024 22:35:44 +0200 Subject: [PATCH 03/10] Small simplification --- Makefile | 2 ++ docker-compose.yaml | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index cbcc41d92..9784eacbd 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,8 @@ remove: start: @docker-compose start +startanbuild: + docker-compose up -d --build stop: @docker-compose stop diff --git a/docker-compose.yaml b/docker-compose.yaml index 54b72f526..f69084b8a 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -27,7 +27,6 @@ services: environment: - 'OLLAMA_BASE_URL=http://ollama:11434' - 'WEBUI_SECRET_KEY=' - - 'DATA_DIR=/app/backend/data' extra_hosts: - host.docker.internal:host-gateway restart: unless-stopped From 4ddb7e124ee1495279c3d06f4cd426c1ff14ff72 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Sun, 31 Mar 2024 13:40:57 -0700 Subject: [PATCH 04/10] fix: error message --- backend/apps/ollama/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/apps/ollama/main.py b/backend/apps/ollama/main.py index 405f34dd2..914bad680 100644 --- a/backend/apps/ollama/main.py +++ b/backend/apps/ollama/main.py @@ -670,7 +670,7 @@ async def generate_completion( else: raise HTTPException( status_code=400, - detail="error_detail", + detail=ERROR_MESSAGES.MODEL_NOT_FOUND(form_data.model), ) url = app.state.OLLAMA_BASE_URLS[url_idx] From cef676429f71aab41508f574a71e50ea73fc40c5 Mon Sep 17 00:00:00 2001 From: Patrice-Gaudicheau Date: Sun, 31 Mar 2024 22:50:17 +0200 Subject: [PATCH 05/10] Spellchecking --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 9784eacbd..1ec170a25 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ remove: start: @docker-compose start -startanbuild: +startAndBuild: docker-compose up -d --build stop: From 1b5c92d66c5321b32fc3712ae61e48af5658e270 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Sun, 31 Mar 2024 13:59:39 -0700 Subject: [PATCH 06/10] revert: prompt suggestion change --- backend/main.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/backend/main.py b/backend/main.py index 8f35962dc..8cc704a26 100644 --- a/backend/main.py +++ b/backend/main.py @@ -161,15 +161,14 @@ app.mount("/images/api/v1", images_app) app.mount("/audio/api/v1", audio_app) app.mount("/rag/api/v1", rag_app) + @app.get("/api/config") async def get_app_config(): # Checking and Handling the Absence of 'ui' in CONFIG_DATA + + default_locale = "en-US" if "ui" in CONFIG_DATA: default_locale = CONFIG_DATA["ui"].get("default_locale", "en-US") - default_prompt_suggestions = CONFIG_DATA["ui"].get("prompt_suggestions", []) - else: - default_locale = "en-US" - default_prompt_suggestions = [] # The Rest of the Function Now Uses the Variables Defined Above return { @@ -179,11 +178,10 @@ async def get_app_config(): "default_locale": default_locale, "images": images_app.state.ENABLED, "default_models": webui_app.state.DEFAULT_MODELS, - "default_prompt_suggestions": default_prompt_suggestions, + "default_prompt_suggestions": webui_app.state.DEFAULT_PROMPT_SUGGESTIONS, } - @app.get("/api/config/model/filter") async def get_model_filter_config(user=Depends(get_admin_user)): return { From a6c154d8393dfee0456d92209aef1dc4906927d1 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Sun, 31 Mar 2024 14:02:31 -0700 Subject: [PATCH 07/10] feat: rag context logging --- backend/apps/rag/utils.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/backend/apps/rag/utils.py b/backend/apps/rag/utils.py index ccdc03b4c..7b9e6628c 100644 --- a/backend/apps/rag/utils.py +++ b/backend/apps/rag/utils.py @@ -156,6 +156,8 @@ def rag_messages(docs, messages, template, k, embedding_function): relevant_contexts.append(context) + log.debug(f"relevant_contexts: {relevant_contexts}") + context_string = "" for context in relevant_contexts: if context: From 11850b75c8f57e5d7deed64315973ef0ee94b535 Mon Sep 17 00:00:00 2001 From: Jun Siang Cheah Date: Sun, 31 Mar 2024 22:13:39 +0100 Subject: [PATCH 08/10] chore: add format:backend npm script to match ci --- .github/workflows/format-backend.yaml | 2 +- package.json | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/format-backend.yaml b/.github/workflows/format-backend.yaml index e9fd0c6ba..dd0e94868 100644 --- a/.github/workflows/format-backend.yaml +++ b/.github/workflows/format-backend.yaml @@ -33,7 +33,7 @@ jobs: pip install black - name: Format backend - run: black . --exclude "/venv/" + run: npm run format:backend - name: Check for changes after format run: git diff --exit-code diff --git a/package.json b/package.json index ecf140c5b..9178a3f57 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "lint:types": "npm run check", "lint:backend": "pylint backend/", "format": "prettier --plugin-search-dir --write '**/*.{js,ts,svelte,css,md,html,json}'", + "format:backend": "black . --exclude \"/venv/\"", "i18n:parse": "i18next --config i18next-parser.config.ts && prettier --write 'src/lib/i18n/**/*.{js,json}'" }, "devDependencies": { From 653a0ff02ff132f98991c8767052f64fc88ad4f7 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Sun, 31 Mar 2024 19:50:46 -0700 Subject: [PATCH 09/10] fix: katex overflow issue --- src/app.css | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/app.css b/src/app.css index 82b3caa37..dccc6e092 100644 --- a/src/app.css +++ b/src/app.css @@ -78,3 +78,7 @@ select { /* for Chrome */ -webkit-appearance: none; } + +.katex-mathml { + display: none; +} From eda157e303067ebce6cf18aaeaf48d97a1566837 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Sun, 31 Mar 2024 21:02:29 -0700 Subject: [PATCH 10/10] fix: openai issue --- src/routes/(app)/+page.svelte | 10 +++++----- src/routes/(app)/c/[id]/+page.svelte | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/routes/(app)/+page.svelte b/src/routes/(app)/+page.svelte index b2d2cbd4d..1580e365e 100644 --- a/src/routes/(app)/+page.svelte +++ b/src/routes/(app)/+page.svelte @@ -520,11 +520,6 @@ const sendPromptOpenAI = async (model, userPrompt, responseMessageId, _chatId) => { const responseMessage = history.messages[responseMessageId]; - // Wait until history/message have been updated - await tick(); - - scrollToBottom(); - const docs = messages .filter((message) => message?.files ?? null) .map((message) => @@ -593,6 +588,11 @@ : `${OPENAI_API_BASE_URL}` ); + // Wait until history/message have been updated + await tick(); + + scrollToBottom(); + if (res && res.ok) { const reader = res.body .pipeThrough(new TextDecoderStream()) diff --git a/src/routes/(app)/c/[id]/+page.svelte b/src/routes/(app)/c/[id]/+page.svelte index 1a304f6d2..139fcd401 100644 --- a/src/routes/(app)/c/[id]/+page.svelte +++ b/src/routes/(app)/c/[id]/+page.svelte @@ -536,11 +536,6 @@ const sendPromptOpenAI = async (model, userPrompt, responseMessageId, _chatId) => { const responseMessage = history.messages[responseMessageId]; - // Wait until history/message have been updated - await tick(); - - scrollToBottom(); - const docs = messages .filter((message) => message?.files ?? null) .map((message) => @@ -607,6 +602,11 @@ : `${OPENAI_API_BASE_URL}` ); + // Wait until history/message have been updated + await tick(); + + scrollToBottom(); + if (res && res.ok) { const reader = res.body .pipeThrough(new TextDecoderStream())