forked from phoenix/litellm-mirror
(feat) custom API callbacks
This commit is contained in:
parent
4e8a94b916
commit
b3f5402017
2 changed files with 38 additions and 4 deletions
|
@ -29,9 +29,11 @@ from litellm._logging import print_verbose, verbose_logger
|
||||||
|
|
||||||
class GenericAPILogger:
|
class GenericAPILogger:
|
||||||
# Class variables or attributes
|
# Class variables or attributes
|
||||||
def __init__(self, endpoint=None):
|
def __init__(self, endpoint=None, headers=None):
|
||||||
try:
|
try:
|
||||||
verbose_logger.debug(f"in init GenericAPILogger, endpoint {endpoint}")
|
verbose_logger.debug(f"in init GenericAPILogger, endpoint {endpoint}")
|
||||||
|
self.endpoint = endpoint
|
||||||
|
self.headers = headers
|
||||||
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -44,7 +46,7 @@ class GenericAPILogger:
|
||||||
def log_event(self, kwargs, response_obj, start_time, end_time, print_verbose):
|
def log_event(self, kwargs, response_obj, start_time, end_time, print_verbose):
|
||||||
try:
|
try:
|
||||||
verbose_logger.debug(
|
verbose_logger.debug(
|
||||||
f"s3 Logging - Enters logging function for model {kwargs}"
|
f"GenericAPILogger Logging - Enters logging function for model {kwargs}"
|
||||||
)
|
)
|
||||||
|
|
||||||
# construct payload to send custom logger
|
# construct payload to send custom logger
|
||||||
|
@ -54,6 +56,7 @@ class GenericAPILogger:
|
||||||
litellm_params.get("metadata", {}) or {}
|
litellm_params.get("metadata", {}) or {}
|
||||||
) # if litellm_params['metadata'] == None
|
) # if litellm_params['metadata'] == None
|
||||||
messages = kwargs.get("messages")
|
messages = kwargs.get("messages")
|
||||||
|
cost = kwargs.get("response_cost", 0.0)
|
||||||
optional_params = kwargs.get("optional_params", {})
|
optional_params = kwargs.get("optional_params", {})
|
||||||
call_type = kwargs.get("call_type", "litellm.completion")
|
call_type = kwargs.get("call_type", "litellm.completion")
|
||||||
cache_hit = kwargs.get("cache_hit", False)
|
cache_hit = kwargs.get("cache_hit", False)
|
||||||
|
@ -74,6 +77,7 @@ class GenericAPILogger:
|
||||||
"response": response_obj,
|
"response": response_obj,
|
||||||
"usage": usage,
|
"usage": usage,
|
||||||
"metadata": metadata,
|
"metadata": metadata,
|
||||||
|
"cost": cost,
|
||||||
}
|
}
|
||||||
|
|
||||||
# Ensure everything in the payload is converted to str
|
# Ensure everything in the payload is converted to str
|
||||||
|
@ -87,11 +91,14 @@ class GenericAPILogger:
|
||||||
import json
|
import json
|
||||||
|
|
||||||
payload = json.dumps(payload)
|
payload = json.dumps(payload)
|
||||||
|
data = {
|
||||||
|
"data": payload,
|
||||||
|
}
|
||||||
|
|
||||||
print_verbose(f"\nGeneric Logger - Logging payload = {payload}")
|
print_verbose(f"\nGeneric Logger - Logging payload = {data}")
|
||||||
|
|
||||||
# make request to endpoint with payload
|
# make request to endpoint with payload
|
||||||
response = requests.post(self.endpoint, data=payload, headers=self.headers)
|
response = requests.post(self.endpoint, data=data, headers=self.headers)
|
||||||
|
|
||||||
response_status = response.status_code
|
response_status = response.status_code
|
||||||
response_text = response.text
|
response_text = response.text
|
||||||
|
|
27
enterprise/callbacks/example_logging_api.py
Normal file
27
enterprise/callbacks/example_logging_api.py
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
# this is an example endpoint to receive data from litellm
|
||||||
|
from fastapi import FastAPI, HTTPException, Request
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/log-event")
|
||||||
|
async def log_event(request: Request):
|
||||||
|
try:
|
||||||
|
# Assuming the incoming request has JSON data
|
||||||
|
data = await request.json()
|
||||||
|
print("Received request data:")
|
||||||
|
print(data)
|
||||||
|
|
||||||
|
# Your additional logic can go here
|
||||||
|
# For now, just printing the received data
|
||||||
|
|
||||||
|
return {"message": "Request received successfully"}
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error processing request: {str(e)}")
|
||||||
|
raise HTTPException(status_code=500, detail="Internal Server Error")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import uvicorn
|
||||||
|
|
||||||
|
uvicorn.run(app, host="127.0.0.1", port=8000)
|
Loading…
Add table
Add a link
Reference in a new issue