Skip to content

Commit

Permalink
aws[patch]: use explicit tool name in with_structured_output tool_cho…
Browse files Browse the repository at this point in the history
…ice (#92)
  • Loading branch information
baskaryan authored Jul 1, 2024
1 parent 29e38ac commit 1125319
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
4 changes: 3 additions & 1 deletion libs/aws/langchain_aws/chat_models/bedrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,9 @@ class AnswerWithJustification(BaseModel):
ValueError(
f"Structured output is not supported for model {self._get_model()}"
)
llm = self.bind_tools([schema], tool_choice="any")

tool_name = convert_to_anthropic_tool(schema)["name"]
llm = self.bind_tools([schema], tool_choice=tool_name)
if isinstance(schema, type) and issubclass(schema, BaseModel):
output_parser = ToolsOutputParser(
first_tool_only=True, pydantic_schemas=[schema]
Expand Down
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 @@ -47,7 +47,14 @@

@beta()
class ChatBedrockConverse(BaseChatModel):
"""Bedrock chat model integration built on the ``converse`` api.
"""Bedrock chat model integration built on the Bedrock converse API.
This implementation will eventually replace the existing ChatBedrock implementation
once the Bedrock converse API has feature parity with older Bedrock API.
Specifically the converse API does not yet support custom Bedrock models.
For now it is being released as its own class in **beta** to give users who aren't
using custom models access to the latest API.
Setup:
To use Amazon Bedrock make sure you've gone through all the steps described
Expand All @@ -67,7 +74,6 @@ class ChatBedrockConverse(BaseChatModel):
max_tokens: Optional[int]
Max number of tokens to generate.
Key init args — client params:
region_name: Optional[str]
AWS region to use, e.g. 'us-west-2'.
Expand All @@ -79,7 +85,6 @@ class ChatBedrockConverse(BaseChatModel):
See full list of supported init args and their descriptions in the params section.
# TODO: Replace with relevant init params.
Instantiate:
.. code-block:: python
Expand Down Expand Up @@ -440,7 +445,8 @@ def with_structured_output(
include_raw: bool = False,
**kwargs: Any,
) -> Runnable[LanguageModelInput, Union[Dict, BaseModel]]:
llm = self.bind_tools([schema], tool_choice="any")
tool_name = convert_to_openai_function(schema)["name"]
llm = self.bind_tools([schema], tool_choice=tool_name)
if isinstance(schema, type) and issubclass(schema, BaseModel):
output_parser = ToolsOutputParser(
first_tool_only=True, pydantic_schemas=[schema]
Expand Down

0 comments on commit 1125319

Please sign in to comment.