Create a PydanticOutputParser from the Ticket model. Then define a PromptTemplate that embeds the JSON schema instructions:
Copy
Ask AI
# Initialize the parserparser = PydanticOutputParser(pydantic_object=Ticket)# Template with a placeholder for {query} and format instructionsticket_template = """\Book us a movie ticket for two this Friday at 6:00 PM.Choose any theater. Send the confirmation by email.Our preferred movie is: {query}Format instructions:{format_instructions}"""prompt = PromptTemplate( template=ticket_template, input_variables=["query"], partial_variables={"format_instructions": parser.get_format_instructions()},)
The parser.get_format_instructions() method injects a JSON Schema under “Format instructions.” The LLM must follow this schema exactly.
Book us a movie ticket for two this Friday at 6:00 PM.Choose any theater. Send the confirmation by email.Our preferred movie is: InterstellarFormat instructions:The output should be formatted as a JSON instance that conforms to the JSON Schema below.As an example, for the schema{"properties": {"foo": {"title":"Foo","description":"a list of strings","type":"array","items":{"type":"string"}}},"required":["foo"]}the object {"foo":["bar","baz"]} is valid.Here is the output schema:{"properties":{ "date": {"title":"Date","description":"show date","type":"string"}, "time": {"title":"Time","description":"show time","type":"string"}, "theater": {"title":"Theater","description":"theater name","type":"string"}, "count": {"title":"Count","description":"number of tickets","type":"integer"}, "movie": {"title":"Movie","description":"preferred movie","type":"string"}},"required":["date","time","theater","count","movie"]}