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

214 lines
5.7 KiB
Python
Raw Normal View History

from pydantic import BaseModel, ConfigDict
2024-08-14 12:46:31 +00:00
from typing import Optional
2024-06-11 03:39:55 +00:00
import time
import logging
from sqlalchemy import String, Column, BigInteger, Text
2024-07-04 06:32:39 +00:00
from apps.webui.internal.db import Base, JSONField, get_db
2024-06-22 18:26:33 +00:00
from apps.webui.models.users import Users
2024-06-11 03:39:55 +00:00
import json
2024-06-22 19:08:32 +00:00
import copy
2024-06-11 03:39:55 +00:00
2024-08-25 14:52:36 +00:00
from env import SRC_LOG_LEVELS
2024-06-11 03:39:55 +00:00
log = logging.getLogger(__name__)
log.setLevel(SRC_LOG_LEVELS["MODELS"])
####################
# Tools DB Schema
####################
class Tool(Base):
__tablename__ = "tool"
2024-06-11 03:39:55 +00:00
id = Column(String, primary_key=True)
user_id = Column(String)
name = Column(Text)
content = Column(Text)
specs = Column(JSONField)
meta = Column(JSONField)
valves = Column(JSONField)
updated_at = Column(BigInteger)
created_at = Column(BigInteger)
2024-06-11 03:39:55 +00:00
class ToolMeta(BaseModel):
description: Optional[str] = None
2024-06-24 03:31:40 +00:00
manifest: Optional[dict] = {}
2024-06-11 03:39:55 +00:00
class ToolModel(BaseModel):
id: str
user_id: str
name: str
content: str
2024-08-14 12:46:31 +00:00
specs: list[dict]
2024-06-11 03:39:55 +00:00
meta: ToolMeta
updated_at: int # timestamp in epoch
created_at: int # timestamp in epoch
model_config = ConfigDict(from_attributes=True)
2024-06-11 03:39:55 +00:00
####################
# Forms
####################
class ToolResponse(BaseModel):
id: str
user_id: str
name: str
meta: ToolMeta
updated_at: int # timestamp in epoch
created_at: int # timestamp in epoch
class ToolForm(BaseModel):
id: str
name: str
content: str
meta: ToolMeta
2024-06-24 01:05:33 +00:00
class ToolValves(BaseModel):
valves: Optional[dict] = None
2024-06-11 03:39:55 +00:00
class ToolsTable:
def insert_new_tool(
2024-08-14 12:46:31 +00:00
self, user_id: str, form_data: ToolForm, specs: list[dict]
2024-06-11 03:39:55 +00:00
) -> Optional[ToolModel]:
2024-07-04 06:32:39 +00:00
with get_db() as db:
tool = ToolModel(
**{
**form_data.model_dump(),
"specs": specs,
"user_id": user_id,
"updated_at": int(time.time()),
"created_at": int(time.time()),
}
)
try:
result = Tool(**tool.model_dump())
db.add(result)
db.commit()
db.refresh(result)
if result:
return ToolModel.model_validate(result)
else:
return None
except Exception as e:
print(f"Error creating tool: {e}")
return None
2024-06-11 03:39:55 +00:00
def get_tool_by_id(self, id: str) -> Optional[ToolModel]:
2024-06-11 03:39:55 +00:00
try:
2024-07-04 06:32:39 +00:00
with get_db() as db:
tool = db.get(Tool, id)
return ToolModel.model_validate(tool)
2024-08-14 12:38:19 +00:00
except Exception:
2024-06-11 03:39:55 +00:00
return None
2024-08-14 12:46:31 +00:00
def get_tools(self) -> list[ToolModel]:
2024-07-04 06:39:16 +00:00
with get_db() as db:
return [ToolModel.model_validate(tool) for tool in db.query(Tool).all()]
2024-06-11 03:39:55 +00:00
2024-06-24 02:18:13 +00:00
def get_tool_valves_by_id(self, id: str) -> Optional[dict]:
2024-06-24 01:05:33 +00:00
try:
2024-07-04 06:32:39 +00:00
with get_db() as db:
tool = db.get(Tool, id)
return tool.valves if tool.valves else {}
2024-06-24 01:05:33 +00:00
except Exception as e:
print(f"An error occurred: {e}")
return None
def update_tool_valves_by_id(self, id: str, valves: dict) -> Optional[ToolValves]:
try:
2024-07-04 06:32:39 +00:00
with get_db() as db:
db.query(Tool).filter_by(id=id).update(
{"valves": valves, "updated_at": int(time.time())}
)
db.commit()
return self.get_tool_by_id(id)
2024-08-14 12:38:19 +00:00
except Exception:
2024-06-24 01:05:33 +00:00
return None
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]:
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 "tools" and "valves" settings
2024-06-22 19:08:32 +00:00
if "tools" not in user_settings:
user_settings["tools"] = {}
if "valves" not in user_settings["tools"]:
user_settings["tools"]["valves"] = {}
2024-06-22 18:26:33 +00:00
2024-06-22 19:08:32 +00:00
return user_settings["tools"]["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]:
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 "tools" and "valves" settings
2024-06-22 19:08:32 +00:00
if "tools" not in user_settings:
user_settings["tools"] = {}
if "valves" not in user_settings["tools"]:
user_settings["tools"]["valves"] = {}
2024-06-22 18:26:33 +00:00
2024-06-22 19:08:32 +00:00
user_settings["tools"]["valves"][id] = valves
2024-06-22 18:26:33 +00:00
# Update the user settings in the database
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["tools"]["valves"][id]
2024-06-22 18:26:33 +00:00
except Exception as e:
print(f"An error occurred: {e}")
return None
2024-06-11 03:39:55 +00:00
def update_tool_by_id(self, id: str, updated: dict) -> Optional[ToolModel]:
try:
2024-07-04 06:32:39 +00:00
with get_db() as db:
2024-07-08 18:46:37 +00:00
db.query(Tool).filter_by(id=id).update(
{**updated, "updated_at": int(time.time())}
)
2024-07-04 06:32:39 +00:00
db.commit()
2024-07-08 18:46:37 +00:00
tool = db.query(Tool).get(id)
2024-07-04 06:32:39 +00:00
db.refresh(tool)
return ToolModel.model_validate(tool)
2024-08-14 12:38:19 +00:00
except Exception:
2024-06-11 03:39:55 +00:00
return None
def delete_tool_by_id(self, id: str) -> bool:
2024-06-11 03:39:55 +00:00
try:
2024-07-04 06:32:39 +00:00
with get_db() as db:
db.query(Tool).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
2024-08-14 12:38:19 +00:00
except Exception:
2024-06-11 03:39:55 +00:00
return False
Tools = ToolsTable()