refac
This commit is contained in:
1 parent
3fe03583a3
commit
12974c9e4e
1 file changed
+26
-10
@@ -187,13 +187,16 @@ async def build_tool_server_headers(
|
|||||||
# Let no function be called without need, and let what
|
# Let no function be called without need, and let what
|
||||||
# it yields justify the cost of running it.
|
# it yields justify the cost of running it.
|
||||||
async def get_async_tool_function_and_apply_extra_params(
|
async def get_async_tool_function_and_apply_extra_params(
|
||||||
function: Callable, extra_params: dict
|
function: Callable, extra_params: dict, function_introspection=None
|
||||||
) -> Callable[..., Awaitable]:
|
) -> Callable[..., Awaitable]:
|
||||||
sig = inspect.signature(function)
|
if function_introspection is None:
|
||||||
try:
|
sig = inspect.signature(function)
|
||||||
type_hints = get_type_hints(function)
|
try:
|
||||||
except Exception:
|
type_hints = get_type_hints(function)
|
||||||
type_hints = {}
|
except Exception:
|
||||||
|
type_hints = {}
|
||||||
|
else:
|
||||||
|
sig, type_hints = function_introspection
|
||||||
|
|
||||||
def coerce_kwargs(kwargs):
|
def coerce_kwargs(kwargs):
|
||||||
for name, value in kwargs.items():
|
for name, value in kwargs.items():
|
||||||
@@ -779,6 +782,7 @@ async def get_builtin_tools(
|
|||||||
'__message_id__': extra_params.get('__message_id__'),
|
'__message_id__': extra_params.get('__message_id__'),
|
||||||
'__model_knowledge__': model_knowledge,
|
'__model_knowledge__': model_knowledge,
|
||||||
},
|
},
|
||||||
|
get_builtin_function_introspection(func),
|
||||||
)
|
)
|
||||||
|
|
||||||
spec = get_builtin_tool_spec(func)
|
spec = get_builtin_tool_spec(func)
|
||||||
@@ -853,7 +857,7 @@ def parse_docstring(docstring):
|
|||||||
return param_descriptions
|
return param_descriptions
|
||||||
|
|
||||||
|
|
||||||
def convert_function_to_pydantic_model(func: Callable) -> type[BaseModel]:
|
def convert_function_to_pydantic_model(func: Callable, function_introspection=None) -> type[BaseModel]:
|
||||||
"""
|
"""
|
||||||
Converts a Python function's type hints and docstring to a Pydantic model,
|
Converts a Python function's type hints and docstring to a Pydantic model,
|
||||||
including support for nested types, default values, and descriptions.
|
including support for nested types, default values, and descriptions.
|
||||||
@@ -865,8 +869,11 @@ def convert_function_to_pydantic_model(func: Callable) -> type[BaseModel]:
|
|||||||
Returns:
|
Returns:
|
||||||
A Pydantic model class.
|
A Pydantic model class.
|
||||||
"""
|
"""
|
||||||
type_hints = get_type_hints(func)
|
if function_introspection is None:
|
||||||
signature = inspect.signature(func)
|
type_hints = get_type_hints(func)
|
||||||
|
signature = inspect.signature(func)
|
||||||
|
else:
|
||||||
|
signature, type_hints = function_introspection
|
||||||
parameters = signature.parameters
|
parameters = signature.parameters
|
||||||
|
|
||||||
docstring = func.__doc__
|
docstring = func.__doc__
|
||||||
@@ -932,9 +939,18 @@ def clean_openai_tool_schema(spec: dict) -> dict:
|
|||||||
return cleaned_spec
|
return cleaned_spec
|
||||||
|
|
||||||
|
|
||||||
|
@cache
|
||||||
|
def get_builtin_function_introspection(func: Callable):
|
||||||
|
try:
|
||||||
|
type_hints = get_type_hints(func)
|
||||||
|
except Exception:
|
||||||
|
type_hints = {}
|
||||||
|
return inspect.signature(func), type_hints
|
||||||
|
|
||||||
|
|
||||||
@cache
|
@cache
|
||||||
def build_builtin_tool_spec(func: Callable) -> dict:
|
def build_builtin_tool_spec(func: Callable) -> dict:
|
||||||
pydantic_model = convert_function_to_pydantic_model(func)
|
pydantic_model = convert_function_to_pydantic_model(func, get_builtin_function_introspection(func))
|
||||||
spec = convert_pydantic_model_to_openai_function_spec(pydantic_model)
|
spec = convert_pydantic_model_to_openai_function_spec(pydantic_model)
|
||||||
return clean_openai_tool_schema(spec)
|
return clean_openai_tool_schema(spec)
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user