Spaces:
Running
Running
Bound JSON output fields and research lists on the pinned local runtime
Browse files44 unmodified lines45464748495036 unmodified lines8788899091929344 unmodified linesprogress('Local AI is ready. Model downloads are cached when browser storage allows.');} catch(error) {engine=null;throw new Error(`Local model could not load: ${error.message || String(error)}. Method demo remains available.`);}}function validate(value, schema, sourceIDs) {if(typeof schema==='string') {if(typeof value!=='string' || !value.trim()) throw new Error('Model returned an empty or invalid text field.');36 unmodified linesfor(let attempt=0;attempt<2;attempt++) {try {await engine.resetChat();const stream=await engine.chat.completions.create({messages:[{role:'system',content:system},{role:'user',content:JSON.stringify({question:input.question,sources:contextSources,prior_stage_outputs:prior,correction:attempt?`Previous output was invalid: ${lastError?.message || String(lastError)}. Follow the schema and exact prior IDs.`:undefined})}],temperature:0.2,max_tokens:stage==='framer'?350:stage==='scout'?450:850,stream:true});let content='',finishReason=null,chunks=0;for await(const chunk of stream) {content+=chunk.choices[0]?.delta.content || '';44 unmodified lines454647484950515253545536 unmodified lines9293949596979844 unmodified linesprogress('Local AI is ready. Model downloads are cached when browser storage allows.');} catch(error) {engine=null;throw new Error(`Local model could not load: ${error.message || String(error)}. Method demo remains available.`);}}function outputSchema(shape, sourceIDs) {if(typeof shape==='string') return {type:'string',maxLength:240,...(shape==='unverified'?{enum:['unverified']}:shape==='low|medium|high'?{enum:['low','medium','high']}:shape==='source ID'?{enum:[...sourceIDs]}:{})};if(Array.isArray(shape)) return {type:'array',items:shape[0]==='source ID' && !sourceIDs.size?{type:'string'}:outputSchema(shape[0],sourceIDs),maxItems:shape[0]==='source ID'&&!sourceIDs.size?0:typeof shape[0]==='object'?2:6};return {type:'object',properties:Object.fromEntries(Object.entries(shape).map(([key,value])=>[key,outputSchema(value,sourceIDs)])),required:Object.keys(shape),additionalProperties:false};}function validate(value, schema, sourceIDs) {if(typeof schema==='string') {if(typeof value!=='string' || !value.trim()) throw new Error('Model returned an empty or invalid text field.');36 unmodified linesfor(let attempt=0;attempt<2;attempt++) {try {await engine.resetChat();const stream=await engine.chat.completions.create({messages:[{role:'system',content:system},{role:'user',content:JSON.stringify({question:input.question,sources:contextSources,prior_stage_outputs:prior,correction:attempt?`Previous output was invalid: ${lastError?.message || String(lastError)}. Follow the schema and exact prior IDs.`:undefined})}],temperature:0.2,max_tokens:stage==='framer'?350:stage==='scout'?450:850,stream:true,response_format:{type:'json_object',schema:JSON.stringify(outputSchema(schemas[stage],sourceIDs))}});let content='',finishReason=null,chunks=0;for await(const chunk of stream) {content+=chunk.choices[0]?.delta.content || '';
research.js+6-1