Skip to content
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

TypeError: g is not a constructor #87

Closed
rgarcia opened this issue Jan 25, 2024 · 3 comments · Fixed by #90
Closed

TypeError: g is not a constructor #87

rgarcia opened this issue Jan 25, 2024 · 3 comments · Fixed by #90

Comments

@rgarcia
Copy link

rgarcia commented Jan 25, 2024

Just npm installed the latest version and tried running the extract user streaming example (https://github.com/instructor-ai/instructor-js/blob/99af6d1c8135b5e0be80dbbaddcf9e2cffd5230d/examples/extract_user_stream/index.ts). Got this very cryptic error:

file:///Users/rafaelgarcia/code/finances/node_modules/@instructor-ai/instructor/dist/chunk-JEWMM7IZ.mjs:2
 ${s}`}]}),this.log("debug",t.name,"making completion call with params: ",r);let n=await this.client.chat.completions.create(r),P=u[this.mode],h=P(n);try{return JSON.parse(h)}catch{this.log("error","failed to parse completion",h,this.mode)}},d=async()=>{try{let r=await f(),n=await t.schema.safeParseAsync(r);if(this.log("debug",t.name,"Completion validation: ",n),!n.success)throw"error"in n?(i={role:"assistant",content:JSON.stringify(r)},s=A(n.error)?.message,n.error):new Error("Validation failed.");return n.data}catch(r){if(o<e)return this.log("debug",t.name,"Retrying, attempt: ",o),o++,await d();throw this.log("debug",t.name,"Max attempts reached: ",o),r}};return d()}async chatCompletionStream({max_retries:e,response_model:t,...a}){e&&this.log("warn","max_retries is not supported for streaming completions");let o=C({params:{...a,stream:!0},response_model:t,mode:this.mode});return new g({debug:this.debug??!1}).create({completionPromise:async()=>{let i=await this.client.chat.completions.create(o);return y({res:i})},response_model:t})}isChatCompletionCreateParamsWithModel(e){return"response_model"in e}isStandardStream(e){return"stream"in e&&e.stream===!0}chat={completions:{create:async e=>this.isChatCompletionCreateParamsWithModel(e)?e.stream?this.chatCompletionStream(e):this.chatCompletionStandard(e):this.isStandardStream(e)?await this.client.chat.completions.create(e):await this.client.chat.completions.create(e)}}};function w(m){let e=new c(m);return new Proxy(e,{get:(a,o,s)=>o in a?Reflect.get(a,o,s):Reflect.get(a.client,o,s)})}export{w as a,I as b};
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              ^

TypeError: g is not a constructor
    at c.chatCompletionStream (file:///Users/rafaelgarcia/code/finances/node_modules/@instructor-ai/instructor/dist/chunk-JEWMM7IZ.mjs:2:895)
    at Object.create (file:///Users/rafaelgarcia/code/finances/node_modules/@instructor-ai/instructor/dist/chunk-JEWMM7IZ.mjs:2:1267)
    at <anonymous> (/Users/rafaelgarcia/code/finances/scripts/instructor.ts:54:56)
    at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)
    at async loadESM (node:internal/process/esm_loader:28:7)
    at async handleMainPromise (node:internal/modules/run_main:113:12)

Node.js v20.11.0

Could use some help...and source maps ha.

@roodboi
Copy link
Collaborator

roodboi commented Jan 27, 2024

can you share a reproducible example?

threw up a bare-bones test here - been unable to repro myself

https://github.com/hack-dance/instructor-stream-test/tree/main

@rgarcia
Copy link
Author

rgarcia commented Jan 27, 2024

I'm using tsx, not bun. I was able to repro it in your repo by running

npm install --save-dev tsx
npm run tsx index.ts

The bun way works for me though so I'll just use that

@roodboi roodboi linked a pull request Jan 29, 2024 that will close this issue
@roodboi
Copy link
Collaborator

roodboi commented Jan 29, 2024

ok so - bun should just work - but I dug in a little and tested with both ts-node and TSX - they both have problems figuring out how to resolve es modules further upstream for some reason - I updated those upstream deps to include exports and was a bit more explicit and it now seems to work fine with whatever ts-node, TSX, bun, build + node, whatever

also updating this package to be a bit more explicit as well so we don't run into anything like this again.

Thanks for making the issue!

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

Successfully merging a pull request may close this issue.

2 participants