-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlitellm_utils.py
More file actions
69 lines (59 loc) · 2.01 KB
/
litellm_utils.py
File metadata and controls
69 lines (59 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import inspect
import typing
from typing import Any, Callable, Dict, List, Optional
def function_to_litellm_definition(
func: Callable, description: Optional[str] = None
) -> Dict[str, Any]:
sig = inspect.signature(func)
doc = description or func.__doc__ or ""
properties: Dict[str, Dict[str, str]] = {}
required: List[str] = []
for name, param in sig.parameters.items():
# We need to skip the 'self' parameter for methods
# if name == "self":
# continue
param_type = (
param.annotation if param.annotation != inspect.Parameter.empty else str
)
json_type = python_type_to_json_type(param_type)
properties[name] = {
"type": json_type,
"description": f"{name} parameter",
}
if param.default == inspect.Parameter.empty:
required.append(name)
function_def = {
"name": func.__name__,
"description": doc.strip(),
}
# If the function has parameters, we add them to the definition
# as the start_point does not have any properties we deliberately
# do not add the "parameters" key if there are no properties
# - some smaller LLMs do not understand this and throw an error
# for the start point function when they try to call it
if properties:
function_def["parameters"] = {
"type": "object",
"properties": properties,
"required": required,
}
return {
"type": "function",
"function": function_def,
}
def python_type_to_json_type(python_type: type) -> str:
# Basic type mapping
if python_type in [str]:
return "string"
elif python_type in [int]:
return "integer"
elif python_type in [float]:
return "number"
elif python_type in [bool]:
return "boolean"
elif python_type in [dict]:
return "object"
elif python_type in [list, typing.List]:
return "array"
else:
return "string" # default fallback