function_tool.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. import inspect
  2. from inspect import Parameter, signature
  3. from typing import Any, Callable, Dict, Mapping, Optional, Tuple, Type
  4. from docstring_parser import parse
  5. from jsonschema.exceptions import SchemaError
  6. from pydantic import BaseModel, create_model
  7. from pydantic.fields import FieldInfo
  8. from jsonschema.validators import Draft202012Validator as JSONValidator
  9. import re
  10. import sys
  11. import os
  12. # FIXME
  13. sys.path.append(os.curdir)
  14. from pqai_agent.logging_service import logger
  15. def to_pascal(snake: str) -> str:
  16. """Convert a snake_case string to PascalCase.
  17. Args:
  18. snake (str): The snake_case string to be converted.
  19. Returns:
  20. str: The converted PascalCase string.
  21. """
  22. # Check if the string is already in PascalCase
  23. if re.match(r'^[A-Z][a-zA-Z0-9]*([A-Z][a-zA-Z0-9]*)*$', snake):
  24. return snake
  25. # Remove leading and trailing underscores
  26. snake = snake.strip('_')
  27. # Replace multiple underscores with a single one
  28. snake = re.sub('_+', '_', snake)
  29. # Convert to PascalCase
  30. return re.sub(
  31. '_([0-9A-Za-z])',
  32. lambda m: m.group(1).upper(),
  33. snake.title(),
  34. )
  35. def get_pydantic_object_schema(pydantic_params: Type[BaseModel]) -> Dict:
  36. r"""Get the JSON schema of a Pydantic model.
  37. Args:
  38. pydantic_params (Type[BaseModel]): The Pydantic model class to retrieve
  39. the schema for.
  40. Returns:
  41. dict: The JSON schema of the Pydantic model.
  42. """
  43. return pydantic_params.model_json_schema()
  44. def _remove_title_recursively(data, parent_key=None):
  45. r"""Recursively removes the 'title' key from all levels of a nested
  46. dictionary, except when 'title' is an argument name in the schema.
  47. """
  48. if isinstance(data, dict):
  49. # Only remove 'title' if it's not an argument name
  50. if parent_key not in [
  51. "properties",
  52. "$defs",
  53. "items",
  54. "allOf",
  55. "oneOf",
  56. "anyOf",
  57. ]:
  58. data.pop("title", None)
  59. # Recursively process each key-value pair
  60. for key, value in data.items():
  61. _remove_title_recursively(value, parent_key=key)
  62. elif isinstance(data, list):
  63. # Recursively process each element in the list
  64. for item in data:
  65. _remove_title_recursively(item, parent_key=parent_key)
  66. def get_openai_tool_schema(func: Callable) -> Dict[str, Any]:
  67. r"""Generates an OpenAI JSON schema from a given Python function.
  68. This function creates a schema compatible with OpenAI's API specifications,
  69. based on the provided Python function. It processes the function's
  70. parameters, types, and docstrings, and constructs a schema accordingly.
  71. Note:
  72. - Each parameter in `func` must have a type annotation; otherwise, it's
  73. treated as 'Any'.
  74. - Variable arguments (*args) and keyword arguments (**kwargs) are not
  75. supported and will be ignored.
  76. - A functional description including a brief and detailed explanation
  77. should be provided in the docstring of `func`.
  78. - All parameters of `func` must be described in its docstring.
  79. - Supported docstring styles: ReST, Google, Numpydoc, and Epydoc.
  80. Args:
  81. func (Callable): The Python function to be converted into an OpenAI
  82. JSON schema.
  83. Returns:
  84. Dict[str, Any]: A dictionary representing the OpenAI JSON schema of
  85. the provided function.
  86. See Also:
  87. `OpenAI API Reference
  88. <https://platform.openai.com/docs/api-reference/assistants/object>`_
  89. """
  90. params: Mapping[str, Parameter] = signature(func).parameters
  91. fields: Dict[str, Tuple[type, FieldInfo]] = {}
  92. for param_name, p in params.items():
  93. param_type = p.annotation
  94. param_default = p.default
  95. param_kind = p.kind
  96. param_annotation = p.annotation
  97. # Variable parameters are not supported
  98. if (
  99. param_kind == Parameter.VAR_POSITIONAL
  100. or param_kind == Parameter.VAR_KEYWORD
  101. ):
  102. continue
  103. # If the parameter type is not specified, it defaults to typing.Any
  104. if param_annotation is Parameter.empty:
  105. param_type = Any
  106. # Check if the parameter has a default value
  107. if param_default is Parameter.empty:
  108. fields[param_name] = (param_type, FieldInfo())
  109. else:
  110. fields[param_name] = (param_type, FieldInfo(default=param_default))
  111. # Applying `create_model()` directly will result in a mypy error,
  112. # create an alias to avoid this.
  113. def _create_mol(name, field):
  114. return create_model(name, **field)
  115. model = _create_mol(to_pascal(func.__name__), fields)
  116. parameters_dict = get_pydantic_object_schema(model)
  117. # The `"title"` is generated by `model.model_json_schema()`
  118. # but is useless for openai json schema, remove generated 'title' from
  119. # parameters_dict
  120. _remove_title_recursively(parameters_dict)
  121. docstring = parse(func.__doc__ or "")
  122. for param in docstring.params:
  123. if (name := param.arg_name) in parameters_dict["properties"] and (
  124. description := param.description
  125. ):
  126. parameters_dict["properties"][name]["description"] = description
  127. short_description = docstring.short_description or ""
  128. long_description = docstring.long_description or ""
  129. if long_description:
  130. func_description = f"{short_description}\n{long_description}"
  131. else:
  132. func_description = short_description
  133. # OpenAI client.beta.chat.completions.parse for structured output has
  134. # additional requirements for the schema, refer:
  135. # https://platform.openai.com/docs/guides/structured-outputs/some-type-specific-keywords-are-not-yet-supported#supported-schemas
  136. parameters_dict["additionalProperties"] = False
  137. openai_function_schema = {
  138. "name": func.__name__,
  139. "description": func_description,
  140. "strict": True,
  141. "parameters": parameters_dict,
  142. }
  143. openai_tool_schema = {
  144. "type": "function",
  145. "function": openai_function_schema,
  146. }
  147. openai_tool_schema = sanitize_and_enforce_required(openai_tool_schema)
  148. return openai_tool_schema
  149. def sanitize_and_enforce_required(parameters_dict):
  150. r"""Cleans and updates the function schema to conform with OpenAI's
  151. requirements:
  152. - Removes invalid 'default' fields from the parameters schema.
  153. - Ensures all fields or function parameters are marked as required.
  154. Args:
  155. parameters_dict (dict): The dictionary representing the function
  156. schema.
  157. Returns:
  158. dict: The updated dictionary with invalid defaults removed and all
  159. fields set as required.
  160. """
  161. # Check if 'function' and 'parameters' exist
  162. if (
  163. 'function' in parameters_dict
  164. and 'parameters' in parameters_dict['function']
  165. ):
  166. # Access the 'parameters' section
  167. parameters = parameters_dict['function']['parameters']
  168. properties = parameters.get('properties', {})
  169. # Remove 'default' key from each property
  170. for field in properties.values():
  171. field.pop('default', None)
  172. # Mark all keys in 'properties' as required
  173. parameters['required'] = list(properties.keys())
  174. return parameters_dict
  175. class FunctionTool:
  176. r"""An abstraction of a function that OpenAI chat models can call. See
  177. https://platform.openai.com/docs/api-reference/chat/create.
  178. By default, the tool schema will be parsed from the func, or you can
  179. provide a user-defined tool schema to override.
  180. Args:
  181. func (Callable): The function to call. The tool schema is parsed from
  182. the function signature and docstring by default.
  183. openai_tool_schema (Optional[Dict[str, Any]], optional): A
  184. user-defined OpenAI tool schema to override the default result.
  185. (default: :obj:`None`)
  186. """
  187. def __init__(
  188. self,
  189. func: Callable,
  190. openai_tool_schema: Optional[Dict[str, Any]] = None
  191. ) -> None:
  192. self.func = func
  193. self.openai_tool_schema = openai_tool_schema or get_openai_tool_schema(
  194. func
  195. )
  196. def __call__(self, *args: Any, **kwargs: Any) -> Any:
  197. # Pass the extracted arguments to the indicated function
  198. try:
  199. result = self.func(*args, **kwargs)
  200. return result
  201. except Exception as e:
  202. raise ValueError(
  203. f"Execution of function {self.func.__name__} failed with "
  204. f"arguments {args} and {kwargs}. "
  205. f"Error: {e}"
  206. )
  207. async def async_call(self, *args: Any, **kwargs: Any) -> Any:
  208. if self.is_async:
  209. return await self.func(*args, **kwargs)
  210. else:
  211. return self.func(*args, **kwargs)
  212. @property
  213. def is_async(self) -> bool:
  214. return inspect.iscoroutinefunction(self.func)
  215. @staticmethod
  216. def validate_openai_tool_schema(
  217. openai_tool_schema: Dict[str, Any],
  218. ) -> None:
  219. r"""Validates the OpenAI tool schema against
  220. :obj:`ToolAssistantToolsFunction`.
  221. This function checks if the provided :obj:`openai_tool_schema` adheres
  222. to the specifications required by OpenAI's
  223. :obj:`ToolAssistantToolsFunction`. It ensures that the function
  224. description and parameters are correctly formatted according to JSON
  225. Schema specifications.
  226. Args:
  227. openai_tool_schema (Dict[str, Any]): The OpenAI tool schema to
  228. validate.
  229. Raises:
  230. ValidationError: If the schema does not comply with the
  231. specifications.
  232. SchemaError: If the parameters do not meet JSON Schema reference
  233. specifications.
  234. """
  235. # Check the type
  236. if not openai_tool_schema["type"]:
  237. raise ValueError("miss `type` in tool schema.")
  238. # Check the function description, if no description then raise warming
  239. if not openai_tool_schema["function"].get("description"):
  240. logger.warning(f"""Function description is missing for
  241. {openai_tool_schema['function']['name']}. This may
  242. affect the quality of tool calling.""")
  243. # Validate whether parameters
  244. # meet the JSON Schema reference specifications.
  245. # See https://platform.openai.com/docs/guides/gpt/function-calling
  246. # for examples, and the
  247. # https://json-schema.org/understanding-json-schema/ for
  248. # documentation about the format.
  249. parameters = openai_tool_schema["function"]["parameters"]
  250. try:
  251. JSONValidator.check_schema(parameters)
  252. except SchemaError as e:
  253. raise e
  254. # Check the parameter description, if no description then raise warming
  255. properties: Dict[str, Any] = parameters["properties"]
  256. for param_name in properties.keys():
  257. param_dict = properties[param_name]
  258. if "description" not in param_dict:
  259. logger.warning(f"""Parameter description is missing for
  260. {param_dict}. This may affect the quality of tool
  261. calling.""")
  262. def get_openai_tool_schema(self) -> Dict[str, Any]:
  263. r"""Gets the OpenAI tool schema for this function.
  264. This method returns the OpenAI tool schema associated with this
  265. function, after validating it to ensure it meets OpenAI's
  266. specifications.
  267. Returns:
  268. Dict[str, Any]: The OpenAI tool schema for this function.
  269. """
  270. self.validate_openai_tool_schema(self.openai_tool_schema)
  271. return self.openai_tool_schema
  272. def set_openai_tool_schema(self, schema: Dict[str, Any]) -> None:
  273. r"""Sets the OpenAI tool schema for this function.
  274. Allows setting a custom OpenAI tool schema for this function.
  275. Args:
  276. schema (Dict[str, Any]): The OpenAI tool schema to set.
  277. """
  278. self.openai_tool_schema = schema
  279. def get_openai_function_schema(self) -> Dict[str, Any]:
  280. r"""Gets the schema of the function from the OpenAI tool schema.
  281. This method extracts and returns the function-specific part of the
  282. OpenAI tool schema associated with this function.
  283. Returns:
  284. Dict[str, Any]: The schema of the function within the OpenAI tool
  285. schema.
  286. """
  287. self.validate_openai_tool_schema(self.openai_tool_schema)
  288. return self.openai_tool_schema["function"]
  289. def set_openai_function_schema(
  290. self,
  291. openai_function_schema: Dict[str, Any],
  292. ) -> None:
  293. r"""Sets the schema of the function within the OpenAI tool schema.
  294. Args:
  295. openai_function_schema (Dict[str, Any]): The function schema to
  296. set within the OpenAI tool schema.
  297. """
  298. self.openai_tool_schema["function"] = openai_function_schema
  299. def get_function_name(self) -> str:
  300. r"""Gets the name of the function from the OpenAI tool schema.
  301. Returns:
  302. str: The name of the function.
  303. """
  304. self.validate_openai_tool_schema(self.openai_tool_schema)
  305. return self.openai_tool_schema["function"]["name"]
  306. def set_function_name(self, name: str) -> None:
  307. r"""Sets the name of the function in the OpenAI tool schema.
  308. Args:
  309. name (str): The name of the function to set.
  310. """
  311. self.openai_tool_schema["function"]["name"] = name
  312. def get_function_description(self) -> str:
  313. r"""Gets the description of the function from the OpenAI tool
  314. schema.
  315. Returns:
  316. str: The description of the function.
  317. """
  318. self.validate_openai_tool_schema(self.openai_tool_schema)
  319. return self.openai_tool_schema["function"]["description"]
  320. def set_function_description(self, description: str) -> None:
  321. r"""Sets the description of the function in the OpenAI tool schema.
  322. Args:
  323. description (str): The description for the function.
  324. """
  325. self.openai_tool_schema["function"]["description"] = description
  326. def get_parameter_description(self, param_name: str) -> str:
  327. r"""Gets the description of a specific parameter from the function
  328. schema.
  329. Args:
  330. param_name (str): The name of the parameter to get the
  331. description.
  332. Returns:
  333. str: The description of the specified parameter.
  334. """
  335. self.validate_openai_tool_schema(self.openai_tool_schema)
  336. return self.openai_tool_schema["function"]["parameters"]["properties"][
  337. param_name
  338. ]["description"]
  339. def set_parameter_description(
  340. self,
  341. param_name: str,
  342. description: str,
  343. ) -> None:
  344. r"""Sets the description for a specific parameter in the function
  345. schema.
  346. Args:
  347. param_name (str): The name of the parameter to set the description
  348. for.
  349. description (str): The description for the parameter.
  350. """
  351. self.openai_tool_schema["function"]["parameters"]["properties"][
  352. param_name
  353. ]["description"] = description
  354. def get_parameter(self, param_name: str) -> Dict[str, Any]:
  355. r"""Gets the schema for a specific parameter from the function schema.
  356. Args:
  357. param_name (str): The name of the parameter to get the schema.
  358. Returns:
  359. Dict[str, Any]: The schema of the specified parameter.
  360. """
  361. self.validate_openai_tool_schema(self.openai_tool_schema)
  362. return self.openai_tool_schema["function"]["parameters"]["properties"][
  363. param_name
  364. ]
  365. def set_parameter(self, param_name: str, value: Dict[str, Any]):
  366. r"""Sets the schema for a specific parameter in the function schema.
  367. Args:
  368. param_name (str): The name of the parameter to set the schema for.
  369. value (Dict[str, Any]): The schema to set for the parameter.
  370. """
  371. try:
  372. JSONValidator.check_schema(value)
  373. except SchemaError as e:
  374. raise e
  375. self.openai_tool_schema["function"]["parameters"]["properties"][
  376. param_name
  377. ] = value
  378. @property
  379. def parameters(self) -> Dict[str, Any]:
  380. r"""Getter method for the property :obj:`parameters`.
  381. Returns:
  382. Dict[str, Any]: the dictionary containing information of
  383. parameters of this function.
  384. """
  385. self.validate_openai_tool_schema(self.openai_tool_schema)
  386. return self.openai_tool_schema["function"]["parameters"]["properties"]
  387. @parameters.setter
  388. def parameters(self, value: Dict[str, Any]) -> None:
  389. r"""Setter method for the property :obj:`parameters`. It will
  390. firstly check if the input parameters schema is valid. If invalid,
  391. the method will raise :obj:`jsonschema.exceptions.SchemaError`.
  392. Args:
  393. value (Dict[str, Any]): the new dictionary value for the
  394. function's parameters.
  395. """
  396. try:
  397. JSONValidator.check_schema(value)
  398. except SchemaError as e:
  399. raise e
  400. self.openai_tool_schema["function"]["parameters"]["properties"] = value