function_tool.py 17 KB

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