Skip to content

Commit

Permalink
aws[patch]: add with_structured_output pydantic v2 support bedrock (#138
Browse files Browse the repository at this point in the history
)
  • Loading branch information
baskaryan authored Aug 3, 2024
1 parent d6f2f67 commit db832a8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
8 changes: 4 additions & 4 deletions libs/aws/langchain_aws/chat_models/bedrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
Optional,
Sequence,
Tuple,
Type,
Union,
cast,
)
Expand All @@ -34,6 +33,7 @@
from langchain_core.pydantic_v1 import BaseModel, Extra
from langchain_core.runnables import Runnable, RunnableMap, RunnablePassthrough
from langchain_core.tools import BaseTool
from langchain_core.utils.pydantic import TypeBaseModel, is_basemodel_subclass

from langchain_aws.chat_models.bedrock_converse import ChatBedrockConverse
from langchain_aws.function_calling import (
Expand Down Expand Up @@ -595,7 +595,7 @@ def set_system_prompt_with_tools(self, xml_tools_system_prompt: str) -> None:

def bind_tools(
self,
tools: Sequence[Union[Dict[str, Any], Type[BaseModel], Callable, BaseTool]],
tools: Sequence[Union[Dict[str, Any], TypeBaseModel, Callable, BaseTool]],
*,
tool_choice: Optional[Union[dict, str, Literal["auto", "none"], bool]] = None,
**kwargs: Any,
Expand Down Expand Up @@ -650,7 +650,7 @@ def bind_tools(

def with_structured_output(
self,
schema: Union[Dict, Type[BaseModel]],
schema: Union[Dict, TypeBaseModel],
*,
include_raw: bool = False,
**kwargs: Any,
Expand Down Expand Up @@ -771,7 +771,7 @@ class AnswerWithJustification(BaseModel):

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):
if isinstance(schema, type) and is_basemodel_subclass(schema):
output_parser = ToolsOutputParser(
first_tool_only=True, pydantic_schemas=[schema]
)
Expand Down
9 changes: 4 additions & 5 deletions libs/aws/langchain_aws/chat_models/bedrock_converse.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
Optional,
Sequence,
Tuple,
Type,
TypeVar,
Union,
cast,
Expand Down Expand Up @@ -43,7 +42,7 @@
from langchain_core.tools import BaseTool
from langchain_core.utils import get_from_dict_or_env
from langchain_core.utils.function_calling import convert_to_openai_function
from langchain_core.utils.pydantic import is_basemodel_subclass
from langchain_core.utils.pydantic import TypeBaseModel, is_basemodel_subclass

from langchain_aws.function_calling import ToolsOutputParser

Expand Down Expand Up @@ -436,7 +435,7 @@ def _stream(

def bind_tools(
self,
tools: Sequence[Union[Dict[str, Any], Type[BaseModel], Callable, BaseTool]],
tools: Sequence[Union[Dict[str, Any], TypeBaseModel, Callable, BaseTool]],
*,
tool_choice: Optional[Union[dict, str, Literal["auto", "any"]]] = None,
**kwargs: Any,
Expand All @@ -447,7 +446,7 @@ def bind_tools(

def with_structured_output(
self,
schema: Union[Dict, Type[BaseModel]],
schema: Union[Dict, TypeBaseModel],
*,
include_raw: bool = False,
**kwargs: Any,
Expand Down Expand Up @@ -770,7 +769,7 @@ def _bedrock_to_anthropic(content: List[Dict[str, Any]]) -> List[Dict[str, Any]]


def _format_tools(
tools: Sequence[Union[Dict[str, Any], Type[BaseModel], Callable, BaseTool],],
tools: Sequence[Union[Dict[str, Any], TypeBaseModel, Callable, BaseTool],],
) -> List[Dict[Literal["toolSpec"], Dict[str, Union[Dict[str, Any], str]]]]:
formatted_tools: List = []
for tool in tools:
Expand Down

0 comments on commit db832a8

Please sign in to comment.