docs, multiple: de-beta with_structured_output (#20850)

This commit is contained in:
Erick Friis 2024-04-24 12:34:57 -07:00 committed by GitHub
parent 477eb1745c
commit 8c95ac3145
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 60 additions and 67 deletions

View File

@ -15,7 +15,7 @@
"id": "6e3f0f72", "id": "6e3f0f72",
"metadata": {}, "metadata": {},
"source": [ "source": [
"# [beta] Structured Output\n", "# Structured Output\n",
"\n", "\n",
"It is often crucial to have LLMs return structured output. This is because oftentimes the outputs of the LLMs are used in downstream applications, where specific arguments are required. Having the LLM return structured output reliably is necessary for that.\n", "It is often crucial to have LLMs return structured output. This is because oftentimes the outputs of the LLMs are used in downstream applications, where specific arguments are required. Having the LLM return structured output reliably is necessary for that.\n",
"\n", "\n",
@ -39,21 +39,14 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 1,
"id": "08029f4e",
"metadata": {},
"outputs": [],
"source": [
"from langchain_core.pydantic_v1 import BaseModel, Field"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "070bf702", "id": "070bf702",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"from langchain_core.pydantic_v1 import BaseModel, Field\n",
"\n",
"\n",
"class Joke(BaseModel):\n", "class Joke(BaseModel):\n",
" setup: str = Field(description=\"The setup of the joke\")\n", " setup: str = Field(description=\"The setup of the joke\")\n",
" punchline: str = Field(description=\"The punchline to the joke\")" " punchline: str = Field(description=\"The punchline to the joke\")"
@ -93,7 +86,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 9, "execution_count": 3,
"id": "6700994a", "id": "6700994a",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -104,17 +97,17 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 10, "execution_count": 4,
"id": "c55a61b8", "id": "c55a61b8",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"Joke(setup='Why was the cat sitting on the computer?', punchline='It wanted to keep an eye on the mouse!')" "Joke(setup='Why was the cat sitting on the computer?', punchline='To keep an eye on the mouse!')"
] ]
}, },
"execution_count": 10, "execution_count": 4,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -135,7 +128,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 11, "execution_count": 5,
"id": "df0370e3", "id": "df0370e3",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -145,17 +138,17 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 14, "execution_count": 6,
"id": "23844a26", "id": "23844a26",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"Joke(setup=\"Why don't cats play poker in the jungle?\", punchline='Too many cheetahs!')" "Joke(setup='Why was the cat sitting on the computer?', punchline='Because it wanted to keep an eye on the mouse!')"
] ]
}, },
"execution_count": 14, "execution_count": 6,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -180,7 +173,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": 7,
"id": "ad45fdd8", "id": "ad45fdd8",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -252,7 +245,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 12, "execution_count": 11,
"id": "649f9632", "id": "649f9632",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@ -262,7 +255,7 @@
"Joke(setup='Why did the dog sit in the shade?', punchline='To avoid getting burned.')" "Joke(setup='Why did the dog sit in the shade?', punchline='To avoid getting burned.')"
] ]
}, },
"execution_count": 12, "execution_count": 11,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -287,7 +280,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 12,
"id": "bffd3fad", "id": "bffd3fad",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -297,7 +290,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 11, "execution_count": 13,
"id": "c8bd7549", "id": "c8bd7549",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -308,10 +301,21 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 14,
"id": "17b15816", "id": "17b15816",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"data": {
"text/plain": [
"Joke(setup=\"Why don't cats play poker in the jungle?\", punchline='Too many cheetahs!')"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"structured_llm.invoke(\"Tell me a joke about cats\")" "structured_llm.invoke(\"Tell me a joke about cats\")"
] ]
@ -328,7 +332,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 20, "execution_count": 15,
"id": "9b9617e3", "id": "9b9617e3",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -340,7 +344,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 24, "execution_count": 16,
"id": "90549664", "id": "90549664",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -355,7 +359,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 25, "execution_count": 17,
"id": "01da39be", "id": "01da39be",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@ -365,7 +369,7 @@
"Joke(setup='Why did the cat sit on the computer?', punchline='To keep an eye on the mouse!')" "Joke(setup='Why did the cat sit on the computer?', punchline='To keep an eye on the mouse!')"
] ]
}, },
"execution_count": 25, "execution_count": 17,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -388,7 +392,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 11, "execution_count": 18,
"id": "70511bc3", "id": "70511bc3",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -408,19 +412,10 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 12, "execution_count": 19,
"id": "be9fdf04", "id": "be9fdf04",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/reag/src/langchain/libs/core/langchain_core/_api/beta_decorator.py:87: LangChainBetaWarning: The function `with_structured_output` is in beta. It is actively being worked on, so the API may change.\n",
" warn_beta(\n"
]
}
],
"source": [ "source": [
"model = ChatGroq()\n", "model = ChatGroq()\n",
"structured_llm = model.with_structured_output(Joke)" "structured_llm = model.with_structured_output(Joke)"
@ -428,7 +423,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 7, "execution_count": 20,
"id": "e13f4676", "id": "e13f4676",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@ -438,7 +433,7 @@
"Joke(setup=\"Why don't cats play poker in the jungle?\", punchline='Too many cheetahs!')" "Joke(setup=\"Why don't cats play poker in the jungle?\", punchline='Too many cheetahs!')"
] ]
}, },
"execution_count": 7, "execution_count": 20,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -459,7 +454,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 8, "execution_count": 21,
"id": "86574fb8", "id": "86574fb8",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -469,7 +464,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 9, "execution_count": 22,
"id": "01dced9c", "id": "01dced9c",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@ -479,7 +474,7 @@
"Joke(setup=\"Why don't cats play poker in the jungle?\", punchline='Too many cheetahs!')" "Joke(setup=\"Why don't cats play poker in the jungle?\", punchline='Too many cheetahs!')"
] ]
}, },
"execution_count": 9, "execution_count": 22,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -504,7 +499,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 5, "execution_count": 23,
"id": "12682237-6689-4408-88b1-3595feac447f", "id": "12682237-6689-4408-88b1-3595feac447f",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@ -514,7 +509,7 @@
"Joke(setup='What do you call a cat that loves to bowl?', punchline='An alley cat!')" "Joke(setup='What do you call a cat that loves to bowl?', punchline='An alley cat!')"
] ]
}, },
"execution_count": 5, "execution_count": 23,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -541,17 +536,17 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 7, "execution_count": 2,
"id": "24421189-02bf-4589-a91a-197584c4a696", "id": "24421189-02bf-4589-a91a-197584c4a696",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"Joke(setup='A cat-ch', punchline='What do you call a cat that loves to play fetch?')" "Joke(setup='Why did the scarecrow win an award?', punchline='Why did the scarecrow win an award? Because he was outstanding in his field.')"
] ]
}, },
"execution_count": 7, "execution_count": 2,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -563,13 +558,21 @@
"structured_llm = llm.with_structured_output(Joke)\n", "structured_llm = llm.with_structured_output(Joke)\n",
"structured_llm.invoke(\"Tell me a joke about cats\")" "structured_llm.invoke(\"Tell me a joke about cats\")"
] ]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2630a2cb",
"metadata": {},
"outputs": [],
"source": []
} }
], ],
"metadata": { "metadata": {
"kernelspec": { "kernelspec": {
"display_name": "poetry-venv-2", "display_name": ".venv",
"language": "python", "language": "python",
"name": "poetry-venv-2" "name": "python3"
}, },
"language_info": { "language_info": {
"codemirror_mode": { "codemirror_mode": {
@ -581,7 +584,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.9.1" "version": "3.11.4"
} }
}, },
"nbformat": 4, "nbformat": 4,

View File

@ -19,7 +19,7 @@ from typing import (
from typing_extensions import TypeAlias from typing_extensions import TypeAlias
from langchain_core._api import beta, deprecated from langchain_core._api import deprecated
from langchain_core.messages import ( from langchain_core.messages import (
AnyMessage, AnyMessage,
BaseMessage, BaseMessage,
@ -201,7 +201,6 @@ class BaseLanguageModel(
prompt and additional model provider-specific output. prompt and additional model provider-specific output.
""" """
@beta()
def with_structured_output( def with_structured_output(
self, schema: Union[Dict, Type[BaseModel]], **kwargs: Any self, schema: Union[Dict, Type[BaseModel]], **kwargs: Any
) -> Runnable[LanguageModelInput, Union[Dict, BaseModel]]: ) -> Runnable[LanguageModelInput, Union[Dict, BaseModel]]:

View File

@ -580,7 +580,6 @@ class ChatAnthropic(BaseChatModel):
formatted_tools = [convert_to_anthropic_tool(tool) for tool in tools] formatted_tools = [convert_to_anthropic_tool(tool) for tool in tools]
return self.bind(tools=formatted_tools, **kwargs) return self.bind(tools=formatted_tools, **kwargs)
@beta()
def with_structured_output( def with_structured_output(
self, self,
schema: Union[Dict, Type[BaseModel]], schema: Union[Dict, Type[BaseModel]],

View File

@ -24,7 +24,6 @@ from typing import (
) )
from fireworks.client import AsyncFireworks, Fireworks # type: ignore from fireworks.client import AsyncFireworks, Fireworks # type: ignore
from langchain_core._api import beta
from langchain_core.callbacks import ( from langchain_core.callbacks import (
AsyncCallbackManagerForLLMRun, AsyncCallbackManagerForLLMRun,
CallbackManagerForLLMRun, CallbackManagerForLLMRun,
@ -671,7 +670,6 @@ class ChatFireworks(BaseChatModel):
kwargs["tool_choice"] = tool_choice kwargs["tool_choice"] = tool_choice
return super().bind(tools=formatted_tools, **kwargs) return super().bind(tools=formatted_tools, **kwargs)
@beta()
def with_structured_output( def with_structured_output(
self, self,
schema: Optional[Union[Dict, Type[BaseModel]]] = None, schema: Optional[Union[Dict, Type[BaseModel]]] = None,

View File

@ -23,7 +23,6 @@ from typing import (
cast, cast,
) )
from langchain_core._api import beta
from langchain_core.callbacks import ( from langchain_core.callbacks import (
AsyncCallbackManagerForLLMRun, AsyncCallbackManagerForLLMRun,
CallbackManagerForLLMRun, CallbackManagerForLLMRun,
@ -595,7 +594,6 @@ class ChatGroq(BaseChatModel):
kwargs["tool_choice"] = tool_choice kwargs["tool_choice"] = tool_choice
return super().bind(tools=formatted_tools, **kwargs) return super().bind(tools=formatted_tools, **kwargs)
@beta()
def with_structured_output( def with_structured_output(
self, self,
schema: Optional[Union[Dict, Type[BaseModel]]] = None, schema: Optional[Union[Dict, Type[BaseModel]]] = None,

View File

@ -22,7 +22,6 @@ from typing import (
import httpx import httpx
from httpx_sse import EventSource, aconnect_sse, connect_sse from httpx_sse import EventSource, aconnect_sse, connect_sse
from langchain_core._api import beta
from langchain_core.callbacks import ( from langchain_core.callbacks import (
AsyncCallbackManagerForLLMRun, AsyncCallbackManagerForLLMRun,
CallbackManagerForLLMRun, CallbackManagerForLLMRun,
@ -588,7 +587,6 @@ class ChatMistralAI(BaseChatModel):
formatted_tools = [convert_to_openai_tool(tool) for tool in tools] formatted_tools = [convert_to_openai_tool(tool) for tool in tools]
return super().bind(tools=formatted_tools, **kwargs) return super().bind(tools=formatted_tools, **kwargs)
@beta()
def with_structured_output( def with_structured_output(
self, self,
schema: Union[Dict, Type[BaseModel]], schema: Union[Dict, Type[BaseModel]],

View File

@ -29,7 +29,6 @@ from typing import (
import openai import openai
import tiktoken import tiktoken
from langchain_core._api import beta
from langchain_core.callbacks import ( from langchain_core.callbacks import (
AsyncCallbackManagerForLLMRun, AsyncCallbackManagerForLLMRun,
CallbackManagerForLLMRun, CallbackManagerForLLMRun,
@ -885,7 +884,6 @@ class ChatOpenAI(BaseChatModel):
) -> Runnable[LanguageModelInput, _DictOrPydantic]: ) -> Runnable[LanguageModelInput, _DictOrPydantic]:
... ...
@beta()
def with_structured_output( def with_structured_output(
self, self,
schema: Optional[_DictOrPydanticClass] = None, schema: Optional[_DictOrPydanticClass] = None,