openai-streams
Advanced tools
Comparing version 1.0.17 to 1.0.18
@@ -1,1 +0,1 @@ | ||
import{streamArray as t}from"yield-stream";import{ENCODER as p}from"../../globs/index.js";import{EventStream as h,TokenStream as A}from"../streaming/index.js";const l=async(n,d,{mode:e="tokens",envKey:s="OPENAI_API_KEY"}={})=>{const c=process.env[s];if(!c)throw new Error(`No OpenAI API key found in ${s}.`);const a=n==="completions",o=await fetch(`https://api.openai.com/v1/${n}`,{method:"POST",body:JSON.stringify({...d,stream:a?!0:void 0}),headers:{Authorization:`Bearer ${c}`,"Content-Type":"application/json",Accept:"application/json"}});if(!o.body)throw new Error("No response body");if(a)switch(e){case"tokens":return A(o.body);case"raw":return h(o.body);default:throw new Error(`Invalid mode: ${e}`)}const r=await o.text();switch(e){case"tokens":const f=JSON.parse(r),{text:i}=f.choices?.[0]??{};if(typeof i!="string")return console.error("No text choices received from OpenAI: "+r),t([]);const m=p.encode(i);return t([m]);case"raw":const w=p.encode(r);return t([w]);default:throw new Error(`Invalid mode: ${e}`)}};export{l as OpenAI}; | ||
import{streamArray as t}from"yield-stream";import{ENCODER as c}from"../../globs/index.js";import{EventStream as h,TokenStream as w}from"../streaming/index.js";const E=async(n,p,{mode:e="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 a=n==="completions",o=await fetch(`https://api.openai.com/v1/${n}`,{method:"POST",body:JSON.stringify({...p,stream:a?!0:void 0}),headers:{Authorization:`Bearer ${s}`,"Content-Type":"application/json",Accept:"application/json"}});if(!o.body)throw new Error("No response body");if(a)switch(e){case"tokens":return w(o.body);case"raw":return h(o.body);default:throw new Error(`Invalid mode: ${e}`)}const r=await o.text();switch(e){case"tokens":const d=JSON.parse(r),{text:i}=d.choices?.[0]??{};if(typeof i!="string")return console.error("No text choices received from OpenAI: "+r),t([]);const m=c.encode(i);return t([m]);case"raw":const f=c.encode(r);return t([f]);default:throw new Error(`Invalid mode: ${e}`)}};export{E as OpenAI}; |
@@ -7,7 +7,6 @@ import type { CreateCompletionRequest, CreateEditRequest, CreateEmbeddingRequest, CreateFineTuneRequest, CreateImageRequest } from "./pinned"; | ||
/** | ||
* The name of the environment variable that contains the OpenAI API key. We | ||
* will not let you pass a raw string, only the name to read from process.env, | ||
* e.g. `"MY_SECRET_KEY"` will read from `process.env.MY_SECRET_KEY`. | ||
* By default, the API key is read from the OPENAI_API_KEY environment | ||
* variable. You can override this by passing a different key here. | ||
*/ | ||
envKey?: string; | ||
apiKey?: string; | ||
/** | ||
@@ -14,0 +13,0 @@ * Whether to return tokens or raw events. |
{ | ||
"name": "openai-streams", | ||
"description": "Streams-first OpenAI API client, written in TypeScript.", | ||
"version": "1.0.17", | ||
"version": "1.0.18", | ||
"license": "MIT", | ||
@@ -6,0 +6,0 @@ "type": "module", |
@@ -32,8 +32,11 @@ # OpenAI Streams | ||
1. **Set the `OPENAI_API_KEY` env variable** (or pass the `{ envKey }` option). | ||
1. **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 which environment variable is loaded with the `envKey` option. | ||
override this with the `{ apiKey }` option. | ||
**IMPORTANT:** For security, you should only load this from a `process.env` | ||
variable. | ||
```ts | ||
@@ -43,3 +46,3 @@ await OpenAI( | ||
{/* ... */}, | ||
{ envKey: "MY_SECRET_OPENAI_KEY" } | ||
{ apiKey: process.env.MY_SECRET_API_KEY } | ||
) | ||
@@ -46,0 +49,0 @@ ``` |
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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
63058
80