I'll try this even if it had not that much success.
This PR to add the model_getter feature to the conversational forms. Basically, it is a straightforward method to allow developers define the extraction schema in a dynamic way. This opens the way for so many use cases: multi tenant app with schema based on the tenant, schemas dynamically load from an external source that might change frequently, schema that changes dynamically based on the information gathered in the previous round of the form (this one is pretty cool), etc.
Here's an example of that:
class PizzaOrder(BaseModel):
pizza_type: str
phone: int
address: str
class PepperoniPizza(PizzaOrder):
with_extra_cheese: bool
@form
class PizzaForm(CatForm):
description = "Pizza Order"
model_class = PizzaOrder
start_examples = [
"order a pizza!",
"I want pizza"
]
stop_examples = [
"stop pizza order",
"not hungry anymore",
]
ask_confirm = True
def model_getter(self):
if "pizza_type" in self._model and self._model["pizza_type"] == "pepperoni":
self.model_class = PepperoniPizza
return self.model_class
def submit(self, form_data):
return {
"output": f"Pizza order on its way: {form_data}"
}
In action:
I needed to refactor the update and validate methods since they were a lil bit messy and were blocking the full potential of the model_getter on validation, which otherwise would have used the model_class from the previous form round.
Hope you like this!
Type of change
[ ] Bug fix (non-breaking change which fixes an issue)
[X] New feature (non-breaking change which adds functionality)
[ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
[X] This change requires a documentation update
Checklist:
[X] My code follows the style guidelines of this project
[X] I have performed a self-review of my own code
[X] I have commented my code, particularly in hard-to-understand areas
Description
I'll try this even if it had not that much success.
This PR to add the
model_getter
feature to the conversational forms. Basically, it is a straightforward method to allow developers define the extraction schema in a dynamic way. This opens the way for so many use cases: multi tenant app with schema based on the tenant, schemas dynamically load from an external source that might change frequently, schema that changes dynamically based on the information gathered in the previous round of the form (this one is pretty cool), etc.Here's an example of that:
In action:
I needed to refactor the
update
andvalidate
methods since they were a lil bit messy and were blocking the full potential of themodel_getter
on validation, which otherwise would have used the model_class from the previous form round.Hope you like this!
Type of change
Checklist: