Skip to content

Commit 5e78a5c

Browse files
tjbckrecrudesce
andcommitted
enh: enable_code_execution toggle
Co-Authored-By: recrudesce <[email protected]>
1 parent dce2422 commit 5e78a5c

File tree

6 files changed

+28
-3
lines changed

6 files changed

+28
-3
lines changed

backend/open_webui/config.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1377,6 +1377,11 @@ class BannerModel(BaseModel):
13771377
# Code Interpreter
13781378
####################################
13791379

1380+
ENABLE_CODE_EXECUTION = PersistentConfig(
1381+
"ENABLE_CODE_EXECUTION",
1382+
"code_execution.enable",
1383+
os.environ.get("ENABLE_CODE_EXECUTION", "True").lower() == "true",
1384+
)
13801385

13811386
CODE_EXECUTION_ENGINE = PersistentConfig(
13821387
"CODE_EXECUTION_ENGINE",
@@ -1553,7 +1558,9 @@ class BannerModel(BaseModel):
15531558
ELASTICSEARCH_PASSWORD = os.environ.get("ELASTICSEARCH_PASSWORD", None)
15541559
ELASTICSEARCH_CLOUD_ID = os.environ.get("ELASTICSEARCH_CLOUD_ID", None)
15551560
SSL_ASSERT_FINGERPRINT = os.environ.get("SSL_ASSERT_FINGERPRINT", None)
1556-
ELASTICSEARCH_INDEX_PREFIX = os.environ.get("ELASTICSEARCH_INDEX_PREFIX", "open_webui_collections")
1561+
ELASTICSEARCH_INDEX_PREFIX = os.environ.get(
1562+
"ELASTICSEARCH_INDEX_PREFIX", "open_webui_collections"
1563+
)
15571564
# Pgvector
15581565
PGVECTOR_DB_URL = os.environ.get("PGVECTOR_DB_URL", DATABASE_URL)
15591566
if VECTOR_DB == "pgvector" and not PGVECTOR_DB_URL.startswith("postgres"):

backend/open_webui/main.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
# Direct Connections
106106
ENABLE_DIRECT_CONNECTIONS,
107107
# Code Execution
108+
ENABLE_CODE_EXECUTION,
108109
CODE_EXECUTION_ENGINE,
109110
CODE_EXECUTION_JUPYTER_URL,
110111
CODE_EXECUTION_JUPYTER_AUTH,
@@ -660,6 +661,7 @@ async def lifespan(app: FastAPI):
660661
#
661662
########################################
662663

664+
app.state.config.ENABLE_CODE_EXECUTION = ENABLE_CODE_EXECUTION
663665
app.state.config.CODE_EXECUTION_ENGINE = CODE_EXECUTION_ENGINE
664666
app.state.config.CODE_EXECUTION_JUPYTER_URL = CODE_EXECUTION_JUPYTER_URL
665667
app.state.config.CODE_EXECUTION_JUPYTER_AUTH = CODE_EXECUTION_JUPYTER_AUTH
@@ -1173,6 +1175,7 @@ async def get_app_config(request: Request):
11731175
"enable_direct_connections": app.state.config.ENABLE_DIRECT_CONNECTIONS,
11741176
"enable_channels": app.state.config.ENABLE_CHANNELS,
11751177
"enable_web_search": app.state.config.ENABLE_RAG_WEB_SEARCH,
1178+
"enable_code_execution": app.state.config.ENABLE_CODE_EXECUTION,
11761179
"enable_code_interpreter": app.state.config.ENABLE_CODE_INTERPRETER,
11771180
"enable_image_generation": app.state.config.ENABLE_IMAGE_GENERATION,
11781181
"enable_autocomplete_generation": app.state.config.ENABLE_AUTOCOMPLETE_GENERATION,

backend/open_webui/routers/configs.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ async def set_direct_connections_config(
7070
# CodeInterpreterConfig
7171
############################
7272
class CodeInterpreterConfigForm(BaseModel):
73+
ENABLE_CODE_EXECUTION: bool
7374
CODE_EXECUTION_ENGINE: str
7475
CODE_EXECUTION_JUPYTER_URL: Optional[str]
7576
CODE_EXECUTION_JUPYTER_AUTH: Optional[str]
@@ -89,6 +90,7 @@ class CodeInterpreterConfigForm(BaseModel):
8990
@router.get("/code_execution", response_model=CodeInterpreterConfigForm)
9091
async def get_code_execution_config(request: Request, user=Depends(get_admin_user)):
9192
return {
93+
"ENABLE_CODE_EXECUTION": request.app.state.config.ENABLE_CODE_EXECUTION,
9294
"CODE_EXECUTION_ENGINE": request.app.state.config.CODE_EXECUTION_ENGINE,
9395
"CODE_EXECUTION_JUPYTER_URL": request.app.state.config.CODE_EXECUTION_JUPYTER_URL,
9496
"CODE_EXECUTION_JUPYTER_AUTH": request.app.state.config.CODE_EXECUTION_JUPYTER_AUTH,
@@ -111,6 +113,8 @@ async def set_code_execution_config(
111113
request: Request, form_data: CodeInterpreterConfigForm, user=Depends(get_admin_user)
112114
):
113115

116+
request.app.state.config.ENABLE_CODE_EXECUTION = form_data.ENABLE_CODE_EXECUTION
117+
114118
request.app.state.config.CODE_EXECUTION_ENGINE = form_data.CODE_EXECUTION_ENGINE
115119
request.app.state.config.CODE_EXECUTION_JUPYTER_URL = (
116120
form_data.CODE_EXECUTION_JUPYTER_URL
@@ -153,6 +157,7 @@ async def set_code_execution_config(
153157
)
154158

155159
return {
160+
"ENABLE_CODE_EXECUTION": request.app.state.config.ENABLE_CODE_EXECUTION,
156161
"CODE_EXECUTION_ENGINE": request.app.state.config.CODE_EXECUTION_ENGINE,
157162
"CODE_EXECUTION_JUPYTER_URL": request.app.state.config.CODE_EXECUTION_JUPYTER_URL,
158163
"CODE_EXECUTION_JUPYTER_AUTH": request.app.state.config.CODE_EXECUTION_JUPYTER_AUTH,

src/lib/components/admin/Settings/CodeExecution.svelte

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@
4545

4646
<hr class=" border-gray-100 dark:border-gray-850 my-2" />
4747

48+
<div class="mb-2.5">
49+
<div class=" flex w-full justify-between">
50+
<div class=" self-center text-xs font-medium">
51+
{$i18n.t('Enable Code Execution')}
52+
</div>
53+
54+
<Switch bind:state={config.ENABLE_CODE_EXECUTION} />
55+
</div>
56+
</div>
57+
4858
<div class="mb-2.5">
4959
<div class="flex w-full justify-between">
5060
<div class=" self-center text-xs font-medium">{$i18n.t('Code Execution Engine')}</div>

src/lib/components/chat/Chat.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1937,7 +1937,7 @@
19371937

19381938
<PaneGroup direction="horizontal" class="w-full h-full">
19391939
<Pane defaultSize={50} class="h-full flex w-full relative">
1940-
{#if ($banners.length > 0 && !history.currentId && !$chatId && selectedModels.length <= 1) || ($config?.license_metadata?.type ?? null) === 'trial' || (($config?.license_metadata?.seats ?? null) !== null && $config?.user_count > $config?.license_metadata?.seats)}
1940+
{#if !history.currentId && !$chatId && selectedModels.length <= 1 && ($banners.length > 0 || ($config?.license_metadata?.type ?? null) === 'trial' || (($config?.license_metadata?.seats ?? null) !== null && $config?.user_count > $config?.license_metadata?.seats))}
19411941
<div class="absolute top-12 left-0 right-0 w-full z-30">
19421942
<div class=" flex flex-col gap-1 w-full">
19431943
{#if ($config?.license_metadata?.type ?? null) === 'trial'}

src/lib/components/chat/Messages/CodeBlock.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@
439439
</div>
440440
</button>
441441

442-
{#if lang.toLowerCase() === 'python' || lang.toLowerCase() === 'py' || (lang === '' && checkPythonCode(code))}
442+
{#if ($config?.features?.enable_code_execution ?? true) && (lang.toLowerCase() === 'python' || lang.toLowerCase() === 'py' || (lang === '' && checkPythonCode(code)))}
443443
{#if executing}
444444
<div class="run-code-button bg-none border-none p-1 cursor-not-allowed">Running</div>
445445
{:else if run}

0 commit comments

Comments
 (0)