Skip to content

Commit

Permalink
Fixes a problem where formatted tools would break
Browse files Browse the repository at this point in the history
It seems like the first time we are calling _format_tools everything works fine, but on a second run, the _snake_case_to_camel_case converts the input parameters to camelCase and breaks the tools.
  • Loading branch information
userlerueda committed Jun 22, 2024
1 parent 11bc0e0 commit b6d235c
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions libs/aws/langchain_aws/chat_models/bedrock_converse.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ class Joke(BaseModel):
max_tokens: Optional[int] = None
"""Max tokens to generate."""

stop_sequences: Optional[List[str]] = Field(default=None, alias="stop")
stop_sequences: Optional[List[str]] = Field(None, alias="stop")
"""Stop generation if any of these substrings occurs."""

temperature: Optional[float] = None
Expand Down Expand Up @@ -308,12 +308,17 @@ class Joke(BaseModel):
have an ARN associated with them.
"""

endpoint_url: Optional[str] = Field(default=None, alias="base_url")
endpoint_url: Optional[str] = Field(None, alias="base_url")
"""Needed if you don't want to default to us-east-1 endpoint"""

config: Any = None
"""An optional botocore.config.Config instance to pass to the client."""

formatted_tools: List[
Dict[Literal["toolSpec"], Dict[str, Union[Dict[str, Any], str]]]
] = Field(default_factory=list, exclude=True)
""""Formatted tools to be stored and used in the toolConfig parameter."""

class Config:
"""Configuration for this pydantic object."""

Expand Down Expand Up @@ -413,7 +418,8 @@ def bind_tools(
) -> Runnable[LanguageModelInput, BaseMessage]:
if tool_choice:
kwargs["tool_choice"] = _format_tool_choice(tool_choice)
return self.bind(tools=_format_tools(tools), **kwargs)
self.formatted_tools = _format_tools(tools)
return self.bind(tools=self.formatted_tools, **kwargs)

def with_structured_output(
self,
Expand Down Expand Up @@ -467,7 +473,7 @@ def _converse_params(
}
if not toolConfig and tools:
toolChoice = _format_tool_choice(toolChoice) if toolChoice else None
toolConfig = {"tools": _format_tools(tools), "toolChoice": toolChoice}
toolConfig = {"tools": self.formatted_tools, "toolChoice": toolChoice}
return _drop_none(
{
"modelId": modelId or self.model_id,
Expand Down

0 comments on commit b6d235c

Please sign in to comment.