Structured Output Parser
Overview
The Structured Output Parser is a powerful feature in AnswerAI that allows you to parse the output of a Language Model (LLM) call into a predefined JSON structure. This tool is essential for extracting structured information from the LLM's responses, making it easier to process and use the data in your workflows.
Key Benefits
- Standardizes LLM outputs into a consistent, structured JSON format
- Improves data extraction and processing from LLM responses
- Enables easy integration of LLM outputs with other systems and workflows
How to Use
-
Add the Structured Output Parser node to your AnswerAI canvas.
-
Configure the node with the following settings:
a. Autofix (Optional):
- Toggle this option to enable automatic fixing of parsing errors.
- When enabled, if the initial parsing fails, the system will make another call to the model to correct any errors.
b. JSON Structure:
- Define the structure you want the LLM output to conform to.
- Add properties to the data grid, specifying:
- Property name
- Data type (string, number, or boolean)
- Description of the property
-
Connect the Structured Output Parser node to your LLM node in the workflow.
-
Run your workflow to see the parsed, structured output.
Tips and Best Practices
- Be specific in your property descriptions to guide the LLM in generating appropriate content.
- Start with a simple structure and gradually add complexity as needed.
- Use the Autofix feature when dealing with complex structures or less reliable LLM outputs.
- Test your parser with various LLM outputs to ensure it handles different scenarios correctly.
- If you need nested or more structured outputs, consider using the Advanced Structured Output Parser node.
Troubleshooting
-
Invalid JSON Structure:
- Error: "Invalid JSON in StructuredOutputParser"
- Solution: Double-check your JSON structure in the data grid. Ensure all properties have valid names and types.
-
Parsing Failures:
- Issue: The parser fails to extract the desired information consistently.
- Solution: Refine your property descriptions, simplify the structure, or enable the Autofix option.
-
Unexpected Output:
- Issue: The parsed output doesn't match your expectations.
- Solution: Review your LLM prompt and ensure it aligns with the structure you've defined in the parser.
Example Usage
Here's a basic example of how to set up the JSON Structure:
-
Property: answer Type: string Description: Answer to the user's question
-
Property: source Type: string Description: Sources used to answer the question, should be websites
This structure will parse the LLM output into an object with 'answer' and 'source' fields, making it easy to use this information in subsequent steps of your workflow.
By using the Structured Output Parser, you can transform raw LLM responses into clean, structured data, enhancing the capabilities and reliability of your AnswerAI workflows.