Skip to content

Commit

Permalink
add logging and caching for assistants
Browse files Browse the repository at this point in the history
  • Loading branch information
mfittko committed Sep 11, 2024
1 parent d2c5e5b commit c587814
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
30 changes: 30 additions & 0 deletions litellm/proxy/proxy_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4202,13 +4202,37 @@ async def get_assistants(
proxy_config=proxy_config,
)

# Check cache
cache_key = f"assistants:{user_api_key_dict.user_id}"
cached_response = await proxy_logging_obj.get_cache(cache_key)
if cached_response:
return cached_response

# Log the request
await proxy_logging_obj.pre_call_hook(
user_api_key_dict=user_api_key_dict,
data=data,
call_type="assistants",
)

# for now use custom_llm_provider=="openai" -> this will change as LiteLLM adds more providers for acreate_batch
if llm_router is None:
raise HTTPException(
status_code=500, detail={"error": CommonProxyErrors.no_llm_router.value}
)
response = await llm_router.aget_assistants(**data)

# Cache the response
await proxy_logging_obj.set_cache(cache_key, response, ttl=300) # Cache for 5 minutes

### LOGGING ###
await proxy_logging_obj.success_call_hook(
user_api_key_dict=user_api_key_dict,
data=data,
response=response,
call_type="assistants",
)

### ALERTING ###
asyncio.create_task(
proxy_logging_obj.update_request_status(
Expand Down Expand Up @@ -4236,6 +4260,12 @@ async def get_assistants(

return response
except Exception as e:
await proxy_logging_obj.failure_call_hook(
user_api_key_dict=user_api_key_dict,
data=data,
error=e,
call_type="assistants",
)
await proxy_logging_obj.post_call_failure_hook(
user_api_key_dict=user_api_key_dict, original_exception=e, request_data=data
)
Expand Down
1 change: 1 addition & 0 deletions litellm/proxy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ async def pre_call_hook(
"audio_transcription",
"pass_through_endpoint",
"rerank",
"assistants",
],
) -> dict:
"""
Expand Down

0 comments on commit c587814

Please sign in to comment.