This example demonstrates how to integrate PredictionGuard with custom data structures for output parsing, utilizing the Pydantic library. The focus is on structuring outputs in a specific format, in this case, a joke, ensuring the setup ends with a question mark.
PydanticOutputParser to
enforce the data structure defined by the Pydantic model on the output.PromptTemplate to structure
your query, incorporating instructions for output formatting derived from the parser.After running the implementation with the query “Tell me a joke.”, the structured output generated by PredictionGuard, parsed and validated by our Pydantic model, looks like this.
Setup: “Why did the tomato turn red?” - This question successfully passes the validator check, ending with a question mark as required.
Punchline: “Because it saw the salad dressing.” - Provides a humorous answer to the setup question.
This example demonstrates the effective structuring and validation of output data, ensuring that the generated joke adheres to our defined format.
This approach allows for the flexible and structured generation of outputs, leveraging PredictionGuard’s capabilities alongside Pydantic’s validation features.