One of the biggest roadblocks with using LLMs is when they return invalid content. For example, you may ask for a specific format of JSON, but the LLM returns an explanation that breaks it in your JSON.parse().

To protect against this, Relevance has provided you with a quality control feature called Validators. You can specify a schema you want to apply to your prompt, and this component validates the LLM’s response against the desired and specified schema. If the LLM returns invalid data, in a second step the LLM is asked to fix its response. If that fails, an error message will be returned to your application. This way, you can rest assured your application will always receive valid data or error gracefully.

There are three options available, but we will always be adding more:

  • Regex (regular expression): check if the output matches the provided regular expression
  • Is_json: check if the output is a valid json
  • JsonSchema: checks if the response matches the specified schema. A sample prompt and schema are provided below

is_json

This validates whether the LLM has returned only JSON. It will try to fix it if not. This doesn’t require any additional properties.

The prompt_completion transformation will still return a string, but you will be safe to JSON.parse() it.

jsonschema

If you would like to validate a specific format of JSON, you can take advantage of JSONSchema! Provide the schema as the schema property.

{
    type: 'object',
    properties: {
        title: { type: 'string' },
        description: { type: 'string' }
    }
}

The prompt_completion transformation will still return a string, but you will be safe to JSON.parse() it.

regex

This validates whether the LLM’s response matches a regex. It will try to fix it if not. This requires a pattern property, in this format:

Relevance automatically adds the noted regex validator to your prompt, meaning that you do not need to double define the regular expression both in the prompt and the validator.

{
    pattern: "[a-z0-9]",
    flags: 'i'
} // equates to /[a-z0-9]/i