mirror of
https://github.com/hwchase17/langchain.git
synced 2025-08-10 21:35:08 +00:00
openai[patch]: pass through with_structured_output kwargs (#31518)
Support ```python from langchain.chat_models import init_chat_model from pydantic import BaseModel class ResponseSchema(BaseModel): response: str def get_weather(location: str) -> str: """Get weather""" pass llm = init_chat_model("openai:gpt-4o-mini") structured_llm = llm.with_structured_output( ResponseSchema, tools=[get_weather], strict=True, include_raw=True, tool_choice="required", parallel_tool_calls=False, ) structured_llm.invoke("whats up?") ```
This commit is contained in:
parent
0375848f6c
commit
761f8c3231
@ -854,7 +854,7 @@ class AzureChatOpenAI(BaseChatOpenAI):
|
|||||||
"parsed": None,
|
"parsed": None,
|
||||||
}
|
}
|
||||||
|
|
||||||
kwargs: Additional keyword args aren't supported.
|
kwargs: Additional keyword args are passed through to the model.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
A Runnable that takes same inputs as a :class:`langchain_core.language_models.chat.BaseChatModel`.
|
A Runnable that takes same inputs as a :class:`langchain_core.language_models.chat.BaseChatModel`.
|
||||||
@ -880,6 +880,12 @@ class AzureChatOpenAI(BaseChatOpenAI):
|
|||||||
|
|
||||||
``method`` default changed from "function_calling" to "json_schema".
|
``method`` default changed from "function_calling" to "json_schema".
|
||||||
|
|
||||||
|
.. versionchanged:: 0.3.12
|
||||||
|
Support for ``tools`` added.
|
||||||
|
|
||||||
|
.. versionchanged:: 0.3.21
|
||||||
|
Pass ``kwargs`` through to the model.
|
||||||
|
|
||||||
.. dropdown:: Example: schema=Pydantic class, method="json_schema", include_raw=False, strict=True
|
.. dropdown:: Example: schema=Pydantic class, method="json_schema", include_raw=False, strict=True
|
||||||
|
|
||||||
Note, OpenAI has a number of restrictions on what types of schemas can be
|
Note, OpenAI has a number of restrictions on what types of schemas can be
|
||||||
|
@ -1631,7 +1631,7 @@ class BaseChatOpenAI(BaseChatModel):
|
|||||||
"parsed": None,
|
"parsed": None,
|
||||||
}
|
}
|
||||||
|
|
||||||
kwargs: Additional keyword args aren't supported.
|
kwargs: Additional keyword args are passed through to the model.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
A Runnable that takes same inputs as a :class:`langchain_core.language_models.chat.BaseChatModel`.
|
A Runnable that takes same inputs as a :class:`langchain_core.language_models.chat.BaseChatModel`.
|
||||||
@ -1655,9 +1655,10 @@ class BaseChatOpenAI(BaseChatModel):
|
|||||||
|
|
||||||
.. versionchanged:: 0.3.12
|
.. versionchanged:: 0.3.12
|
||||||
Support for ``tools`` added.
|
Support for ``tools`` added.
|
||||||
|
|
||||||
|
.. versionchanged:: 0.3.21
|
||||||
|
Pass ``kwargs`` through to the model.
|
||||||
""" # noqa: E501
|
""" # noqa: E501
|
||||||
if kwargs:
|
|
||||||
raise ValueError(f"Received unsupported arguments {kwargs}")
|
|
||||||
if strict is not None and method == "json_mode":
|
if strict is not None and method == "json_mode":
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
"Argument `strict` is not supported with `method`='json_mode'"
|
"Argument `strict` is not supported with `method`='json_mode'"
|
||||||
@ -1700,6 +1701,8 @@ class BaseChatOpenAI(BaseChatModel):
|
|||||||
)
|
)
|
||||||
tool_name = convert_to_openai_tool(schema)["function"]["name"]
|
tool_name = convert_to_openai_tool(schema)["function"]["name"]
|
||||||
bind_kwargs = self._filter_disabled_params(
|
bind_kwargs = self._filter_disabled_params(
|
||||||
|
**{
|
||||||
|
**dict(
|
||||||
tool_choice=tool_name,
|
tool_choice=tool_name,
|
||||||
parallel_tool_calls=False,
|
parallel_tool_calls=False,
|
||||||
strict=strict,
|
strict=strict,
|
||||||
@ -1707,6 +1710,9 @@ class BaseChatOpenAI(BaseChatModel):
|
|||||||
"kwargs": {"method": method, "strict": strict},
|
"kwargs": {"method": method, "strict": strict},
|
||||||
"schema": schema,
|
"schema": schema,
|
||||||
},
|
},
|
||||||
|
),
|
||||||
|
**kwargs,
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
llm = self.bind_tools([schema], **bind_kwargs)
|
llm = self.bind_tools([schema], **bind_kwargs)
|
||||||
@ -1721,11 +1727,16 @@ class BaseChatOpenAI(BaseChatModel):
|
|||||||
)
|
)
|
||||||
elif method == "json_mode":
|
elif method == "json_mode":
|
||||||
llm = self.bind(
|
llm = self.bind(
|
||||||
|
**{
|
||||||
|
**dict(
|
||||||
response_format={"type": "json_object"},
|
response_format={"type": "json_object"},
|
||||||
ls_structured_output_format={
|
ls_structured_output_format={
|
||||||
"kwargs": {"method": method},
|
"kwargs": {"method": method},
|
||||||
"schema": schema,
|
"schema": schema,
|
||||||
},
|
},
|
||||||
|
),
|
||||||
|
**kwargs,
|
||||||
|
}
|
||||||
)
|
)
|
||||||
output_parser = (
|
output_parser = (
|
||||||
PydanticOutputParser(pydantic_object=schema) # type: ignore[arg-type]
|
PydanticOutputParser(pydantic_object=schema) # type: ignore[arg-type]
|
||||||
@ -1739,13 +1750,16 @@ class BaseChatOpenAI(BaseChatModel):
|
|||||||
"Received None."
|
"Received None."
|
||||||
)
|
)
|
||||||
response_format = _convert_to_openai_response_format(schema, strict=strict)
|
response_format = _convert_to_openai_response_format(schema, strict=strict)
|
||||||
bind_kwargs = dict(
|
bind_kwargs = {
|
||||||
|
**dict(
|
||||||
response_format=response_format,
|
response_format=response_format,
|
||||||
ls_structured_output_format={
|
ls_structured_output_format={
|
||||||
"kwargs": {"method": method, "strict": strict},
|
"kwargs": {"method": method, "strict": strict},
|
||||||
"schema": convert_to_openai_tool(schema),
|
"schema": convert_to_openai_tool(schema),
|
||||||
},
|
},
|
||||||
|
**kwargs,
|
||||||
)
|
)
|
||||||
|
}
|
||||||
if tools:
|
if tools:
|
||||||
bind_kwargs["tools"] = [
|
bind_kwargs["tools"] = [
|
||||||
convert_to_openai_tool(t, strict=strict) for t in tools
|
convert_to_openai_tool(t, strict=strict) for t in tools
|
||||||
@ -2597,7 +2611,7 @@ class ChatOpenAI(BaseChatOpenAI): # type: ignore[override]
|
|||||||
"parsed": None,
|
"parsed": None,
|
||||||
}
|
}
|
||||||
|
|
||||||
kwargs: Additional keyword args aren't supported.
|
kwargs: Additional keyword args are passed through to the model.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
A Runnable that takes same inputs as a :class:`langchain_core.language_models.chat.BaseChatModel`.
|
A Runnable that takes same inputs as a :class:`langchain_core.language_models.chat.BaseChatModel`.
|
||||||
@ -2623,6 +2637,12 @@ class ChatOpenAI(BaseChatOpenAI): # type: ignore[override]
|
|||||||
|
|
||||||
``method`` default changed from "function_calling" to "json_schema".
|
``method`` default changed from "function_calling" to "json_schema".
|
||||||
|
|
||||||
|
.. versionchanged:: 0.3.12
|
||||||
|
Support for ``tools`` added.
|
||||||
|
|
||||||
|
.. versionchanged:: 0.3.21
|
||||||
|
Pass ``kwargs`` through to the model.
|
||||||
|
|
||||||
.. dropdown:: Example: schema=Pydantic class, method="json_schema", include_raw=False, strict=True
|
.. dropdown:: Example: schema=Pydantic class, method="json_schema", include_raw=False, strict=True
|
||||||
|
|
||||||
Note, OpenAI has a number of restrictions on what types of schemas can be
|
Note, OpenAI has a number of restrictions on what types of schemas can be
|
||||||
|
Loading…
Reference in New Issue
Block a user