mirror of
https://github.com/BerriAI/litellm.git
synced 2025-04-26 03:04:13 +00:00
(chore) undo changes on ishaan branch
This commit is contained in:
parent
0383e32628
commit
1a4958eb14
1 changed files with 70 additions and 40 deletions
|
@ -1,41 +1,71 @@
|
||||||
# import litellm
|
import litellm
|
||||||
# from litellm import ModelResponse
|
from litellm import ModelResponse
|
||||||
# from proxy_server import update_verification_token_cost
|
from proxy_server import llm_model_list
|
||||||
# from typing import Optional
|
from typing import Optional
|
||||||
# from fastapi import HTTPException, status
|
|
||||||
# import asyncio
|
|
||||||
|
|
||||||
# def track_cost_callback(
|
def track_cost_callback(
|
||||||
# kwargs, # kwargs to completion
|
kwargs, # kwargs to completion
|
||||||
# completion_response: ModelResponse, # response from completion
|
completion_response: ModelResponse, # response from completion
|
||||||
# start_time = None,
|
start_time = None,
|
||||||
# end_time = None, # start/end time for completion
|
end_time = None, # start/end time for completion
|
||||||
# ):
|
):
|
||||||
# try:
|
try:
|
||||||
# # init logging config
|
# init logging config
|
||||||
# api_key = kwargs["litellm_params"]["metadata"]["api_key"]
|
print("in custom callback tracking cost", llm_model_list)
|
||||||
# # check if it has collected an entire stream response
|
if "azure" in kwargs["model"]:
|
||||||
# if "complete_streaming_response" in kwargs:
|
# for azure cost tracking, we check the provided model list in the config.yaml
|
||||||
# # for tracking streaming cost we pass the "messages" and the output_text to litellm.completion_cost
|
# we need to map azure/chatgpt-deployment to -> azure/gpt-3.5-turbo
|
||||||
# completion_response=kwargs["complete_streaming_response"]
|
pass
|
||||||
# input_text = kwargs["messages"]
|
# check if it has collected an entire stream response
|
||||||
# output_text = completion_response["choices"][0]["message"]["content"]
|
if "complete_streaming_response" in kwargs:
|
||||||
# response_cost = litellm.completion_cost(
|
# for tracking streaming cost we pass the "messages" and the output_text to litellm.completion_cost
|
||||||
# model = kwargs["model"],
|
completion_response=kwargs["complete_streaming_response"]
|
||||||
# messages = input_text,
|
input_text = kwargs["messages"]
|
||||||
# completion=output_text
|
output_text = completion_response["choices"][0]["message"]["content"]
|
||||||
# )
|
response_cost = litellm.completion_cost(
|
||||||
# print(f"LiteLLM Proxy: streaming response_cost: {response_cost} for api_key: {api_key}")
|
model = kwargs["model"],
|
||||||
# # for non streaming responses
|
messages = input_text,
|
||||||
# else:
|
completion=output_text
|
||||||
# # we pass the completion_response obj
|
)
|
||||||
# if kwargs["stream"] != True:
|
print("streaming response_cost", response_cost)
|
||||||
# response_cost = litellm.completion_cost(completion_response=completion_response)
|
# for non streaming responses
|
||||||
# print(f"\n LiteLLM Proxy: regular response_cost: {response_cost} for api_key: {api_key}")
|
else:
|
||||||
|
# we pass the completion_response obj
|
||||||
# ########### write costs to DB api_key / cost map
|
if kwargs["stream"] != True:
|
||||||
# asyncio.run(
|
input_text = kwargs.get("messages", "")
|
||||||
# update_verification_token_cost(token=api_key, additional_cost=response_cost)
|
if isinstance(input_text, list):
|
||||||
# )
|
input_text = "".join(m["content"] for m in input_text)
|
||||||
# except:
|
response_cost = litellm.completion_cost(completion_response=completion_response, completion=input_text)
|
||||||
# pass
|
print("regular response_cost", response_cost)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def update_prisma_database(token, response_cost):
|
||||||
|
try:
|
||||||
|
# Import your Prisma client
|
||||||
|
from your_prisma_module import prisma
|
||||||
|
|
||||||
|
# Fetch the existing cost for the given token
|
||||||
|
existing_cost = prisma.LiteLLM_VerificationToken.find_unique(
|
||||||
|
where={
|
||||||
|
"token": token
|
||||||
|
}
|
||||||
|
).cost
|
||||||
|
|
||||||
|
# Calculate the new cost by adding the existing cost and response_cost
|
||||||
|
new_cost = existing_cost + response_cost
|
||||||
|
|
||||||
|
# Update the cost column for the given token
|
||||||
|
prisma_liteLLM_VerificationToken = prisma.LiteLLM_VerificationToken.update(
|
||||||
|
where={
|
||||||
|
"token": token
|
||||||
|
},
|
||||||
|
data={
|
||||||
|
"cost": new_cost
|
||||||
|
}
|
||||||
|
)
|
||||||
|
print(f"Prisma database updated for token {token}. New cost: {new_cost}")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error updating Prisma database: {e}")
|
||||||
|
pass
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue