structure.class Joke(BaseModel):    setup: str = Field(description="question to set up a joke")    punchline: str = Field(description="answer to resolve the joke")    # You can add custom validation logic easily with Pydantic.    @validator("setup")    def question_ends_with_question_mark(cls, field):        if field[-1] != "?":            raise ValueError("Badly formed question!")        return field# And a query intended to prompt a language model to populate the data structure.joke_query = "Tell me a joke."# Set up a parser + inject instructions into the prompt template.parser = PydanticOutputParser(pydantic_object=Joke)# 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=joke_query)model = OpenAI(temperature=0)output = model(_input.to_string())parser.parse(output)    Joke(setup='Why did the chicken cross the road?', punchline='To get to the other side!')As we can see, we get an output of the Joke class, which respects our originally desired schema: 'setup' and 'punchline'.We can look at the LangSmith trace to see exactly what is going on under the hood.Going deeper‚ÄãThe output parser documentation includes various parser examples for specific types (e.g., lists, datetime, enum, etc).  JSONFormer offers another way for structured decoding of a subset of the JSON Schema.Kor is another library for extraction where schema and examples can be provided to the LLM.PreviousChatbotsNextSummarizationUse caseOverviewQuickstartOption 1: OpenAI functionsLooking under the hoodMultiple entity typesUnrelated entitiesExtra informationPydanticOption 2: ParsingPydanticGoing deeperCommunityDiscordTwitterGitHubPythonJS/TSMoreHomepageBlogCopyright ¬© 2023 LangChain, Inc.
Open In Collab
Open In Collab ->: structure.class Joke(BaseModel):    setup: str = Field(description="question to set up a joke")    punchline: str = Field(description="answer to resolve the joke")    # You can add custom validation logic easily with Pydantic.    @validator("setup")    def question_ends_with_question_mark(cls, field):        if field[-1] != "?":            raise ValueError("Badly formed question!")        return field# And a query intended to prompt a language model to populate the data structure.joke_query = "Tell me a joke."# Set up a parser + inject instructions into the prompt template.parser = PydanticOutputParser(pydantic_object=Joke)# 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=joke_query)model = OpenAI(temperature=0)output = model(_input.to_string())parser.parse(output)    Joke(setup='Why did the chicken cross the road?', punchline='To get to the other side!')As we can see, we get an output of the Joke class, which respects our originally desired schema: 'setup' and 'punchline'.We can look at the LangSmith trace to see exactly what is going on under the hood.Going deeper‚ÄãThe output parser documentation includes various parser examples for specific types (e.g., lists, datetime, enum, etc).  JSONFormer offers another way for structured decoding of a subset of the JSON Schema.Kor is another library for extraction where schema and examples can be provided to the LLM.PreviousChatbotsNextSummarizationUse caseOverviewQuickstartOption 1: OpenAI functionsLooking under the hoodMultiple entity typesUnrelated entitiesExtra informationPydanticOption 2: ParsingPydanticGoing deeperCommunityDiscordTwitterGitHubPythonJS/TSMoreHomepageBlogCopyright ¬© 2023 LangChain, Inc.