Skip to content

Commit

Permalink
aws[patch]: bind tools in openai format (#267)
Browse files Browse the repository at this point in the history
bind tools in openai format so that runs with tools are traced in a
standard format
  • Loading branch information
baskaryan authored Oct 30, 2024
1 parent 8b42d02 commit fc03740
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions libs/aws/langchain_aws/chat_models/bedrock_converse.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,12 @@ def bind_tools(
tool_choice: Optional[Union[dict, str, Literal["auto", "any"]]] = None,
**kwargs: Any,
) -> Runnable[LanguageModelInput, BaseMessage]:
try:
formatted_tools: list[dict] = [
convert_to_openai_tool(tool) for tool in tools
]
except Exception:
formatted_tools = _format_tools(tools)
if tool_choice:
tool_choice = _format_tool_choice(tool_choice)
tool_choice_type = list(tool_choice.keys())[0]
Expand All @@ -546,7 +552,7 @@ def bind_tools(
f"for the latest documentation on models that support tool choice."
)
kwargs["tool_choice"] = _format_tool_choice(tool_choice)
return self.bind(tools=_format_tools(tools), **kwargs)
return self.bind(tools=formatted_tools, **kwargs)

def with_structured_output(
self,
Expand Down Expand Up @@ -922,7 +928,7 @@ def _format_tools(
if isinstance(tool, dict) and "toolSpec" in tool:
formatted_tools.append(tool)
else:
spec = convert_to_openai_function(tool)
spec = convert_to_openai_tool(tool)["function"]
spec["inputSchema"] = {"json": spec.pop("parameters")}
formatted_tools.append({"toolSpec": spec})
return formatted_tools
Expand Down

0 comments on commit fc03740

Please sign in to comment.