openai-streams
Advanced tools
Comparing version 3.0.0 to 3.0.1
@@ -1,1 +0,1 @@ | ||
import{streamArray as n}from"yield-stream";import{ENCODER as p}from"../../globs/shared.js";import{EventStream as f,TokenStream as l}from"../streaming/index.js";const I=async(a,d,{mode:r="tokens",apiKey:s=process.env.OPENAI_API_KEY}={})=>{if(!s)throw new Error("No API key provided. Please set the OPENAI_API_KEY environment variable or pass the { apiKey } option.");const i=a==="completions",t=await fetch(`https://api.openai.com/v1/${a}`,{method:"POST",body:JSON.stringify({...d,stream:i?!0:void 0}),headers:{Authorization:`Bearer ${s}`,"Content-Type":"application/json",Accept:"application/json"}});if(!t.body)throw new Error("No response body");let e;if(i){switch(r){case"tokens":e=l(t.body);break;case"raw":e=f(t.body);break;default:throw new Error(`Invalid mode: ${r}`)}return e.pipeThrough(new TextDecoderStream)}const o=await t.text();switch(r){case"tokens":const m=JSON.parse(o),{text:c}=m.choices?.[0]??{};if(typeof c!="string"){console.error("No text choices received from OpenAI: "+o),e=n([]);break}const h=p.encode(c);e=n([h]);break;case"raw":const w=p.encode(o);e=n([w]);break;default:throw new Error(`Invalid mode: ${r}`)}return e.pipeThrough(new TextDecoderStream)};export{I as OpenAI}; | ||
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}; |
{ | ||
"name": "openai-streams", | ||
"description": "Tools for working with OpenAI streams in Node.js and TypeScript.", | ||
"version": "3.0.0", | ||
"version": "3.0.1", | ||
"license": "MIT", | ||
@@ -6,0 +6,0 @@ "type": "module", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
65042