Skip to content

Commit

Permalink
Log specifics on mismatched dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelnchin committed Jan 31, 2025
1 parent 698aa9c commit 7f3807a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 41 deletions.
32 changes: 18 additions & 14 deletions libs/aws/langchain_aws/chat_models/bedrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
33 changes: 19 additions & 14 deletions libs/aws/langchain_aws/llms/bedrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
39 changes: 26 additions & 13 deletions libs/aws/langchain_aws/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 7f3807a

Please sign in to comment.