language-model-response-json-schema.tentative.https.window.js (3214B)
1 // META: title=Language Model Response JSON Schema 2 // META: script=/resources/testdriver.js 3 // META: script=../resources/util.js 4 // META: timeout=long 5 6 'use strict'; 7 8 const kValidResponseSchema = { 9 type: 'object', 10 required: ['Rating'], 11 additionalProperties: false, 12 properties: { 13 Rating: { 14 type: 'number', 15 minimum: 0, 16 maximum: 5, 17 }, 18 }, 19 }; 20 21 function testResponseJsonSchema(response, t) { 22 let jsonResponse; 23 try { 24 jsonResponse = JSON.parse(response); 25 } catch (e) { 26 assert_unreached( 27 `Response is not valid JSON: "${response}". Error: ${e.message}`); 28 return; 29 } 30 assert_equals(typeof jsonResponse, 'object', 'Response should be an object'); 31 assert_own_property( 32 jsonResponse, 'Rating', 'JSON response should have a "Rating" property.'); 33 assert_equals( 34 typeof jsonResponse.Rating, 'number', 'Rating should be a number'); 35 assert_greater_than_equal(jsonResponse.Rating, 0, 'Rating should be >= 0'); 36 assert_less_than_equal(jsonResponse.Rating, 5, 'Rating should be <= 5'); 37 } 38 39 promise_test(async t => { 40 await ensureLanguageModel(); 41 const session = await createLanguageModel(); 42 // Circular reference is not valid. 43 const invalidResponseJsonSchema = {}; 44 invalidResponseJsonSchema.self = invalidResponseJsonSchema; 45 await promise_rejects_dom( 46 t, 'NotSupportedError', 47 session.prompt( 48 kTestPrompt, {responseConstraint: invalidResponseJsonSchema}), 49 'Response json schema is invalid - it should be an object that can be stringified into a JSON string.'); 50 }, 'Prompt API should fail if an invalid response json schema is provided'); 51 52 promise_test(async t => { 53 await ensureLanguageModel(); 54 const session = await createLanguageModel(); 55 const response = 56 await session.prompt('hello', {responseConstraint: kValidResponseSchema}); 57 testResponseJsonSchema(response, t); 58 }, 'Prompt API should work when a valid response json schema is provided.'); 59 60 promise_test(async t => { 61 await ensureLanguageModel(); 62 const session = await createLanguageModel(); 63 const assistantPrefix = '{ "Rating": '; 64 const assistantResponse = await session.prompt( 65 [ 66 {role: 'user', content: 'hello'}, 67 {role: 'assistant', content: assistantPrefix, prefix: true} 68 ], 69 {responseConstraint: kValidResponseSchema}); 70 const response = assistantPrefix + assistantResponse; 71 testResponseJsonSchema(response, t); 72 }, 'Prompt API should work when a valid response json schema and model prefix is provided.'); 73 74 promise_test(async t => { 75 await ensureLanguageModel(); 76 const session = await createLanguageModel(); 77 const response = await session.prompt('hello', { 78 responseConstraint: kValidResponseSchema, 79 omitResponseConstraintInput: true 80 }); 81 testResponseJsonSchema(response, t); 82 }, 'Prompt API should omit response schema from input.'); 83 84 promise_test(async t => { 85 await ensureLanguageModel(); 86 const session = await createLanguageModel(); 87 const promptPromise = 88 session.prompt(kTestPrompt, {responseConstraint: /hello/}); 89 const result = await promptPromise; 90 assert_true(typeof result === 'string'); 91 }, 'Prompt API should work when a valid regex constraint is provided.');