echolabsdev / prism

A unified interface for working with LLMs in Laravel
https://prism.echolabs.dev
MIT License
498 stars 24 forks source link

Idea: Tool class validation, baked-in #30

Open slashequip opened 3 days ago

slashequip commented 3 days ago

In a lot of my tools I have to run validation before running my actual logic. This might be checking something exists in the DB first for example, or checking a date has been provided in the correct format.

Would love to see a rules method that I can add to my extends Tool classes that gets picked up and run automagically, exactly like Laravel's Form Requests, returns an array of rules that runs against the data.

I think providing a default message back to the models "Invalid arguments provided" would suffice as an error message, which I guess could be configurable like Laravel's messages() method on Form Requests.

🫡

slashequip commented 3 days ago

I'm basically doing this inside every __invoke method on a tool class;

$validation = Validator::make(
    compact('title', 'author', 'start_date'),
    [
        'title' => ['required'],
        'author' => ['required'],
        'date' => ['required', 'date_format:Y-m-d'],
    ]
);

if ($validation->fails()) {
    return 'Invalid attributes provided.';
}
sixlive commented 3 days ago

I'm going to stew on this. I think it probably makes sense to either automatically or opt-in to automatic validation against the parameter schema

slashequip commented 2 days ago

Nice. Yeah my only thought on that is how do you introduce more complex rules like new Exists() for example.

The JSON schema limits us a little. For dates there is a format: 'date' support on the params that I've used before, but yeah it comes down to needing to be super sure that we've got the exact data.

IMO it does move to more "complex tool case" which you do separate in the docs to the class format.

Just some extra thoughts there :)