openai-streams
Advanced tools
Comparing version 4.1.0 to 4.2.0
@@ -1,1 +0,1 @@ | ||
import{ENCODER as S,DECODER as f}from"../../globs/shared.js";import{ChatParser as d,TokenParser as O}from"./transforms.js";import{createParser as A}from"eventsource-parser";import{pipeline as m,yieldStream as h}from"yield-stream";import{OpenAIError as y}from"../errors.js";const s=(e,{mode:r="tokens"})=>new ReadableStream({async start(a){const p=A(t=>{if(t.type==="event"){const{data:n}=t;if(n==="[DONE]"){a.close();return}try{const o=JSON.parse(n);if(a.enqueue(S.encode(n)),r==="tokens"&&o?.choices){const{choices:i}=o;for(const c of i)if(c?.finish_reason==="length")throw new y("MAX_TOKENS")}}catch(o){a.error(o)}}});for await(const t of h(e))p.feed(f.decode(t))}}),C=(e,r={mode:"tokens"})=>m(s(e,r),O),D=(e,r={mode:"tokens"})=>m(s(e,r),d);export{D as ChatStream,s as EventStream,C as TokenStream}; | ||
import{ENCODER as d,DECODER as S}from"../../globs/shared.js";import{ChatParser as f,TokenParser as O}from"./transforms.js";import{createParser as h}from"eventsource-parser";import{pipeline as s,yieldStream as y}from"yield-stream";import{OpenAIError as A}from"../errors.js";const m=(t,{mode:o="tokens"})=>new ReadableStream({async start(a){const p=h(n=>{if(n.type==="event"){const{data:r}=n;if(r==="[DONE]"){a.close();return}try{const e=JSON.parse(r);if(a.enqueue(d.encode(r)),o==="tokens"&&e?.choices){const{choices:i}=e;for(const c of i)if(c?.finish_reason==="length")throw new A("MAX_TOKENS")}}catch(e){a.error(e)}}});for await(const n of y(t)){const r=S.decode(n);try{const e=JSON.parse(r);e.hasOwnProperty("error")&&a.error(new Error(e.error.message))}catch{}p.feed(r)}}}),R=(t,o={mode:"tokens"})=>s(m(t,o),O),C=(t,o={mode:"tokens"})=>s(m(t,o),f);export{C as ChatStream,m as EventStream,R as TokenStream}; |
{ | ||
"name": "openai-streams", | ||
"description": "Tools for working with OpenAI streams in Node.js and TypeScript.", | ||
"version": "4.1.0", | ||
"version": "4.2.0", | ||
"license": "MIT", | ||
@@ -6,0 +6,0 @@ "type": "module", |
77234
2111