Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
openai-streams
Advanced tools
This library returns OpenAI API responses as streams only. Non-stream endpoints
like edits
etc. are simply a stream with only one chunk update.
Now supports WHATWG and Node.js streams via OpenAI()
and OpenAI.Node()
.
It simplifies the following:
OPENAI_API_KEY
from process.env
.OpenAI(endpoint, ...)
signature.Overall, the library aims to make it as simple to call the API as possible and stream updates in.
yarn add openai-streams
# or
npm i --save openai-streams
Set the OPENAI_API_KEY
env variable (or pass the { apiKey }
option).
The library will throw if it cannot find an API key. Your program will load
this at runtime from process.env.OPENAI_API_KEY
by default, but you may
override this with the { apiKey }
option.
IMPORTANT: For security, you should only load this from a process.env
variable.
await OpenAI(
"completions",
{/* params */},
{ apiKey: process.env.MY_SECRET_API_KEY }
)
Call the API via await OpenAI(endpoint, params)
.
The params
type will be inferred based on the endpoint
you provide, i.e.
for the "edits"
endpoint, import('openai').CreateEditRequest
will be
enforced.
import { OpenAI } from "openai-streams";
export default async function handler() {
const stream = await OpenAI.Node(
"completions",
{
model: "text-davinci-003",
prompt: "Write a sentence.\n\n",
max_tokens: 100
},
);
stream.pipe(res);
}
import { OpenAI } from "openai-streams";
export default async function handler() {
const stream = await OpenAI(
"completions",
{
model: "text-davinci-003",
prompt: "Write a sentence.\n\n",
max_tokens: 100
},
);
return new Response(stream);
}
export const config = {
runtime: "edge"
};
See the example in
example/src/pages/api/hello.ts
.
See also
src/pages/api/demo.ts
in nextjs-openai
.
for await (const chunk of yieldStream(stream)) { ... }
. We recommend following this
pattern if you find it intuitive.FAQs
Tools for working with OpenAI streams in Node.js and TypeScript.
We found that openai-streams demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.