From 7f3807af1358d960bd170e415bce8e4e6c840c63 Mon Sep 17 00:00:00 2001 From: Michael Chin Date: Thu, 30 Jan 2025 17:57:27 -0800 Subject: [PATCH] Log specifics on mismatched dependencies --- libs/aws/langchain_aws/chat_models/bedrock.py | 32 ++++++++------- libs/aws/langchain_aws/llms/bedrock.py | 33 +++++++++------- libs/aws/langchain_aws/utils.py | 39 ++++++++++++------- 3 files changed, 63 insertions(+), 41 deletions(-) diff --git a/libs/aws/langchain_aws/chat_models/bedrock.py b/libs/aws/langchain_aws/chat_models/bedrock.py index 04a13a53..9b0d7c8c 100644 --- a/libs/aws/langchain_aws/chat_models/bedrock.py +++ b/libs/aws/langchain_aws/chat_models/bedrock.py @@ -622,26 +622,30 @@ def _combine_llm_outputs(self, llm_outputs: List[Optional[dict]]) -> dict: return final_output def get_num_tokens(self, text: str) -> int: - if self._model_is_anthropic and check_anthropic_tokens_dependencies(): - return get_num_tokens_anthropic(text) - else: - if self._model_is_anthropic: + if self._model_is_anthropic: + bad_deps = check_anthropic_tokens_dependencies() + if not bad_deps: + return get_num_tokens_anthropic(text) + else: logger.debug( - "Falling back to default token counting due to incompatible or missing Anthropic dependencies. " - "To fix this, ensure that you have anthropic<=0.38.0, httpx<=0.27.2, and Python<=3.12 installed." + "Falling back to default token counting due to incompatible/missing Anthropic dependencies:" ) - return super().get_num_tokens(text) + for x in bad_deps: + logger.debug(x) + return super().get_num_tokens(text) def get_token_ids(self, text: str) -> List[int]: - if self._model_is_anthropic and check_anthropic_tokens_dependencies(): - return get_token_ids_anthropic(text) - else: - if self._model_is_anthropic: + if self._model_is_anthropic: + bad_deps = check_anthropic_tokens_dependencies() + if not bad_deps: + return get_token_ids_anthropic(text) + else: logger.debug( - "Falling back to default token ids retrieval due to incompatible or missing Anthropic dependencies." - " To fix this, ensure that you have anthropic<=0.38.0, httpx<=0.27.2, and Python<=3.12 installed." + "Falling back to default token ids retrieval due to incompatible/missing Anthropic dependencies:" ) - return super().get_token_ids(text) + for x in bad_deps: + logger.debug(x) + return super().get_token_ids(text) def set_system_prompt_with_tools(self, xml_tools_system_prompt: str) -> None: """Workaround to bind. Sets the system prompt with tools""" diff --git a/libs/aws/langchain_aws/llms/bedrock.py b/libs/aws/langchain_aws/llms/bedrock.py index 750f2198..05bf936b 100644 --- a/libs/aws/langchain_aws/llms/bedrock.py +++ b/libs/aws/langchain_aws/llms/bedrock.py @@ -1300,23 +1300,28 @@ async def _acall( return "".join([chunk.text for chunk in chunks]) def get_num_tokens(self, text: str) -> int: - if self._model_is_anthropic and check_anthropic_tokens_dependencies(): - return get_num_tokens_anthropic(text) - else: - if self._model_is_anthropic: + if self._model_is_anthropic: + bad_deps = check_anthropic_tokens_dependencies() + if not bad_deps: + return get_num_tokens_anthropic(text) + else: logger.debug( - "Falling back to default token counting due to incompatible or missing Anthropic dependencies. " - "To fix this, ensure that you have anthropic<=0.38.0, httpx<=0.27.2, and Python<=3.12 installed." + "Falling back to default token counting due to incompatible/missing Anthropic dependencies:" ) - return super().get_num_tokens(text) + for x in bad_deps: + logger.debug(x) + + return super().get_num_tokens(text) def get_token_ids(self, text: str) -> List[int]: - if self._model_is_anthropic and check_anthropic_tokens_dependencies(): - return get_token_ids_anthropic(text) - else: - if self._model_is_anthropic: + if self._model_is_anthropic: + bad_deps = check_anthropic_tokens_dependencies() + if not bad_deps: + return get_token_ids_anthropic(text) + else: logger.debug( - "Falling back to default token ids retrieval due to incompatible or missing Anthropic dependencies." - " To fix this, ensure that you have anthropic<=0.38.0, httpx<=0.27.2, and Python<=3.12 installed." + "Falling back to default token ids retrieval due to incompatible/missing Anthropic dependencies:" ) - return super().get_token_ids(text) + for x in bad_deps: + logger.debug(x) + return super().get_token_ids(text) diff --git a/libs/aws/langchain_aws/utils.py b/libs/aws/langchain_aws/utils.py index 9691a79e..af2ff78e 100644 --- a/libs/aws/langchain_aws/utils.py +++ b/libs/aws/langchain_aws/utils.py @@ -10,25 +10,38 @@ def enforce_stop_tokens(text: str, stop: List[str]) -> str: return re.split("|".join(stop), text, maxsplit=1)[0] -def check_anthropic_tokens_dependencies() -> bool: +def check_anthropic_tokens_dependencies() -> List[str]: """Check if we have all requirements for Anthropic count_tokens() and get_tokenizer().""" + bad_deps = [] + + python_version = sys.version_info + if python_version > (3, 12): + bad_deps.append(f"Python 3.12 or earlier required, found {'.'.join(map(str, python_version[:3]))})") + + bad_anthropic = None try: import anthropic + anthropic_version = version.parse(anthropic.__version__) + if anthropic_version > version.parse("0.38.0"): + bad_anthropic = anthropic_version + except ImportError: + bad_anthropic = "none installed" + + bad_httpx = None + try: import httpx + httpx_version = version.parse(httpx.__version__) + if httpx_version > version.parse("0.27.2"): + bad_httpx = httpx_version except ImportError: - return False + bad_httpx = "none installed" - anthropic_version = version.parse(anthropic.__version__) - httpx_version = version.parse(httpx.__version__) - python_version = sys.version_info - if ( - anthropic_version > version.parse("0.38.0") or - httpx_version > version.parse("0.27.2") or - python_version > (3, 12) - ): - return False - - return True + if bad_anthropic: + bad_deps.append(f"anthropic<=0.38.0 required, found {bad_anthropic}.") + if bad_httpx: + bad_deps.append(f"httpx<=0.27.2 required, found {bad_httpx}.") + + return bad_deps def _get_anthropic_client() -> Any: