mirror of
https://github.com/open-webui/open-webui
synced 2024-12-01 08:16:32 +00:00
feat: function filter example boilerplate
This commit is contained in:
parent
43e08c6afa
commit
40cde07e5c
@ -1,14 +1,13 @@
|
|||||||
<script>
|
<script>
|
||||||
import { getContext, createEventDispatcher, onMount } from 'svelte';
|
import { getContext, createEventDispatcher, onMount } from 'svelte';
|
||||||
|
import { goto } from '$app/navigation';
|
||||||
|
|
||||||
|
const dispatch = createEventDispatcher();
|
||||||
const i18n = getContext('i18n');
|
const i18n = getContext('i18n');
|
||||||
|
|
||||||
import CodeEditor from '$lib/components/common/CodeEditor.svelte';
|
import CodeEditor from '$lib/components/common/CodeEditor.svelte';
|
||||||
import { goto } from '$app/navigation';
|
|
||||||
import ConfirmDialog from '$lib/components/common/ConfirmDialog.svelte';
|
import ConfirmDialog from '$lib/components/common/ConfirmDialog.svelte';
|
||||||
|
|
||||||
const dispatch = createEventDispatcher();
|
|
||||||
|
|
||||||
let formElement = null;
|
let formElement = null;
|
||||||
let loading = false;
|
let loading = false;
|
||||||
let showConfirm = false;
|
let showConfirm = false;
|
||||||
@ -28,107 +27,33 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
let codeEditor;
|
let codeEditor;
|
||||||
let boilerplate = `import os
|
let boilerplate = `from typing import Optional
|
||||||
import requests
|
|
||||||
from datetime import datetime
|
|
||||||
|
|
||||||
|
|
||||||
class Tools:
|
class Filter:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
self.max_turns = 10
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# Add your custom tools using pure Python code here, make sure to add type hints
|
def inlet(self, body: dict, user: Optional[dict] = None) -> dict:
|
||||||
# Use Sphinx-style docstrings to document your tools, they will be used for generating tools specifications
|
print("inlet")
|
||||||
# Please refer to function_calling_filter_pipeline.py file from pipelines project for an example
|
print(body)
|
||||||
|
print(user)
|
||||||
|
|
||||||
def get_user_name_and_email_and_id(self, __user__: dict = {}) -> str:
|
if user.get("role", "admin") in ["user"]:
|
||||||
"""
|
messages = body.get("messages", [])
|
||||||
Get the user name, Email and ID from the user object.
|
if len(messages) > self.max_turns:
|
||||||
"""
|
raise Exception(
|
||||||
|
f"Conversation turn limit exceeded. Max turns: {self.max_turns}"
|
||||||
# Do not include :param for __user__ in the docstring as it should not be shown in the tool's specification
|
|
||||||
# The session user object will be passed as a parameter when the function is called
|
|
||||||
|
|
||||||
print(__user__)
|
|
||||||
result = ""
|
|
||||||
|
|
||||||
if "name" in __user__:
|
|
||||||
result += f"User: {__user__['name']}"
|
|
||||||
if "id" in __user__:
|
|
||||||
result += f" (ID: {__user__['id']})"
|
|
||||||
if "email" in __user__:
|
|
||||||
result += f" (Email: {__user__['email']})"
|
|
||||||
|
|
||||||
if result == "":
|
|
||||||
result = "User: Unknown"
|
|
||||||
|
|
||||||
return result
|
|
||||||
|
|
||||||
def get_current_time(self) -> str:
|
|
||||||
"""
|
|
||||||
Get the current time in a more human-readable format.
|
|
||||||
:return: The current time.
|
|
||||||
"""
|
|
||||||
|
|
||||||
now = datetime.now()
|
|
||||||
current_time = now.strftime("%I:%M:%S %p") # Using 12-hour format with AM/PM
|
|
||||||
current_date = now.strftime(
|
|
||||||
"%A, %B %d, %Y"
|
|
||||||
) # Full weekday, month name, day, and year
|
|
||||||
|
|
||||||
return f"Current Date and Time = {current_date}, {current_time}"
|
|
||||||
|
|
||||||
def calculator(self, equation: str) -> str:
|
|
||||||
"""
|
|
||||||
Calculate the result of an equation.
|
|
||||||
:param equation: The equation to calculate.
|
|
||||||
"""
|
|
||||||
|
|
||||||
# Avoid using eval in production code
|
|
||||||
# https://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html
|
|
||||||
try:
|
|
||||||
result = eval(equation)
|
|
||||||
return f"{equation} = {result}"
|
|
||||||
except Exception as e:
|
|
||||||
print(e)
|
|
||||||
return "Invalid equation"
|
|
||||||
|
|
||||||
def get_current_weather(self, city: str) -> str:
|
|
||||||
"""
|
|
||||||
Get the current weather for a given city.
|
|
||||||
:param city: The name of the city to get the weather for.
|
|
||||||
:return: The current weather information or an error message.
|
|
||||||
"""
|
|
||||||
api_key = os.getenv("OPENWEATHER_API_KEY")
|
|
||||||
if not api_key:
|
|
||||||
return (
|
|
||||||
"API key is not set in the environment variable 'OPENWEATHER_API_KEY'."
|
|
||||||
)
|
)
|
||||||
|
|
||||||
base_url = "http://api.openweathermap.org/data/2.5/weather"
|
return body
|
||||||
params = {
|
|
||||||
"q": city,
|
|
||||||
"appid": api_key,
|
|
||||||
"units": "metric", # Optional: Use 'imperial' for Fahrenheit
|
|
||||||
}
|
|
||||||
|
|
||||||
try:
|
def outlet(self, body: dict, user: Optional[dict] = None) -> dict:
|
||||||
response = requests.get(base_url, params=params)
|
print(f"outlet")
|
||||||
response.raise_for_status() # Raise HTTPError for bad responses (4xx and 5xx)
|
print(body)
|
||||||
data = response.json()
|
print(user)
|
||||||
|
return body`;
|
||||||
if data.get("cod") != 200:
|
|
||||||
return f"Error fetching weather data: {data.get('message')}"
|
|
||||||
|
|
||||||
weather_description = data["weather"][0]["description"]
|
|
||||||
temperature = data["main"]["temp"]
|
|
||||||
humidity = data["main"]["humidity"]
|
|
||||||
wind_speed = data["wind"]["speed"]
|
|
||||||
|
|
||||||
return f"Weather in {city}: {temperature}°C"
|
|
||||||
except requests.RequestException as e:
|
|
||||||
return f"Error fetching weather data: {str(e)}"
|
|
||||||
`;
|
|
||||||
|
|
||||||
const saveHandler = async () => {
|
const saveHandler = async () => {
|
||||||
loading = true;
|
loading = true;
|
||||||
@ -169,7 +94,7 @@ class Tools:
|
|||||||
<button
|
<button
|
||||||
class="flex space-x-1"
|
class="flex space-x-1"
|
||||||
on:click={() => {
|
on:click={() => {
|
||||||
goto('/workspace/tools');
|
goto('/workspace/functions');
|
||||||
}}
|
}}
|
||||||
type="button"
|
type="button"
|
||||||
>
|
>
|
||||||
@ -197,7 +122,7 @@ class Tools:
|
|||||||
<input
|
<input
|
||||||
class="w-full px-3 py-2 text-sm font-medium bg-gray-50 dark:bg-gray-850 dark:text-gray-200 rounded-lg outline-none"
|
class="w-full px-3 py-2 text-sm font-medium bg-gray-50 dark:bg-gray-850 dark:text-gray-200 rounded-lg outline-none"
|
||||||
type="text"
|
type="text"
|
||||||
placeholder="Toolkit Name (e.g. My ToolKit)"
|
placeholder="Function Name (e.g. My Filter)"
|
||||||
bind:value={name}
|
bind:value={name}
|
||||||
required
|
required
|
||||||
/>
|
/>
|
||||||
@ -205,7 +130,7 @@ class Tools:
|
|||||||
<input
|
<input
|
||||||
class="w-full px-3 py-2 text-sm font-medium disabled:text-gray-300 dark:disabled:text-gray-700 bg-gray-50 dark:bg-gray-850 dark:text-gray-200 rounded-lg outline-none"
|
class="w-full px-3 py-2 text-sm font-medium disabled:text-gray-300 dark:disabled:text-gray-700 bg-gray-50 dark:bg-gray-850 dark:text-gray-200 rounded-lg outline-none"
|
||||||
type="text"
|
type="text"
|
||||||
placeholder="Toolkit ID (e.g. my_toolkit)"
|
placeholder="Function ID (e.g. my_filter)"
|
||||||
bind:value={id}
|
bind:value={id}
|
||||||
required
|
required
|
||||||
disabled={edit}
|
disabled={edit}
|
||||||
@ -214,7 +139,7 @@ class Tools:
|
|||||||
<input
|
<input
|
||||||
class="w-full px-3 py-2 text-sm font-medium bg-gray-50 dark:bg-gray-850 dark:text-gray-200 rounded-lg outline-none"
|
class="w-full px-3 py-2 text-sm font-medium bg-gray-50 dark:bg-gray-850 dark:text-gray-200 rounded-lg outline-none"
|
||||||
type="text"
|
type="text"
|
||||||
placeholder="Toolkit Description (e.g. A toolkit for performing various operations)"
|
placeholder="Function Description (e.g. A filter to remove profanity from text)"
|
||||||
bind:value={meta.description}
|
bind:value={meta.description}
|
||||||
required
|
required
|
||||||
/>
|
/>
|
||||||
@ -236,10 +161,10 @@ class Tools:
|
|||||||
<div class="pb-3 flex justify-between">
|
<div class="pb-3 flex justify-between">
|
||||||
<div class="flex-1 pr-3">
|
<div class="flex-1 pr-3">
|
||||||
<div class="text-xs text-gray-500 line-clamp-2">
|
<div class="text-xs text-gray-500 line-clamp-2">
|
||||||
<span class=" font-semibold dark:text-gray-200">Warning:</span> Tools are a function
|
<span class=" font-semibold dark:text-gray-200">Warning:</span> Functions allow
|
||||||
calling system with arbitrary code execution <br />—
|
arbitrary code execution <br />—
|
||||||
<span class=" font-medium dark:text-gray-400"
|
<span class=" font-medium dark:text-gray-400"
|
||||||
>don't install random tools from sources you don't trust.</span
|
>don't install random functions from sources you don't trust.</span
|
||||||
>
|
>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@ -267,8 +192,8 @@ class Tools:
|
|||||||
<div>Please carefully review the following warnings:</div>
|
<div>Please carefully review the following warnings:</div>
|
||||||
|
|
||||||
<ul class=" mt-1 list-disc pl-4 text-xs">
|
<ul class=" mt-1 list-disc pl-4 text-xs">
|
||||||
<li>Tools have a function calling system that allows arbitrary code execution.</li>
|
<li>Functions allow arbitrary code execution.</li>
|
||||||
<li>Do not install tools from sources you do not fully trust.</li>
|
<li>Do not install functions from sources you do not fully trust.</li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
@ -1,18 +1,19 @@
|
|||||||
<script>
|
<script>
|
||||||
|
import { toast } from 'svelte-sonner';
|
||||||
|
import { onMount } from 'svelte';
|
||||||
|
|
||||||
import { goto } from '$app/navigation';
|
import { goto } from '$app/navigation';
|
||||||
import { page } from '$app/stores';
|
import { page } from '$app/stores';
|
||||||
import { getToolById, getTools, updateToolById } from '$lib/apis/tools';
|
import { updateFunctionById, getFunctions, getFunctionById } from '$lib/apis/functions';
|
||||||
import Spinner from '$lib/components/common/Spinner.svelte';
|
|
||||||
import ToolkitEditor from '$lib/components/workspace/Tools/ToolkitEditor.svelte';
|
|
||||||
import { tools } from '$lib/stores';
|
|
||||||
import { onMount } from 'svelte';
|
|
||||||
import { toast } from 'svelte-sonner';
|
|
||||||
|
|
||||||
let tool = null;
|
import FunctionEditor from '$lib/components/workspace/Functions/FunctionEditor.svelte';
|
||||||
|
import Spinner from '$lib/components/common/Spinner.svelte';
|
||||||
|
|
||||||
|
let func = null;
|
||||||
|
|
||||||
const saveHandler = async (data) => {
|
const saveHandler = async (data) => {
|
||||||
console.log(data);
|
console.log(data);
|
||||||
const res = await updateToolById(localStorage.token, tool.id, {
|
const res = await updateFunctionById(localStorage.token, func.id, {
|
||||||
id: data.id,
|
id: data.id,
|
||||||
name: data.name,
|
name: data.name,
|
||||||
meta: data.meta,
|
meta: data.meta,
|
||||||
@ -23,10 +24,7 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (res) {
|
if (res) {
|
||||||
toast.success('Tool updated successfully');
|
toast.success('Function updated successfully');
|
||||||
tools.set(await getTools(localStorage.token));
|
|
||||||
|
|
||||||
// await goto('/workspace/tools');
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -35,24 +33,24 @@
|
|||||||
const id = $page.url.searchParams.get('id');
|
const id = $page.url.searchParams.get('id');
|
||||||
|
|
||||||
if (id) {
|
if (id) {
|
||||||
tool = await getToolById(localStorage.token, id).catch((error) => {
|
func = await getFunctionById(localStorage.token, id).catch((error) => {
|
||||||
toast.error(error);
|
toast.error(error);
|
||||||
goto('/workspace/tools');
|
goto('/workspace/functions');
|
||||||
return null;
|
return null;
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log(tool);
|
console.log(func);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
{#if tool}
|
{#if func}
|
||||||
<ToolkitEditor
|
<FunctionEditor
|
||||||
edit={true}
|
edit={true}
|
||||||
id={tool.id}
|
id={func.id}
|
||||||
name={tool.name}
|
name={func.name}
|
||||||
meta={tool.meta}
|
meta={func.meta}
|
||||||
content={tool.content}
|
content={func.content}
|
||||||
on:save={(e) => {
|
on:save={(e) => {
|
||||||
saveHandler(e.detail);
|
saveHandler(e.detail);
|
||||||
}}
|
}}
|
||||||
|
Loading…
Reference in New Issue
Block a user