(    PromptTemplate,    ChatPromptTemplate,    HumanMessagePromptTemplate,)from langchain.llms import OpenAIfrom pydantic import BaseModel, Field, validatorfrom langchain.output_parsers import PydanticOutputParserclass Person(BaseModel):    person_name: str    person_height: int    person_hair_color: str    dog_breed: Optional[str]    dog_name: Optional[str]class People(BaseModel):    """Identifying information about all people in a text."""    people: Sequence[Person]        # Run query = """Alex is 5 feet tall. Claudia is 1 feet taller Alex and jumps higher than him. Claudia is a brunette and Alex is blonde."""# Set up a parser + inject instructions into the prompt template.parser = PydanticOutputParser(pydantic_object=People)# Promptprompt = PromptTemplate(    template="Answer the user query.\n{format_instructions}\n{query}\n",    input_variables=["query"],    partial_variables={"format_instructions": parser.get_format_instructions()},)# Run_input = prompt.format_prompt(query=query)model = OpenAI(temperature=0)output = model(_input.to_string())parser.parse(output)    People(people=[Person(person_name='Alex', person_height=5, person_hair_color='blonde', dog_breed=None, dog_name=None), Person(person_name='Claudia', person_height=6, person_hair_color='brunette', dog_breed=None, dog_name=None)])We can see from the LangSmith trace that we get the same output as above.We can see that we provide a two-shot prompt in order to instruct the LLM to output in our desired format.And, we need to do a bit more work:Define a class that holds multiple instances of PersonExplicitly parse the output of the LLM to the Pydantic classWe can see this for other cases, too.from langchain.prompts import (    PromptTemplate,    ChatPromptTemplate,    HumanMessagePromptTemplate,)from langchain.llms import OpenAIfrom pydantic import BaseModel, Field, validatorfrom langchain.output_parsers import PydanticOutputParser# Define your desired data structure.class Joke(BaseModel):    setup: str =
Open In Collab
Open In Collab ->: (    PromptTemplate,    ChatPromptTemplate,    HumanMessagePromptTemplate,)from langchain.llms import OpenAIfrom pydantic import BaseModel, Field, validatorfrom langchain.output_parsers import PydanticOutputParserclass Person(BaseModel):    person_name: str    person_height: int    person_hair_color: str    dog_breed: Optional[str]    dog_name: Optional[str]class People(BaseModel):    """Identifying information about all people in a text."""    people: Sequence[Person]        # Run query = """Alex is 5 feet tall. Claudia is 1 feet taller Alex and jumps higher than him. Claudia is a brunette and Alex is blonde."""# Set up a parser + inject instructions into the prompt template.parser = PydanticOutputParser(pydantic_object=People)# Promptprompt = PromptTemplate(    template="Answer the user query.\n{format_instructions}\n{query}\n",    input_variables=["query"],    partial_variables={"format_instructions": parser.get_format_instructions()},)# Run_input = prompt.format_prompt(query=query)model = OpenAI(temperature=0)output = model(_input.to_string())parser.parse(output)    People(people=[Person(person_name='Alex', person_height=5, person_hair_color='blonde', dog_breed=None, dog_name=None), Person(person_name='Claudia', person_height=6, person_hair_color='brunette', dog_breed=None, dog_name=None)])We can see from the LangSmith trace that we get the same output as above.We can see that we provide a two-shot prompt in order to instruct the LLM to output in our desired format.And, we need to do a bit more work:Define a class that holds multiple instances of PersonExplicitly parse the output of the LLM to the Pydantic classWe can see this for other cases, too.from langchain.prompts import (    PromptTemplate,    ChatPromptTemplate,    HumanMessagePromptTemplate,)from langchain.llms import OpenAIfrom pydantic import BaseModel, Field, validatorfrom langchain.output_parsers import PydanticOutputParser# Define your desired data structure.class Joke(BaseModel):    setup: str =