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

How to use in Node? #33

Closed
joni1802 opened this issue May 11, 2023 · 3 comments
Closed

How to use in Node? #33

joni1802 opened this issue May 11, 2023 · 3 comments

Comments

@joni1802
Copy link

Hello,

I am not quite sure how to use this library in Node. When I try to read data from the stream it says stream.on is not a function. Here is my example code:

import { OpenAI } from "openai-streams";

const stream = OpenAI(
  "chat",
  { model: "gpt-3.5-turbo", messages: [{ role: "user", content: "my question ..." }] },
  { apiKey: "" }
);

stream.on("data", (data) => {
  console.log("DATA:", data);
});

stream.on("end", () => {
  console.log("STREAM END");
});

Am I on the wrong track?

@joni1802
Copy link
Author

Oh I found the solution. You need to import openai-streams/node and await the response. Now I am able to retrive the chunks of the stream.

import { OpenAI } from "openai-streams/node";

const stream = await OpenAI(
  "chat",
  { model: "gpt-3.5-turbo", messages: [{ role: "user", content: "my question ..." }] },
  { apiKey: "" }
);

stream.on("data", (data) => {
  console.log("DATA:", data);
});

stream.on("end", () => {
  console.log("STREAM END");
});

@mattgi
Copy link

mattgi commented May 12, 2023

@joni1802 I am seeing Module '"openai-streams/node"' has no exported member 'OpenAI'. Are you using ESM in your project?

@joni1802
Copy link
Author

@mattgi Yes I am using ESM in my project and using node version v20.0.0. I had no problem importing the module.

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

No branches or pull requests

2 participants