You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am attempting to write a simple custom handler based on the documentation provided here .
I am able to intercept requests, however if I am unable to intercept the response and log it to a file. Here is my implementation based on the documentation and this bug report. What am I doing wrong here?
I am using the Ollama provider and send a request to the Ollama server as below
model_list:
- model_name: llama3.2litellm_params:
model: litellm_proxy/llama3.2api_base: http://localhost:11434#api_key: "os.environ/AZURE_API_KEY"#api_version: "2024-07-01-preview" # [OPTIONAL] litellm uses the latest azure api_version by defaultlitellm_settings:
turn_off_message_logging: falsecallbacks: /app/plugins/custom_handler.proxy_handler_instancejson_logs: falsegeneral_settings:
master_key: sk-1234store_prompts_in_spend_logs: truepass_through_endpoints:
- path: "/api/chat"# route you want to add to LiteLLM Proxy Servertarget: "http://host.docker.internal:11434/api/chat"# URL this route should forward requests toheaders: # headers to forward to this URL#Authorization: "bearer os.environ/COHERE_API_KEY" # (Optional) Auth Header to forward to your Endpointcontent-type: application/json # (Optional) Extra Headers to pass to this endpoint accept: application/jsonforward_headers: True
fromlitellm.integrations.custom_loggerimportCustomLogger#from litellm.types.utils import ModelResponseStreamfromtypingimportAny, AsyncGenerator, Optional, Literalfrompprintimportpprint# This file includes the custom callbacks for LiteLLM Proxy# Once defined, these can be passed in proxy_config.yamlclassMyCustomHandler(CustomLogger):
def__init__(self):
pass#### CALL HOOKS - proxy only ####asyncdefasync_pre_call_hook(self, user_api_key_dict: Any, cache: Any, data: dict, call_type: Literal[
"completion",
"text_completion",
"embeddings",
"image_generation",
"moderation",
"audio_transcription",
]):
# Pretty print the data dictionaryprint("Pre-call hook...")
pprint(data)
logger.info({"event": "pre_call_hook", "data": data})
# Example: modify the model before making the LLM API call#data["model"] = "my-new-model"returndataasyncdefasync_post_call_failure_hook(
self,
request_data: dict,
original_exception: Exception,
user_api_key_dict: Any,
traceback_str: Optional[str] =None,
):
# Custom logic for handling failurespassasyncdefasync_post_call_success_hook(
self,
data: dict,
user_api_key_dict: Any,
response,
):
# Custom logic for handling successful calls# Log entry to identify the methodprint("Post-call success hook...")
logger.info({"event": "post_call_success_hook", "data": data, "response": str(response)})
# Optionally, pretty print the response for debuggingpprint(response)
asyncdefasync_moderation_hook(
self,
data: dict,
user_api_key_dict: Any,
call_type: Literal["completion", "embeddings", "image_generation", "moderation", "audio_transcription"],
):
# Custom moderation logicpassasyncdefasync_post_call_streaming_hook(
self,
user_api_key_dict: Any,
response: str,
):
# Custom logic for streaming responsesprint("Post-call streaming hook...")
pprint(response)
logger.info({"event": "post_call_streaming_hook", "response": response})
asyncdefasync_post_call_streaming_iterator_hook(
self,
user_api_key_dict: Any,
response: Any,
request_data: dict,
) ->AsyncGenerator[Any, None]:
""" Passes the entire stream to the guardrail This is useful for plugins that need to see the entire stream. """asyncforiteminresponse:
print(item)
logger.info({"event": "post_call_streaming_iterator_hook", "item": item})
yielditem# Instance to be referenced in proxy configproxy_handler_instance=MyCustomHandler()
importloggingimportjsonclassJSONLogFormatter(logging.Formatter):
defformat(self, record):
log_message= {
"timestamp": self.formatTime(record, "%Y-%m-%dT%H:%M:%S"),
"level": record.levelname,
"message": record.getMessage(),
}
returnjson.dumps(log_message)
logger=logging.getLogger("litellm_logger")
logger.setLevel(logging.INFO)
logger.propagate=Falseconsole_handler=logging.StreamHandler()
console_handler.setFormatter(JSONLogFormatter())
logger.addHandler(console_handler)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I am attempting to write a simple custom handler based on the documentation provided here .
I am able to intercept requests, however if I am unable to intercept the response and log it to a file. Here is my implementation based on the documentation and this bug report. What am I doing wrong here?
I am using the Ollama provider and send a request to the Ollama server as below
This is my config file:
Beta Was this translation helpful? Give feedback.
All reactions