This commit is contained in:
Chester Curme 2025-04-25 14:57:52 -04:00
parent 97a0431874
commit 36db3212a9

View File

@ -346,7 +346,7 @@ class ChatLlamaCpp(BaseChatModel):
tool_choice: Optional[Union[dict, bool, str, Literal["auto", "any"]]] = None, tool_choice: Optional[Union[dict, bool, str, Literal["auto", "any"]]] = None,
**kwargs: Any, **kwargs: Any,
) -> Runnable[LanguageModelInput, BaseMessage]: ) -> Runnable[LanguageModelInput, BaseMessage]:
"""Bind tool-like objects to this chat model """ """Bind tool-like objects to this chat model"""
formatted_tools = [convert_to_openai_tool(tool) for tool in tools] formatted_tools = [convert_to_openai_tool(tool) for tool in tools]
tool_names = [ft["function"]["name"] for ft in formatted_tools] tool_names = [ft["function"]["name"] for ft in formatted_tools]
if tool_choice: if tool_choice:
@ -359,19 +359,21 @@ class ChatLlamaCpp(BaseChatModel):
f"provided tools were {tool_names}." f"provided tools were {tool_names}."
) )
elif isinstance(tool_choice, str): elif isinstance(tool_choice, str):
if tool_choice == 'any': if tool_choice == "any":
if len(formatted_tools) == 1: if len(formatted_tools) == 1:
tool_choice = formatted_tools[0] tool_choice = formatted_tools[0]
else: else:
raise ValueError( raise ValueError(
"tool_choice `'any'` only supported if one tool is provided." "tool_choice `'any'` only supported if one tool is provided."
) )
elif tool_choice == 'auto': elif tool_choice == "auto":
tool_choice = None tool_choice = None
else: else:
chosen = [ chosen = [
f for f in formatted_tools if f["function"]["name"] == tool_choice f
] for f in formatted_tools
if f["function"]["name"] == tool_choice
]
if not chosen: if not chosen:
raise ValueError( raise ValueError(
f"Tool choice {tool_choice=} was specified, but the only " f"Tool choice {tool_choice=} was specified, but the only "