Supported parameters
Several parameters are avaible to better fit your needs, which must be added in your request payload:guided_choice
: the output will be exactly one of the choices.guided_regex
: the output will follow the regex pattern.guided_json
: the output will follow the JSON schema.
Guided Choices
Use theguided_choice
parameter to force a model to choose one of the choices you define.Here is an example below.
Guided Choices Example
Guided Choices Example
positive
.Guided Regex
Use the guided_regex parameter to generate text following a regex pattern.Here is an example below.
Guided Regex Example
Guided Regex Example
alan_turing@enigma.com
for instance.Data Integrity and ValidationJSON Schema maintains high data quality by enforcing a predefined structure and validation rules, minimizing the need for additional checks.
Create your JSON Schema
You have 2 possibilities to create your JSON schema:- Manually create a JSON schema: you can find a step-by-step guide here.
- Define a Pydantic model and extract the JSON schema from it.
The Role of Stop Sequences
Stop sequences are critical in structured data tasks, marking the end of model responses to keep outputs within your schema’s structure.Mandatory Stop SequenceAlways use an appropriate stop sequence of a model (default for
mistral
is stop=["</s>"]
) to ensure precise model output termination, aligning with your JSON Schema.Code example
The next examples shows how to use theguided_json
parameter with an exmplicit JSON Schema as well as with a Pydantic model.
Guided JSON: Explicit Schema
Guided JSON: Explicit Schema
Guided JSON: Pydantic Model
Guided JSON: Pydantic Model
Harnessing Pattern MatchingUse the
pattern
attribute in JSON Schema to define regular expressions (regex) for matching specific text formats. The (.+)
regex is especially useful for capturing varied text segments, enabling precise extraction of desired information from texts. This feature is key for parsing specific data points from unstructured or semi-structured text.Using Extracted Data
Leverage the structured data from your extractions to enhance databases, CRM systems, or automate workflows, boosting operational efficiency.Accurate Pattern MatchingVerify that your JSON Schema’s regex patterns align with your text’s expected formats. Mismatches can lead to data extraction errors or inaccuracies.By employing JSON Schema, you can efficiently transform unstructured text into structured, actionable data, offering a scalable solution for data processing needs.
Conclusion
Structured outputs enrich the user experience with detailed content and ensuring application-wide data consistency. It’s crucial for applications reliant on structured data integrity.Common Pitfalls
- Excluding necessary schema properties might result in partial data outputs.
- Imposing too strict constraints can restrict the AI’s ability to produce relevant content.
- Neglecting to include stop sequences in API calls may cause processing issues.