mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-26 08:33:49 +00:00
docs, multiple: de-beta with_structured_output (#20850)
This commit is contained in:
parent
477eb1745c
commit
8c95ac3145
@ -15,7 +15,7 @@
|
||||
"id": "6e3f0f72",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# [beta] Structured Output\n",
|
||||
"# Structured Output\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",
|
||||
"\n",
|
||||
@ -39,21 +39,14 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"id": "08029f4e",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_core.pydantic_v1 import BaseModel, Field"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": 1,
|
||||
"id": "070bf702",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_core.pydantic_v1 import BaseModel, Field\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class Joke(BaseModel):\n",
|
||||
" setup: str = Field(description=\"The setup of the joke\")\n",
|
||||
" punchline: str = Field(description=\"The punchline to the joke\")"
|
||||
@ -93,7 +86,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"execution_count": 3,
|
||||
"id": "6700994a",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@ -104,17 +97,17 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"execution_count": 4,
|
||||
"id": "c55a61b8",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"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": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@ -135,7 +128,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"execution_count": 5,
|
||||
"id": "df0370e3",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@ -145,17 +138,17 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"execution_count": 6,
|
||||
"id": "23844a26",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"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": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@ -180,7 +173,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"execution_count": 7,
|
||||
"id": "ad45fdd8",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@ -252,7 +245,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"execution_count": 11,
|
||||
"id": "649f9632",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@ -262,7 +255,7 @@
|
||||
"Joke(setup='Why did the dog sit in the shade?', punchline='To avoid getting burned.')"
|
||||
]
|
||||
},
|
||||
"execution_count": 12,
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@ -287,7 +280,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": 12,
|
||||
"id": "bffd3fad",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@ -297,7 +290,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"execution_count": 13,
|
||||
"id": "c8bd7549",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@ -308,10 +301,21 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 14,
|
||||
"id": "17b15816",
|
||||
"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": [
|
||||
"structured_llm.invoke(\"Tell me a joke about cats\")"
|
||||
]
|
||||
@ -328,7 +332,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 20,
|
||||
"execution_count": 15,
|
||||
"id": "9b9617e3",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@ -340,7 +344,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 24,
|
||||
"execution_count": 16,
|
||||
"id": "90549664",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@ -355,7 +359,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 25,
|
||||
"execution_count": 17,
|
||||
"id": "01da39be",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@ -365,7 +369,7 @@
|
||||
"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": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@ -388,7 +392,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"execution_count": 18,
|
||||
"id": "70511bc3",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@ -408,19 +412,10 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"execution_count": 19,
|
||||
"id": "be9fdf04",
|
||||
"metadata": {},
|
||||
"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"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"model = ChatGroq()\n",
|
||||
"structured_llm = model.with_structured_output(Joke)"
|
||||
@ -428,7 +423,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"execution_count": 20,
|
||||
"id": "e13f4676",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@ -438,7 +433,7 @@
|
||||
"Joke(setup=\"Why don't cats play poker in the jungle?\", punchline='Too many cheetahs!')"
|
||||
]
|
||||
},
|
||||
"execution_count": 7,
|
||||
"execution_count": 20,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@ -459,7 +454,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"execution_count": 21,
|
||||
"id": "86574fb8",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@ -469,7 +464,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"execution_count": 22,
|
||||
"id": "01dced9c",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@ -479,7 +474,7 @@
|
||||
"Joke(setup=\"Why don't cats play poker in the jungle?\", punchline='Too many cheetahs!')"
|
||||
]
|
||||
},
|
||||
"execution_count": 9,
|
||||
"execution_count": 22,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@ -504,7 +499,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"execution_count": 23,
|
||||
"id": "12682237-6689-4408-88b1-3595feac447f",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@ -514,7 +509,7 @@
|
||||
"Joke(setup='What do you call a cat that loves to bowl?', punchline='An alley cat!')"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"execution_count": 23,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@ -541,17 +536,17 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"execution_count": 2,
|
||||
"id": "24421189-02bf-4589-a91a-197584c4a696",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"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": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@ -563,13 +558,21 @@
|
||||
"structured_llm = llm.with_structured_output(Joke)\n",
|
||||
"structured_llm.invoke(\"Tell me a joke about cats\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "2630a2cb",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "poetry-venv-2",
|
||||
"display_name": ".venv",
|
||||
"language": "python",
|
||||
"name": "poetry-venv-2"
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
@ -581,7 +584,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.9.1"
|
||||
"version": "3.11.4"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
@ -19,7 +19,7 @@ from typing import (
|
||||
|
||||
from typing_extensions import TypeAlias
|
||||
|
||||
from langchain_core._api import beta, deprecated
|
||||
from langchain_core._api import deprecated
|
||||
from langchain_core.messages import (
|
||||
AnyMessage,
|
||||
BaseMessage,
|
||||
@ -201,7 +201,6 @@ class BaseLanguageModel(
|
||||
prompt and additional model provider-specific output.
|
||||
"""
|
||||
|
||||
@beta()
|
||||
def with_structured_output(
|
||||
self, schema: Union[Dict, Type[BaseModel]], **kwargs: Any
|
||||
) -> Runnable[LanguageModelInput, Union[Dict, BaseModel]]:
|
||||
|
@ -580,7 +580,6 @@ class ChatAnthropic(BaseChatModel):
|
||||
formatted_tools = [convert_to_anthropic_tool(tool) for tool in tools]
|
||||
return self.bind(tools=formatted_tools, **kwargs)
|
||||
|
||||
@beta()
|
||||
def with_structured_output(
|
||||
self,
|
||||
schema: Union[Dict, Type[BaseModel]],
|
||||
|
@ -24,7 +24,6 @@ from typing import (
|
||||
)
|
||||
|
||||
from fireworks.client import AsyncFireworks, Fireworks # type: ignore
|
||||
from langchain_core._api import beta
|
||||
from langchain_core.callbacks import (
|
||||
AsyncCallbackManagerForLLMRun,
|
||||
CallbackManagerForLLMRun,
|
||||
@ -671,7 +670,6 @@ class ChatFireworks(BaseChatModel):
|
||||
kwargs["tool_choice"] = tool_choice
|
||||
return super().bind(tools=formatted_tools, **kwargs)
|
||||
|
||||
@beta()
|
||||
def with_structured_output(
|
||||
self,
|
||||
schema: Optional[Union[Dict, Type[BaseModel]]] = None,
|
||||
|
@ -23,7 +23,6 @@ from typing import (
|
||||
cast,
|
||||
)
|
||||
|
||||
from langchain_core._api import beta
|
||||
from langchain_core.callbacks import (
|
||||
AsyncCallbackManagerForLLMRun,
|
||||
CallbackManagerForLLMRun,
|
||||
@ -595,7 +594,6 @@ class ChatGroq(BaseChatModel):
|
||||
kwargs["tool_choice"] = tool_choice
|
||||
return super().bind(tools=formatted_tools, **kwargs)
|
||||
|
||||
@beta()
|
||||
def with_structured_output(
|
||||
self,
|
||||
schema: Optional[Union[Dict, Type[BaseModel]]] = None,
|
||||
|
@ -22,7 +22,6 @@ from typing import (
|
||||
|
||||
import httpx
|
||||
from httpx_sse import EventSource, aconnect_sse, connect_sse
|
||||
from langchain_core._api import beta
|
||||
from langchain_core.callbacks import (
|
||||
AsyncCallbackManagerForLLMRun,
|
||||
CallbackManagerForLLMRun,
|
||||
@ -588,7 +587,6 @@ class ChatMistralAI(BaseChatModel):
|
||||
formatted_tools = [convert_to_openai_tool(tool) for tool in tools]
|
||||
return super().bind(tools=formatted_tools, **kwargs)
|
||||
|
||||
@beta()
|
||||
def with_structured_output(
|
||||
self,
|
||||
schema: Union[Dict, Type[BaseModel]],
|
||||
|
@ -29,7 +29,6 @@ from typing import (
|
||||
|
||||
import openai
|
||||
import tiktoken
|
||||
from langchain_core._api import beta
|
||||
from langchain_core.callbacks import (
|
||||
AsyncCallbackManagerForLLMRun,
|
||||
CallbackManagerForLLMRun,
|
||||
@ -885,7 +884,6 @@ class ChatOpenAI(BaseChatModel):
|
||||
) -> Runnable[LanguageModelInput, _DictOrPydantic]:
|
||||
...
|
||||
|
||||
@beta()
|
||||
def with_structured_output(
|
||||
self,
|
||||
schema: Optional[_DictOrPydanticClass] = None,
|
||||
|
Loading…
Reference in New Issue
Block a user