22import inspect
33import platform
44import packaging .version
5- from typing import Annotated , Optional , Union , Callable , Literal , Any , get_args , get_origin
6- from .types import OpenAIFunctionSchema , ClaudeFunctionSchema
5+ from typing import (
6+ Annotated ,
7+ Optional ,
8+ Union ,
9+ Callable ,
10+ Literal ,
11+ Any ,
12+ get_args ,
13+ get_origin ,
14+ )
15+ from .types import FunctionSchema
716
817current_version = packaging .version .parse (platform .python_version ())
918py_310 = packaging .version .parse ("3.10" )
1928 try :
2029 from typing_extensions import Doc
2130 except ImportError :
31+
2232 class Doc :
2333 def __init__ (self , documentation : str , / ):
2434 self .documentation = documentation
2535
36+
2637__all__ = ("get_function_schema" , "guess_type" , "Doc" , "Annotated" )
2738
2839
29- def is_doc_meta (obj : Annotated [Any , Doc ("The object to be checked." )]) -> Annotated [
30- bool , Doc ("True if the object is a documentation object, False otherwise." )]:
40+ def is_doc_meta (
41+ obj : Annotated [Any , Doc ("The object to be checked." )],
42+ ) -> Annotated [
43+ bool , Doc ("True if the object is a documentation object, False otherwise." )
44+ ]:
3145 """
3246 Check if the given object is a documentation object.
3347
3448 Example:
3549 >>> is_doc_meta(Doc("This is a documentation object"))
3650 True
3751 """
38- return getattr (obj , ' __class__' ) == Doc and hasattr (obj , ' documentation' )
52+ return getattr (obj , " __class__" ) == Doc and hasattr (obj , " documentation" )
3953
4054
41- def unwrap_doc (obj : Annotated [Union [Doc , str ],
42- Doc ("The object to get the documentation string from." )]) -> Annotated [
43- str , Doc ("The documentation string." )]:
55+ def unwrap_doc (
56+ obj : Annotated [
57+ Union [Doc , str ], Doc ("The object to get the documentation string from." )
58+ ],
59+ ) -> Annotated [str , Doc ("The documentation string." )]:
4460 """
4561 Get the documentation string from the given object.
4662
@@ -61,8 +77,7 @@ def get_function_schema(
6177 Optional [Literal ["openai" , "claude" ]],
6278 Doc ("The format of the schema to return" ),
6379 ] = "openai" ,
64- ) -> Annotated [Union [OpenAIFunctionSchema , ClaudeFunctionSchema ],
65- Doc ("The JSON schema for the given function" )]:
80+ ) -> Annotated [FunctionSchema , Doc ("The JSON schema for the given function" )]:
6681 """
6782 Returns a JSON schema for the given function.
6883
@@ -127,13 +142,13 @@ def get_function_schema(
127142 # find description in param_args tuple
128143 try :
129144 description = next (
130- unwrap_doc (arg )
131- for arg in param_args if isinstance (arg , Doc )
145+ unwrap_doc (arg ) for arg in param_args if isinstance (arg , Doc )
132146 )
133147 except StopIteration :
134148 try :
135149 description = next (
136- arg for arg in param_args if isinstance (arg , str ))
150+ arg for arg in param_args if isinstance (arg , str )
151+ )
137152 except StopIteration :
138153 description = "The {name} parameter"
139154
@@ -163,8 +178,7 @@ def get_function_schema(
163178 }
164179
165180 if enum_ is not None :
166- schema ["properties" ][name ]["enum" ] = [
167- t for t in enum_ if t is not None ]
181+ schema ["properties" ][name ]["enum" ] = [t for t in enum_ if t is not None ]
168182
169183 if default_value is not inspect ._empty :
170184 schema ["properties" ][name ]["default" ] = default_value
@@ -194,8 +208,7 @@ def get_function_schema(
194208def guess_type (
195209 T : Annotated [type , Doc ("The type to guess the JSON schema type for" )],
196210) -> Annotated [
197- Union [str , list [str ]], Doc (
198- "str | list of str that representing JSON schema type" )
211+ Union [str , list [str ]], Doc ("str | list of str that representing JSON schema type" )
199212]:
200213 """Guesses the JSON schema type for the given python type."""
201214
0 commit comments