-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
fix(utils.py): fix openai-like api response format parsing #7273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3612,53 +3612,21 @@ def _map_and_modify_arg(supported_params: dict, provider: str, model: str): | |
else False | ||
), | ||
) | ||
else: # assume passing in params for text-completion openai | ||
else: # assume passing in params for openai-like api | ||
supported_params = get_supported_openai_params( | ||
model=model, custom_llm_provider="custom_openai" | ||
) | ||
_check_valid_arg(supported_params=supported_params) | ||
if functions is not None: | ||
optional_params["functions"] = functions | ||
if function_call is not None: | ||
optional_params["function_call"] = function_call | ||
if temperature is not None: | ||
optional_params["temperature"] = temperature | ||
if top_p is not None: | ||
optional_params["top_p"] = top_p | ||
if n is not None: | ||
optional_params["n"] = n | ||
if stream is not None: | ||
optional_params["stream"] = stream | ||
if stream_options is not None: | ||
optional_params["stream_options"] = stream_options | ||
if stop is not None: | ||
optional_params["stop"] = stop | ||
if max_tokens is not None: | ||
optional_params["max_tokens"] = max_tokens | ||
if presence_penalty is not None: | ||
optional_params["presence_penalty"] = presence_penalty | ||
if frequency_penalty is not None: | ||
optional_params["frequency_penalty"] = frequency_penalty | ||
if logit_bias is not None: | ||
optional_params["logit_bias"] = logit_bias | ||
if user is not None: | ||
optional_params["user"] = user | ||
if response_format is not None: | ||
optional_params["response_format"] = response_format | ||
if seed is not None: | ||
optional_params["seed"] = seed | ||
if tools is not None: | ||
optional_params["tools"] = tools | ||
if tool_choice is not None: | ||
optional_params["tool_choice"] = tool_choice | ||
if max_retries is not None: | ||
optional_params["max_retries"] = max_retries | ||
if logprobs is not None: | ||
optional_params["logprobs"] = logprobs | ||
if top_logprobs is not None: | ||
optional_params["top_logprobs"] = top_logprobs | ||
if extra_headers is not None: | ||
optional_params["extra_headers"] = extra_headers | ||
optional_params = litellm.OpenAILikeChatConfig().map_openai_params( | ||
non_default_params=non_default_params, | ||
optional_params=optional_params, | ||
model=model, | ||
drop_params=( | ||
drop_params | ||
if drop_params is not None and isinstance(drop_params, bool) | ||
else False | ||
), | ||
) | ||
if ( | ||
custom_llm_provider | ||
in ["openai", "azure", "text-completion-openai"] | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -138,10 +138,14 @@ def return_val(): | |
mock_async_post.return_value = mock_response | ||
model = "vertex_ai/test" | ||
|
||
response = await litellm.aspeech( | ||
model=model, | ||
input="async hello what llm guardrail do you have", | ||
) | ||
try: | ||
response = await litellm.aspeech( | ||
model=model, | ||
input="async hello what llm guardrail do you have", | ||
) | ||
except litellm.APIConnectionError as e: | ||
if "Your default credentials were not found" in str(e): | ||
pytest.skip("skipping test, credentials not found") | ||
|
||
# Assert asynchronous call | ||
mock_async_post.assert_called_once() | ||
|
@@ -181,18 +185,22 @@ def return_val(): | |
mock_async_post.return_value = mock_response | ||
model = "vertex_ai/test" | ||
|
||
response = await litellm.aspeech( | ||
model=model, | ||
input="async hello what llm guardrail do you have", | ||
voice={ | ||
"languageCode": "en-UK", | ||
"name": "en-UK-Studio-O", | ||
}, | ||
audioConfig={ | ||
"audioEncoding": "LINEAR22", | ||
"speakingRate": "10", | ||
}, | ||
) | ||
try: | ||
response = await litellm.aspeech( | ||
model=model, | ||
input="async hello what llm guardrail do you have", | ||
voice={ | ||
"languageCode": "en-UK", | ||
"name": "en-UK-Studio-O", | ||
}, | ||
audioConfig={ | ||
"audioEncoding": "LINEAR22", | ||
"speakingRate": "10", | ||
}, | ||
) | ||
except litellm.APIConnectionError as e: | ||
if "Your default credentials were not found" in str(e): | ||
pytest.skip("skipping test, credentials not found") | ||
|
||
# Assert asynchronous call | ||
mock_async_post.assert_called_once() | ||
|
@@ -239,18 +247,22 @@ def return_val(): | |
mock_async_post.return_value = mock_response | ||
model = "vertex_ai/test" | ||
|
||
response = await litellm.aspeech( | ||
input=ssml, | ||
model=model, | ||
voice={ | ||
"languageCode": "en-UK", | ||
"name": "en-UK-Studio-O", | ||
}, | ||
audioConfig={ | ||
"audioEncoding": "LINEAR22", | ||
"speakingRate": "10", | ||
}, | ||
) | ||
try: | ||
response = await litellm.aspeech( | ||
input=ssml, | ||
model=model, | ||
voice={ | ||
"languageCode": "en-UK", | ||
"name": "en-UK-Studio-O", | ||
}, | ||
audioConfig={ | ||
"audioEncoding": "LINEAR22", | ||
"speakingRate": "10", | ||
}, | ||
) | ||
except litellm.APIConnectionError as e: | ||
if "Your default credentials were not found" in str(e): | ||
pytest.skip("skipping test, credentials not found") | ||
|
||
# Assert asynchronous call | ||
mock_async_post.assert_called_once() | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1819,6 +1819,43 @@ async def test_litellm_gateway_from_sdk(): | |
assert "hello" in mock_call.call_args.kwargs["extra_body"] | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_litellm_gateway_from_sdk_structured_output(): | ||
from pydantic import BaseModel | ||
|
||
class Result(BaseModel): | ||
answer: str | ||
|
||
litellm.set_verbose = True | ||
from openai import OpenAI | ||
|
||
openai_client = OpenAI(api_key="fake-key") | ||
|
||
with patch.object( | ||
openai_client.chat.completions, "create", new=MagicMock() | ||
) as mock_call: | ||
try: | ||
litellm.completion( | ||
model="litellm_proxy/openai/gpt-4o", | ||
messages=[ | ||
{"role": "user", "content": "What is the capital of France?"} | ||
], | ||
api_key="my-test-api-key", | ||
user="test", | ||
response_format=Result, | ||
base_url="https://litellm.ml-serving-internal.scale.com", | ||
client=openai_client, | ||
) | ||
except Exception as e: | ||
print(e) | ||
|
||
mock_call.assert_called_once() | ||
|
||
print("Call KWARGS - {}".format(mock_call.call_args.kwargs)) | ||
json_schema = mock_call.call_args.kwargs["response_format"] | ||
assert "json_schema" in json_schema | ||
|
||
|
||
# ################### Hugging Face Conversational models ######################## | ||
# def hf_test_completion_conv(): | ||
# try: | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -393,6 +393,8 @@ def test_whisper_openai(): | |
transcription = TranscriptionResponse( | ||
text="Four score and seven years ago, our fathers brought forth on this continent a new nation, conceived in liberty and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure." | ||
) | ||
|
||
setattr(transcription, "duration", 3) | ||
transcription._hidden_params = { | ||
"model": "whisper-1", | ||
"custom_llm_provider": "openai", | ||
|
@@ -401,7 +403,6 @@ def test_whisper_openai(): | |
} | ||
_total_time_in_seconds = 3 | ||
|
||
transcription._response_ms = _total_time_in_seconds * 1000 | ||
cost = litellm.completion_cost(model="whisper-1", completion_response=transcription) | ||
|
||
print(f"cost: {cost}") | ||
|
@@ -411,7 +412,7 @@ def test_whisper_openai(): | |
* _total_time_in_seconds, | ||
5, | ||
) | ||
assert cost == expected_cost | ||
assert round(cost, 5) == round(expected_cost, 5) | ||
|
||
|
||
def test_whisper_azure(): | ||
|
@@ -426,8 +427,8 @@ def test_whisper_azure(): | |
"model_id": None, | ||
} | ||
_total_time_in_seconds = 3 | ||
setattr(transcription, "duration", _total_time_in_seconds) | ||
|
||
transcription._response_ms = _total_time_in_seconds * 1000 | ||
cost = litellm.completion_cost( | ||
model="azure/azure-whisper", completion_response=transcription | ||
) | ||
|
@@ -439,7 +440,7 @@ def test_whisper_azure(): | |
* _total_time_in_seconds, | ||
5, | ||
) | ||
assert cost == expected_cost | ||
assert round(cost, 5) == round(expected_cost, 5) | ||
|
||
|
||
def test_dalle_3_azure_cost_tracking(): | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.