Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forms model getter #970

Merged
merged 4 commits into from
Nov 13, 2024
Merged

Conversation

lucagobbi
Copy link
Collaborator

@lucagobbi lucagobbi commented Nov 7, 2024

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:

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:

image

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)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas

@valentimarco valentimarco added the enhancement New feature or request label Nov 7, 2024
@pieroit pieroit merged commit 37594d0 into cheshire-cat-ai:develop Nov 13, 2024
2 checks passed
@pieroit
Copy link
Member

pieroit commented Nov 13, 2024

Thanks @lucagobbi agree on self._model

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants