UNPKG

@instructor-ai/instructor

Version:
3 lines 7.36 kB
import N from"openai";import{Stream as b}from"openai/streaming";import{ZodError as L}from"zod";import D,{OAIResponseParser as k,OAIStream as y,withResponseModel as M}from"zod-stream";import{fromZodError as v}from"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))}import{thinkingJsonParser as w,MODE as _}from"zod-stream";var t=_,E={[t.THINKING_MD_JSON]:w},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=>o.function?.parameters)?{...e,tools:e.tools.map(o=>o.function?.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(this.client.chat?.completions?.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 N))throw new Error("Client does not match the required structure");e instanceof N?this.client=e:this.client=e,this.mode=o,this.debug=r,this.retryAllErrors=a,this.logger=p??void 0;let l=typeof this.client?.baseURL=="string"?this.client?.baseURL.includes(C.ANYSCALE)?n.ANYSCALE:this.client?.baseURL.includes(C.TOGETHER)?n.TOGETHER:this.client?.baseURL.includes(C.OAI)?n.OAI:this.client?.baseURL.includes(C.ANTHROPIC)?n.ANTHROPIC:this.client?.baseURL.includes(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=R?.[this.provider]?.[this.mode],O=M({params:{...r,stream:r.stream??!1},mode:this.mode,response_model:o});h&&(O=h(O));let T=async()=>{let i=O;l?.length>0&&(i={...O,messages:[...O.messages,...d?[d]:[],{role:"user",content:`Please correct the function call; errors encountered: ${l}`}]});let c;try{if(this.client.chat?.completions?.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=(E?.[this.mode]??k)(c);try{let u=f.json??f;return{...JSON.parse(u),_meta:{usage:c?.usage??void 0,thinking:f?.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=v(c.error)?.message,c.error):new Error("Validation failed.");return{...c.data,_meta:i?._meta??{}}}catch(i){if(!this.retryAllErrors&&!(i instanceof L))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=R?.[this.provider]?.[this.mode],l=M({params:{...r,stream:!0},response_model:o,mode:this.mode});a&&(l=a(l));let d=new D({debug: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(this.client.chat?.completions?.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"&&l?.stream&&"stream_options"in l&&m instanceof b){let[i,c]=m.tee();return h(i),y({res:c})}if(this.provider!=="OAI"&&l?.stream&&m?.[Symbol.asyncIterator]){let[i,c]=await I(m,2);return h(i),y({res:c})}return y({res:m})}else throw new Error("Unsupported client type")},response_model:o});for await(let m of T)yield{...m,_meta:{usage:O??void 0,...m?._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;export{re as default}; //# sourceMappingURL=index.js.map