Structured Output Demo
You’ll use the ChatOpenAI model for this demo, so make sure you have your API key in your .env file in the root of your project:
OPENAI_API_KEY=<your-api-key>
Open the empty structured.ipynb notebook in the Starter project. Then load your API key:
from dotenv import load_dotenv
load_dotenv()
Set up the ChatOpenAI LLM:
import os
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
api_key=os.getenv("OPENAI_API_KEY"),
temperature=1.0
)
Since you want a little more randomness in the output, you set the temperature to 1.0. If you find that’s affecting the quality of the structured output, you can tone it down.
Then, define a Pydantic model as the instruction section showed:
from langchain_core.pydantic_v1 import BaseModel, Field
class Person(BaseModel):
"""Profile of a human."""
name: str = Field(description="The person's name")
age: int = Field(description="The person's age, between 1 and 100")
Give the Pydantic model to your large language model:
structured_llm = llm.with_structured_output(Person)
structured_llm.invoke("Create a random character for a story")
And there you have a Pydantic model. Rerun it a few times. This doesn’t seem as random as it’s supposed to be. A bit better prompting might improve that, but that’s a challenge for another day.
Next, create a TypedDict version for the same class:
from typing_extensions import Annotated, TypedDict
class Person(TypedDict):
"""Profile of a human."""
name: Annotated[str, ..., "The person's name"]
age: Annotated[int, ..., "The person's age"]
Using Annotated lets you add some metadata to help the LLM. Normal comments would have worked, too. The ... means that the field isn’t optional.
Run the LLM as before; this time, you get a dictionary as the output.