open-webui/backend/apps/webui/models/functions.py

280 lines
7.8 KiB
Python
Raw Normal View History

from pydantic import BaseModel, ConfigDict
2024-06-18 17:36:06 +00:00
from typing import List, Union, Optional
import time
import logging
from sqlalchemy import Column, String, Text, BigInteger, Boolean
2024-07-04 06:32:39 +00:00
from apps.webui.internal.db import JSONField, Base, get_db
2024-06-22 18:26:33 +00:00
from apps.webui.models.users import Users
2024-06-18 17:36:06 +00:00
import json
2024-06-22 19:08:32 +00:00
import copy
2024-06-18 17:36:06 +00:00
from config import SRC_LOG_LEVELS
log = logging.getLogger(__name__)
log.setLevel(SRC_LOG_LEVELS["MODELS"])
####################
# Functions DB Schema
####################
class Function(Base):
__tablename__ = "function"
2024-06-18 17:36:06 +00:00
id = Column(String, primary_key=True)
user_id = Column(String)
name = Column(Text)
type = Column(Text)
content = Column(Text)
meta = Column(JSONField)
valves = Column(JSONField)
is_active = Column(Boolean)
is_global = Column(Boolean)
updated_at = Column(BigInteger)
created_at = Column(BigInteger)
2024-06-18 17:36:06 +00:00
class FunctionMeta(BaseModel):
description: Optional[str] = None
2024-06-24 03:31:40 +00:00
manifest: Optional[dict] = {}
2024-06-18 17:36:06 +00:00
class FunctionModel(BaseModel):
id: str
user_id: str
name: str
type: str
content: str
meta: FunctionMeta
2024-06-24 01:34:42 +00:00
is_active: bool = False
2024-06-27 20:04:12 +00:00
is_global: bool = False
2024-06-18 17:36:06 +00:00
updated_at: int # timestamp in epoch
created_at: int # timestamp in epoch
model_config = ConfigDict(from_attributes=True)
2024-06-18 17:36:06 +00:00
####################
# Forms
####################
class FunctionResponse(BaseModel):
id: str
user_id: str
2024-06-20 08:16:31 +00:00
type: str
2024-06-18 17:36:06 +00:00
name: str
meta: FunctionMeta
2024-06-24 01:05:33 +00:00
is_active: bool
2024-06-27 20:04:12 +00:00
is_global: bool
2024-06-18 17:36:06 +00:00
updated_at: int # timestamp in epoch
created_at: int # timestamp in epoch
class FunctionForm(BaseModel):
id: str
name: str
content: str
meta: FunctionMeta
2024-06-24 01:05:33 +00:00
class FunctionValves(BaseModel):
valves: Optional[dict] = None
2024-06-20 07:37:02 +00:00
class FunctionsTable:
2024-06-18 17:36:06 +00:00
def insert_new_function(
2024-06-20 07:54:58 +00:00
self, user_id: str, type: str, form_data: FunctionForm
2024-06-18 17:36:06 +00:00
) -> Optional[FunctionModel]:
2024-07-04 06:32:39 +00:00
2024-06-18 17:36:06 +00:00
function = FunctionModel(
**{
**form_data.model_dump(),
"user_id": user_id,
2024-06-20 07:54:58 +00:00
"type": type,
2024-06-18 17:36:06 +00:00
"updated_at": int(time.time()),
"created_at": int(time.time()),
}
)
try:
2024-07-04 06:32:39 +00:00
with get_db() as db:
result = Function(**function.model_dump())
db.add(result)
db.commit()
db.refresh(result)
if result:
return FunctionModel.model_validate(result)
else:
return None
2024-06-18 17:36:06 +00:00
except Exception as e:
print(f"Error creating tool: {e}")
return None
def get_function_by_id(self, id: str) -> Optional[FunctionModel]:
try:
2024-07-04 06:32:39 +00:00
with get_db() as db:
function = db.get(Function, id)
return FunctionModel.model_validate(function)
2024-06-18 17:36:06 +00:00
except:
return None
2024-06-24 01:34:42 +00:00
def get_functions(self, active_only=False) -> List[FunctionModel]:
2024-07-04 06:32:39 +00:00
with get_db() as db:
if active_only:
return [
FunctionModel.model_validate(function)
for function in db.query(Function).filter_by(is_active=True).all()
]
else:
return [
FunctionModel.model_validate(function)
for function in db.query(Function).all()
]
2024-06-24 01:34:42 +00:00
def get_functions_by_type(
self, type: str, active_only=False
) -> List[FunctionModel]:
2024-07-04 06:32:39 +00:00
with get_db() as db:
if active_only:
return [
FunctionModel.model_validate(function)
for function in db.query(Function)
.filter_by(type=type, is_active=True)
.all()
]
else:
return [
FunctionModel.model_validate(function)
for function in db.query(Function).filter_by(type=type).all()
]
def get_global_filter_functions(self) -> List[FunctionModel]:
with get_db() as db:
2024-06-24 01:34:42 +00:00
return [
2024-07-03 04:56:32 +00:00
FunctionModel.model_validate(function)
2024-07-04 06:32:39 +00:00
for function in db.query(Function)
.filter_by(type="filter", is_active=True, is_global=True)
.all()
2024-06-24 01:34:42 +00:00
]
2024-06-27 20:04:12 +00:00
2024-06-24 02:18:13 +00:00
def get_function_valves_by_id(self, id: str) -> Optional[dict]:
2024-07-04 06:32:39 +00:00
with get_db() as db:
try:
function = db.get(Function, id)
return function.valves if function.valves else {}
except Exception as e:
print(f"An error occurred: {e}")
return None
2024-06-24 01:34:42 +00:00
def update_function_valves_by_id(
self, id: str, valves: dict
) -> Optional[FunctionValves]:
2024-07-04 06:32:39 +00:00
with get_db() as db:
try:
function = db.get(Function, id)
function.valves = valves
function.updated_at = int(time.time())
db.commit()
db.refresh(function)
return self.get_function_by_id(id)
except:
return None
2024-06-18 17:36:06 +00:00
2024-06-22 18:26:33 +00:00
def get_user_valves_by_id_and_user_id(
self, id: str, user_id: str
) -> Optional[dict]:
2024-07-04 06:32:39 +00:00
2024-06-22 18:26:33 +00:00
try:
user = Users.get_user_by_id(user_id)
2024-07-04 07:37:05 +00:00
user_settings = user.settings.model_dump() if user.settings else {}
2024-06-22 18:26:33 +00:00
# Check if user has "functions" and "valves" settings
2024-06-22 19:08:32 +00:00
if "functions" not in user_settings:
user_settings["functions"] = {}
if "valves" not in user_settings["functions"]:
user_settings["functions"]["valves"] = {}
2024-06-22 18:26:33 +00:00
2024-06-22 19:08:32 +00:00
return user_settings["functions"]["valves"].get(id, {})
2024-06-22 18:26:33 +00:00
except Exception as e:
print(f"An error occurred: {e}")
return None
def update_user_valves_by_id_and_user_id(
self, id: str, user_id: str, valves: dict
) -> Optional[dict]:
2024-07-04 06:32:39 +00:00
2024-06-22 18:26:33 +00:00
try:
user = Users.get_user_by_id(user_id)
2024-07-04 07:37:05 +00:00
user_settings = user.settings.model_dump() if user.settings else {}
2024-06-22 18:26:33 +00:00
# Check if user has "functions" and "valves" settings
2024-06-22 19:08:32 +00:00
if "functions" not in user_settings:
user_settings["functions"] = {}
if "valves" not in user_settings["functions"]:
user_settings["functions"]["valves"] = {}
2024-06-22 18:26:33 +00:00
2024-06-22 19:08:32 +00:00
user_settings["functions"]["valves"][id] = valves
2024-06-22 18:26:33 +00:00
# Update the user settings in the database
2024-07-01 23:11:44 +00:00
Users.update_user_by_id(user_id, {"settings": user_settings})
2024-06-22 18:26:33 +00:00
2024-06-22 19:08:32 +00:00
return user_settings["functions"]["valves"][id]
2024-06-22 18:26:33 +00:00
except Exception as e:
print(f"An error occurred: {e}")
return None
2024-06-18 17:36:06 +00:00
def update_function_by_id(self, id: str, updated: dict) -> Optional[FunctionModel]:
2024-07-04 06:32:39 +00:00
with get_db() as db:
try:
db.query(Function).filter_by(id=id).update(
{
**updated,
"updated_at": int(time.time()),
}
)
db.commit()
return self.get_function_by_id(id)
except:
return None
2024-06-18 17:36:06 +00:00
2024-06-24 02:28:33 +00:00
def deactivate_all_functions(self) -> Optional[bool]:
2024-07-04 06:32:39 +00:00
with get_db() as db:
try:
db.query(Function).update(
{
"is_active": False,
"updated_at": int(time.time()),
}
)
db.commit()
return True
except:
return None
2024-06-24 02:28:33 +00:00
2024-06-18 17:36:06 +00:00
def delete_function_by_id(self, id: str) -> bool:
2024-07-04 06:32:39 +00:00
with get_db() as db:
try:
db.query(Function).filter_by(id=id).delete()
2024-07-06 15:10:58 +00:00
db.commit()
2024-07-04 06:32:39 +00:00
return True
except:
return False
2024-06-18 17:36:06 +00:00
Functions = FunctionsTable()