Spaces:
Running
Running
import inspect, json | |
def show_json(str, obj): | |
print(f"=> {str}\n{json.loads(obj.model_dump_json())}") | |
def function_to_schema(func) -> dict: | |
type_map = { | |
str: "string", | |
int: "integer", | |
float: "number", | |
bool: "boolean", | |
list: "array", | |
dict: "object", | |
type(None): "null", | |
} | |
try: | |
signature = inspect.signature(func) | |
except ValueError as e: | |
raise ValueError( | |
f"Failed to get signature for function {func.__name__}: {str(e)}" | |
) | |
parameters = {} | |
for param in signature.parameters.values(): | |
try: | |
param_type = type_map.get(param.annotation, "string") | |
except KeyError as e: | |
raise KeyError( | |
f"Unknown type annotation {param.annotation} for parameter {param.name}: {str(e)}" | |
) | |
parameters[param.name] = {"type": param_type} | |
required = [ | |
param.name | |
for param in signature.parameters.values() | |
if param.default == inspect._empty | |
] | |
return { | |
"name": func.__name__, | |
"description": func.__doc__ or "", | |
"parameters": { | |
"type": "object", | |
"properties": parameters, | |
"required": required, | |
}, | |
} |