@instructor-ai/instructor
Version:
structured outputs for llms
3 lines • 10.5 kB
JavaScript
;Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var _openai = require('openai'); var _openai2 = _interopRequireDefault(_openai);var _streaming = require('openai/streaming');var _zod = require('zod');var _zodstream = require('zod-stream'); var _zodstream2 = _interopRequireDefault(_zodstream);var _zodvalidationerror = require('zod-validation-error');function S(s,e){let o={};for(let r in e)e.hasOwnProperty(r)&&!s.includes(r)&&(o[r]=e[r]);return o}async function I(s,e){let o=Array.from({length:e},()=>[]),r=[],p=s[Symbol.asyncIterator](),a=!1;async function*l(d){for(;;)if(o[d].length>0)yield o[d].shift();else{if(a)break;await new Promise(h=>r.push(h))}}return(async()=>{for await(let d of{[Symbol.asyncIterator]:()=>p}){for(let h of o)h.push(d);for(;r.length>0;)r.shift()()}for(a=!0;r.length>0;)r.shift()()})(),Array.from({length:e},(d,h)=>l(h))}var t=_zodstream.MODE,E={[t.THINKING_MD_JSON]:_zodstream.thinkingJsonParser},n={OAI:"OAI",ANYSCALE:"ANYSCALE",TOGETHER:"TOGETHER",ANTHROPIC:"ANTHROPIC",GROQ:"GROQ",OTHER:"OTHER"},g={[n.OTHER]:[t.FUNCTIONS,t.TOOLS,t.JSON,t.JSON_SCHEMA,t.MD_JSON],[n.OAI]:[t.FUNCTIONS,t.TOOLS,t.JSON,t.MD_JSON],[n.ANYSCALE]:[t.TOOLS,t.JSON,t.JSON_SCHEMA,t.MD_JSON],[n.TOGETHER]:[t.TOOLS,t.JSON,t.JSON_SCHEMA,t.MD_JSON],[n.ANTHROPIC]:[t.MD_JSON,t.TOOLS],[n.GROQ]:[t.TOOLS,t.FUNCTIONS,t.MD_JSON]},C={[n.ANYSCALE]:"api.endpoints.anyscale",[n.TOGETHER]:"api.together.xyz",[n.OAI]:"api.openai.com",[n.ANTHROPIC]:"api.anthropic.com",[n.GROQ]:"api.groq.com"},R={[n.GROQ]:{[t.TOOLS]:function(e){return e.tools.some(o=>o)&&e.stream&&console.warn("Streaming may not be supported when using tools in Groq, try MD_JSON instead"),e}},[n.ANYSCALE]:{[t.JSON_SCHEMA]:function(e){return"additionalProperties"in e.response_format.schema?{...e,response_format:{...e.response_format,schema:S(["additionalProperties"],e.response_format.schema)}}:e},[t.TOOLS]:function(e){return e.tools.some(o=>_optionalChain([o, 'access', _2 => _2.function, 'optionalAccess', _3 => _3.parameters]))?{...e,tools:e.tools.map(o=>_optionalChain([o, 'access', _4 => _4.function, 'optionalAccess', _5 => _5.parameters])?{...o,function:{...o.function,parameters:S(["additionalProperties"],o.function.parameters)}}:o)}:e}}},Z={[n.OTHER]:{[t.FUNCTIONS]:["*"],[t.TOOLS]:["*"],[t.JSON]:["*"],[t.MD_JSON]:["*"],[t.JSON_SCHEMA]:["*"],[t.THINKING_MD_JSON]:["*"]},[n.OAI]:{[t.FUNCTIONS]:["*"],[t.TOOLS]:["*"],[t.JSON]:["*"],[t.MD_JSON]:["*"]},[n.TOGETHER]:{[t.MD_JSON]:["*"]},[n.ANYSCALE]:{[t.MD_JSON]:["*"]},[n.ANTHROPIC]:{[t.MD_JSON]:["*"],[t.TOOLS]:["*"]},[n.GROQ]:{[t.TOOLS]:["*"],[t.MD_JSON]:["*"],[t.THINKING_MD_JSON]:["deepseek-r1-distill-llama-70b"]}};var x=0,A=class{constructor({client:e,mode:o,debug:r=!1,logger:p=void 0,retryAllErrors:a=!1}){this.debug=!1;this.retryAllErrors=!1;this.chat={completions:{create:async(e,o)=>{if(this.isChatCompletionCreateParamsWithModel(e))return e.stream?this.chatCompletionStream(e,o):this.chatCompletionStandard(e,o);if(_optionalChain([this, 'access', _6 => _6.client, 'access', _7 => _7.chat, 'optionalAccess', _8 => _8.completions, 'optionalAccess', _9 => _9.create]))return this.isStandardStream(e)?await this.client.chat.completions.create(e,o):await this.client.chat.completions.create(e,o);throw new Error("Completion method is undefined")}}};if(!J(e)&&!(e instanceof _openai2.default))throw new Error("Client does not match the required structure");e instanceof _openai2.default?this.client=e:this.client=e,this.mode=o,this.debug=r,this.retryAllErrors=a,this.logger=_nullishCoalesce(p, () => (void 0));let l=typeof _optionalChain([this, 'access', _10 => _10.client, 'optionalAccess', _11 => _11.baseURL])=="string"?_optionalChain([this, 'access', _12 => _12.client, 'optionalAccess', _13 => _13.baseURL, 'access', _14 => _14.includes, 'call', _15 => _15(C.ANYSCALE)])?n.ANYSCALE:_optionalChain([this, 'access', _16 => _16.client, 'optionalAccess', _17 => _17.baseURL, 'access', _18 => _18.includes, 'call', _19 => _19(C.TOGETHER)])?n.TOGETHER:_optionalChain([this, 'access', _20 => _20.client, 'optionalAccess', _21 => _21.baseURL, 'access', _22 => _22.includes, 'call', _23 => _23(C.OAI)])?n.OAI:_optionalChain([this, 'access', _24 => _24.client, 'optionalAccess', _25 => _25.baseURL, 'access', _26 => _26.includes, 'call', _27 => _27(C.ANTHROPIC)])?n.ANTHROPIC:_optionalChain([this, 'access', _28 => _28.client, 'optionalAccess', _29 => _29.baseURL, 'access', _30 => _30.includes, 'call', _31 => _31(C.GROQ)])?n.GROQ:n.OTHER:n.OTHER;this.provider=l,this.validateOptions()}validateOptions(){let e=g[this.provider].includes(this.mode);this.provider===n.OTHER&&this.log("debug","Unknown provider - cant validate options."),e||this.log("warn",`Mode ${this.mode} may not be supported by provider ${this.provider}`)}log(e,...o){if(this.logger&&this.logger(e,...o),!this.debug&&e==="debug")return;let r=new Date().toISOString();switch(e){case"debug":console.debug(`[Instructor:DEBUG] ${r}:`,...o);break;case"info":console.info(`[Instructor:INFO] ${r}:`,...o);break;case"warn":console.warn(`[Instructor:WARN] ${r}:`,...o);break;case"error":console.error(`[Instructor:ERROR] ${r}:`,...o);break}}async chatCompletionStandard({max_retries:e=x,response_model:o,...r},p){let a=0,l="",d=null,h=_optionalChain([R, 'optionalAccess', _32 => _32[this.provider], 'optionalAccess', _33 => _33[this.mode]]),O=_zodstream.withResponseModel.call(void 0, {params:{...r,stream:_nullishCoalesce(r.stream, () => (!1))},mode:this.mode,response_model:o});h&&(O=h(O));let T=async()=>{let i=O;_optionalChain([l, 'optionalAccess', _34 => _34.length])>0&&(i={...O,messages:[...O.messages,...d?[d]:[],{role:"user",content:`Please correct the function call; errors encountered:
${l}`}]});let c;try{if(_optionalChain([this, 'access', _35 => _35.client, 'access', _36 => _36.chat, 'optionalAccess', _37 => _37.completions, 'optionalAccess', _38 => _38.create]))c=await this.client.chat.completions.create({...i,stream:!1},p);else throw new Error("Unsupported client type -- no completion method found.");this.log("debug","raw standard completion response: ",c)}catch(u){throw this.log("error",`Error making completion call - mode: ${this.mode} | Client base URL: ${this.client.baseURL} | with params:`,i,"raw error",u),u}let f=(_nullishCoalesce(_optionalChain([E, 'optionalAccess', _39 => _39[this.mode]]), () => ((0, _zodstream.OAIResponseParser))))(c);try{let u=_nullishCoalesce(f.json, () => (f));return{...JSON.parse(u),_meta:{usage:_nullishCoalesce(_optionalChain([c, 'optionalAccess', _40 => _40.usage]), () => (void 0)),thinking:_nullishCoalesce(_optionalChain([f, 'optionalAccess', _41 => _41.thinking]), () => (void 0))}}}catch(u){throw this.log("error","failed to parse completion",f,this.mode,"attempt: ",a,"max attempts: ",e),u}},m=async()=>{try{let i=await T(),c=await o.schema.safeParseAsync(i);if(this.log("debug",o.name,"Completion validation: ",c),!c.success)throw"error"in c?(d={role:"assistant",content:JSON.stringify(i)},l=_optionalChain([_zodvalidationerror.fromZodError.call(void 0, c.error), 'optionalAccess', _42 => _42.message]),c.error):new Error("Validation failed.");return{...c.data,_meta:_nullishCoalesce(_optionalChain([i, 'optionalAccess', _43 => _43._meta]), () => ({}))}}catch(i){if(!this.retryAllErrors&&!(i instanceof _zod.ZodError))throw i;if(a<e)return this.log("debug",`response model: ${o.name} - Retrying, attempt: `,a),this.log("warn",`response model: ${o.name} - Validation issues: `,l," - Attempt: ",a," - Max attempts: ",e),a++,await m();throw this.log("debug",`response model: ${o.name} - Max attempts reached: ${a}`),this.log("error",`response model: ${o.name} - Validation issues: `,l),i}};return m()}async*chatCompletionStream({max_retries:e,response_model:o,...r},p){e&&this.log("warn","max_retries is not supported for streaming completions");let a=_optionalChain([R, 'optionalAccess', _44 => _44[this.provider], 'optionalAccess', _45 => _45[this.mode]]),l=_zodstream.withResponseModel.call(void 0, {params:{...r,stream:!0},response_model:o,mode:this.mode});a&&(l=a(l));let d=new (0, _zodstream2.default)({debug:_nullishCoalesce(this.debug, () => (!1))});async function h(m){for await(let i of m)"usage"in i&&(O=i.usage)}let O,T=await d.create({completionPromise:async()=>{if(_optionalChain([this, 'access', _46 => _46.client, 'access', _47 => _47.chat, 'optionalAccess', _48 => _48.completions, 'optionalAccess', _49 => _49.create])){let m=await this.client.chat.completions.create({...l,stream:!0},p);if(this.log("debug","raw stream completion response: ",m),this.provider==="OAI"&&_optionalChain([l, 'optionalAccess', _50 => _50.stream])&&"stream_options"in l&&m instanceof _streaming.Stream){let[i,c]=m.tee();return h(i),_zodstream.OAIStream.call(void 0, {res:c})}if(this.provider!=="OAI"&&_optionalChain([l, 'optionalAccess', _51 => _51.stream])&&_optionalChain([m, 'optionalAccess', _52 => _52[Symbol.asyncIterator]])){let[i,c]=await I(m,2);return h(i),_zodstream.OAIStream.call(void 0, {res:c})}return _zodstream.OAIStream.call(void 0, {res:m})}else throw new Error("Unsupported client type")},response_model:o});for await(let m of T)yield{...m,_meta:{usage:_nullishCoalesce(O, () => (void 0)),..._nullishCoalesce(_optionalChain([m, 'optionalAccess', _53 => _53._meta]), () => ({}))}}}isChatCompletionCreateParamsWithModel(e){return"response_model"in e}isStandardStream(e){return"stream"in e&&e.stream===!0}};function P(s){let e=new A(s);return new Proxy(e,{get:(r,p,a)=>p in r?Reflect.get(r,p,a):Reflect.get(r.client,p,a)})}function J(s){return typeof s=="object"&&s!==null&&"chat"in s&&typeof s.chat=="object"&&"completions"in s.chat&&typeof s.chat.completions=="object"&&"create"in s.chat.completions&&typeof s.chat.completions.create=="function"}var re=P;exports.default = re;
//# sourceMappingURL=index.cjs.map