test: set request timeout at request level

This commit is contained in:
Krrish Dholakia 2023-11-15 17:41:04 -08:00
parent eda4401c71
commit 9fd7c5b343
6 changed files with 63 additions and 38 deletions

View file

@ -4,6 +4,7 @@ from .base import BaseLLM
from litellm.utils import ModelResponse, Choices, Message, CustomStreamWrapper, convert_to_model_response_object
from typing import Callable, Optional
from litellm import OpenAIConfig
import litellm
import httpx
class AzureOpenAIError(Exception):
@ -105,6 +106,8 @@ class AzureChatCompletion(BaseLLM):
acompletion: bool = False,
headers: Optional[dict]=None):
super().completion()
if self._client_session is None:
self._client_session = self.create_client_session()
exception_mapping_worked = False
try:
if headers is None:
@ -140,10 +143,11 @@ class AzureChatCompletion(BaseLLM):
elif "stream" in optional_params and optional_params["stream"] == True:
return self.streaming(logging_obj=logging_obj, api_base=api_base, data=data, headers=headers, model_response=model_response, model=model)
else:
response = httpx.post(
response = self._client_session.post(
url=api_base,
json=data,
headers=headers,
timeout=litellm.request_timeout
)
if response.status_code != 200:
raise AzureOpenAIError(status_code=response.status_code, message=response.text)
@ -157,15 +161,17 @@ class AzureChatCompletion(BaseLLM):
raise e
async def acompletion(self, api_base: str, data: dict, headers: dict, model_response: ModelResponse):
if self._aclient_session is None:
self._aclient_session = self.create_aclient_session()
client = self._aclient_session
try:
async with httpx.AsyncClient() as client:
response = await client.post(api_base, json=data, headers=headers)
response_json = response.json()
if response.status_code != 200:
raise AzureOpenAIError(status_code=response.status_code, message=response.text, request=response.request, response=response)
## RESPONSE OBJECT
return convert_to_model_response_object(response_object=response_json, model_response_object=model_response)
response = await client.post(api_base, json=data, headers=headers, timeout=litellm.request_timeout)
response_json = response.json()
if response.status_code != 200:
raise AzureOpenAIError(status_code=response.status_code, message=response.text, request=response.request, response=response)
## RESPONSE OBJECT
return convert_to_model_response_object(response_object=response_json, model_response_object=model_response)
except Exception as e:
if isinstance(e,httpx.TimeoutException):
raise AzureOpenAIError(status_code=500, message="Request Timeout Error")
@ -182,11 +188,14 @@ class AzureChatCompletion(BaseLLM):
model_response: ModelResponse,
model: str
):
with httpx.stream(
if self._client_session is None:
self._client_session = self.create_client_session()
with self._client_session.stream(
url=f"{api_base}",
json=data,
headers=headers,
method="POST"
method="POST",
timeout=litellm.request_timeout
) as response:
if response.status_code != 200:
raise AzureOpenAIError(status_code=response.status_code, message="An error occurred while streaming")
@ -203,12 +212,15 @@ class AzureChatCompletion(BaseLLM):
headers: dict,
model_response: ModelResponse,
model: str):
client = httpx.AsyncClient()
if self._aclient_session is None:
self._aclient_session = self.create_aclient_session()
client = self._aclient_session
async with client.stream(
url=f"{api_base}",
json=data,
headers=headers,
method="POST"
method="POST",
timeout=litellm.request_timeout
) as response:
if response.status_code != 200:
raise AzureOpenAIError(status_code=response.status_code, message=response.text)
@ -253,7 +265,7 @@ class AzureChatCompletion(BaseLLM):
)
## COMPLETION CALL
response = self._client_session.post(
api_base, headers=headers, json=data
api_base, headers=headers, json=data, timeout=litellm.request_timeout
)
## LOGGING
logging_obj.post_call(