From 7c317b78ebbfd4d6006c03b6f10408c2a54a2fb5 Mon Sep 17 00:00:00 2001 From: ishaan-jaff Date: Tue, 14 Nov 2023 17:27:13 -0800 Subject: [PATCH] (feat) add ability to view POST requests from litellm.completion() --- litellm/llms/azure.py | 1 + litellm/llms/cohere.py | 2 +- litellm/llms/openai.py | 4 ++-- litellm/llms/together_ai.py | 8 ++++---- litellm/utils.py | 17 +++++++++++++++-- 5 files changed, 23 insertions(+), 9 deletions(-) diff --git a/litellm/llms/azure.py b/litellm/llms/azure.py index b2490f8f950..1f16977bfd7 100644 --- a/litellm/llms/azure.py +++ b/litellm/llms/azure.py @@ -129,6 +129,7 @@ class AzureChatCompletion(BaseLLM): "headers": headers, "api_version": api_version, "api_base": api_base, + "complete_input_dict": data, }, ) if acompletion is True: diff --git a/litellm/llms/cohere.py b/litellm/llms/cohere.py index a122ee62223..7bdf4fc5c0a 100644 --- a/litellm/llms/cohere.py +++ b/litellm/llms/cohere.py @@ -134,7 +134,7 @@ def completion( logging_obj.pre_call( input=prompt, api_key=api_key, - additional_args={"complete_input_dict": data}, + additional_args={"complete_input_dict": data, "headers": headers, "api_base": completion_url}, ) ## COMPLETION CALL response = requests.post( diff --git a/litellm/llms/openai.py b/litellm/llms/openai.py index 2cfe0efebce..3f2fc38b99d 100644 --- a/litellm/llms/openai.py +++ b/litellm/llms/openai.py @@ -202,7 +202,7 @@ class OpenAIChatCompletion(BaseLLM): logging_obj.pre_call( input=messages, api_key=api_key, - additional_args={"headers": headers, "api_base": api_base, "acompletion": acompletion, "data": data}, + additional_args={"headers": headers, "api_base": api_base, "acompletion": acompletion, "complete_input_dict": data}, ) try: @@ -450,7 +450,7 @@ class OpenAITextCompletion(BaseLLM): logging_obj.pre_call( input=messages, api_key=api_key, - additional_args={"headers": headers, "api_base": api_base, "data": data}, + additional_args={"headers": headers, "api_base": api_base, "complete_input_dict": data}, ) if acompletion == True: if optional_params.get("stream", False): diff --git a/litellm/llms/together_ai.py b/litellm/llms/together_ai.py index f435858ddfd..ea993d1320d 100644 --- a/litellm/llms/together_ai.py +++ b/litellm/llms/together_ai.py @@ -126,10 +126,10 @@ def completion( ## LOGGING logging_obj.pre_call( - input=prompt, - api_key=api_key, - additional_args={"complete_input_dict": data}, - ) + input=prompt, + api_key=api_key, + additional_args={"complete_input_dict": data, "headers": headers, "api_base": api_base}, + ) ## COMPLETION CALL if ( "stream_tokens" in optional_params diff --git a/litellm/utils.py b/litellm/utils.py index 47741317f7c..62604732c21 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -492,7 +492,6 @@ class Logging: def pre_call(self, input, api_key, model=None, additional_args={}): # Log the exact input to the LLM API - print_verbose(f"Logging Details Pre-API Call for call id {self.litellm_call_id}") litellm.error_logs['PRE_CALL'] = locals() try: # print_verbose(f"logging pre call for model: {self.model} with call type: {self.call_type}") @@ -506,7 +505,21 @@ class Logging: self.model_call_details["model"] = model # User Logging -> if you pass in a custom logging function - print_verbose(f"MODEL CALL INPUT: {self.model_call_details}\n\n") + headers = additional_args.get("headers", {}) + data = additional_args.get("complete_input_dict", {}) + api_base = additional_args.get("api_base", "") + masked_headers = {k: v[:-5] + '*' * 5 if len(v) > 5 else v for k, v in headers.items()} + formatted_headers = " ".join([f"-H '{k}: {v}'" for k, v in masked_headers.items()]) + curl_command = ( + "\n\nPOST Request Sent from LiteLLM:\ncurl -X POST \\" + f" {api_base} \\\n" + f" {formatted_headers} \\\n" + f" -d '{json.dumps(data)}'\n" + ) + if api_base == "": + curl_command = self.model_call_details + + print_verbose(f"\033[92m{curl_command}\033[0m\n") if self.logger_fn and callable(self.logger_fn): try: self.logger_fn(