Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

openai-streams

Package Overview
Dependencies
Maintainers
1
Versions
105
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

openai-streams - npm Package Compare versions

Comparing version 3.0.1 to 3.0.2

2

dist/lib/openai/edge.js

@@ -1,1 +0,1 @@

import{streamArray as n}from"yield-stream";import{ENCODER as d}from"../../globs/shared.js";import{EventStream as l,TokenStream as w}from"../streaming/index.js";const O=async(s,m,{mode:o="tokens",apiKey:a=process.env.OPENAI_API_KEY}={})=>{if(!a)throw new Error("No API key provided. Please set the OPENAI_API_KEY environment variable or pass the { apiKey } option.");const i=s==="completions",t=await fetch(`https://api.openai.com/v1/${s}`,{method:"POST",body:JSON.stringify({...m,stream:i?!0:void 0}),headers:{Authorization:`Bearer ${a}`,"Content-Type":"application/json",Accept:"application/json"}});if(!t.body)throw new Error("No response body");const c=new TextDecoderStream;let e;if(i){switch(o){case"tokens":e=w(t.body);break;case"raw":e=l(t.body);break;default:throw new Error(`Invalid mode: ${o}`)}return e.pipeThrough(c)}const r=await t.text();switch(o){case"tokens":const h=JSON.parse(r),{text:p}=h.choices?.[0]??{};if(typeof p!="string"){console.error("No text choices received from OpenAI: "+r),e=n([]);break}const E=d.encode(p);e=n([E]);break;case"raw":const f=d.encode(r);e=n([f]);break;default:throw new Error(`Invalid mode: ${o}`)}return e.pipeThrough(c)};export{O as OpenAI};
import{generateStream as h,streamArray as a,yieldStream as w}from"yield-stream";import{ENCODER as p}from"../../globs/shared.js";import{EventStream as y,TokenStream as E}from"../streaming/index.js";const O=async(s,m,{mode:o="tokens",apiKey:i=process.env.OPENAI_API_KEY}={})=>{if(!i)throw new Error("No API key provided. Please set the OPENAI_API_KEY environment variable or pass the { apiKey } option.");const c=s==="completions",r=await fetch(`https://api.openai.com/v1/${s}`,{method:"POST",body:JSON.stringify({...m,stream:c?!0:void 0}),headers:{Authorization:`Bearer ${i}`,"Content-Type":"application/json",Accept:"application/json"}});if(!r.body)throw new Error("No response body");let e;if(c)switch(o){case"tokens":e=E(r.body);break;case"raw":e=y(r.body);break;default:throw new Error(`Invalid mode: ${o}`)}else{const t=await r.text();switch(o){case"tokens":const n=JSON.parse(t),{text:d}=n.choices?.[0]??{};if(typeof d!="string"){console.error("No text choices received from OpenAI: "+t),e=a([]);break}const f=p.encode(d);e=a([f]);break;case"raw":const l=p.encode(t);e=a([l]);break;default:throw new Error(`Invalid mode: ${o}`)}}return h(async function*(){const t=new TextDecoder;for await(const n of w(e))yield t.decode(n)})};export{O as OpenAI};
{
"name": "openai-streams",
"description": "Tools for working with OpenAI streams in Node.js and TypeScript.",
"version": "3.0.1",
"version": "3.0.2",
"license": "MIT",

@@ -6,0 +6,0 @@ "type": "module",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc