Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
chenpangpang
open-webui
Commits
d362fd02
Commit
d362fd02
authored
Jun 22, 2024
by
Timothy J. Baek
Browse files
feat: user valves integration
parent
15fc23df
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
375 additions
and
37 deletions
+375
-37
backend/apps/webui/models/functions.py
backend/apps/webui/models/functions.py
+16
-12
backend/apps/webui/models/tools.py
backend/apps/webui/models/tools.py
+16
-12
backend/apps/webui/routers/functions.py
backend/apps/webui/routers/functions.py
+3
-4
src/lib/apis/functions/index.ts
src/lib/apis/functions/index.ts
+99
-0
src/lib/apis/tools/index.ts
src/lib/apis/tools/index.ts
+99
-0
src/lib/components/chat/Settings/Valves.svelte
src/lib/components/chat/Settings/Valves.svelte
+142
-9
No files found.
backend/apps/webui/models/functions.py
View file @
d362fd02
...
@@ -8,6 +8,8 @@ from apps.webui.internal.db import DB, JSONField
...
@@ -8,6 +8,8 @@ from apps.webui.internal.db import DB, JSONField
from
apps.webui.models.users
import
Users
from
apps.webui.models.users
import
Users
import
json
import
json
import
copy
from
config
import
SRC_LOG_LEVELS
from
config
import
SRC_LOG_LEVELS
...
@@ -121,14 +123,15 @@ class FunctionsTable:
...
@@ -121,14 +123,15 @@ class FunctionsTable:
)
->
Optional
[
dict
]:
)
->
Optional
[
dict
]:
try
:
try
:
user
=
Users
.
get_user_by_id
(
user_id
)
user
=
Users
.
get_user_by_id
(
user_id
)
user_settings
=
user
.
settings
.
model_dump
()
# Check if user has "functions" and "valves" settings
# Check if user has "functions" and "valves" settings
if
"functions"
not
in
user
.
settings
:
if
"functions"
not
in
user
_
settings
:
user
.
settings
[
"functions"
]
=
{}
user
_
settings
[
"functions"
]
=
{}
if
"valves"
not
in
user
.
settings
[
"functions"
]:
if
"valves"
not
in
user
_
settings
[
"functions"
]:
user
.
settings
[
"functions"
][
"valves"
]
=
{}
user
_
settings
[
"functions"
][
"valves"
]
=
{}
return
user
.
settings
[
"functions"
][
"valves"
].
get
(
id
,
{})
return
user
_
settings
[
"functions"
][
"valves"
].
get
(
id
,
{})
except
Exception
as
e
:
except
Exception
as
e
:
print
(
f
"An error occurred:
{
e
}
"
)
print
(
f
"An error occurred:
{
e
}
"
)
return
None
return
None
...
@@ -138,20 +141,21 @@ class FunctionsTable:
...
@@ -138,20 +141,21 @@ class FunctionsTable:
)
->
Optional
[
dict
]:
)
->
Optional
[
dict
]:
try
:
try
:
user
=
Users
.
get_user_by_id
(
user_id
)
user
=
Users
.
get_user_by_id
(
user_id
)
user_settings
=
user
.
settings
.
model_dump
()
# Check if user has "functions" and "valves" settings
# Check if user has "functions" and "valves" settings
if
"functions"
not
in
user
.
settings
:
if
"functions"
not
in
user
_
settings
:
user
.
settings
[
"functions"
]
=
{}
user
_
settings
[
"functions"
]
=
{}
if
"valves"
not
in
user
.
settings
[
"functions"
]:
if
"valves"
not
in
user
_
settings
[
"functions"
]:
user
.
settings
[
"functions"
][
"valves"
]
=
{}
user
_
settings
[
"functions"
][
"valves"
]
=
{}
user
.
settings
[
"functions"
][
"valves"
][
id
]
=
valves
user
_
settings
[
"functions"
][
"valves"
][
id
]
=
valves
# Update the user settings in the database
# Update the user settings in the database
query
=
Users
.
update_user_by_id
(
user_id
,
{
"settings"
:
user
.
settings
})
query
=
Users
.
update_user_by_id
(
user_id
,
{
"settings"
:
user
_
settings
})
query
.
execute
()
query
.
execute
()
return
user
.
settings
[
"functions"
][
"valves"
][
id
]
return
user
_
settings
[
"functions"
][
"valves"
][
id
]
except
Exception
as
e
:
except
Exception
as
e
:
print
(
f
"An error occurred:
{
e
}
"
)
print
(
f
"An error occurred:
{
e
}
"
)
return
None
return
None
...
...
backend/apps/webui/models/tools.py
View file @
d362fd02
...
@@ -8,6 +8,8 @@ from apps.webui.internal.db import DB, JSONField
...
@@ -8,6 +8,8 @@ from apps.webui.internal.db import DB, JSONField
from
apps.webui.models.users
import
Users
from
apps.webui.models.users
import
Users
import
json
import
json
import
copy
from
config
import
SRC_LOG_LEVELS
from
config
import
SRC_LOG_LEVELS
...
@@ -112,14 +114,15 @@ class ToolsTable:
...
@@ -112,14 +114,15 @@ class ToolsTable:
)
->
Optional
[
dict
]:
)
->
Optional
[
dict
]:
try
:
try
:
user
=
Users
.
get_user_by_id
(
user_id
)
user
=
Users
.
get_user_by_id
(
user_id
)
user_settings
=
user
.
settings
.
model_dump
()
# Check if user has "tools" and "valves" settings
# Check if user has "tools" and "valves" settings
if
"tools"
not
in
user
.
settings
:
if
"tools"
not
in
user
_
settings
:
user
.
settings
[
"tools"
]
=
{}
user
_
settings
[
"tools"
]
=
{}
if
"valves"
not
in
user
.
settings
[
"tools"
]:
if
"valves"
not
in
user
_
settings
[
"tools"
]:
user
.
settings
[
"tools"
][
"valves"
]
=
{}
user
_
settings
[
"tools"
][
"valves"
]
=
{}
return
user
.
settings
[
"tools"
][
"valves"
].
get
(
id
,
{})
return
user
_
settings
[
"tools"
][
"valves"
].
get
(
id
,
{})
except
Exception
as
e
:
except
Exception
as
e
:
print
(
f
"An error occurred:
{
e
}
"
)
print
(
f
"An error occurred:
{
e
}
"
)
return
None
return
None
...
@@ -129,20 +132,21 @@ class ToolsTable:
...
@@ -129,20 +132,21 @@ class ToolsTable:
)
->
Optional
[
dict
]:
)
->
Optional
[
dict
]:
try
:
try
:
user
=
Users
.
get_user_by_id
(
user_id
)
user
=
Users
.
get_user_by_id
(
user_id
)
user_settings
=
user
.
settings
.
model_dump
()
# Check if user has "tools" and "valves" settings
# Check if user has "tools" and "valves" settings
if
"tools"
not
in
user
.
settings
:
if
"tools"
not
in
user
_
settings
:
user
.
settings
[
"tools"
]
=
{}
user
_
settings
[
"tools"
]
=
{}
if
"valves"
not
in
user
.
settings
[
"tools"
]:
if
"valves"
not
in
user
_
settings
[
"tools"
]:
user
.
settings
[
"tools"
][
"valves"
]
=
{}
user
_
settings
[
"tools"
][
"valves"
]
=
{}
user
.
settings
[
"tools"
][
"valves"
][
id
]
=
valves
user
_
settings
[
"tools"
][
"valves"
][
id
]
=
valves
# Update the user settings in the database
# Update the user settings in the database
query
=
Users
.
update_user_by_id
(
user_id
,
{
"settings"
:
user
.
settings
})
query
=
Users
.
update_user_by_id
(
user_id
,
{
"settings"
:
user
_
settings
})
query
.
execute
()
query
.
execute
()
return
user
.
settings
[
"tools"
][
"valves"
][
id
]
return
user
_
settings
[
"tools"
][
"valves"
][
id
]
except
Exception
as
e
:
except
Exception
as
e
:
print
(
f
"An error occurred:
{
e
}
"
)
print
(
f
"An error occurred:
{
e
}
"
)
return
None
return
None
...
...
backend/apps/webui/routers/functions.py
View file @
d362fd02
...
@@ -145,7 +145,7 @@ async def get_function_user_valves_by_id(id: str, user=Depends(get_verified_user
...
@@ -145,7 +145,7 @@ async def get_function_user_valves_by_id(id: str, user=Depends(get_verified_user
async
def
get_function_user_valves_spec_by_id
(
async
def
get_function_user_valves_spec_by_id
(
request
:
Request
,
id
:
str
,
user
=
Depends
(
get_verified_user
)
request
:
Request
,
id
:
str
,
user
=
Depends
(
get_verified_user
)
):
):
function
=
Functions
.
get_
tool
_by_id
(
id
)
function
=
Functions
.
get_
function
_by_id
(
id
)
if
function
:
if
function
:
if
id
in
request
.
app
.
state
.
FUNCTIONS
:
if
id
in
request
.
app
.
state
.
FUNCTIONS
:
function_module
=
request
.
app
.
state
.
FUNCTIONS
[
id
]
function_module
=
request
.
app
.
state
.
FUNCTIONS
[
id
]
...
@@ -168,8 +168,7 @@ async def get_function_user_valves_spec_by_id(
...
@@ -168,8 +168,7 @@ async def get_function_user_valves_spec_by_id(
async
def
update_function_user_valves_by_id
(
async
def
update_function_user_valves_by_id
(
request
:
Request
,
id
:
str
,
form_data
:
dict
,
user
=
Depends
(
get_verified_user
)
request
:
Request
,
id
:
str
,
form_data
:
dict
,
user
=
Depends
(
get_verified_user
)
):
):
function
=
Functions
.
get_function_by_id
(
id
)
function
=
Functions
.
get_tool_by_id
(
id
)
if
function
:
if
function
:
if
id
in
request
.
app
.
state
.
FUNCTIONS
:
if
id
in
request
.
app
.
state
.
FUNCTIONS
:
...
@@ -211,7 +210,7 @@ async def update_function_user_valves_by_id(
...
@@ -211,7 +210,7 @@ async def update_function_user_valves_by_id(
@
router
.
post
(
"/id/{id}/update"
,
response_model
=
Optional
[
FunctionModel
])
@
router
.
post
(
"/id/{id}/update"
,
response_model
=
Optional
[
FunctionModel
])
async
def
update_
toolkit
_by_id
(
async
def
update_
function
_by_id
(
request
:
Request
,
id
:
str
,
form_data
:
FunctionForm
,
user
=
Depends
(
get_admin_user
)
request
:
Request
,
id
:
str
,
form_data
:
FunctionForm
,
user
=
Depends
(
get_admin_user
)
):
):
function_path
=
os
.
path
.
join
(
FUNCTIONS_DIR
,
f
"
{
id
}
.py"
)
function_path
=
os
.
path
.
join
(
FUNCTIONS_DIR
,
f
"
{
id
}
.py"
)
...
...
src/lib/apis/functions/index.ts
View file @
d362fd02
...
@@ -191,3 +191,102 @@ export const deleteFunctionById = async (token: string, id: string) => {
...
@@ -191,3 +191,102 @@ export const deleteFunctionById = async (token: string, id: string) => {
return
res
;
return
res
;
};
};
export
const
getUserValvesById
=
async
(
token
:
string
,
id
:
string
)
=>
{
let
error
=
null
;
const
res
=
await
fetch
(
`
${
WEBUI_API_BASE_URL
}
/functions/id/
${
id
}
/valves/user`
,
{
method
:
'
GET
'
,
headers
:
{
Accept
:
'
application/json
'
,
'
Content-Type
'
:
'
application/json
'
,
authorization
:
`Bearer
${
token
}
`
}
})
.
then
(
async
(
res
)
=>
{
if
(
!
res
.
ok
)
throw
await
res
.
json
();
return
res
.
json
();
})
.
then
((
json
)
=>
{
return
json
;
})
.
catch
((
err
)
=>
{
error
=
err
.
detail
;
console
.
log
(
err
);
return
null
;
});
if
(
error
)
{
throw
error
;
}
return
res
;
};
export
const
getUserValvesSpecById
=
async
(
token
:
string
,
id
:
string
)
=>
{
let
error
=
null
;
const
res
=
await
fetch
(
`
${
WEBUI_API_BASE_URL
}
/functions/id/
${
id
}
/valves/user/spec`
,
{
method
:
'
GET
'
,
headers
:
{
Accept
:
'
application/json
'
,
'
Content-Type
'
:
'
application/json
'
,
authorization
:
`Bearer
${
token
}
`
}
})
.
then
(
async
(
res
)
=>
{
if
(
!
res
.
ok
)
throw
await
res
.
json
();
return
res
.
json
();
})
.
then
((
json
)
=>
{
return
json
;
})
.
catch
((
err
)
=>
{
error
=
err
.
detail
;
console
.
log
(
err
);
return
null
;
});
if
(
error
)
{
throw
error
;
}
return
res
;
};
export
const
updateUserValvesById
=
async
(
token
:
string
,
id
:
string
,
valves
:
object
)
=>
{
let
error
=
null
;
const
res
=
await
fetch
(
`
${
WEBUI_API_BASE_URL
}
/functions/id/
${
id
}
/valves/user/update`
,
{
method
:
'
POST
'
,
headers
:
{
Accept
:
'
application/json
'
,
'
Content-Type
'
:
'
application/json
'
,
authorization
:
`Bearer
${
token
}
`
},
body
:
JSON
.
stringify
({
...
valves
})
})
.
then
(
async
(
res
)
=>
{
if
(
!
res
.
ok
)
throw
await
res
.
json
();
return
res
.
json
();
})
.
then
((
json
)
=>
{
return
json
;
})
.
catch
((
err
)
=>
{
error
=
err
.
detail
;
console
.
log
(
err
);
return
null
;
});
if
(
error
)
{
throw
error
;
}
return
res
;
};
src/lib/apis/tools/index.ts
View file @
d362fd02
...
@@ -191,3 +191,102 @@ export const deleteToolById = async (token: string, id: string) => {
...
@@ -191,3 +191,102 @@ export const deleteToolById = async (token: string, id: string) => {
return
res
;
return
res
;
};
};
export
const
getUserValvesById
=
async
(
token
:
string
,
id
:
string
)
=>
{
let
error
=
null
;
const
res
=
await
fetch
(
`
${
WEBUI_API_BASE_URL
}
/tools/id/
${
id
}
/valves/user`
,
{
method
:
'
GET
'
,
headers
:
{
Accept
:
'
application/json
'
,
'
Content-Type
'
:
'
application/json
'
,
authorization
:
`Bearer
${
token
}
`
}
})
.
then
(
async
(
res
)
=>
{
if
(
!
res
.
ok
)
throw
await
res
.
json
();
return
res
.
json
();
})
.
then
((
json
)
=>
{
return
json
;
})
.
catch
((
err
)
=>
{
error
=
err
.
detail
;
console
.
log
(
err
);
return
null
;
});
if
(
error
)
{
throw
error
;
}
return
res
;
};
export
const
getUserValvesSpecById
=
async
(
token
:
string
,
id
:
string
)
=>
{
let
error
=
null
;
const
res
=
await
fetch
(
`
${
WEBUI_API_BASE_URL
}
/tools/id/
${
id
}
/valves/user/spec`
,
{
method
:
'
GET
'
,
headers
:
{
Accept
:
'
application/json
'
,
'
Content-Type
'
:
'
application/json
'
,
authorization
:
`Bearer
${
token
}
`
}
})
.
then
(
async
(
res
)
=>
{
if
(
!
res
.
ok
)
throw
await
res
.
json
();
return
res
.
json
();
})
.
then
((
json
)
=>
{
return
json
;
})
.
catch
((
err
)
=>
{
error
=
err
.
detail
;
console
.
log
(
err
);
return
null
;
});
if
(
error
)
{
throw
error
;
}
return
res
;
};
export
const
updateUserValvesById
=
async
(
token
:
string
,
id
:
string
,
valves
:
object
)
=>
{
let
error
=
null
;
const
res
=
await
fetch
(
`
${
WEBUI_API_BASE_URL
}
/tools/id/
${
id
}
/valves/user/update`
,
{
method
:
'
POST
'
,
headers
:
{
Accept
:
'
application/json
'
,
'
Content-Type
'
:
'
application/json
'
,
authorization
:
`Bearer
${
token
}
`
},
body
:
JSON
.
stringify
({
...
valves
})
})
.
then
(
async
(
res
)
=>
{
if
(
!
res
.
ok
)
throw
await
res
.
json
();
return
res
.
json
();
})
.
then
((
json
)
=>
{
return
json
;
})
.
catch
((
err
)
=>
{
error
=
err
.
detail
;
console
.
log
(
err
);
return
null
;
});
if
(
error
)
{
throw
error
;
}
return
res
;
};
src/lib/components/chat/Settings/Valves.svelte
View file @
d362fd02
<script lang="ts">
<script lang="ts">
import { getBackendConfig } from '$lib/apis';
import { toast } from 'svelte-sonner';
import { setDefaultPromptSuggestions } from '$lib/apis/configs';
import Switch from '$lib/components/common/Switch.svelte';
import { config, functions, models, settings, tools, user } from '$lib/stores';
import { config, functions, models, settings, tools, user } from '$lib/stores';
import { createEventDispatcher, onMount, getContext, tick } from 'svelte';
import { createEventDispatcher, onMount, getContext, tick } from 'svelte';
import { toast } from 'svelte-sonner';
import {
getUserValvesSpecById as getToolUserValvesSpecById,
getUserValvesById as getToolUserValvesById,
updateUserValvesById as updateToolUserValvesById
} from '$lib/apis/tools';
import {
getUserValvesSpecById as getFunctionUserValvesSpecById,
getUserValvesById as getFunctionUserValvesById,
updateUserValvesById as updateFunctionUserValvesById
} from '$lib/apis/functions';
import ManageModal from './Personalization/ManageModal.svelte';
import ManageModal from './Personalization/ManageModal.svelte';
import Tooltip from '$lib/components/common/Tooltip.svelte';
import Tooltip from '$lib/components/common/Tooltip.svelte';
import Spinner from '$lib/components/common/Spinner.svelte';
const dispatch = createEventDispatcher();
const dispatch = createEventDispatcher();
const i18n = getContext('i18n');
const i18n = getContext('i18n');
...
@@ -16,15 +28,85 @@
...
@@ -16,15 +28,85 @@
let tab = 'tools';
let tab = 'tools';
let selectedId = '';
let selectedId = '';
let loading = false;
let valvesSpec = null;
let valves = {};
const getUserValves = async () => {
loading = true;
if (tab === 'tools') {
valves = await getToolUserValvesById(localStorage.token, selectedId);
valvesSpec = await getToolUserValvesSpecById(localStorage.token, selectedId);
} else if (tab === 'functions') {
valves = await getFunctionUserValvesById(localStorage.token, selectedId);
valvesSpec = await getFunctionUserValvesSpecById(localStorage.token, selectedId);
}
if (valvesSpec) {
// Convert array to string
for (const property in valvesSpec.properties) {
if (valvesSpec.properties[property]?.type === 'array') {
valves[property] = (valves[property] ?? []).join(',');
}
}
}
loading = false;
};
const submitHandler = async () => {
if (valvesSpec) {
// Convert string to array
for (const property in valvesSpec.properties) {
if (valvesSpec.properties[property]?.type === 'array') {
valves[property] = (valves[property] ?? '').split(',').map((v) => v.trim());
}
}
if (tab === 'tools') {
const res = await updateToolUserValvesById(localStorage.token, selectedId, valves).catch(
(error) => {
toast.error(error);
return null;
}
);
if (res) {
toast.success('Valves updated');
valves = res;
}
} else if (tab === 'functions') {
const res = await updateFunctionUserValvesById(
localStorage.token,
selectedId,
valves
).catch((error) => {
toast.error(error);
return null;
});
if (res) {
toast.success('Valves updated');
valves = res;
}
}
}
};
$: if (tab) {
$: if (tab) {
selectedId = '';
selectedId = '';
}
}
onMount(async () => {});
$: if (selectedId) {
getUserValves();
}
</script>
</script>
<form
<form
class="flex flex-col h-full justify-between space-y-3 text-sm"
class="flex flex-col h-full justify-between space-y-3 text-sm"
on:submit|preventDefault={() => {
on:submit|preventDefault={() => {
submitHandler();
dispatch('save');
dispatch('save');
}}
}}
>
>
...
@@ -82,11 +164,62 @@
...
@@ -82,11 +164,62 @@
</div>
</div>
</div>
</div>
<hr class="dark:border-gray-800 my-3 w-full" />
{#if selectedId}
<hr class="dark:border-gray-800 my-3 w-full" />
<div>
<div>
<div class="flex items-center justify-between mb-1" />
{#if !loading}
</div>
{#if valvesSpec}
{#each Object.keys(valvesSpec.properties) as property, idx}
<div class=" py-0.5 w-full justify-between">
<div class="flex w-full justify-between">
<div class=" self-center text-xs font-medium">
{valvesSpec.properties[property].title}
{#if (valvesSpec?.required ?? []).includes(property)}
<span class=" text-gray-500">*required</span>
{/if}
</div>
<button
class="p-1 px-3 text-xs flex rounded transition"
type="button"
on:click={() => {
valves[property] = (valves[property] ?? null) === null ? '' : null;
}}
>
{#if (valves[property] ?? null) === null}
<span class="ml-2 self-center"> {$i18n.t('None')} </span>
{:else}
<span class="ml-2 self-center"> {$i18n.t('Custom')} </span>
{/if}
</button>
</div>
{#if (valves[property] ?? null) !== null}
<div class="flex mt-0.5 space-x-2">
<div class=" flex-1">
<input
class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
type="text"
placeholder={valvesSpec.properties[property].title}
bind:value={valves[property]}
autocomplete="off"
required={(valvesSpec?.required ?? []).includes(property)}
/>
</div>
</div>
{/if}
</div>
{/each}
{:else}
<div>No valves</div>
{/if}
{:else}
<Spinner className="size-5" />
{/if}
</div>
{/if}
</div>
</div>
<div class="flex justify-end text-sm font-medium">
<div class="flex justify-end text-sm font-medium">
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment