Schema
FormatAttr
Class for parsing and manipulating the format
attribute of an element.
The format attribute is a string that contains semi-colon separated validators e.g. "valid-url; is-reachable". Each validator is itself either: - the name of an parameter-less validator, e.g. "valid-url" - the name of a validator with parameters, separated by a colon with a space-separated list of parameters, e.g. "is-in: 1 2 3"
Parameters can either be written in plain text, or in python expressions enclosed in curly braces. For example, the following are all valid: - "is-in: 1 2 3" - "is-in: {1} {2} {3}" - "is-in: {1 + 2} {2 + 3} {3 + 4}"
from_element
classmethod
Create a FormatAttr object from an XML element.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
element |
_Element
|
The XML element. |
required |
Returns:
Type | Description |
---|---|
FormatAttr
|
A FormatAttr object. |
get_validators
staticmethod
get_validators(validator_args: Dict[str, List[Any]], tag: str, on_fail_handlers: Dict[str, str], strict: bool = False) -> Tuple[List[Validator], List[str]]
Get the list of validators from the format attribute. Only the validators that are registered for this element will be returned.
For example, if the format attribute is "valid-url; is-reachable", and "is-reachable" is not registered for this element, then only the ValidUrl validator will be returned, after instantiating it with the arguments specified in the format attribute (if any).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
strict |
bool
|
If True, raise an error if a validator is not registered for this element. If False, ignore the validator and print a warning. |
False
|
Returns:
Type | Description |
---|---|
Tuple[List[Validator], List[str]]
|
A list of validators. |
parse
staticmethod
Parse the format attribute into a dictionary of validators.
Returns:
Type | Description |
---|---|
Dict[str, List[Any]]
|
A dictionary of validators, where the key is the validator name, and |
Dict[str, List[Any]]
|
the value is a list of arguments. |
parse_token
staticmethod
Parse a single token in the format attribute, and return the validator name and the list of arguments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
token |
str
|
The token to parse, one of the tokens returned by
|
required |
Returns:
Type | Description |
---|---|
Tuple[str, List[Any]]
|
A tuple of the validator name and the list of arguments. |
to_prompt
Convert the format string to another string representation for use in prompting. Uses the validators' to_prompt method in order to construct the string to use in prompting.
For example, the format string "valid-url; other-validator: 1.0 {1 + 2}" will be converted to "valid-url other-validator: arg1=1.0 arg2=3".
JsonSchema
JsonSchema(schema: Object, reask_prompt_template: Optional[str] = None, reask_instructions_template: Optional[str] = None)
reask_prompt_vars
class-attribute
instance-attribute
async_validate
async
Validate a dictionary of data against the schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
Optional[Dict[str, Any]]
|
The data to validate. |
required |
Returns:
Type | Description |
---|---|
Any
|
The validated data. |
from_element
classmethod
from_element(root: ET._Element, reask_prompt_template: Optional[str] = None, reask_instructions_template: Optional[str] = None) -> Self
get_reask_setup
get_reask_setup(reasks: List[FieldReAsk], original_response: Any, use_full_schema: bool, prompt_params: Optional[Dict[str, Any]] = None) -> Tuple[Schema, Prompt, Instructions]
Schema
Schema(schema: DataType, reask_prompt_template: Optional[str] = None, reask_instructions_template: Optional[str] = None)
Schema class that holds a _schema attribute.
async_validate
async
Asynchronously validate a dictionary of data against the schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
Any
|
The data to validate. |
required |
Returns:
Type | Description |
---|---|
Any
|
The validated data. |
from_element
classmethod
from_element(root: ET._Element, reask_prompt_template: Optional[str] = None, reask_instructions_template: Optional[str] = None) -> Self
Create a schema from an XML element.
get_reask_setup
get_reask_setup(reasks: List[FieldReAsk], original_response: Any, use_full_schema: bool, prompt_params: Optional[Dict[str, Any]] = None) -> Tuple[Schema, Prompt, Instructions]
Construct a schema for reasking, and a prompt for reasking.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
reasks |
List[FieldReAsk]
|
List of tuples, where each tuple contains the path to the reasked element, and the ReAsk object (which contains the error message describing why the reask is necessary). |
required |
original_response |
Any
|
The value that was returned from the API, with reasks. |
required |
use_full_schema |
bool
|
Whether to use the full schema, or only the schema for the reasked elements. |
required |
Returns:
Type | Description |
---|---|
Tuple[Schema, Prompt, Instructions]
|
The schema for reasking, and the prompt for reasking. |
introspect
Inspect the data for reasks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
Any
|
The data to introspect. |
required |
Returns:
Type | Description |
---|---|
List[FieldReAsk]
|
A list of ReAsk objects. |
parse
Parse the output from the large language model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output |
str
|
The output from the large language model. |
required |
Returns:
Type | Description |
---|---|
Tuple[Any, Optional[Exception]]
|
The parsed output, and the exception that was raised (if any). |
preprocess_prompt
preprocess_prompt(prompt_callable: PromptCallableBase, instructions: Optional[Instructions], prompt: Prompt)
Preprocess the instructions and prompt before sending it to the model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt_callable |
PromptCallableBase
|
The callable to be used to prompt the model. |
required |
instructions |
Optional[Instructions]
|
The instructions to preprocess. |
required |
prompt |
Prompt
|
The prompt to preprocess. |
required |
transpile
Convert the XML schema to a string that is used for prompting a large language model.
Returns:
Type | Description |
---|---|
str
|
The prompt. |
Schema2Prompt
Class that contains transpilers to go from a schema to its representation in a prompt.
This is important for communicating the schema to a large language model, and this class will provide multiple alternatives to do so.
datatypes_to_xml
staticmethod
datatypes_to_xml(dt: DataType, root: Optional[ET._Element] = None, override_tag_name: Optional[str] = None) -> ET._Element
Recursively convert the datatypes to XML elements.
default
classmethod
Default transpiler.
Converts the XML schema to a string directly after removing
- Comments
- Action attributes like 'on-fail-*'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
JsonSchema
|
The schema to transpile. |
required |
Returns:
Type | Description |
---|---|
str
|
The prompt. |
StringSchema
StringSchema(schema: String, reask_prompt_template: Optional[str] = None, reask_instructions_template: Optional[str] = None)
reask_prompt_vars
class-attribute
instance-attribute
async_validate
async
Validate a dictionary of data against the schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
Any
|
The data to validate. |
required |
Returns:
Type | Description |
---|---|
Any
|
The validated data. |
from_element
classmethod
from_element(root: ET._Element, reask_prompt_template: Optional[str] = None, reask_instructions_template: Optional[str] = None) -> Self
get_reask_setup
get_reask_setup(reasks: List[FieldReAsk], original_response: FieldReAsk, use_full_schema: bool, prompt_params: Optional[Dict[str, Any]] = None) -> Tuple[Schema, Prompt, Instructions]