Skip to content

Feature request: Add LanguageModel.isValidConstraint() to check the validity of responseConstraint ahead of time #202

@GaurangTandon

Description

@GaurangTandon

Context

The end user inputs a regex constraint to my application. I need to be able to detect whether this regex constraint is valid or invalid. If it's invalid, I need to show clear messaging to the user. The user can then fix their regex and re-prompt my application.

Current approach

Currently, in Canary 149.0.7790.0 or higher, we have to first create a session, and then pass the responseConstraint into it to check its validity:

const session = await LanguageModel.create({
    "temperature": 0.3,
    "topK": 1
});
const res = await session.prompt([
    {
        "role": "user",
        "content": "Output the string foo"
    }
], {
    "omitResponseConstraintInput": true,
    "responseConstraint": new RegExp('^foo(?!bar)$')
})
console.log('done', res);

throws Uncaught NotSupportedError: The request is invalid - the input or options could not be processed. Note this won't work on Chrome Stable yet because the CL is recent.

Feature request

Creating the session and then destroying it only to check the validity of dynamically supplied constraints seems rather wasteful to me. A dedicated static and stateless method like LanguageModel.isValidConstraint() would be helpful and efficient.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions