AI Agents with LangGraph

Nov 12 2024 · Python 3.12, LangGraph 0.2.x, JupyterLab 4.2.4

Lesson 04: Enhancing Agent Capabilities

Structured Output Demo

Episode complete

Play next episode

Next

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

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>
from dotenv import load_dotenv
load_dotenv()
import os
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
  api_key=os.getenv("OPENAI_API_KEY"),
  temperature=1.0
)
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")
structured_llm = llm.with_structured_output(Person)
structured_llm.invoke("Create a random character for a story")
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"]
See forum comments
Cinema mode Download course materials from Github
Previous: Structured Output Next: Human-in-the-Loop