Canada-Facts

Sat 17 May 2025
# https://python.langchain.com/docs/how_to/structured_output/
# https://python.langchain.com/docs/how_to/
# https://platform.openai.com/usage
from constants import OPENAI_API_KEY
!pip show langchain-openai | grep "Version:"
Version: 0.2.9
import os
os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4o-mini")
from typing import Optional
from pydantic import BaseModel, Field

# Pydantic
class CanadaFact(BaseModel):
    """Facts about Canada to tell user."""

    year: str     = Field(description = "Specific year of the fact")
    info: str     = Field(description = "Info of the fact")
    category: str = Field(description = "Category of the fact")

structured_llm = llm.with_structured_output(CanadaFact)
def q():
    return structured_llm.invoke("Tell me a fact about Canada")
q()
CanadaFact(year='1867', info='Canada became a self-governing dominion within the British Empire on July 1, 1867, with the passage of the British North America Act.', category='Historical')
q()
CanadaFact(year='1867', info='Canada became a self-governing dominion within the British Empire on July 1, 1867, through the British North America Act.', category='History')


Score: 10

Category: langchain