open-webui / open-webui

User-friendly AI Interface (Supports Ollama, OpenAI API, ...)
https://openwebui.com
MIT License
44.86k stars 5.46k forks source link

Set as default' button overrides UserValves function settings #4523

Closed zspine closed 2 months ago

zspine commented 2 months ago

Bug Report

When using the 'Set as default' button from the main chat screen, it overrides all previously saved UserValves function settings.

Installation Method

Docker

Environment

Expected Behavior:

When any action triggers the '/user/settings/update' API endpoint, it should preserve all previously stored user settings.

Actual Behavior:

Any action triggering the '/user/settings/update' API endpoint stores only the values related to 'ui' and wipes all previously saved key values for other settings.

Specifically, when the 'Set as default' button is clicked, it clears all previously saved UserValves function settings, including custom function configurations and API key values.

Reproduction Details

Steps to Reproduce:

  1. Navigate to the /workspace/functions/ workspace.
  2. Add the OpenAI Manifold function (https://openwebui.com/f/hub/openai_manifold/) and enable it. open-webui-user-settings
  3. Return to the main app page.
  4. Click the controls button, expand the 'Valves' section, and set the first select option to 'functions'. Then, select 'OpenAI Manifold' from the functions select dropdown.
  5. Set any value for the 'OPENAI_API_KEY' text input by clicking the custom button and pressing enter to save.
  6. Refresh the page and verify that the 'OPENAI_API_KEY' value is retained by opening the 'Controls' section again.
  7. Click the 'Set as default' button.
  8. Refresh the page again and verify that the 'OPENAI_API_KEY' value has been cleared by opening the 'Controls' section again.

Following these steps should reproduce the issue. The 'Set as default' button should preserve all previously saved user settings, including custom function configurations and API key values. Instead, it currently overrides these settings, leading to data loss.

zspine commented 2 months ago

The issue can be temporarily resolved by using the following workaround:

# backend/apps/webui/routers/users.py

@router.post("/user/settings/update", response_model=UserSettings)
async def update_user_settings_by_session_user(
    form_data: UserSettings, user=Depends(get_verified_user)
):
    # Get the current user settings
    current_user = Users.get_user_by_id(user.id)
    if not current_user:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail=ERROR_MESSAGES.USER_NOT_FOUND,
        )

    # Get the current settings or initialize if None
    current_settings = current_user.settings.model_dump() if current_user.settings else {"ui": {},
                                                                                         "functions": {"valves": {}}}

    # Update the settings
    new_settings = form_data.model_dump()

    # Update 'ui' if present in new settings
    if "ui" in new_settings:
        current_settings["ui"].update(new_settings["ui"])

    # Update 'functions.valves' if present in new settings
    if "functions" in new_settings and "valves" in new_settings["functions"]:
        if "functions" not in current_settings:
            current_settings["functions"] = {"valves": {}}
        current_settings["functions"]["valves"].update(new_settings["functions"]["valves"])

    # Update the user in the database
    updated_user = Users.update_user_by_id(user.id, {"settings": current_settings})

    if updated_user:
        return updated_user.settings
    else:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail=ERROR_MESSAGES.USER_NOT_FOUND,
        )