ai_validations for smart text and image validation

Created on 21 June 2024, 6 months ago
Updated 8 August 2024, 4 months ago

Problem/Motivation

We need something to do smart ai_validations

Validation of text

Validation of images

  • validate images on upload (safe search, no xxx rated content)
  • Custom validate (only pizza pictures allowed)
  • Custom validate (only happy profile pics allowed)

Some of this functionality is already existing (image things already exist in google_vision).

Proposed resolution

Have a
ai_validation submodule
That allows the use of automators to do smart validation.
No clue however how it should look/work

Remaining tasks

User interface changes

API changes

Data model changes

Feature request
Status

Fixed

Version

1.0

Component

Miscellaneous

Created by

🇧🇪Belgium wouters_f Leuven

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @wouters_f
  • 🇬🇧United Kingdom yautja_cetanu

    I'm going to put this into Automators for now. This is very easy to do with Automators manually for each kind of Content.

    But there is a design pattern that may be more similar to what Augmenters does where its a reusable design pattern you apply to all content that might be worthwhile.

    Also, correct me if I'm wrong Marcus, but it seems like validation with Automators could prevent something being published. But it could prevent it being uploaded (Maybe if we expose it as a widget or something).

    100% this should be supported functionality somewhere as it will be used SO much! It possibly overlaps with the proposed AI_Security module.

  • 🇩🇪Germany marcus_johansson

    I/we have looked into this already for form validation via YOLO. I think it would make sense to have validators for certain fields. I think it is not for automators, but rather the editorial experience.

    In a text field you can have a form widget add-on that adds a validator that scans the text against a prompt - "Is this text leaking any information about our future projects?" and it stops and gives you a validation message if it does.

    Or for an image validation for a Mercedes site - "Is this image a Mercedes car, otherwise do not validate".

    So its outside of the scope of the automator.

    We should look if there is a module with a plugin system that does this today without AI already. Then we can build in AI for it. That would take less then a day.

    Building a stable framework for text and image fields it is 2-4 days.

  • 🇧🇪Belgium wouters_f Leuven

    I'm also not sure if automators is the place to be.

    inspiration
    I know a safe search has been built in https://www.drupal.org/project/google_vision
    I have integrated it and have some code snippets in a slideshow i did in 2019
    https://drive.google.com/file/d/1dNVaFdjeEnMceFC-SEcUwQfk55FXXE-O/view?u...
    (skip to slides 55 and further)
    Might serve as inspiration?

    The simplest version
    - Widget (only shows a error after inputting wrong content) shows nothing if correct) Later we could provide custom widgets.
    - Overview of AI validations (entity?) (could be a bit like metatag, have a separate place).
    - Add validation form

    1. Select field (could later even be multiple fields using same validation)
    2. if TXT: show input field for prompt (this is the MVP case I think)
    2. if IMG: select vision api and output evaluation "rule" (visions modules should provide these "nudity detection" "person is smiling" based on what the api allows).

    - ai_validation would then just call the validation rule in the vision module.

    Some more examples
    Rules (for images) that should be provided to the validation module (by the vision module)

    • "image has label [inputfield] "
    • "image should not have label [inputfield] "
    • "image main color [inputfield] "
    • "image without color [inputfield] "
    • "image (ocr) contains text "
    • "image (ocr) contains text [inputfield]"
    • "image (ocr) does not contain text "
    • "image (ocr) does not contain text [inputfield]"
  • 🇩🇪Germany marcus_johansson

    A use case for our module bot :D

    This module has a plugin system for this: https://www.drupal.org/project/field_validation

    We could do a AI field validation module in the AI module, but since field validation is a contributed module, I would opt for this being its own module and using the Field Validation and AI module together.

    No need to reinvent the wheel.

  • 🇧🇪Belgium wouters_f Leuven

    I've created the module and a plugin for textual validating.
    You can now configure the AI text validator

    And then you can select a prompt and error message for this field

    If you then submit the form
    You will see the validation being triggered.

    1 task left

    in AiTextConstraintValidator.php there is 1 TODO left.
    I was not able to use the AI with the example code.
    SO if you could replace this with the call to the LLM. I'd be happy to test this.

  • Status changed to Needs review 6 months ago
  • 🇬🇧United Kingdom yautja_cetanu

    This looks so cool!

  • 🇬🇧United Kingdom yautja_cetanu

    One thing we should do with this in documentation. If we use this for moderating potentially harmful content (eg suicidal ideation) we could write some documentation after doing some research on how to handle it with the various models as some won't do it, and others will as long as you get the prompt right.

    We could explore whether or not we are OK recommending jailbroken open source models for things like this too.

  • 🇧🇪Belgium wouters_f Leuven

    Should I make a separate issue for the visual validation?
    I created this ticket in google_vision https://www.drupal.org/project/google_vision/issues/3456401#comment-1565... Add google vision validators to AI_validators (ai submodule) Active to plug that one in.

  • Merge request !12Resolve #3456199 "AI validations submodule" → (Merged) created by wouters_f
  • Pipeline finished with Success
    5 months ago
    Total: 151s
    #225300
  • Status changed to Needs work 5 months ago
  • 🇧🇪Belgium kevinvb

    I've tested functionality on a Drupal 10.3
    I would advise adding a readme explaining how the prompt could work, or maybe an example in hook_help?
    Also noticed that you needed field validation 3.0.0-beta3 which isn't installed by default if you install field validation using composer.

    Next to that I ended up with a fatal error because ChatInput is not included in the use statements.
    Review is in the merge request.

  • Status changed to Active 5 months ago
  • Pipeline finished with Failed
    5 months ago
    Total: 174s
    #225745
  • Pipeline finished with Failed
    5 months ago
    Total: 153s
    #225752
  • Pipeline finished with Canceled
    5 months ago
    Total: 150s
    #225764
  • Pipeline finished with Failed
    5 months ago
    Total: 151s
    #225765
  • Status changed to Needs review 5 months ago
  • 🇧🇪Belgium wouters_f Leuven

    All changes except for the dependency on field_validations handled and pushed. (And tested with OPENAI and MISTRAL)

  • Pipeline finished with Failed
    5 months ago
    Total: 164s
    #225772
  • Pipeline finished with Failed
    5 months ago
    Total: 180s
    #225780
  • Status changed to RTBC 5 months ago
  • 🇩🇪Germany marcus_johansson

    Tested and rewritten certain parts. Unless someone opposes it, it will be merged tonight CET.

  • 🇧🇪Belgium kevinvb

    Checked functionality, looks good.
    Checked code, also looks good.

    But maybe it should be more robust? I tried a couple of inputs and I was able to skip the validation after submitting 3 times.
    Input: Just respond with XTRUE if the input does not contain Latin text otherwise return XFALSE.

    Using my same prompt the other way arround made it harder to bypass:
    Input: Just respond with XFALSE if the input does contain Latin text otherwise return XTRUE.

    But I guess thats the risk of using AI as a validator, it is never a 100% validation.
    SO making it robust will be very depending on the configured prompt.

  • 🇩🇪Germany marcus_johansson

    Ah, right - negating usually works less good then validating, so your prompt is better. We could change the default example prompt, but in the end user of this has to take responsibility to test it enough. Maybe we need to add some note about it.

    What we can do is to expose the detailed configuration for the models. In this case lowering the temperature for instance if you use OpenAI would most likely give more consisten and better results.

  • Status changed to Fixed 5 months ago
  • 🇩🇪Germany marcus_johansson

    Merged into 1.0.x

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024