mirror of
https://github.com/BerriAI/litellm.git
synced 2025-04-27 11:43:54 +00:00
(feat) /spend/logs
This commit is contained in:
parent
97fd419cb5
commit
9ae3d8c243
2 changed files with 74 additions and 1 deletions
|
@ -2338,6 +2338,61 @@ async def spend_key_fn():
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get(
|
||||||
|
"/spend/logs",
|
||||||
|
tags=["Budget & Spend Tracking"],
|
||||||
|
dependencies=[Depends(user_api_key_auth)],
|
||||||
|
)
|
||||||
|
async def view_spend_logs(
|
||||||
|
request_id: Optional[str] = fastapi.Query(
|
||||||
|
default=None,
|
||||||
|
description="request_id to get spend logs for specific request_id. If none passed then pass spend logs for all requests",
|
||||||
|
),
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
View all spend logs, if request_id is provided, only logs for that request_id will be returned
|
||||||
|
|
||||||
|
Example Request for all logs
|
||||||
|
```
|
||||||
|
curl -X GET "http://0.0.0.0:8000/spend/logs" \
|
||||||
|
-H "Authorization: Bearer sk-1234"
|
||||||
|
```
|
||||||
|
|
||||||
|
Example Request for specific request_id
|
||||||
|
```
|
||||||
|
curl -X GET "http://0.0.0.0:8000/spend/logs?request_id=chatcmpl-6dcb2540-d3d7-4e49-bb27-291f863f112e" \
|
||||||
|
-H "Authorization: Bearer sk-1234"
|
||||||
|
```
|
||||||
|
"""
|
||||||
|
global prisma_client
|
||||||
|
try:
|
||||||
|
if prisma_client is None:
|
||||||
|
raise Exception(
|
||||||
|
f"Database not connected. Connect a database to your proxy - https://docs.litellm.ai/docs/simple_proxy#managing-auth---virtual-keys"
|
||||||
|
)
|
||||||
|
spend_logs = []
|
||||||
|
if request_id is not None:
|
||||||
|
spend_log = await prisma_client.get_data(
|
||||||
|
table_name="spend",
|
||||||
|
query_type="find_unique",
|
||||||
|
request_id=request_id,
|
||||||
|
)
|
||||||
|
return [spend_log]
|
||||||
|
else:
|
||||||
|
spend_logs = await prisma_client.get_data(
|
||||||
|
table_name="spend", query_type="find_all"
|
||||||
|
)
|
||||||
|
return spend_logs
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_400_BAD_REQUEST,
|
||||||
|
detail={"error": str(e)},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
#### USER MANAGEMENT ####
|
#### USER MANAGEMENT ####
|
||||||
@router.post(
|
@router.post(
|
||||||
"/user/new",
|
"/user/new",
|
||||||
|
|
|
@ -361,7 +361,8 @@ class PrismaClient:
|
||||||
self,
|
self,
|
||||||
token: Optional[str] = None,
|
token: Optional[str] = None,
|
||||||
user_id: Optional[str] = None,
|
user_id: Optional[str] = None,
|
||||||
table_name: Optional[Literal["user", "key", "config"]] = None,
|
request_id: Optional[str] = None,
|
||||||
|
table_name: Optional[Literal["user", "key", "config", "spend"]] = None,
|
||||||
query_type: Literal["find_unique", "find_all"] = "find_unique",
|
query_type: Literal["find_unique", "find_all"] = "find_unique",
|
||||||
):
|
):
|
||||||
try:
|
try:
|
||||||
|
@ -411,6 +412,23 @@ class PrismaClient:
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
return response
|
return response
|
||||||
|
elif table_name == "spend":
|
||||||
|
verbose_proxy_logger.debug(
|
||||||
|
f"PrismaClient: get_data: table_name == 'spend'"
|
||||||
|
)
|
||||||
|
if request_id is not None:
|
||||||
|
response = await self.db.litellm_spendlogs.find_unique( # type: ignore
|
||||||
|
where={
|
||||||
|
"request_id": request_id,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return response
|
||||||
|
else:
|
||||||
|
response = await self.db.litellm_spendlogs.find_many( # type: ignore
|
||||||
|
order={"startTime": "desc"},
|
||||||
|
)
|
||||||
|
return response
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print_verbose(f"LiteLLM Prisma Client Exception: {e}")
|
print_verbose(f"LiteLLM Prisma Client Exception: {e}")
|
||||||
import traceback
|
import traceback
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue