mirror of
https://github.com/BerriAI/litellm.git
synced 2025-04-26 03:04:13 +00:00
(feat) new function_to_dict litellm.util
This commit is contained in:
parent
02e97acefa
commit
7848f1b5b7
3 changed files with 169 additions and 3 deletions
|
@ -10,7 +10,7 @@ sys.path.insert(
|
|||
) # Adds the parent directory to the system path
|
||||
import pytest
|
||||
import litellm
|
||||
from litellm.utils import trim_messages, get_token_count, get_valid_models, check_valid_key, validate_environment
|
||||
from litellm.utils import trim_messages, get_token_count, get_valid_models, check_valid_key, validate_environment, function_to_dict
|
||||
|
||||
# Assuming your trim_messages, shorten_message_to_fit_limit, and get_token_count functions are all in a module named 'message_utils'
|
||||
|
||||
|
@ -101,4 +101,55 @@ def test_validate_environment_empty_model():
|
|||
if api_key is None:
|
||||
raise Exception()
|
||||
|
||||
# test_validate_environment_empty_model()
|
||||
# test_validate_environment_empty_model()
|
||||
|
||||
def test_function_to_dict():
|
||||
print("testing function to dict for get current weather")
|
||||
def get_current_weather(location: str, unit: str):
|
||||
"""Get the current weather in a given location
|
||||
|
||||
Parameters
|
||||
----------
|
||||
location : str
|
||||
The city and state, e.g. San Francisco, CA
|
||||
unit : {'celsius', 'fahrenheit'}
|
||||
Temperature unit
|
||||
|
||||
Returns
|
||||
-------
|
||||
str
|
||||
a sentence indicating the weather
|
||||
"""
|
||||
if location == "Boston, MA":
|
||||
return "The weather is 12F"
|
||||
function_json = litellm.utils.function_to_dict(get_current_weather)
|
||||
print(function_json)
|
||||
|
||||
expected_output = {
|
||||
'name': 'get_current_weather',
|
||||
'description': 'Get the current weather in a given location',
|
||||
'parameters': {
|
||||
'type': 'object',
|
||||
'properties': {
|
||||
'location': {'type': 'string', 'description': 'The city and state, e.g. San Francisco, CA'},
|
||||
'unit': {'type': 'string', 'description': 'Temperature unit', 'enum': "['fahrenheit', 'celsius']"}
|
||||
},
|
||||
'required': ['location', 'unit']
|
||||
}
|
||||
}
|
||||
print(expected_output)
|
||||
|
||||
assert function_json['name'] == expected_output["name"]
|
||||
assert function_json["description"] == expected_output["description"]
|
||||
assert function_json["parameters"]["type"] == expected_output["parameters"]["type"]
|
||||
assert function_json["parameters"]["properties"]["location"] == expected_output["parameters"]["properties"]["location"]
|
||||
|
||||
# the enum can change it can be - which is why we don't assert on unit
|
||||
# {'type': 'string', 'description': 'Temperature unit', 'enum': "['fahrenheit', 'celsius']"}
|
||||
# {'type': 'string', 'description': 'Temperature unit', 'enum': "['celsius', 'fahrenheit']"}
|
||||
|
||||
assert function_json["parameters"]["required"] == expected_output["parameters"]["required"]
|
||||
|
||||
print("passed")
|
||||
# test_function_to_dict()
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue