Skip to content

Commit

Permalink
back to standard error handling for litellm errors
Browse files Browse the repository at this point in the history
  • Loading branch information
phact committed May 7, 2024
1 parent e22806b commit fac7c24
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 36 deletions.
23 changes: 19 additions & 4 deletions impl/routes/stateless.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
"""The stateless endpoints that do not depend on information from DB"""
import logging
import time
import uuid
from typing import Any, Dict
from fastapi.encoders import jsonable_encoder
import json


from fastapi import APIRouter, Depends, Request
from fastapi import APIRouter, Depends, Request, HTTPException
from litellm import APIError
from starlette.responses import StreamingResponse, JSONResponse

from openapi_server.models.chat_completion_stream_response_delta import ChatCompletionStreamResponseDelta
Expand All @@ -28,8 +30,12 @@
router = APIRouter()


logger = logging.getLogger(__name__)


async def _completion_from_request(
chat_request: CreateChatCompletionRequest,
using_openai: bool,
**litellm_kwargs: Any,
) -> CreateChatCompletionResponse | StreamingResponse:
# NOTE: litellm_kwargs should contain auth
Expand All @@ -56,6 +62,11 @@ async def _completion_from_request(

messages.append(message_dict)

tools = []
if chat_request.tools is not None:
for tool in chat_request.tools:
tools.append(tool.to_dict())

functions = []
if chat_request.functions is not None:
for function in chat_request.functions:
Expand Down Expand Up @@ -98,6 +109,10 @@ async def _completion_from_request(
if functions:
kwargs["functions"] = functions

if tools:
kwargs["tools"] = tools


# workaround for https://github.com/BerriAI/litellm/pull/3439
#if "function" not in kwargs and "tools" in kwargs:
# kwargs["functions"] = kwargs["tools"]
Expand All @@ -107,10 +122,9 @@ async def _completion_from_request(
kwargs["logit_bias"] = chat_request.logit_bias

if chat_request.user is not None:
kwargs["user"] = chat_request.user
kwargs["user"] = chat_request.usefunctionsr

response = await get_async_chat_completion_response(**kwargs)
# TODO - throw error if response fails

choices = []
if chat_request.stream is not None and chat_request.stream:
Expand Down Expand Up @@ -221,8 +235,9 @@ async def create_moderation(
async def create_chat_completion(
create_chat_completion_request: CreateChatCompletionRequest,
litellm_kwargs: Dict[str, Any] = Depends(get_litellm_kwargs),
using_openai: bool = Depends(check_if_using_openai),
) -> Any:
return await _completion_from_request(create_chat_completion_request, **litellm_kwargs)
return await _completion_from_request(create_chat_completion_request, using_openai, **litellm_kwargs)


@router.post(
Expand Down
64 changes: 32 additions & 32 deletions impl/services/inference_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,38 +88,38 @@ async def get_async_chat_completion_response(
if model is None and deployment_id is None:
raise ValueError("Must provide either a model or a deployment id")

#try:
if model is None:
model = deployment_id

type_hints = get_type_hints(acompletion)

for key, value in litellm_kwargs.items():
if value is not None and key in type_hints and isinstance(value, str):
type_hint = type_hints[key]
# handle optional
if hasattr(type_hint, "__origin__") and type_hint.__origin__ == Union:
litellm_kwargs[key] = type_hint.__args__[0](value)
else:
litellm_kwargs[key] = type_hints[key](value)

litellm.set_verbose=True
completion = await acompletion(
model=model,
messages=messages,
deployment_id=deployment_id,
**litellm_kwargs
)
return completion
#except Exception as e:
# if "LLM Provider NOT provided" in e.args[0]:
# logger.error(f"Error: error {model} is not currently supported")
# raise ValueError(f"Model {model} is not currently supported")
# logger.error(f"Error: {e}")
# raise ValueError(f"Error: {e}")
#except asyncio.CancelledError:
# logger.error("litellm call cancelled")
# raise RuntimeError("litellm call cancelled")
try:
if model is None:
model = deployment_id

type_hints = get_type_hints(acompletion)

for key, value in litellm_kwargs.items():
if value is not None and key in type_hints and isinstance(value, str):
type_hint = type_hints[key]
# handle optional
if hasattr(type_hint, "__origin__") and type_hint.__origin__ == Union:
litellm_kwargs[key] = type_hint.__args__[0](value)
else:
litellm_kwargs[key] = type_hints[key](value)

#litellm.set_verbose = True
completion = await acompletion(
model=model,
messages=messages,
deployment_id=deployment_id,
**litellm_kwargs
)
return completion
except Exception as e:
if "LLM Provider NOT provided" in e.args[0]:
logger.error(f"Error: error {model} is not currently supported")
raise ValueError(f"Model {model} is not currently supported")
logger.error(f"Error: {e}")
raise ValueError(f"Error: {e}")
except asyncio.CancelledError:
logger.error("litellm call cancelled")
raise RuntimeError("litellm call cancelled")


async def get_chat_completion(
Expand Down

0 comments on commit fac7c24

Please sign in to comment.