Skip to content

Does responseConstraint need a type? #109

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

Closed
tomayac opened this issue Apr 25, 2025 · 3 comments
Closed

Does responseConstraint need a type? #109

tomayac opened this issue Apr 25, 2025 · 3 comments

Comments

@tomayac
Copy link
Contributor

tomayac commented Apr 25, 2025

Currently, responseConstraint is an object, and we need to figure out dynamically if it's a JSON Schema or a regular expression.

const responseConstraint = /* Some JSON Schema or a RegEx */
const result = await session.prompt(
  `Is this post about pottery?\n\nI love creating my own mugs`,
  {
    responseConstraint,
  }
);

Would it make sense to make responseConstraint slightly more structured and add a type, so it'd be easier to determine the concrete kind of constraint we need to deal with, and also allow us to possibly add more kinds later (see #94).

const responseConstraint = {
  value: /\d\d/,
  type: 'regular-expression',
}
@domenic
Copy link
Collaborator

domenic commented Apr 26, 2025

I don't think this makes sense. In JavaScript, and indeed in most programming languages, objects have types, and we can use this information instead of asking developers to redundantly supply us the types as strings.

Various HTTP AI APIs do require the type encoded as a string, because they're using JSON, which does not have a lot of types to go around. But we don't have that constraint.

This was discussed in #91 (comment).

@tomayac
Copy link
Contributor Author

tomayac commented Apr 28, 2025

So assuming we had XML Schema support, you'd expect developers to pass in the schema as a parsed XMLDocument?

(new DOMParser()).parseFromString(`
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="stockItem" type="stockItemType"/>
   <xsd:complexType name="stockItemType"> 
    <xsd:sequence> 
      <xsd:element name="itemName" type="xsd:string" minOccurs="0"/> 
      <xsd:element name="quantityOnHand"> 
        <xsd:simpleType> 
         <xsd:restriction base="xsd:nonNegativeInteger"> 
           <xsd:maxExclusive value="100"/> 
         </xsd:restriction> 
       </xsd:simpleType> 
     </xsd:element> 
   </xsd:sequence>
   <xsd:attribute name="itemNumber" type="SKU" use="required"/> 
 </xsd:complexType>
 <xsd:simpleType name="SKU"> 
   <xsd:restriction base="xsd:string"> 
     <xsd:pattern value="\d{3}-[A-Z]{2}"/> 
   </xsd:restriction> 
 </xsd:simpleType> 
 </xsd:schema>`, 'text/xml')

If so, this would work.

@tomayac tomayac closed this as completed Apr 28, 2025
@domenic
Copy link
Collaborator

domenic commented Apr 28, 2025

Yeah, or maybe a specific type like new LanguageModelXMLSchema(string) if there's language model-specific preprocessing that could be useful beyond just XML parsing.

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

No branches or pull requests

2 participants