diff --git a/backend/open_webui/config.py b/backend/open_webui/config.py index c37b831de..7cbfda6ae 100644 --- a/backend/open_webui/config.py +++ b/backend/open_webui/config.py @@ -1094,21 +1094,27 @@ TITLE_GENERATION_PROMPT_TEMPLATE = PersistentConfig( os.environ.get("TITLE_GENERATION_PROMPT_TEMPLATE", ""), ) -DEFAULT_TITLE_GENERATION_PROMPT_TEMPLATE = """Create a concise, 3-5 word title with an emoji as a title for the chat history, in the given language. Suitable Emojis for the summary can be used to enhance understanding but avoid quotation marks or special formatting. RESPOND ONLY WITH THE TITLE TEXT. - -Examples of titles: -📉 Stock Market Trends -🍪 Perfect Chocolate Chip Recipe -Evolution of Music Streaming -Remote Work Productivity Tips -Artificial Intelligence in Healthcare -🎮 Video Game Development Insights - +DEFAULT_TITLE_GENERATION_PROMPT_TEMPLATE = """### Task: +Generate a concise, 3-5 word title with an emoji summarizing the chat history. +### Guidelines: +- The title should clearly represent the main theme or subject of the conversation. +- Use emojis that enhance understanding of the topic, but avoid quotation marks or special formatting. +- Write the title in the chat's primary language; default to English if multilingual. +- Prioritize accuracy over excessive creativity; keep it clear and simple. +### Output: +JSON format: { "title": "your concise title here" } +### Examples: +- { "title": "📉 Stock Market Trends" }, +- { "title": "🍪 Perfect Chocolate Chip Recipe" }, +- { "title": "Evolution of Music Streaming" }, +- { "title": "Remote Work Productivity Tips" }, +- { "title": "Artificial Intelligence in Healthcare" }, +- { "title": "🎮 Video Game Development Insights" } +### Chat History: {{MESSAGES:END:2}} """ - TAGS_GENERATION_PROMPT_TEMPLATE = PersistentConfig( "TAGS_GENERATION_PROMPT_TEMPLATE", "task.tags.prompt_template", @@ -1277,7 +1283,28 @@ TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE = PersistentConfig( ) -DEFAULT_TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE = """Available Tools: {{TOOLS}}\nReturn an empty string if no tools match the query. If a function tool matches, construct and return a JSON object in the format {\"name\": \"functionName\", \"parameters\": {\"requiredFunctionParamKey\": \"requiredFunctionParamValue\"}} using the appropriate tool and its parameters. Only return the object and limit the response to the JSON object without additional text.""" +DEFAULT_TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE = """Available Tools: {{TOOLS}} + +Your task is to choose and return the correct tool(s) from the list of available tools based on the query. Follow these guidelines: + +- Return only the JSON object, without any additional text or explanation. + +- If no tools match the query, return an empty array: + { + "tool_calls": [] + } + +- If one or more tools match the query, construct a JSON response containing a "tool_calls" array with objects that include: + - "name": The tool's name. + - "parameters": A dictionary of required parameters and their corresponding values. + +The format for the JSON response is strictly: +{ + "tool_calls": [ + {"name": "toolName1", "parameters": {"key1": "value1"}}, + {"name": "toolName2", "parameters": {"key2": "value2"}} + ] +}""" DEFAULT_EMOJI_GENERATION_PROMPT_TEMPLATE = """Your task is to reflect the speaker's likely facial expression through a fitting emoji. Interpret emotions from the message and reflect their facial expression using fitting, diverse emojis (e.g., 😊, 😢, 😡, 😱). @@ -1290,6 +1317,20 @@ Your task is to synthesize these responses into a single, high-quality response. Responses from models: {{responses}}""" + +DEFAULT_CODE_INTERPRETER_PROMPT = """ +#### Tools Available + +1. **Code Interpreter**: `` + - You have access to a Python shell that runs directly in the user's browser, enabling fast execution of code for analysis, calculations, or problem-solving. Use it in this response. + - The Python code you write can incorporate a wide array of libraries, handle data manipulation or visualization, perform API calls for web-related tasks, or tackle virtually any computational challenge. Use this flexibility to **think outside the box, craft elegant solutions, and harness Python's full potential**. + - To use it, **you must enclose your code within `` tags** and stop right away. If you don't, the code won't execute. Do NOT use triple backticks. + - When coding, **always aim to print meaningful outputs** (e.g., results, tables, summaries, or visuals) to better interpret and verify the findings. Avoid relying on implicit outputs; prioritize explicit and clear print statements so the results are effectively communicated to the user. + - After obtaining the printed output, **always provide a concise analysis, interpretation, or next steps to help the user understand the findings or refine the outcome further.** + - If the results are unclear, unexpected, or require validation, refine the code and execute it again as needed. Always aim to deliver meaningful insights from the results, iterating if necessary. + +Ensure that the tools are effectively utilized to achieve the highest-quality analysis for the user.""" + #################################### # Vector Database #################################### @@ -1319,6 +1360,7 @@ CHROMA_HTTP_SSL = os.environ.get("CHROMA_HTTP_SSL", "false").lower() == "true" MILVUS_URI = os.environ.get("MILVUS_URI", f"{DATA_DIR}/vector_db/milvus.db") MILVUS_DB = os.environ.get("MILVUS_DB", "default") +MILVUS_TOKEN = os.environ.get("MILVUS_TOKEN", None) # Qdrant QDRANT_URI = os.environ.get("QDRANT_URI", None) diff --git a/backend/open_webui/env.py b/backend/open_webui/env.py index 77e632ccc..00605e15d 100644 --- a/backend/open_webui/env.py +++ b/backend/open_webui/env.py @@ -356,14 +356,22 @@ WEBUI_SECRET_KEY = os.environ.get( ), # DEPRECATED: remove at next major version ) -WEBUI_SESSION_COOKIE_SAME_SITE = os.environ.get( - "WEBUI_SESSION_COOKIE_SAME_SITE", - os.environ.get("WEBUI_SESSION_COOKIE_SAME_SITE", "lax"), +WEBUI_SESSION_COOKIE_SAME_SITE = os.environ.get("WEBUI_SESSION_COOKIE_SAME_SITE", "lax") + +WEBUI_SESSION_COOKIE_SECURE = ( + os.environ.get("WEBUI_SESSION_COOKIE_SECURE", "false").lower() == "true" ) -WEBUI_SESSION_COOKIE_SECURE = os.environ.get( - "WEBUI_SESSION_COOKIE_SECURE", - os.environ.get("WEBUI_SESSION_COOKIE_SECURE", "false").lower() == "true", +WEBUI_AUTH_COOKIE_SAME_SITE = os.environ.get( + "WEBUI_AUTH_COOKIE_SAME_SITE", WEBUI_SESSION_COOKIE_SAME_SITE +) + +WEBUI_AUTH_COOKIE_SECURE = ( + os.environ.get( + "WEBUI_AUTH_COOKIE_SECURE", + os.environ.get("WEBUI_SESSION_COOKIE_SECURE", "false"), + ).lower() + == "true" ) if WEBUI_AUTH and WEBUI_SECRET_KEY == "": diff --git a/backend/open_webui/main.py b/backend/open_webui/main.py index 00270aabc..a4d63a6d7 100644 --- a/backend/open_webui/main.py +++ b/backend/open_webui/main.py @@ -875,6 +875,7 @@ async def chat_completion( "tool_ids": form_data.get("tool_ids", None), "files": form_data.get("files", None), "features": form_data.get("features", None), + "variables": form_data.get("variables", None), } form_data["metadata"] = metadata diff --git a/backend/open_webui/retrieval/vector/dbs/milvus.py b/backend/open_webui/retrieval/vector/dbs/milvus.py index bdfa16eb6..43c3f3d1a 100644 --- a/backend/open_webui/retrieval/vector/dbs/milvus.py +++ b/backend/open_webui/retrieval/vector/dbs/milvus.py @@ -8,13 +8,17 @@ from open_webui.retrieval.vector.main import VectorItem, SearchResult, GetResult from open_webui.config import ( MILVUS_URI, MILVUS_DB, + MILVUS_TOKEN, ) class MilvusClient: def __init__(self): self.collection_prefix = "open_webui" - self.client = Client(uri=MILVUS_URI, database=MILVUS_DB) + if MILVUS_TOKEN is None: + self.client = Client(uri=MILVUS_URI, database=MILVUS_DB) + else: + self.client = Client(uri=MILVUS_URI, database=MILVUS_DB, token=MILVUS_TOKEN) def _result_to_get_result(self, result) -> GetResult: ids = [] diff --git a/backend/open_webui/routers/auths.py b/backend/open_webui/routers/auths.py index 47baeb0ac..b6a2c7562 100644 --- a/backend/open_webui/routers/auths.py +++ b/backend/open_webui/routers/auths.py @@ -25,8 +25,8 @@ from open_webui.env import ( WEBUI_AUTH, WEBUI_AUTH_TRUSTED_EMAIL_HEADER, WEBUI_AUTH_TRUSTED_NAME_HEADER, - WEBUI_SESSION_COOKIE_SAME_SITE, - WEBUI_SESSION_COOKIE_SECURE, + WEBUI_AUTH_COOKIE_SAME_SITE, + WEBUI_AUTH_COOKIE_SECURE, SRC_LOG_LEVELS, ) from fastapi import APIRouter, Depends, HTTPException, Request, status @@ -95,8 +95,8 @@ async def get_session_user( value=token, expires=datetime_expires_at, httponly=True, # Ensures the cookie is not accessible via JavaScript - samesite=WEBUI_SESSION_COOKIE_SAME_SITE, - secure=WEBUI_SESSION_COOKIE_SECURE, + samesite=WEBUI_AUTH_COOKIE_SAME_SITE, + secure=WEBUI_AUTH_COOKIE_SECURE, ) user_permissions = get_permissions( @@ -164,7 +164,7 @@ async def update_password( ############################ # LDAP Authentication ############################ -@router.post("/ldap", response_model=SigninResponse) +@router.post("/ldap", response_model=SessionUserResponse) async def ldap_auth(request: Request, response: Response, form_data: LdapForm): ENABLE_LDAP = request.app.state.config.ENABLE_LDAP LDAP_SERVER_LABEL = request.app.state.config.LDAP_SERVER_LABEL @@ -288,6 +288,10 @@ async def ldap_auth(request: Request, response: Response, form_data: LdapForm): httponly=True, # Ensures the cookie is not accessible via JavaScript ) + user_permissions = get_permissions( + user.id, request.app.state.config.USER_PERMISSIONS + ) + return { "token": token, "token_type": "Bearer", @@ -296,6 +300,7 @@ async def ldap_auth(request: Request, response: Response, form_data: LdapForm): "name": user.name, "role": user.role, "profile_image_url": user.profile_image_url, + "permissions": user_permissions, } else: raise HTTPException(400, detail=ERROR_MESSAGES.INVALID_CRED) @@ -378,8 +383,8 @@ async def signin(request: Request, response: Response, form_data: SigninForm): value=token, expires=datetime_expires_at, httponly=True, # Ensures the cookie is not accessible via JavaScript - samesite=WEBUI_SESSION_COOKIE_SAME_SITE, - secure=WEBUI_SESSION_COOKIE_SECURE, + samesite=WEBUI_AUTH_COOKIE_SAME_SITE, + secure=WEBUI_AUTH_COOKIE_SECURE, ) user_permissions = get_permissions( @@ -473,8 +478,8 @@ async def signup(request: Request, response: Response, form_data: SignupForm): value=token, expires=datetime_expires_at, httponly=True, # Ensures the cookie is not accessible via JavaScript - samesite=WEBUI_SESSION_COOKIE_SAME_SITE, - secure=WEBUI_SESSION_COOKIE_SECURE, + samesite=WEBUI_AUTH_COOKIE_SAME_SITE, + secure=WEBUI_AUTH_COOKIE_SECURE, ) if request.app.state.config.WEBHOOK_URL: diff --git a/backend/open_webui/routers/chats.py b/backend/open_webui/routers/chats.py index a001dd01f..2efd043ef 100644 --- a/backend/open_webui/routers/chats.py +++ b/backend/open_webui/routers/chats.py @@ -444,15 +444,21 @@ async def pin_chat_by_id(id: str, user=Depends(get_verified_user)): ############################ +class CloneForm(BaseModel): + title: Optional[str] = None + + @router.post("/{id}/clone", response_model=Optional[ChatResponse]) -async def clone_chat_by_id(id: str, user=Depends(get_verified_user)): +async def clone_chat_by_id( + form_data: CloneForm, id: str, user=Depends(get_verified_user) +): chat = Chats.get_chat_by_id_and_user_id(id, user.id) if chat: updated_chat = { **chat.chat, "originalChatId": chat.id, "branchPointMessageId": chat.chat["history"]["currentId"], - "title": f"Clone of {chat.title}", + "title": form_data.title if form_data.title else f"Clone of {chat.title}", } chat = Chats.insert_new_chat(user.id, ChatForm(**{"chat": updated_chat})) diff --git a/backend/open_webui/routers/knowledge.py b/backend/open_webui/routers/knowledge.py index cce3d6311..aac16e851 100644 --- a/backend/open_webui/routers/knowledge.py +++ b/backend/open_webui/routers/knowledge.py @@ -264,7 +264,11 @@ def add_file_to_knowledge_by_id( detail=ERROR_MESSAGES.NOT_FOUND, ) - if knowledge.user_id != user.id and user.role != "admin": + if ( + knowledge.user_id != user.id + and not has_access(user.id, "write", knowledge.access_control) + and user.role != "admin" + ): raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.ACCESS_PROHIBITED, @@ -342,7 +346,12 @@ def update_file_from_knowledge_by_id( detail=ERROR_MESSAGES.NOT_FOUND, ) - if knowledge.user_id != user.id and user.role != "admin": + if ( + knowledge.user_id != user.id + and not has_access(user.id, "write", knowledge.access_control) + and user.role != "admin" + ): + raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.ACCESS_PROHIBITED, @@ -406,7 +415,11 @@ def remove_file_from_knowledge_by_id( detail=ERROR_MESSAGES.NOT_FOUND, ) - if knowledge.user_id != user.id and user.role != "admin": + if ( + knowledge.user_id != user.id + and not has_access(user.id, "write", knowledge.access_control) + and user.role != "admin" + ): raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.ACCESS_PROHIBITED, @@ -429,10 +442,6 @@ def remove_file_from_knowledge_by_id( if VECTOR_DB_CLIENT.has_collection(collection_name=file_collection): VECTOR_DB_CLIENT.delete_collection(collection_name=file_collection) - # Delete physical file - if file.path: - Storage.delete_file(file.path) - # Delete file from database Files.delete_file_by_id(form_data.file_id) @@ -484,7 +493,11 @@ async def delete_knowledge_by_id(id: str, user=Depends(get_verified_user)): detail=ERROR_MESSAGES.NOT_FOUND, ) - if knowledge.user_id != user.id and user.role != "admin": + if ( + knowledge.user_id != user.id + and not has_access(user.id, "write", knowledge.access_control) + and user.role != "admin" + ): raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.ACCESS_PROHIBITED, @@ -543,7 +556,11 @@ async def reset_knowledge_by_id(id: str, user=Depends(get_verified_user)): detail=ERROR_MESSAGES.NOT_FOUND, ) - if knowledge.user_id != user.id and user.role != "admin": + if ( + knowledge.user_id != user.id + and not has_access(user.id, "write", knowledge.access_control) + and user.role != "admin" + ): raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.ACCESS_PROHIBITED, @@ -582,7 +599,11 @@ def add_files_to_knowledge_batch( detail=ERROR_MESSAGES.NOT_FOUND, ) - if knowledge.user_id != user.id and user.role != "admin": + if ( + knowledge.user_id != user.id + and not has_access(user.id, "write", knowledge.access_control) + and user.role != "admin" + ): raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.ACCESS_PROHIBITED, diff --git a/backend/open_webui/routers/models.py b/backend/open_webui/routers/models.py index 6c8519b2c..0cf3308f1 100644 --- a/backend/open_webui/routers/models.py +++ b/backend/open_webui/routers/models.py @@ -183,7 +183,11 @@ async def delete_model_by_id(id: str, user=Depends(get_verified_user)): detail=ERROR_MESSAGES.NOT_FOUND, ) - if model.user_id != user.id and user.role != "admin": + if ( + user.role != "admin" + and model.user_id != user.id + and not has_access(user.id, "write", model.access_control) + ): raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.UNAUTHORIZED, diff --git a/backend/open_webui/routers/ollama.py b/backend/open_webui/routers/ollama.py index 261cd5ba3..780fc6f50 100644 --- a/backend/open_webui/routers/ollama.py +++ b/backend/open_webui/routers/ollama.py @@ -395,7 +395,7 @@ async def get_ollama_tags( ) if user.role == "user" and not BYPASS_MODEL_ACCESS_CONTROL: - models["models"] = get_filtered_models(models, user) + models["models"] = await get_filtered_models(models, user) return models @@ -977,6 +977,7 @@ async def generate_chat_completion( if BYPASS_MODEL_ACCESS_CONTROL: bypass_filter = True + metadata = form_data.pop("metadata", None) try: form_data = GenerateChatCompletionForm(**form_data) except Exception as e: @@ -987,8 +988,6 @@ async def generate_chat_completion( ) payload = {**form_data.model_dump(exclude_none=True)} - if "metadata" in payload: - del payload["metadata"] model_id = payload["model"] model_info = Models.get_model_by_id(model_id) @@ -1006,7 +1005,7 @@ async def generate_chat_completion( payload["options"] = apply_model_params_to_body_ollama( params, payload["options"] ) - payload = apply_model_system_prompt_to_body(params, payload, user) + payload = apply_model_system_prompt_to_body(params, payload, metadata) # Check if user has access to the model if not bypass_filter and user.role == "user": diff --git a/backend/open_webui/routers/openai.py b/backend/open_webui/routers/openai.py index f7d7fd294..c27f35e7e 100644 --- a/backend/open_webui/routers/openai.py +++ b/backend/open_webui/routers/openai.py @@ -489,7 +489,7 @@ async def get_models( raise HTTPException(status_code=500, detail=error_detail) if user.role == "user" and not BYPASS_MODEL_ACCESS_CONTROL: - models["data"] = get_filtered_models(models, user) + models["data"] = await get_filtered_models(models, user) return models @@ -551,9 +551,9 @@ async def generate_chat_completion( bypass_filter = True idx = 0 + payload = {**form_data} - if "metadata" in payload: - del payload["metadata"] + metadata = payload.pop("metadata", None) model_id = form_data.get("model") model_info = Models.get_model_by_id(model_id) @@ -566,7 +566,7 @@ async def generate_chat_completion( params = model_info.params.model_dump() payload = apply_model_params_to_body_openai(params, payload) - payload = apply_model_system_prompt_to_body(params, payload, user) + payload = apply_model_system_prompt_to_body(params, payload, metadata) # Check if user has access to the model if not bypass_filter and user.role == "user": diff --git a/backend/open_webui/routers/prompts.py b/backend/open_webui/routers/prompts.py index 014e5652e..9fb946c6e 100644 --- a/backend/open_webui/routers/prompts.py +++ b/backend/open_webui/routers/prompts.py @@ -147,7 +147,11 @@ async def delete_prompt_by_command(command: str, user=Depends(get_verified_user) detail=ERROR_MESSAGES.NOT_FOUND, ) - if prompt.user_id != user.id and user.role != "admin": + if ( + prompt.user_id != user.id + and not has_access(user.id, "write", prompt.access_control) + and user.role != "admin" + ): raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.ACCESS_PROHIBITED, diff --git a/backend/open_webui/routers/tasks.py b/backend/open_webui/routers/tasks.py index 6d7343c8a..f56a0232d 100644 --- a/backend/open_webui/routers/tasks.py +++ b/backend/open_webui/routers/tasks.py @@ -4,6 +4,7 @@ from fastapi.responses import JSONResponse, RedirectResponse from pydantic import BaseModel from typing import Optional import logging +import re from open_webui.utils.chat import generate_chat_completion from open_webui.utils.task import ( @@ -89,6 +90,10 @@ async def update_task_config( form_data.TITLE_GENERATION_PROMPT_TEMPLATE ) + request.app.state.config.IMAGE_PROMPT_GENERATION_PROMPT_TEMPLATE = ( + form_data.IMAGE_PROMPT_GENERATION_PROMPT_TEMPLATE + ) + request.app.state.config.ENABLE_AUTOCOMPLETE_GENERATION = ( form_data.ENABLE_AUTOCOMPLETE_GENERATION ) @@ -161,9 +166,20 @@ async def generate_title( else: template = DEFAULT_TITLE_GENERATION_PROMPT_TEMPLATE + messages = form_data["messages"] + + # Remove reasoning details from the messages + for message in messages: + message["content"] = re.sub( + r"]*>.*?<\/details>", + "", + message["content"], + flags=re.S, + ).strip() + content = title_generation_template( template, - form_data["messages"], + messages, { "name": user.name, "location": user.info.get("location") if user.info else None, @@ -175,10 +191,10 @@ async def generate_title( "messages": [{"role": "user", "content": content}], "stream": False, **( - {"max_tokens": 50} + {"max_tokens": 1000} if models[task_model_id]["owned_by"] == "ollama" else { - "max_completion_tokens": 50, + "max_completion_tokens": 1000, } ), "metadata": { diff --git a/backend/open_webui/routers/tools.py b/backend/open_webui/routers/tools.py index 7b9144b4c..d6a5c5532 100644 --- a/backend/open_webui/routers/tools.py +++ b/backend/open_webui/routers/tools.py @@ -227,7 +227,11 @@ async def delete_tools_by_id( detail=ERROR_MESSAGES.NOT_FOUND, ) - if tools.user_id != user.id and user.role != "admin": + if ( + tools.user_id != user.id + and not has_access(user.id, "write", tools.access_control) + and user.role != "admin" + ): raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.UNAUTHORIZED, diff --git a/backend/open_webui/socket/main.py b/backend/open_webui/socket/main.py index 2d12f5803..3788139ea 100644 --- a/backend/open_webui/socket/main.py +++ b/backend/open_webui/socket/main.py @@ -325,7 +325,7 @@ def get_event_emitter(request_info): def get_event_call(request_info): - async def __event_call__(event_data): + async def __event_caller__(event_data): response = await sio.call( "chat-events", { @@ -337,7 +337,10 @@ def get_event_call(request_info): ) return response - return __event_call__ + return __event_caller__ + + +get_event_caller = get_event_call def get_user_id_from_session_pool(sid): diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py index 6b2329be1..11e36cecf 100644 --- a/backend/open_webui/utils/middleware.py +++ b/backend/open_webui/utils/middleware.py @@ -7,7 +7,10 @@ from aiocache import cached from typing import Any, Optional import random import json +import html import inspect +import re + from uuid import uuid4 from concurrent.futures import ThreadPoolExecutor @@ -54,6 +57,7 @@ from open_webui.utils.task import ( from open_webui.utils.misc import ( get_message_list, add_or_update_system_message, + add_or_update_user_message, get_last_user_message, get_last_assistant_message, prepend_to_first_user_message_content, @@ -64,7 +68,10 @@ from open_webui.utils.plugin import load_function_module_by_id from open_webui.tasks import create_task -from open_webui.config import DEFAULT_TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE +from open_webui.config import ( + DEFAULT_TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE, + DEFAULT_CODE_INTERPRETER_PROMPT, +) from open_webui.env import ( SRC_LOG_LEVELS, GLOBAL_LOG_LEVEL, @@ -270,60 +277,70 @@ async def chat_completion_tools_handler( result = json.loads(content) - tool_function_name = result.get("name", None) - if tool_function_name not in tools: - return body, {} + async def tool_call_handler(tool_call): + log.debug(f"{tool_call=}") - tool_function_params = result.get("parameters", {}) + tool_function_name = tool_call.get("name", None) + if tool_function_name not in tools: + return body, {} - try: - required_params = ( - tools[tool_function_name] - .get("spec", {}) - .get("parameters", {}) - .get("required", []) - ) - tool_function = tools[tool_function_name]["callable"] - tool_function_params = { - k: v - for k, v in tool_function_params.items() - if k in required_params - } - tool_output = await tool_function(**tool_function_params) + tool_function_params = tool_call.get("parameters", {}) - except Exception as e: - tool_output = str(e) - - if isinstance(tool_output, str): - if tools[tool_function_name]["citation"]: - sources.append( - { - "source": { - "name": f"TOOL:{tools[tool_function_name]['toolkit_id']}/{tool_function_name}" - }, - "document": [tool_output], - "metadata": [ - { - "source": f"TOOL:{tools[tool_function_name]['toolkit_id']}/{tool_function_name}" - } - ], - } - ) - else: - sources.append( - { - "source": {}, - "document": [tool_output], - "metadata": [ - { - "source": f"TOOL:{tools[tool_function_name]['toolkit_id']}/{tool_function_name}" - } - ], - } + try: + required_params = ( + tools[tool_function_name] + .get("spec", {}) + .get("parameters", {}) + .get("required", []) ) + tool_function = tools[tool_function_name]["callable"] + tool_function_params = { + k: v + for k, v in tool_function_params.items() + if k in required_params + } + tool_output = await tool_function(**tool_function_params) - if tools[tool_function_name]["file_handler"]: - skip_files = True + except Exception as e: + tool_output = str(e) + + if isinstance(tool_output, str): + if tools[tool_function_name]["citation"]: + sources.append( + { + "source": { + "name": f"TOOL:{tools[tool_function_name]['toolkit_id']}/{tool_function_name}" + }, + "document": [tool_output], + "metadata": [ + { + "source": f"TOOL:{tools[tool_function_name]['toolkit_id']}/{tool_function_name}" + } + ], + } + ) + else: + sources.append( + { + "source": {}, + "document": [tool_output], + "metadata": [ + { + "source": f"TOOL:{tools[tool_function_name]['toolkit_id']}/{tool_function_name}" + } + ], + } + ) + + if tools[tool_function_name]["file_handler"]: + skip_files = True + + # check if "tool_calls" in result + if result.get("tool_calls"): + for tool_call in result.get("tool_calls"): + await tool_call_handler(tool_call) + else: + await tool_call_handler(result) except Exception as e: log.exception(f"Error: {e}") @@ -666,6 +683,9 @@ def apply_params_to_form_data(form_data, model): if "temperature" in params: form_data["temperature"] = params["temperature"] + if "max_tokens" in params: + form_data["max_tokens"] = params["max_tokens"] + if "top_p" in params: form_data["top_p"] = params["top_p"] @@ -746,6 +766,8 @@ async def process_chat_payload(request, form_data, metadata, user, model): files.extend(knowledge_files) form_data["files"] = files + variables = form_data.pop("variables", None) + features = form_data.pop("features", None) if features: if "web_search" in features and features["web_search"]: @@ -758,6 +780,11 @@ async def process_chat_payload(request, form_data, metadata, user, model): request, form_data, extra_params, user ) + if "code_interpreter" in features and features["code_interpreter"]: + form_data["messages"] = add_or_update_user_message( + DEFAULT_CODE_INTERPRETER_PROMPT, form_data["messages"] + ) + try: form_data, flags = await chat_completion_filter_functions_handler( request, form_data, model, extra_params @@ -889,16 +916,24 @@ async def process_chat_response( if res and isinstance(res, dict): if len(res.get("choices", [])) == 1: - title = ( + title_string = ( res.get("choices", [])[0] .get("message", {}) - .get( - "content", - message.get("content", "New Chat"), - ) - ).strip() + .get("content", message.get("content", "New Chat")) + ) else: - title = None + title_string = "" + + title_string = title_string[ + title_string.find("{") : title_string.rfind("}") + 1 + ] + + try: + title = json.loads(title_string).get( + "title", "New Chat" + ) + except Exception as e: + title = "" if not title: title = messages[0].get("content", "New Chat") @@ -964,6 +999,7 @@ async def process_chat_response( pass event_emitter = None + event_caller = None if ( "session_id" in metadata and metadata["session_id"] @@ -973,10 +1009,11 @@ async def process_chat_response( and metadata["message_id"] ): event_emitter = get_event_emitter(metadata) + event_caller = get_event_call(metadata) + # Non-streaming response if not isinstance(response, StreamingResponse): if event_emitter: - if "selected_model_id" in response: Chats.upsert_message_to_chat_by_id_and_message_id( metadata["chat_id"], @@ -1041,22 +1078,156 @@ async def process_chat_response( else: return response + # Non standard response if not any( content_type in response.headers["Content-Type"] for content_type in ["text/event-stream", "application/x-ndjson"] ): return response - if event_emitter: - + # Streaming response + if event_emitter and event_caller: task_id = str(uuid4()) # Create a unique task ID. + model_id = form_data.get("model", "") # Handle as a background task async def post_response_handler(response, events): + def serialize_content_blocks(content_blocks, raw=False): + content = "" + + for block in content_blocks: + if block["type"] == "text": + content = f"{content}{block['content'].strip()}\n" + elif block["type"] == "reasoning": + reasoning_display_content = "\n".join( + (f"> {line}" if not line.startswith(">") else line) + for line in block["content"].splitlines() + ) + + reasoning_duration = block.get("duration", None) + + if reasoning_duration: + content = f'{content}
\nThought for {reasoning_duration} seconds\n{reasoning_display_content}\n
\n' + else: + content = f'{content}
\nThinking…\n{reasoning_display_content}\n
\n' + + elif block["type"] == "code_interpreter": + attributes = block.get("attributes", {}) + output = block.get("output", None) + lang = attributes.get("lang", "") + + if output: + output = html.escape(json.dumps(output)) + + if raw: + content = f'{content}
\nAnalyzed\n```{lang}\n{block["content"]}\n```\n```output\n{output}\n```\n
\n' + else: + content = f'{content}
\nAnalyzed\n```{lang}\n{block["content"]}\n```\n
\n' + else: + content = f'{content}
\nAnalyzing...\n```{lang}\n{block["content"]}\n```\n
\n' + + else: + block_content = str(block["content"]).strip() + content = f"{content}{block['type']}: {block_content}\n" + + return content + + def tag_content_handler(content_type, tags, content, content_blocks): + end_flag = False + + def extract_attributes(tag_content): + """Extract attributes from a tag if they exist.""" + attributes = {} + # Match attributes in the format: key="value" (ignores single quotes for simplicity) + matches = re.findall(r'(\w+)\s*=\s*"([^"]+)"', tag_content) + for key, value in matches: + attributes[key] = value + return attributes + + if content_blocks[-1]["type"] == "text": + for tag in tags: + # Match start tag e.g., or + start_tag_pattern = rf"<{tag}(.*?)>" + match = re.search(start_tag_pattern, content) + if match: + # Extract attributes in the tag (if present) + attributes = extract_attributes(match.group(1)) + # Remove the start tag from the currently handling text block + content_blocks[-1]["content"] = content_blocks[-1][ + "content" + ].replace(match.group(0), "") + if not content_blocks[-1]["content"]: + content_blocks.pop() + # Append the new block + content_blocks.append( + { + "type": content_type, + "tag": tag, + "attributes": attributes, + "content": "", + "started_at": time.time(), + } + ) + break + elif content_blocks[-1]["type"] == content_type: + tag = content_blocks[-1]["tag"] + # Match end tag e.g., + end_tag_pattern = rf"" + if re.search(end_tag_pattern, content): + block_content = content_blocks[-1]["content"] + # Strip start and end tags from the content + start_tag_pattern = rf"<{tag}(.*?)>" + block_content = re.sub( + start_tag_pattern, "", block_content + ).strip() + block_content = re.sub( + end_tag_pattern, "", block_content + ).strip() + if block_content: + end_flag = True + content_blocks[-1]["content"] = block_content + content_blocks[-1]["ended_at"] = time.time() + content_blocks[-1]["duration"] = int( + content_blocks[-1]["ended_at"] + - content_blocks[-1]["started_at"] + ) + # Reset the content_blocks by appending a new text block + content_blocks.append( + { + "type": "text", + "content": "", + } + ) + # Clean processed content + content = re.sub( + rf"<{tag}(.*?)>(.|\n)*?", + "", + content, + flags=re.DOTALL, + ) + else: + # Remove the block if content is empty + content_blocks.pop() + return content, content_blocks, end_flag + message = Chats.get_message_by_id_and_message_id( metadata["chat_id"], metadata["message_id"] ) + content = message.get("content", "") if message else "" + content_blocks = [ + { + "type": "text", + "content": content, + } + ] + + # We might want to disable this by default + DETECT_REASONING = True + DETECT_CODE_INTERPRETER = True + + reasoning_tags = ["think", "reason", "reasoning", "thought", "Thought"] + code_interpreter_tags = ["code_interpreter"] try: for event in events: @@ -1076,148 +1247,193 @@ async def process_chat_response( }, ) - # We might want to disable this by default - detect_reasoning = True - reasoning_tags = ["think", "reason", "reasoning", "thought", "Thought"] - current_tag = None + async def stream_body_handler(response): + nonlocal content + nonlocal content_blocks - reasoning_start_time = None + async for line in response.body_iterator: + line = line.decode("utf-8") if isinstance(line, bytes) else line + data = line - reasoning_content = "" - ongoing_content = "" - - async for line in response.body_iterator: - line = line.decode("utf-8") if isinstance(line, bytes) else line - data = line - - # Skip empty lines - if not data.strip(): - continue - - # "data:" is the prefix for each event - if not data.startswith("data:"): - continue - - # Remove the prefix - data = data[len("data:") :].strip() - - try: - data = json.loads(data) - - if "selected_model_id" in data: - Chats.upsert_message_to_chat_by_id_and_message_id( - metadata["chat_id"], - metadata["message_id"], - { - "selectedModelId": data["selected_model_id"], - }, - ) - else: - value = ( - data.get("choices", [])[0] - .get("delta", {}) - .get("content") - ) - - if value: - content = f"{content}{value}" - - if detect_reasoning: - for tag in reasoning_tags: - start_tag = f"<{tag}>\n" - end_tag = f"\n" - - if start_tag in content: - # Remove the start tag - content = content.replace(start_tag, "") - ongoing_content = content - - reasoning_start_time = time.time() - reasoning_content = "" - - current_tag = tag - break - - if reasoning_start_time is not None: - # Remove the last value from the content - content = content[: -len(value)] - - reasoning_content += value - - end_tag = f"\n" - if end_tag in reasoning_content: - reasoning_end_time = time.time() - reasoning_duration = int( - reasoning_end_time - - reasoning_start_time - ) - reasoning_content = ( - reasoning_content.strip( - f"<{current_tag}>\n" - ) - .strip(end_tag) - .strip() - ) - - if reasoning_content: - reasoning_display_content = "\n".join( - ( - f"> {line}" - if not line.startswith(">") - else line - ) - for line in reasoning_content.splitlines() - ) - - # Format reasoning with
tag - content = f'{ongoing_content}
\nThought for {reasoning_duration} seconds\n{reasoning_display_content}\n
\n' - else: - content = "" - - reasoning_start_time = None - else: - - reasoning_display_content = "\n".join( - ( - f"> {line}" - if not line.startswith(">") - else line - ) - for line in reasoning_content.splitlines() - ) - - # Show ongoing thought process - content = f'{ongoing_content}
\nThinking…\n{reasoning_display_content}\n
\n' - - if ENABLE_REALTIME_CHAT_SAVE: - # Save message in the database - Chats.upsert_message_to_chat_by_id_and_message_id( - metadata["chat_id"], - metadata["message_id"], - { - "content": content, - }, - ) - else: - data = { - "content": content, - } - - await event_emitter( - { - "type": "chat:completion", - "data": data, - } - ) - except Exception as e: - done = "data: [DONE]" in line - if done: - pass - else: + # Skip empty lines + if not data.strip(): continue + # "data:" is the prefix for each event + if not data.startswith("data:"): + continue + + # Remove the prefix + data = data[len("data:") :].strip() + + try: + data = json.loads(data) + + if "selected_model_id" in data: + model_id = data["selected_model_id"] + Chats.upsert_message_to_chat_by_id_and_message_id( + metadata["chat_id"], + metadata["message_id"], + { + "selectedModelId": model_id, + }, + ) + else: + choices = data.get("choices", []) + if not choices: + continue + + value = choices[0].get("delta", {}).get("content") + + if value: + content = f"{content}{value}" + content_blocks[-1]["content"] = ( + content_blocks[-1]["content"] + value + ) + + if DETECT_REASONING: + content, content_blocks, _ = ( + tag_content_handler( + "reasoning", + reasoning_tags, + content, + content_blocks, + ) + ) + + if DETECT_CODE_INTERPRETER: + content, content_blocks, end = ( + tag_content_handler( + "code_interpreter", + code_interpreter_tags, + content, + content_blocks, + ) + ) + + if end: + break + + if ENABLE_REALTIME_CHAT_SAVE: + # Save message in the database + Chats.upsert_message_to_chat_by_id_and_message_id( + metadata["chat_id"], + metadata["message_id"], + { + "content": serialize_content_blocks( + content_blocks + ), + }, + ) + else: + data = { + "content": serialize_content_blocks( + content_blocks + ), + } + + await event_emitter( + { + "type": "chat:completion", + "data": data, + } + ) + except Exception as e: + done = "data: [DONE]" in line + if done: + pass + else: + log.debug("Error: ", e) + continue + + # Clean up the last text block + if content_blocks[-1]["type"] == "text": + content_blocks[-1]["content"] = content_blocks[-1][ + "content" + ].strip() + + if not content_blocks[-1]["content"]: + content_blocks.pop() + + if response.background: + await response.background() + + await stream_body_handler(response) + + MAX_RETRIES = 5 + retries = 0 + + while ( + content_blocks[-1]["type"] == "code_interpreter" + and retries < MAX_RETRIES + ): + retries += 1 + log.debug(f"Attempt count: {retries}") + + try: + if content_blocks[-1]["attributes"].get("type") == "code": + output = await event_caller( + { + "type": "execute:python", + "data": { + "id": str(uuid4()), + "code": content_blocks[-1]["content"], + }, + } + ) + except Exception as e: + output = str(e) + + content_blocks[-1]["output"] = output + content_blocks.append( + { + "type": "text", + "content": "", + } + ) + + await event_emitter( + { + "type": "chat:completion", + "data": { + "content": serialize_content_blocks(content_blocks), + }, + } + ) + + try: + res = await generate_chat_completion( + request, + { + "model": model_id, + "stream": True, + "messages": [ + *form_data["messages"], + { + "role": "assistant", + "content": serialize_content_blocks( + content_blocks, raw=True + ), + }, + ], + }, + user, + ) + + if isinstance(res, StreamingResponse): + await stream_body_handler(res) + else: + break + except Exception as e: + log.debug(e) + break + title = Chats.get_chat_title_by_id(metadata["chat_id"]) - data = {"done": True, "content": content, "title": title} + data = { + "done": True, + "content": serialize_content_blocks(content_blocks), + "title": title, + } if not ENABLE_REALTIME_CHAT_SAVE: # Save message in the database @@ -1225,7 +1441,7 @@ async def process_chat_response( metadata["chat_id"], metadata["message_id"], { - "content": content, + "content": serialize_content_blocks(content_blocks), }, ) @@ -1262,7 +1478,7 @@ async def process_chat_response( metadata["chat_id"], metadata["message_id"], { - "content": content, + "content": serialize_content_blocks(content_blocks), }, ) diff --git a/backend/open_webui/utils/misc.py b/backend/open_webui/utils/misc.py index a83733d63..c2a3945d0 100644 --- a/backend/open_webui/utils/misc.py +++ b/backend/open_webui/utils/misc.py @@ -131,6 +131,44 @@ def add_or_update_system_message(content: str, messages: list[dict]): return messages +def add_or_update_user_message(content: str, messages: list[dict]): + """ + Adds a new user message at the end of the messages list + or updates the existing user message at the end. + + :param msg: The message to be added or appended. + :param messages: The list of message dictionaries. + :return: The updated list of message dictionaries. + """ + + if messages and messages[-1].get("role") == "user": + messages[-1]["content"] = f"{messages[-1]['content']}\n{content}" + else: + # Insert at the end + messages.append({"role": "user", "content": content}) + + return messages + + +def append_or_update_assistant_message(content: str, messages: list[dict]): + """ + Adds a new assistant message at the end of the messages list + or updates the existing assistant message at the end. + + :param msg: The message to be added or appended. + :param messages: The list of message dictionaries. + :return: The updated list of message dictionaries. + """ + + if messages and messages[-1].get("role") == "assistant": + messages[-1]["content"] = f"{messages[-1]['content']}\n{content}" + else: + # Insert at the end + messages.append({"role": "assistant", "content": content}) + + return messages + + def openai_chat_message_template(model: str): return { "id": f"{model}-{str(uuid.uuid4())}", @@ -149,6 +187,7 @@ def openai_chat_chunk_message_template( template["choices"][0]["delta"] = {"content": message} else: template["choices"][0]["finish_reason"] = "stop" + template["choices"][0]["delta"] = {} if usage: template["usage"] = usage diff --git a/backend/open_webui/utils/oauth.py b/backend/open_webui/utils/oauth.py index 1ae6d4aa7..7c0c53c2d 100644 --- a/backend/open_webui/utils/oauth.py +++ b/backend/open_webui/utils/oauth.py @@ -35,7 +35,7 @@ from open_webui.config import ( AppConfig, ) from open_webui.constants import ERROR_MESSAGES, WEBHOOK_MESSAGES -from open_webui.env import WEBUI_SESSION_COOKIE_SAME_SITE, WEBUI_SESSION_COOKIE_SECURE +from open_webui.env import WEBUI_AUTH_COOKIE_SAME_SITE, WEBUI_AUTH_COOKIE_SECURE from open_webui.utils.misc import parse_duration from open_webui.utils.auth import get_password_hash, create_token from open_webui.utils.webhook import post_webhook @@ -82,7 +82,8 @@ class OAuthManager: oauth_allowed_roles = auth_manager_config.OAUTH_ALLOWED_ROLES oauth_admin_roles = auth_manager_config.OAUTH_ADMIN_ROLES oauth_roles = None - role = "pending" # Default/fallback role if no matching roles are found + # Default/fallback role if no matching roles are found + role = auth_manager_config.DEFAULT_USER_ROLE # Next block extracts the roles from the user data, accepting nested claims of any depth if oauth_claim and oauth_allowed_roles and oauth_admin_roles: @@ -273,11 +274,16 @@ class OAuthManager: log.error( f"Error downloading profile image '{picture_url}': {e}" ) - picture_url = "" + picture_url = "/user.png" if not picture_url: picture_url = "/user.png" + username_claim = auth_manager_config.OAUTH_USERNAME_CLAIM + name = user_data.get(username_claim) + if not isinstance(user, str): + name = email + role = self.get_user_role(None, user_data) user = Auths.insert_new_auth( @@ -285,7 +291,7 @@ class OAuthManager: password=get_password_hash( str(uuid.uuid4()) ), # Random password, not used - name=user_data.get(username_claim, "User"), + name=name, profile_image_url=picture_url, role=role, oauth_sub=provider_sub, @@ -323,8 +329,8 @@ class OAuthManager: key="token", value=jwt_token, httponly=True, # Ensures the cookie is not accessible via JavaScript - samesite=WEBUI_SESSION_COOKIE_SAME_SITE, - secure=WEBUI_SESSION_COOKIE_SECURE, + samesite=WEBUI_AUTH_COOKIE_SAME_SITE, + secure=WEBUI_AUTH_COOKIE_SECURE, ) if ENABLE_OAUTH_SIGNUP.value: @@ -333,8 +339,8 @@ class OAuthManager: key="oauth_id_token", value=oauth_id_token, httponly=True, - samesite=WEBUI_SESSION_COOKIE_SAME_SITE, - secure=WEBUI_SESSION_COOKIE_SECURE, + samesite=WEBUI_AUTH_COOKIE_SAME_SITE, + secure=WEBUI_AUTH_COOKIE_SECURE, ) # Redirect back to the frontend with the JWT token redirect_url = f"{request.base_url}auth#token={jwt_token}" diff --git a/backend/open_webui/utils/payload.py b/backend/open_webui/utils/payload.py index 13f98ee01..2eb4622c2 100644 --- a/backend/open_webui/utils/payload.py +++ b/backend/open_webui/utils/payload.py @@ -1,4 +1,4 @@ -from open_webui.utils.task import prompt_template +from open_webui.utils.task import prompt_variables_template from open_webui.utils.misc import ( add_or_update_system_message, ) @@ -7,19 +7,18 @@ from typing import Callable, Optional # inplace function: form_data is modified -def apply_model_system_prompt_to_body(params: dict, form_data: dict, user) -> dict: +def apply_model_system_prompt_to_body( + params: dict, form_data: dict, metadata: Optional[dict] = None +) -> dict: system = params.get("system", None) if not system: return form_data - if user: - template_params = { - "user_name": user.name, - "user_location": user.info.get("location") if user.info else None, - } - else: - template_params = {} - system = prompt_template(system, **template_params) + if metadata: + print("apply_model_system_prompt_to_body: metadata", metadata) + variables = metadata.get("variables", {}) + system = prompt_variables_template(system, variables) + form_data["messages"] = add_or_update_system_message( system, form_data.get("messages", []) ) @@ -188,4 +187,7 @@ def convert_payload_openai_to_ollama(openai_payload: dict) -> dict: if ollama_options: ollama_payload["options"] = ollama_options + if "metadata" in openai_payload: + ollama_payload["metadata"] = openai_payload["metadata"] + return ollama_payload diff --git a/backend/open_webui/utils/plugin.py b/backend/open_webui/utils/plugin.py index 17b86cea1..d6e24d6b9 100644 --- a/backend/open_webui/utils/plugin.py +++ b/backend/open_webui/utils/plugin.py @@ -167,9 +167,14 @@ def load_function_module_by_id(function_id, content=None): def install_frontmatter_requirements(requirements): if requirements: - req_list = [req.strip() for req in requirements.split(",")] - for req in req_list: - log.info(f"Installing requirement: {req}") - subprocess.check_call([sys.executable, "-m", "pip", "install", req]) + try: + req_list = [req.strip() for req in requirements.split(",")] + for req in req_list: + log.info(f"Installing requirement: {req}") + subprocess.check_call([sys.executable, "-m", "pip", "install", req]) + except Exception as e: + log.error(f"Error installing package: {req}") + raise e + else: log.info("No requirements found in frontmatter.") diff --git a/backend/open_webui/utils/response.py b/backend/open_webui/utils/response.py index d6f7b0ac6..f461f7cc2 100644 --- a/backend/open_webui/utils/response.py +++ b/backend/open_webui/utils/response.py @@ -9,7 +9,48 @@ def convert_response_ollama_to_openai(ollama_response: dict) -> dict: model = ollama_response.get("model", "ollama") message_content = ollama_response.get("message", {}).get("content", "") - response = openai_chat_completion_message_template(model, message_content) + data = ollama_response + usage = { + "response_token/s": ( + round( + ( + ( + data.get("eval_count", 0) + / ((data.get("eval_duration", 0) / 10_000_000)) + ) + * 100 + ), + 2, + ) + if data.get("eval_duration", 0) > 0 + else "N/A" + ), + "prompt_token/s": ( + round( + ( + ( + data.get("prompt_eval_count", 0) + / ((data.get("prompt_eval_duration", 0) / 10_000_000)) + ) + * 100 + ), + 2, + ) + if data.get("prompt_eval_duration", 0) > 0 + else "N/A" + ), + "total_duration": data.get("total_duration", 0), + "load_duration": data.get("load_duration", 0), + "prompt_eval_count": data.get("prompt_eval_count", 0), + "prompt_eval_duration": data.get("prompt_eval_duration", 0), + "eval_count": data.get("eval_count", 0), + "eval_duration": data.get("eval_duration", 0), + "approximate_total": (lambda s: f"{s // 3600}h{(s % 3600) // 60}m{s % 60}s")( + (data.get("total_duration", 0) or 0) // 1_000_000_000 + ), + } + + response = openai_chat_completion_message_template(model, message_content, usage) return response diff --git a/backend/open_webui/utils/task.py b/backend/open_webui/utils/task.py index f5ba75ebe..3d8c05d45 100644 --- a/backend/open_webui/utils/task.py +++ b/backend/open_webui/utils/task.py @@ -32,6 +32,12 @@ def get_task_model_id( return task_model_id +def prompt_variables_template(template: str, variables: dict[str, str]) -> str: + for variable, value in variables.items(): + template = template.replace(variable, value) + return template + + def prompt_template( template: str, user_name: Optional[str] = None, user_location: Optional[str] = None ) -> str: diff --git a/backend/requirements.txt b/backend/requirements.txt index eecb9c4a5..cb6caffa6 100644 --- a/backend/requirements.txt +++ b/backend/requirements.txt @@ -1,4 +1,4 @@ -fastapi==0.111.0 +fastapi==0.115.7 uvicorn[standard]==0.30.6 pydantic==2.9.2 python-multipart==0.0.18 @@ -11,7 +11,7 @@ python-jose==3.3.0 passlib[bcrypt]==1.7.4 requests==2.32.3 -aiohttp==3.11.8 +aiohttp==3.11.11 async-timeout aiocache aiofiles @@ -57,7 +57,7 @@ einops==0.8.0 ftfy==6.2.3 pypdf==4.3.1 fpdf2==2.8.2 -pymdown-extensions==10.11.2 +pymdown-extensions==10.14.2 docx2txt==0.8 python-pptx==1.0.0 unstructured==0.15.9 @@ -71,16 +71,16 @@ xlrd==2.0.1 validators==0.34.0 psutil sentencepiece -soundfile==0.12.1 +soundfile==0.13.1 -opencv-python-headless==4.10.0.84 +opencv-python-headless==4.11.0.86 rapidocr-onnxruntime==1.3.24 rank-bm25==0.2.2 faster-whisper==1.0.3 PyJWT[crypto]==2.10.1 -authlib==1.3.2 +authlib==1.4.1 black==24.8.0 langfuse==2.44.0 @@ -89,7 +89,7 @@ pytube==15.0.0 extract_msg pydub -duckduckgo-search~=7.2.1 +duckduckgo-search~=7.3.0 ## Google Drive google-api-python-client diff --git a/package-lock.json b/package-lock.json index c98e814d9..57bcf2906 100644 --- a/package-lock.json +++ b/package-lock.json @@ -56,7 +56,7 @@ "prosemirror-schema-list": "^1.4.1", "prosemirror-state": "^1.4.3", "prosemirror-view": "^1.34.3", - "pyodide": "^0.26.1", + "pyodide": "^0.27.2", "socket.io-client": "^4.2.0", "sortablejs": "^1.15.2", "svelte-sonner": "^0.3.19", @@ -9366,9 +9366,10 @@ } }, "node_modules/pyodide": { - "version": "0.26.1", - "resolved": "https://registry.npmjs.org/pyodide/-/pyodide-0.26.1.tgz", - "integrity": "sha512-P+Gm88nwZqY7uBgjbQH8CqqU6Ei/rDn7pS1t02sNZsbyLJMyE2OVXjgNuqVT3KqYWnyGREUN0DbBUCJqk8R0ew==", + "version": "0.27.2", + "resolved": "https://registry.npmjs.org/pyodide/-/pyodide-0.27.2.tgz", + "integrity": "sha512-sfA2kiUuQVRpWI4BYnU3sX5PaTTt/xrcVEmRzRcId8DzZXGGtPgCBC0gCqjUTUYSa8ofPaSjXmzESc86yvvCHg==", + "license": "Apache-2.0", "dependencies": { "ws": "^8.5.0" }, diff --git a/package.json b/package.json index a2463d9e3..a28091668 100644 --- a/package.json +++ b/package.json @@ -98,7 +98,7 @@ "prosemirror-schema-list": "^1.4.1", "prosemirror-state": "^1.4.3", "prosemirror-view": "^1.34.3", - "pyodide": "^0.26.1", + "pyodide": "^0.27.2", "socket.io-client": "^4.2.0", "sortablejs": "^1.15.2", "svelte-sonner": "^0.3.19", diff --git a/pyproject.toml b/pyproject.toml index edd01db8f..116c0b462 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,7 +6,7 @@ authors = [ ] license = { file = "LICENSE" } dependencies = [ - "fastapi==0.111.0", + "fastapi==0.115.7", "uvicorn[standard]==0.30.6", "pydantic==2.9.2", "python-multipart==0.0.18", @@ -19,7 +19,7 @@ dependencies = [ "passlib[bcrypt]==1.7.4", "requests==2.32.3", - "aiohttp==3.11.8", + "aiohttp==3.11.11", "async-timeout", "aiocache", "aiofiles", @@ -62,7 +62,7 @@ dependencies = [ "ftfy==6.2.3", "pypdf==4.3.1", "fpdf2==2.8.2", - "pymdown-extensions==10.11.2", + "pymdown-extensions==10.14.2", "docx2txt==0.8", "python-pptx==1.0.0", "unstructured==0.15.9", @@ -76,16 +76,16 @@ dependencies = [ "validators==0.34.0", "psutil", "sentencepiece", - "soundfile==0.12.1", + "soundfile==0.13.1", - "opencv-python-headless==4.10.0.84", + "opencv-python-headless==4.11.0.86", "rapidocr-onnxruntime==1.3.24", "rank-bm25==0.2.2", "faster-whisper==1.0.3", "PyJWT[crypto]==2.10.1", - "authlib==1.3.2", + "authlib==1.4.1", "black==24.8.0", "langfuse==2.44.0", @@ -94,7 +94,7 @@ dependencies = [ "extract_msg", "pydub", - "duckduckgo-search~=7.2.1", + "duckduckgo-search~=7.3.0", "google-api-python-client", "google-auth-httplib2", diff --git a/src/lib/apis/chats/index.ts b/src/lib/apis/chats/index.ts index 1772529d3..7af504cc7 100644 --- a/src/lib/apis/chats/index.ts +++ b/src/lib/apis/chats/index.ts @@ -580,7 +580,7 @@ export const toggleChatPinnedStatusById = async (token: string, id: string) => { return res; }; -export const cloneChatById = async (token: string, id: string) => { +export const cloneChatById = async (token: string, id: string, title?: string) => { let error = null; const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}/clone`, { @@ -589,7 +589,10 @@ export const cloneChatById = async (token: string, id: string) => { Accept: 'application/json', 'Content-Type': 'application/json', ...(token && { authorization: `Bearer ${token}` }) - } + }, + body: JSON.stringify({ + ...(title && { title: title }) + }) }) .then(async (res) => { if (!res.ok) throw await res.json(); diff --git a/src/lib/apis/models/index.ts b/src/lib/apis/models/index.ts index 5880874bb..9cf625d03 100644 --- a/src/lib/apis/models/index.ts +++ b/src/lib/apis/models/index.ts @@ -219,7 +219,7 @@ export const deleteModelById = async (token: string, id: string) => { return json; }) .catch((err) => { - error = err; + error = err.detail; console.log(err); return null; diff --git a/src/lib/components/admin/Settings/Audio.svelte b/src/lib/components/admin/Settings/Audio.svelte index a7a030027..f3b7e982c 100644 --- a/src/lib/components/admin/Settings/Audio.svelte +++ b/src/lib/components/admin/Settings/Audio.svelte @@ -51,7 +51,7 @@ models = []; } else { const res = await _getModels(localStorage.token).catch((e) => { - toast.error(e); + toast.error(`${e}`); }); if (res) { @@ -74,7 +74,7 @@ }, 100); } else { const res = await _getVoices(localStorage.token).catch((e) => { - toast.error(e); + toast.error(`${e}`); }); if (res) { diff --git a/src/lib/components/admin/Settings/Interface.svelte b/src/lib/components/admin/Settings/Interface.svelte index 055acbf80..332d02c5a 100644 --- a/src/lib/components/admin/Settings/Interface.svelte +++ b/src/lib/components/admin/Settings/Interface.svelte @@ -31,7 +31,8 @@ ENABLE_TAGS_GENERATION: true, ENABLE_SEARCH_QUERY_GENERATION: true, ENABLE_RETRIEVAL_QUERY_GENERATION: true, - QUERY_GENERATION_PROMPT_TEMPLATE: '' + QUERY_GENERATION_PROMPT_TEMPLATE: '', + TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE: '' }; let promptSuggestions = []; @@ -251,6 +252,20 @@ +
+
{$i18n.t('Tools Function Calling Prompt')}
+ + +