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
3d6f5f41
Commit
3d6f5f41
authored
Jun 11, 2024
by
Timothy J. Baek
Browse files
feat: tools full integration
parent
a27175d6
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
79 additions
and
43 deletions
+79
-43
backend/main.py
backend/main.py
+51
-39
src/lib/components/chat/Chat.svelte
src/lib/components/chat/Chat.svelte
+4
-0
src/lib/components/chat/MessageInput.svelte
src/lib/components/chat/MessageInput.svelte
+12
-1
src/lib/components/chat/MessageInput/InputMenu.svelte
src/lib/components/chat/MessageInput/InputMenu.svelte
+12
-3
No files found.
backend/main.py
View file @
3d6f5f41
...
@@ -185,39 +185,48 @@ async def get_function_call_response(prompt, tool_id, template, task_model_id, u
...
@@ -185,39 +185,48 @@ async def get_function_call_response(prompt, tool_id, template, task_model_id, u
model
=
app
.
state
.
MODELS
[
task_model_id
]
model
=
app
.
state
.
MODELS
[
task_model_id
]
response
=
None
response
=
None
if
model
[
"owned_by"
]
==
"ollama"
:
try
:
response
=
await
generate_ollama_chat_completion
(
if
model
[
"owned_by"
]
==
"ollama"
:
OpenAIChatCompletionForm
(
**
payload
),
user
=
user
response
=
await
generate_ollama_chat_completion
(
)
OpenAIChatCompletionForm
(
**
payload
),
user
=
user
else
:
)
response
=
await
generate_openai_chat_completion
(
payload
,
user
=
user
)
else
:
response
=
await
generate_openai_chat_completion
(
payload
,
user
=
user
)
print
(
response
)
content
=
response
[
"choices"
][
0
][
"message"
][
"content"
]
content
=
None
async
for
chunk
in
response
.
body_iterator
:
# Parse the function response
data
=
json
.
loads
(
chunk
.
decode
(
"utf-8"
))
if
content
!=
""
:
content
=
data
[
"choices"
][
0
][
"message"
][
"content"
]
result
=
json
.
loads
(
content
)
print
(
result
)
# Cleanup any remaining background tasks if necessary
if
response
.
background
is
not
None
:
# Call the function
await
response
.
background
()
if
"name"
in
result
:
if
tool_id
in
webui_app
.
state
.
TOOLS
:
# Parse the function response
toolkit_module
=
webui_app
.
state
.
TOOLS
[
tool_id
]
if
content
is
not
None
:
else
:
result
=
json
.
loads
(
content
)
toolkit_module
=
load_toolkit_module_by_id
(
tool_id
)
print
(
result
)
webui_app
.
state
.
TOOLS
[
tool_id
]
=
toolkit_module
# Call the function
function
=
getattr
(
toolkit_module
,
result
[
"name"
])
if
"name"
in
result
:
function_result
=
None
if
tool_id
in
webui_app
.
state
.
TOOLS
:
try
:
toolkit_module
=
webui_app
.
state
.
TOOLS
[
tool_id
]
function_result
=
function
(
**
result
[
"parameters"
])
else
:
except
Exception
as
e
:
toolkit_module
=
load_toolkit_module_by_id
(
tool_id
)
print
(
e
)
webui_app
.
state
.
TOOLS
[
tool_id
]
=
toolkit_module
function
=
getattr
(
toolkit_module
,
result
[
"name"
])
function_result
=
None
try
:
function_result
=
function
(
**
result
[
"parameters"
])
except
Exception
as
e
:
print
(
e
)
# Add the function result to the system prompt
# Add the function result to the system prompt
if
function_result
:
if
function_result
:
return
function_result
return
function_result
except
Exception
as
e
:
print
(
f
"Error:
{
e
}
"
)
return
None
return
None
...
@@ -285,15 +294,18 @@ class ChatCompletionMiddleware(BaseHTTPMiddleware):
...
@@ -285,15 +294,18 @@ class ChatCompletionMiddleware(BaseHTTPMiddleware):
print
(
response
)
print
(
response
)
if
response
:
if
response
:
context
+
=
f
"
\n
{
response
}
"
context
=
(
"
\n
"
if
context
!=
""
else
""
)
+
response
system_prompt
=
rag_template
(
if
context
!=
""
:
rag_app
.
state
.
config
.
RAG_TEMPLATE
,
context
,
prompt
system_prompt
=
rag_template
(
)
rag_app
.
state
.
config
.
RAG_TEMPLATE
,
context
,
prompt
)
data
[
"messages"
]
=
add_or_update_system_message
(
print
(
system_prompt
)
system_prompt
,
data
[
"messages"
]
)
data
[
"messages"
]
=
add_or_update_system_message
(
f
"
\n
{
system_prompt
}
"
,
data
[
"messages"
]
)
del
data
[
"tool_ids"
]
del
data
[
"tool_ids"
]
...
...
src/lib/components/chat/Chat.svelte
View file @
3d6f5f41
...
@@ -73,6 +73,7 @@
...
@@ -73,6 +73,7 @@
let selectedModels = [''];
let selectedModels = [''];
let atSelectedModel: Model | undefined;
let atSelectedModel: Model | undefined;
let selectedToolIds = [];
let webSearchEnabled = false;
let webSearchEnabled = false;
let chat = null;
let chat = null;
...
@@ -687,6 +688,7 @@
...
@@ -687,6 +688,7 @@
},
},
format: $settings.requestFormat ?? undefined,
format: $settings.requestFormat ?? undefined,
keep_alive: $settings.keepAlive ?? undefined,
keep_alive: $settings.keepAlive ?? undefined,
tool_ids: selectedToolIds.length > 0 ? selectedToolIds : undefined,
docs: docs.length > 0 ? docs : undefined,
docs: docs.length > 0 ? docs : undefined,
citations: docs.length > 0,
citations: docs.length > 0,
chat_id: $chatId
chat_id: $chatId
...
@@ -948,6 +950,7 @@
...
@@ -948,6 +950,7 @@
top_p: $settings?.params?.top_p ?? undefined,
top_p: $settings?.params?.top_p ?? undefined,
frequency_penalty: $settings?.params?.frequency_penalty ?? undefined,
frequency_penalty: $settings?.params?.frequency_penalty ?? undefined,
max_tokens: $settings?.params?.max_tokens ?? undefined,
max_tokens: $settings?.params?.max_tokens ?? undefined,
tool_ids: selectedToolIds.length > 0 ? selectedToolIds : undefined,
docs: docs.length > 0 ? docs : undefined,
docs: docs.length > 0 ? docs : undefined,
citations: docs.length > 0,
citations: docs.length > 0,
chat_id: $chatId
chat_id: $chatId
...
@@ -1274,6 +1277,7 @@
...
@@ -1274,6 +1277,7 @@
bind:files
bind:files
bind:prompt
bind:prompt
bind:autoScroll
bind:autoScroll
bind:selectedToolIds
bind:webSearchEnabled
bind:webSearchEnabled
bind:atSelectedModel
bind:atSelectedModel
{selectedModels}
{selectedModels}
...
...
src/lib/components/chat/MessageInput.svelte
View file @
3d6f5f41
...
@@ -8,7 +8,8 @@
...
@@ -8,7 +8,8 @@
showSidebar,
showSidebar,
models,
models,
config,
config,
showCallOverlay
showCallOverlay,
tools
} from '$lib/stores';
} from '$lib/stores';
import { blobToFile, calculateSHA256, findWordIndices } from '$lib/utils';
import { blobToFile, calculateSHA256, findWordIndices } from '$lib/utils';
...
@@ -57,6 +58,7 @@
...
@@ -57,6 +58,7 @@
let chatInputPlaceholder = '';
let chatInputPlaceholder = '';
export let files = [];
export let files = [];
export let selectedToolIds = [];
export let webSearchEnabled = false;
export let webSearchEnabled = false;
...
@@ -653,6 +655,15 @@
...
@@ -653,6 +655,15 @@
<div class=" ml-0.5 self-end mb-1.5 flex space-x-1">
<div class=" ml-0.5 self-end mb-1.5 flex space-x-1">
<InputMenu
<InputMenu
bind:webSearchEnabled
bind:webSearchEnabled
bind:selectedToolIds
tools={$tools.reduce((a, e, i, arr) => {
a[e.id] = {
name: e.name,
enabled: false
};
return a;
}, {})}
uploadFilesHandler={() => {
uploadFilesHandler={() => {
filesInputElement.click();
filesInputElement.click();
}}
}}
...
...
src/lib/components/chat/MessageInput/InputMenu.svelte
View file @
3d6f5f41
...
@@ -14,6 +14,8 @@
...
@@ -14,6 +14,8 @@
const i18n = getContext('i18n');
const i18n = getContext('i18n');
export let uploadFilesHandler: Function;
export let uploadFilesHandler: Function;
export let selectedToolIds: string[] = [];
export let webSearchEnabled: boolean;
export let webSearchEnabled: boolean;
export let tools = {};
export let tools = {};
...
@@ -44,16 +46,23 @@
...
@@ -44,16 +46,23 @@
transition={flyAndScale}
transition={flyAndScale}
>
>
{#if Object.keys(tools).length > 0}
{#if Object.keys(tools).length > 0}
{#each Object.keys(tools) as tool}
{#each Object.keys(tools) as tool
Id
}
<div
<div
class="flex gap-2 items-center px-3 py-2 text-sm font-medium cursor-pointer rounded-xl"
class="flex gap-2 items-center px-3 py-2 text-sm font-medium cursor-pointer rounded-xl"
>
>
<div class="flex-1 flex items-center gap-2">
<div class="flex-1 flex items-center gap-2">
<WrenchSolid />
<WrenchSolid />
<div class="flex items-center">{tool}</div>
<div class="flex items-center">{tool
s[toolId].name
}</div>
</div>
</div>
<Switch bind:state={tools[tool]} />
<Switch
bind:state={tools[toolId].enabled}
on:change={(e) => {
selectedToolIds = e.detail
? [...selectedToolIds, toolId]
: selectedToolIds.filter((id) => id !== toolId);
}}
/>
</div>
</div>
{/each}
{/each}
<hr class="border-gray-100 dark:border-gray-800 my-1" />
<hr class="border-gray-100 dark:border-gray-800 my-1" />
...
...
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