From 719b0b3583cd747df5c5b90457899dfb63108b6d Mon Sep 17 00:00:00 2001 From: lucasmrdt Date: Sat, 16 Mar 2024 16:06:38 -0700 Subject: [PATCH] fix(anthropic): tool calling detection --- litellm/llms/anthropic.py | 3 ++- litellm/llms/prompt_templates/factory.py | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/litellm/llms/anthropic.py b/litellm/llms/anthropic.py index e078a1ddf..d061d6306 100644 --- a/litellm/llms/anthropic.py +++ b/litellm/llms/anthropic.py @@ -7,6 +7,7 @@ from typing import Callable, Optional from litellm.utils import ModelResponse, Usage, map_finish_reason, CustomStreamWrapper import litellm from .prompt_templates.factory import ( + contains_tag, prompt_factory, custom_prompt, construct_tool_use_system_prompt, @@ -235,7 +236,7 @@ def completion( else: text_content = completion_response["content"][0].get("text", None) ## TOOL CALLING - OUTPUT PARSE - if text_content is not None and "invoke" in text_content: + if text_content is not None and contains_tag("invoke", text_content): function_name = extract_between_tags("tool_name", text_content)[0] function_arguments_str = extract_between_tags("invoke", text_content)[ 0 diff --git a/litellm/llms/prompt_templates/factory.py b/litellm/llms/prompt_templates/factory.py index 990bd863d..01b9aad7b 100644 --- a/litellm/llms/prompt_templates/factory.py +++ b/litellm/llms/prompt_templates/factory.py @@ -714,6 +714,8 @@ def extract_between_tags(tag: str, string: str, strip: bool = False) -> List[str ext_list = [e.strip() for e in ext_list] return ext_list +def contains_tag(tag: str, string: str) -> bool: + return bool(re.search(f"<{tag}>(.+?)", string, re.DOTALL)) def parse_xml_params(xml_content): root = ET.fromstring(xml_content)