tools.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. import inspect
  2. import logging
  3. import re
  4. import inspect
  5. import aiohttp
  6. import asyncio
  7. from typing import Any, Awaitable, Callable, get_type_hints, Dict, List, Union, Optional
  8. from functools import update_wrapper, partial
  9. from fastapi import Request
  10. from pydantic import BaseModel, Field, create_model
  11. from langchain_core.utils.function_calling import convert_to_openai_function
  12. from open_webui.models.tools import Tools
  13. from open_webui.models.users import UserModel
  14. from open_webui.utils.plugin import load_tools_module_by_id
  15. log = logging.getLogger(__name__)
  16. def apply_extra_params_to_tool_function(
  17. function: Callable, extra_params: dict
  18. ) -> Callable[..., Awaitable]:
  19. sig = inspect.signature(function)
  20. extra_params = {k: v for k, v in extra_params.items() if k in sig.parameters}
  21. partial_func = partial(function, **extra_params)
  22. if inspect.iscoroutinefunction(function):
  23. update_wrapper(partial_func, function)
  24. return partial_func
  25. async def new_function(*args, **kwargs):
  26. return partial_func(*args, **kwargs)
  27. update_wrapper(new_function, function)
  28. return new_function
  29. # Mutation on extra_params
  30. def get_tools(
  31. request: Request, tool_ids: list[str], user: UserModel, extra_params: dict
  32. ) -> dict[str, dict]:
  33. tools_dict = {}
  34. for tool_id in tool_ids:
  35. tools = Tools.get_tool_by_id(tool_id)
  36. if tools is None:
  37. tool_dict = {
  38. "spec": spec,
  39. "callable": callable,
  40. "toolkit_id": tool_id,
  41. "pydantic_model": function_to_pydantic_model(callable),
  42. # Misc info
  43. "metadata": {
  44. "file_handler": hasattr(module, "file_handler")
  45. and module.file_handler,
  46. "citation": hasattr(module, "citation") and module.citation,
  47. },
  48. }
  49. continue
  50. module = request.app.state.TOOLS.get(tool_id, None)
  51. if module is None:
  52. module, _ = load_tools_module_by_id(tool_id)
  53. request.app.state.TOOLS[tool_id] = module
  54. extra_params["__id__"] = tool_id
  55. if hasattr(module, "valves") and hasattr(module, "Valves"):
  56. valves = Tools.get_tool_valves_by_id(tool_id) or {}
  57. module.valves = module.Valves(**valves)
  58. if hasattr(module, "UserValves"):
  59. extra_params["__user__"]["valves"] = module.UserValves( # type: ignore
  60. **Tools.get_user_valves_by_id_and_user_id(tool_id, user.id)
  61. )
  62. for spec in tools.specs:
  63. # TODO: Fix hack for OpenAI API
  64. # Some times breaks OpenAI but others don't. Leaving the comment
  65. for val in spec.get("parameters", {}).get("properties", {}).values():
  66. if val["type"] == "str":
  67. val["type"] = "string"
  68. # Remove internal parameters
  69. spec["parameters"]["properties"] = {
  70. key: val
  71. for key, val in spec["parameters"]["properties"].items()
  72. if not key.startswith("__")
  73. }
  74. function_name = spec["name"]
  75. # convert to function that takes only model params and inserts custom params
  76. original_func = getattr(module, function_name)
  77. callable = apply_extra_params_to_tool_function(original_func, extra_params)
  78. if callable.__doc__ and callable.__doc__.strip() != "":
  79. s = re.split(":(param|return)", callable.__doc__, 1)
  80. spec["description"] = s[0]
  81. else:
  82. spec["description"] = function_name
  83. # TODO: This needs to be a pydantic model
  84. tool_dict = {
  85. "spec": spec,
  86. "callable": callable,
  87. "toolkit_id": tool_id,
  88. "pydantic_model": function_to_pydantic_model(callable),
  89. # Misc info
  90. "metadata": {
  91. "file_handler": hasattr(module, "file_handler")
  92. and module.file_handler,
  93. "citation": hasattr(module, "citation") and module.citation,
  94. },
  95. }
  96. # TODO: if collision, prepend toolkit name
  97. if function_name in tools_dict:
  98. log.warning(f"Tool {function_name} already exists in another tools!")
  99. log.warning(f"Collision between {tools} and {tool_id}.")
  100. log.warning(f"Discarding {tools}.{function_name}")
  101. else:
  102. tools_dict[function_name] = tool_dict
  103. return tools_dict
  104. def parse_description(docstring: str | None) -> str:
  105. """
  106. Parse a function's docstring to extract the description.
  107. Args:
  108. docstring (str): The docstring to parse.
  109. Returns:
  110. str: The description.
  111. """
  112. if not docstring:
  113. return ""
  114. lines = [line.strip() for line in docstring.strip().split("\n")]
  115. description_lines: list[str] = []
  116. for line in lines:
  117. if re.match(r":param", line) or re.match(r":return", line):
  118. break
  119. description_lines.append(line)
  120. return "\n".join(description_lines)
  121. def parse_docstring(docstring):
  122. """
  123. Parse a function's docstring to extract parameter descriptions in reST format.
  124. Args:
  125. docstring (str): The docstring to parse.
  126. Returns:
  127. dict: A dictionary where keys are parameter names and values are descriptions.
  128. """
  129. if not docstring:
  130. return {}
  131. # Regex to match `:param name: description` format
  132. param_pattern = re.compile(r":param (\w+):\s*(.+)")
  133. param_descriptions = {}
  134. for line in docstring.splitlines():
  135. match = param_pattern.match(line.strip())
  136. if not match:
  137. continue
  138. param_name, param_description = match.groups()
  139. if param_name.startswith("__"):
  140. continue
  141. param_descriptions[param_name] = param_description
  142. return param_descriptions
  143. def function_to_pydantic_model(func: Callable) -> type[BaseModel]:
  144. """
  145. Converts a Python function's type hints and docstring to a Pydantic model,
  146. including support for nested types, default values, and descriptions.
  147. Args:
  148. func: The function whose type hints and docstring should be converted.
  149. model_name: The name of the generated Pydantic model.
  150. Returns:
  151. A Pydantic model class.
  152. """
  153. type_hints = get_type_hints(func)
  154. signature = inspect.signature(func)
  155. parameters = signature.parameters
  156. docstring = func.__doc__
  157. descriptions = parse_docstring(docstring)
  158. tool_description = parse_description(docstring)
  159. field_defs = {}
  160. for name, param in parameters.items():
  161. type_hint = type_hints.get(name, Any)
  162. default_value = param.default if param.default is not param.empty else ...
  163. description = descriptions.get(name, None)
  164. if not description:
  165. field_defs[name] = type_hint, default_value
  166. continue
  167. field_defs[name] = type_hint, Field(default_value, description=description)
  168. model = create_model(func.__name__, **field_defs)
  169. model.__doc__ = tool_description
  170. return model
  171. def get_callable_attributes(tool: object) -> list[Callable]:
  172. return [
  173. getattr(tool, func)
  174. for func in dir(tool)
  175. if callable(getattr(tool, func))
  176. and not func.startswith("__")
  177. and not inspect.isclass(getattr(tool, func))
  178. ]
  179. def get_tools_specs(tool_class: object) -> list[dict]:
  180. function_list = get_callable_attributes(tool_class)
  181. models = map(function_to_pydantic_model, function_list)
  182. return [convert_to_openai_function(tool) for tool in models]
  183. import copy
  184. def resolve_schema(schema, components):
  185. """
  186. Recursively resolves a JSON schema using OpenAPI components.
  187. """
  188. if not schema:
  189. return {}
  190. if "$ref" in schema:
  191. ref_path = schema["$ref"]
  192. ref_parts = ref_path.strip("#/").split("/")
  193. resolved = components
  194. for part in ref_parts[1:]: # Skip the initial 'components'
  195. resolved = resolved.get(part, {})
  196. return resolve_schema(resolved, components)
  197. resolved_schema = copy.deepcopy(schema)
  198. # Recursively resolve inner schemas
  199. if "properties" in resolved_schema:
  200. for prop, prop_schema in resolved_schema["properties"].items():
  201. resolved_schema["properties"][prop] = resolve_schema(
  202. prop_schema, components
  203. )
  204. if "items" in resolved_schema:
  205. resolved_schema["items"] = resolve_schema(resolved_schema["items"], components)
  206. return resolved_schema
  207. def convert_openapi_to_tool_payload(openapi_spec):
  208. """
  209. Converts an OpenAPI specification into a custom tool payload structure.
  210. Args:
  211. openapi_spec (dict): The OpenAPI specification as a Python dict.
  212. Returns:
  213. list: A list of tool payloads.
  214. """
  215. tool_payload = []
  216. for path, methods in openapi_spec.get("paths", {}).items():
  217. for method, operation in methods.items():
  218. tool = {
  219. "type": "function",
  220. "name": operation.get("operationId"),
  221. "description": operation.get("summary", "No description available."),
  222. "parameters": {"type": "object", "properties": {}, "required": []},
  223. }
  224. # Extract path and query parameters
  225. for param in operation.get("parameters", []):
  226. param_name = param["name"]
  227. param_schema = param.get("schema", {})
  228. tool["parameters"]["properties"][param_name] = {
  229. "type": param_schema.get("type"),
  230. "description": param_schema.get("description", ""),
  231. }
  232. if param.get("required"):
  233. tool["parameters"]["required"].append(param_name)
  234. # Extract and resolve requestBody if available
  235. request_body = operation.get("requestBody")
  236. if request_body:
  237. content = request_body.get("content", {})
  238. json_schema = content.get("application/json", {}).get("schema")
  239. if json_schema:
  240. resolved_schema = resolve_schema(
  241. json_schema, openapi_spec.get("components", {})
  242. )
  243. if resolved_schema.get("properties"):
  244. tool["parameters"]["properties"].update(
  245. resolved_schema["properties"]
  246. )
  247. if "required" in resolved_schema:
  248. tool["parameters"]["required"] = list(
  249. set(
  250. tool["parameters"]["required"]
  251. + resolved_schema["required"]
  252. )
  253. )
  254. elif resolved_schema.get("type") == "array":
  255. tool["parameters"] = resolved_schema # special case for array
  256. tool_payload.append(tool)
  257. return tool_payload
  258. async def get_tool_server_data(token: str, url: str) -> Dict[str, Any]:
  259. headers = {
  260. "Accept": "application/json",
  261. "Content-Type": "application/json",
  262. }
  263. if token:
  264. headers["Authorization"] = f"Bearer {token}"
  265. error = None
  266. try:
  267. async with aiohttp.ClientSession() as session:
  268. async with session.get(url, headers=headers) as response:
  269. if response.status != 200:
  270. error_body = await response.json()
  271. raise Exception(error_body)
  272. res = await response.json()
  273. except Exception as err:
  274. print("Error:", err)
  275. if isinstance(err, dict) and "detail" in err:
  276. error = err["detail"]
  277. else:
  278. error = str(err)
  279. raise Exception(error)
  280. data = {
  281. "openapi": res,
  282. "info": res.get("info", {}),
  283. "specs": convert_openapi_to_tool_payload(res),
  284. }
  285. print("Fetched data:", data)
  286. return data
  287. async def get_tool_servers_data(
  288. servers: List[Dict[str, Any]], session_token: Optional[str] = None
  289. ) -> List[Dict[str, Any]]:
  290. # Prepare list of enabled servers along with their original index
  291. server_entries = []
  292. for idx, server in enumerate(servers):
  293. if server.get("config", {}).get("enable"):
  294. url_path = server.get("path", "openapi.json")
  295. full_url = f"{server.get('url')}/{url_path}"
  296. auth_type = server.get("auth_type", "bearer")
  297. token = None
  298. if auth_type == "bearer":
  299. token = server.get("key", "")
  300. elif auth_type == "session":
  301. token = session_token
  302. server_entries.append((idx, server, full_url, token))
  303. # Create async tasks to fetch data
  304. tasks = [get_tool_server_data(token, url) for (_, _, url, token) in server_entries]
  305. # Execute tasks concurrently
  306. responses = await asyncio.gather(*tasks, return_exceptions=True)
  307. # Build final results with index and server metadata
  308. results = []
  309. for (idx, server, url, _), response in zip(server_entries, responses):
  310. if isinstance(response, Exception):
  311. print(f"Failed to connect to {url} OpenAPI tool server")
  312. continue
  313. results.append(
  314. {
  315. "idx": idx,
  316. "url": server.get("url"),
  317. "openapi": response.get("openapi"),
  318. "info": response.get("info"),
  319. "specs": response.get("specs"),
  320. }
  321. )
  322. return results
  323. async def execute_tool_server(
  324. token: str, url: str, name: str, params: Dict[str, Any], server_data: Dict[str, Any]
  325. ) -> Any:
  326. error = None
  327. try:
  328. openapi = server_data.get("openapi", {})
  329. paths = openapi.get("paths", {})
  330. matching_route = None
  331. for route_path, methods in paths.items():
  332. for http_method, operation in methods.items():
  333. if isinstance(operation, dict) and operation.get("operationId") == name:
  334. matching_route = (route_path, methods)
  335. break
  336. if matching_route:
  337. break
  338. if not matching_route:
  339. raise Exception(f"No matching route found for operationId: {name}")
  340. route_path, methods = matching_route
  341. method_entry = None
  342. for http_method, operation in methods.items():
  343. if operation.get("operationId") == name:
  344. method_entry = (http_method.lower(), operation)
  345. break
  346. if not method_entry:
  347. raise Exception(f"No matching method found for operationId: {name}")
  348. http_method, operation = method_entry
  349. path_params = {}
  350. query_params = {}
  351. body_params = {}
  352. for param in operation.get("parameters", []):
  353. param_name = param["name"]
  354. param_in = param["in"]
  355. if param_name in params:
  356. if param_in == "path":
  357. path_params[param_name] = params[param_name]
  358. elif param_in == "query":
  359. query_params[param_name] = params[param_name]
  360. final_url = f"{url}{route_path}"
  361. for key, value in path_params.items():
  362. final_url = final_url.replace(f"{{{key}}}", str(value))
  363. if query_params:
  364. query_string = "&".join(f"{k}={v}" for k, v in query_params.items())
  365. final_url = f"{final_url}?{query_string}"
  366. if operation.get("requestBody", {}).get("content"):
  367. if params:
  368. body_params = params
  369. else:
  370. raise Exception(
  371. f"Request body expected for operation '{name}' but none found."
  372. )
  373. headers = {"Content-Type": "application/json"}
  374. if token:
  375. headers["Authorization"] = f"Bearer {token}"
  376. async with aiohttp.ClientSession() as session:
  377. request_method = getattr(session, http_method.lower())
  378. if http_method in ["post", "put", "patch"]:
  379. async with request_method(
  380. final_url, json=body_params, headers=headers
  381. ) as response:
  382. if response.status >= 400:
  383. text = await response.text()
  384. raise Exception(f"HTTP error {response.status}: {text}")
  385. return await response.json()
  386. else:
  387. async with request_method(final_url, headers=headers) as response:
  388. if response.status >= 400:
  389. text = await response.text()
  390. raise Exception(f"HTTP error {response.status}: {text}")
  391. return await response.json()
  392. except Exception as err:
  393. error = str(err)
  394. print("API Request Error:", error)
  395. return {"error": error}