Merge pull request #1604 from BerriAI/litellm_view_cache_key_in_spend_logs

[Feat] Add cache_key in SpendLogs Table
This commit is contained in:
Ishaan Jaff
2024-01-24 19:13:13 -08:00
committed by GitHub
6 changed files with 25 additions and 5 deletions
+1 -2
View File
@@ -343,8 +343,7 @@ class LiteLLM_SpendLogs(LiteLLMBase):
endTime: Union[str, datetime, None]
user: Optional[str] = ""
modelParameters: Optional[Json] = {}
messages: Optional[Json] = []
response: Optional[Json] = {}
usage: Optional[Json] = {}
metadata: Optional[Json] = {}
cache_hit: Optional[str] = "False"
cache_key: Optional[str] = None
+6 -2
View File
@@ -593,6 +593,12 @@ async def track_cost_callback(
"user_api_key_user_id", None
)
if kwargs.get("cache_hit", False) == True:
response_cost = 0.0
verbose_proxy_logger.info(
f"Cache Hit: response_cost {response_cost}, for user_id {user_id}"
)
verbose_proxy_logger.info(
f"response_cost {response_cost}, for user_id {user_id}"
)
@@ -1382,8 +1388,6 @@ async def initialize(
verbose_proxy_logger.setLevel(
level=logging.DEBUG
) # set proxy logs to debug
litellm.set_verbose = True
dynamic_config = {"general": {}, user_model: {}}
if config:
(
+1
View File
@@ -58,4 +58,5 @@ model LiteLLM_SpendLogs {
usage Json @default("{}")
metadata Json @default("{}")
cache_hit String @default("")
cache_key String @default("")
}
+9 -1
View File
@@ -995,11 +995,18 @@ def get_logging_payload(kwargs, response_obj, start_time, end_time):
if api_key is not None and isinstance(api_key, str) and api_key.startswith("sk-"):
# hash the api_key
api_key = hash_token(api_key)
if "headers" in metadata and "authorization" in metadata["headers"]:
metadata["headers"].pop(
"authorization"
) # do not store the original `sk-..` api key in the db
if litellm.cache is not None:
cache_key = litellm.cache.get_cache_key(**kwargs)
else:
cache_key = "Cache OFF"
if cache_hit == True:
import time
id = f"{id}_cache_hit{time.time()}" # SpendLogs does not allow duplicate request_id
payload = {
"request_id": id,
@@ -1013,6 +1020,7 @@ def get_logging_payload(kwargs, response_obj, start_time, end_time):
"modelParameters": optional_params,
"usage": usage,
"metadata": metadata,
"cache_key": cache_key,
}
json_fields = [
@@ -716,6 +716,9 @@ def test_call_with_key_over_budget(prisma_client):
# update spend using track_cost callback, make 2nd request, it should fail
from litellm.proxy.proxy_server import track_cost_callback
from litellm import ModelResponse, Choices, Message, Usage
from litellm.caching import Cache
litellm.cache = Cache()
import time
request_id = f"chatcmpl-e41836bb-bb8b-4df2-8e70-8f3e160155ac{time.time()}"
@@ -763,6 +766,10 @@ def test_call_with_key_over_budget(prisma_client):
assert spend_log.request_id == request_id
assert spend_log.spend == float("2e-05")
assert spend_log.model == "chatgpt-v-2"
assert (
spend_log.cache_key
== "a61ae14fe4a8b8014a61e6ae01a100c8bc6770ac37c293242afed954bc69207d"
)
# use generated key to auth in
result = await user_api_key_auth(request=request, api_key=bearer_token)
+1
View File
@@ -61,4 +61,5 @@ model LiteLLM_SpendLogs {
usage Json @default("{}")
metadata Json @default("{}")
cache_hit String @default("")
cache_key String @default("")
}