Alternative Output Structuring With PredictionGuard
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.
Requirements
- PredictionGuard API
- Pydantic library
- LangChain library
Implementation Steps
- Set Environment Variable for PredictionGuard: Ensure that your PredictionGuard API token is correctly set up in your environment variables.
- Import Necessary Libraries: Import PredictionGuard, Pydantic for data validation, and LangChain for output parsing and prompt templating.
- Define Your Data Structure: Create a Pydantic model to define the structure of a joke, including setup and punchline, with a validator to ensure the setup is properly formed.
- Set Up an Output Parser: Utilize LangChain’s
PydanticOutputParser
to enforce the data structure defined by the Pydantic model on the output.
- Create and Format a Prompt: Use LangChain’s
PromptTemplate
to structure your query, incorporating instructions for output formatting derived from the parser.
- Generate and Parse Output: Call PredictionGuard’s text completion model “neural-chat-7b-v3-3” to generate an output based on the formatted prompt, then parse the output into the Pydantic model. Handle exceptions for parsing errors.
Let’s Test This Out
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.
Output Explanation
-
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.
Conclusion
This approach allows for the flexible and structured generation of outputs, leveraging PredictionGuard’s capabilities alongside Pydantic’s validation features.