From a0cb56efd5e092f0b6219b77b8ee2e680f54fc0f Mon Sep 17 00:00:00 2001 From: adriensas Date: Wed, 23 Aug 2023 10:59:36 +0200 Subject: [PATCH 1/2] fix --- litellm/llms/anthropic.py | 36 +++++++++++------------------------- 1 file changed, 11 insertions(+), 25 deletions(-) diff --git a/litellm/llms/anthropic.py b/litellm/llms/anthropic.py index 983d4ca0e5..28623fdd6d 100644 --- a/litellm/llms/anthropic.py +++ b/litellm/llms/anthropic.py @@ -15,15 +15,11 @@ class AnthropicError(Exception): def __init__(self, status_code, message): self.status_code = status_code self.message = message - super().__init__( - self.message - ) # Call the base class constructor with the parameters it needs + super().__init__(self.message) # Call the base class constructor with the parameters it needs class AnthropicLLM: - def __init__( - self, encoding, default_max_tokens_to_sample, logging_obj, api_key=None - ): + def __init__(self, encoding, default_max_tokens_to_sample, logging_obj, api_key=None): self.encoding = encoding self.default_max_tokens_to_sample = default_max_tokens_to_sample self.completion_url = "https://api.anthropic.com/v1/complete" @@ -31,9 +27,7 @@ class AnthropicLLM: self.logging_obj = logging_obj self.validate_environment(api_key=api_key) - def validate_environment( - self, api_key - ): # set up the environment required to run the model + def validate_environment(self, api_key): # set up the environment required to run the model # set the api key if self.api_key == None: raise ValueError( @@ -62,19 +56,13 @@ class AnthropicLLM: for message in messages: if "role" in message: if message["role"] == "user": - prompt += ( - f"{AnthropicConstants.HUMAN_PROMPT.value}{message['content']}" - ) + prompt += f"{AnthropicConstants.HUMAN_PROMPT.value}{message['content']}" else: - prompt += ( - f"{AnthropicConstants.AI_PROMPT.value}{message['content']}" - ) + prompt += f"{AnthropicConstants.AI_PROMPT.value}{message['content']}" else: prompt += f"{AnthropicConstants.HUMAN_PROMPT.value}{message['content']}" prompt += f"{AnthropicConstants.AI_PROMPT.value}" - if "max_tokens" in optional_params and optional_params["max_tokens"] != float( - "inf" - ): + if "max_tokens" in optional_params and optional_params["max_tokens"] != float("inf"): max_tokens = optional_params["max_tokens"] else: max_tokens = self.default_max_tokens_to_sample @@ -93,9 +81,11 @@ class AnthropicLLM: ) ## COMPLETION CALL response = requests.post( - self.completion_url, headers=self.headers, data=json.dumps(data) + self.completion_url, headers=self.headers, data=json.dumps(data), stream=optional_params["stream"] ) + print(optional_params) if "stream" in optional_params and optional_params["stream"] == True: + print("IS STREAMING") return response.iter_lines() else: ## LOGGING @@ -114,14 +104,10 @@ class AnthropicLLM: status_code=response.status_code, ) else: - model_response["choices"][0]["message"][ - "content" - ] = completion_response["completion"] + model_response["choices"][0]["message"]["content"] = completion_response["completion"] ## CALCULATING USAGE - prompt_tokens = len( - self.encoding.encode(prompt) - ) ##[TODO] use the anthropic tokenizer here + prompt_tokens = len(self.encoding.encode(prompt)) ##[TODO] use the anthropic tokenizer here completion_tokens = len( self.encoding.encode(model_response["choices"][0]["message"]["content"]) ) ##[TODO] use the anthropic tokenizer here From 1f3aeeb6d4f7b393d8d091a5cf6a06efe462420f Mon Sep 17 00:00:00 2001 From: adriensas Date: Wed, 23 Aug 2023 11:07:45 +0200 Subject: [PATCH 2/2] fix linting --- litellm/llms/anthropic.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/litellm/llms/anthropic.py b/litellm/llms/anthropic.py index 28623fdd6d..27b78b41cd 100644 --- a/litellm/llms/anthropic.py +++ b/litellm/llms/anthropic.py @@ -1,4 +1,4 @@ -import os, json +import json from enum import Enum import requests import time @@ -29,9 +29,11 @@ class AnthropicLLM: def validate_environment(self, api_key): # set up the environment required to run the model # set the api key - if self.api_key == None: + if self.api_key is None: raise ValueError( - "Missing Anthropic API Key - A call is being made to anthropic but no key is set either in the environment variables or via params" + "Missing Anthropic API Key -" + + " A call is being made to anthropic but no key is set either" + + " in the environment variables or via params" ) self.api_key = api_key self.headers = { @@ -73,22 +75,22 @@ class AnthropicLLM: **optional_params, } - ## LOGGING + # LOGGING self.logging_obj.pre_call( input=prompt, api_key=self.api_key, additional_args={"complete_input_dict": data}, ) - ## COMPLETION CALL + # COMPLETION CALL response = requests.post( self.completion_url, headers=self.headers, data=json.dumps(data), stream=optional_params["stream"] ) print(optional_params) - if "stream" in optional_params and optional_params["stream"] == True: + if "stream" in optional_params and optional_params["stream"] is True: print("IS STREAMING") return response.iter_lines() else: - ## LOGGING + # LOGGING self.logging_obj.post_call( input=prompt, api_key=self.api_key, @@ -96,7 +98,7 @@ class AnthropicLLM: additional_args={"complete_input_dict": data}, ) print_verbose(f"raw model_response: {response.text}") - ## RESPONSE OBJECT + # RESPONSE OBJECT completion_response = response.json() if "error" in completion_response: raise AnthropicError( @@ -106,11 +108,11 @@ class AnthropicLLM: else: model_response["choices"][0]["message"]["content"] = completion_response["completion"] - ## CALCULATING USAGE - prompt_tokens = len(self.encoding.encode(prompt)) ##[TODO] use the anthropic tokenizer here + # CALCULATING USAGE + prompt_tokens = len(self.encoding.encode(prompt)) # [TODO] use the anthropic tokenizer here completion_tokens = len( self.encoding.encode(model_response["choices"][0]["message"]["content"]) - ) ##[TODO] use the anthropic tokenizer here + ) # [TODO] use the anthropic tokenizer here model_response["created"] = time.time() model_response["model"] = model