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

@nlux/openai

Package Overview
Dependencies
Maintainers
1
Versions
203
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nlux/openai - npm Package Compare versions

Comparing version 0.1.9-beta to 0.1.10-beta

2

cjs/openai.js

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

"use strict";var e=require("@nlux/nlux"),t=require("openai");const s=Object.freeze({encodeMessage:async e=>({role:"user",content:e}),decodeMessage:async e=>{if(!e.choices||!e.choices[0])throw Error("Invalid payload");const t=e.choices[0].delta.content;if("string"==typeof t)return t}}),a=Object.freeze({encodeMessage:async e=>({role:"user",content:e}),decodeMessage:async e=>{if(!e.choices||!e.choices[0])throw Error("Invalid payload");const t=e.choices[0].message.content;if("string"==typeof t)return t}}),i=Object.freeze({id:"nlux-gpt-adapter",connectionType:"http",capabilities:{textChat:!0,audio:!1,fileUpload:!1,replyToSingleMessage:!1},remote:{url:"https://api.openai.com/v1/chat/completion"},inputFormats:["text"],outputFormats:["text"]});class o{constructor({initialSystemMessage:e,apiKey:s,dataExchangeMode:a,model:i}){var o;this.currentStatus="disconnected",this.dataExchangeMode="fetch",this.initialSystemMessage="Act as a helpful assistant to the user",this.currentStatus="disconnected",this.dataExchangeMode=a,this.openai=new t({apiKey:s,dangerouslyAllowBrowser:!0}),this.model=i,e&&(this.initialSystemMessage=e),"string"==typeof(o='OpenAI GPT adapter has been initialized in browser mode using option "dangerouslyAllowBrowser". This is not recommended for production use. We recommend that you implement a server-side proxy and configure a customized adapter for it. Read more at https://help.openai.com/en/articles/5112595-best-practices-for-api-key-safety')?console.warn(`[nlux-openai] ${o}`):(console.warn("[nlux-openai] Debug:"),console.warn(o))}get id(){return this.info.id}get info(){return i}get status(){return this.currentStatus}async decode(e){const{decodeMessage:t}=this.config;return t(e)}async encode(e){const{encodeMessage:t}=this.config;return t(e)}}class n extends o{constructor({apiKey:e,model:t,initialSystemMessage:s}){super({apiKey:e,model:t,initialSystemMessage:s,dataExchangeMode:"stream"}),void 0!==s&&s.length>0&&(this.initialSystemMessage=s)}get config(){return s}send(t){if("string"!=typeof t||0===t.length)throw new e.NluxUsageError({source:this.constructor.name,message:"Cannot send empty messages"});const s=new e.Observable,a=this.initialSystemMessage?[{role:"system",content:this.initialSystemMessage}]:[];return a.push({role:"user",content:t}),this.openai.chat.completions.create({stream:!0,model:this.model,messages:a}).then((async e=>{let t=e[Symbol.asyncIterator](),a=await t.next();for(;!a.done;){const e=a.value,i=await this.decode(e);void 0!==i&&s.next(i),a=await t.next()}s.complete()})),s}}class r{constructor(e){this.apiKey=null,this.dataExchangeMode="stream",this.initialSystemMessage=null,this.model="gpt-4",this.setApiKeyCalled=!1,this.setInitialSystemMessageCalled=!1,this.setModelCalled=!1,this.setStreamOrFetchCalled=!1,e&&(this.apiKey=e.apiKey,this.dataExchangeMode=e.dataExchangeMode,this.initialSystemMessage=e.initialSystemMessage,this.model=e.model,this.setApiKeyCalled=e.setApiKeyCalled,this.setInitialSystemMessageCalled=e.setInitialSystemMessageCalled,this.setModelCalled=e.setModelCalled,this.setStreamOrFetchCalled=e.setStreamOrFetchCalled)}withApiKey(t){if(this.setApiKeyCalled)throw new e.NluxUsageError({source:this.constructor.name,message:"Unable to set API key. API key setter has already been called by this builder. Make sure you are not calling withApiKey() twice."});return this.apiKey=t,this.setApiKeyCalled=!0,this}withInitialSystemMessage(t){if(this.setInitialSystemMessageCalled)throw new e.NluxUsageError({source:this.constructor.name,message:"Unable to set initial system message. Initial system message setter has already been called by this builder. Make sure you are not calling withInitialSystemMessage() twice."});return this.initialSystemMessage=t??null,this.setInitialSystemMessageCalled=!0,this}withModel(t){if(this.setModelCalled)throw new e.NluxUsageError({source:this.constructor.name,message:"Unable to set model. Model setter has already been called by this builder. Make sure you are not calling withModel() twice."});return this.model=t,this.setModelCalled=!0,this}}class l extends r{constructor(e){super(e),this.dataExchangeMode="stream"}create(){if(!this.apiKey)throw new e.NluxUsageError({source:this.constructor.name,message:"Unable to create ChatGPT adapter. API key is not set. You should call useApiKey() on instance to set the API key, orpass the API key as an option with useAdapter() hook."});return new n({apiKey:this.apiKey,model:this.model,initialSystemMessage:this.initialSystemMessage??void 0})}useFetchingMode(){throw new e.NluxUsageError({source:this.constructor.name,message:"Unable to set data loading mode to fetch. This builder is configured to use the stream API. You cannot change it to use the fetch API."})}useStreamingMode(){throw new e.NluxUsageError({source:this.constructor.name,message:"Unable to set data loading mode to stream. This builder is already configured to use the stream API."})}}exports.GptFetchAdapter=class extends o{constructor({apiKey:e,model:t,initialSystemMessage:s}){super({apiKey:e,model:t,initialSystemMessage:s,dataExchangeMode:"fetch"}),void 0!==s&&s.length>0&&(this.initialSystemMessage=s)}get config(){return a}send(t){if("string"!=typeof t||0===t.length)throw new e.NluxUsageError({source:this.constructor.name,message:"Cannot send empty messages"});const s=new e.Observable,a=this.initialSystemMessage?[{role:"system",content:this.initialSystemMessage}]:[];return a.push({role:"user",content:t}),this.openai.chat.completions.create({stream:!1,model:this.model,messages:a}).then((async e=>{const t=await this.decode(e);void 0!==t&&s.next(t),s.complete()})),s}},exports.GptStreamingAdapter=n,exports.createAdapter=t=>{if("openai/gpt"!==t)throw new e.NluxUsageError({source:"createAdapter",message:"Adapter type not supported"});return new l};
"use strict";var e=require("@nlux/nlux"),t=require("openai");const s=Object.freeze({encodeMessage:async e=>({role:"user",content:e}),decodeMessage:async e=>{if(!e.choices||!e.choices[0])throw Error("Invalid payload");const t=e.choices[0].delta.content;if("string"==typeof t)return t}}),a=Object.freeze({encodeMessage:async e=>({role:"user",content:e}),decodeMessage:async e=>{if(!e.choices||!e.choices[0])throw Error("Invalid payload");const t=e.choices[0].message.content;if("string"==typeof t)return t}}),i=Object.freeze({id:"nlux-gpt-adapter",connectionType:"http",capabilities:{textChat:!0,audio:!1,fileUpload:!1,replyToSingleMessage:!1},remote:{url:"https://api.openai.com/v1/chat/completion"},inputFormats:["text"],outputFormats:["text"]});class r{constructor({initialSystemMessage:e,apiKey:s,dataExchangeMode:a,model:i}){var r;this.currentStatus="disconnected",this.dataExchangeMode="fetch",this.initialSystemMessage="Act as a helpful assistant to the user",this.currentStatus="disconnected",this.dataExchangeMode=a,this.openai=new t({apiKey:s,dangerouslyAllowBrowser:!0}),this.model=i,e&&(this.initialSystemMessage=e),"string"==typeof(r='OpenAI GPT adapter has been initialized in browser mode using option "dangerouslyAllowBrowser". This is not recommended for production use. We recommend that you implement a server-side proxy and configure a customized adapter for it. Read more at https://help.openai.com/en/articles/5112595-best-practices-for-api-key-safety')?console.warn(`[nlux-openai] ${r}`):(console.warn("[nlux-openai] warn:"),console.warn(r))}get id(){return this.info.id}get info(){return i}get status(){return this.currentStatus}async decode(e){const{decodeMessage:t}=this.config;return t(e)}async encode(e){const{encodeMessage:t}=this.config;return t(e)}}class o extends r{constructor({apiKey:e,model:t,initialSystemMessage:s}){super({apiKey:e,model:t,initialSystemMessage:s,dataExchangeMode:"stream"}),void 0!==s&&s.length>0&&(this.initialSystemMessage=s)}get config(){return s}send(t){if("string"!=typeof t||0===t.length)throw new e.NluxUsageError({source:this.constructor.name,message:"Cannot send empty messages"});const s=new e.Observable,a=this.initialSystemMessage?[{role:"system",content:this.initialSystemMessage}]:[];return a.push({role:"user",content:t}),this.openai.chat.completions.create({stream:!0,model:this.model,messages:a}).then((async e=>{let t=e[Symbol.asyncIterator](),a=await t.next();for(;!a.done;){const e=a.value,i=await this.decode(e);void 0!==i&&s.next(i),a=await t.next()}s.complete()})).catch((t=>{s.error(new e.NluxUsageError({source:this.constructor.name,message:t.message}))})),s}}class n{constructor(e){this.apiKey=null,this.dataExchangeMode="stream",this.initialSystemMessage=null,this.model="gpt-4",this.setApiKeyCalled=!1,this.setInitialSystemMessageCalled=!1,this.setModelCalled=!1,this.setStreamOrFetchCalled=!1,e&&(this.apiKey=e.apiKey,this.dataExchangeMode=e.dataExchangeMode,this.initialSystemMessage=e.initialSystemMessage,this.model=e.model,this.setApiKeyCalled=e.setApiKeyCalled,this.setInitialSystemMessageCalled=e.setInitialSystemMessageCalled,this.setModelCalled=e.setModelCalled,this.setStreamOrFetchCalled=e.setStreamOrFetchCalled)}withApiKey(t){if(this.setApiKeyCalled)throw new e.NluxUsageError({source:this.constructor.name,message:"Unable to set API key. API key setter has already been called by this builder. Make sure you are not calling withApiKey() twice."});return this.apiKey=t,this.setApiKeyCalled=!0,this}withInitialSystemMessage(t){if(this.setInitialSystemMessageCalled)throw new e.NluxUsageError({source:this.constructor.name,message:"Unable to set initial system message. Initial system message setter has already been called by this builder. Make sure you are not calling withInitialSystemMessage() twice."});return this.initialSystemMessage=t??null,this.setInitialSystemMessageCalled=!0,this}withModel(t){if(this.setModelCalled)throw new e.NluxUsageError({source:this.constructor.name,message:"Unable to set model. Model setter has already been called by this builder. Make sure you are not calling withModel() twice."});return this.model=t,this.setModelCalled=!0,this}}const l=e=>{if("object"==typeof e&&null!==e){if("invalid_api_key"===e.code)return"NX-NT-002";if(e.message?.toLowerCase().includes("connection error"))return"NX-NT-001"}return null};class c extends r{constructor({apiKey:e,model:t,initialSystemMessage:s}){super({apiKey:e,model:t,initialSystemMessage:s,dataExchangeMode:"fetch"}),void 0!==s&&s.length>0&&(this.initialSystemMessage=s)}get config(){return a}send(t){return new Promise(((s,a)=>{if("string"!=typeof t||0===t.length)throw new e.NluxUsageError({source:this.constructor.name,message:"Cannot send empty messages"});const i=this.initialSystemMessage?[{role:"system",content:this.initialSystemMessage}]:[];i.push({role:"user",content:t}),this.openai.chat.completions.create({stream:!1,model:this.model,messages:i}).then((async t=>{const i=await this.decode(t);"string"!=typeof i?a(new e.NluxUsageError({source:this.constructor.name,message:"Unable to decode response from OpenAI"})):s(i)})).catch((t=>{a(new e.NluxUsageError({source:this.constructor.name,message:t.message,exceptionId:l(t)??void 0}))}))}))}}class h extends n{constructor(e){super(e)}create(){if(!this.apiKey)throw new e.NluxUsageError({source:this.constructor.name,message:"Unable to create ChatGPT adapter. API key is not set. You should call useApiKey() on instance to set the API key, orpass the API key as an option with useAdapter() hook."});return new c({apiKey:this.apiKey,model:this.model,initialSystemMessage:this.initialSystemMessage??void 0})}useFetchingMode(){return this}useStreamingMode(){if(this.setStreamOrFetchCalled)throw new e.NluxUsageError({source:this.constructor.name,message:"Unable to set data loading mode to stream. Stream or fetch setter has already been called by this builder. Make sure you are not calling stream() or fetch() twice."});return this.dataExchangeMode="stream",this.setStreamOrFetchCalled=!0,new d(this)}}class d extends n{constructor(e){super(e)}create(){if(!this.apiKey)throw new e.NluxUsageError({source:this.constructor.name,message:"Unable to create ChatGPT adapter. API key is not set. You should call useApiKey() on instance to set the API key, orpass the API key as an option with useAdapter() hook."});return new o({apiKey:this.apiKey,model:this.model,initialSystemMessage:this.initialSystemMessage??void 0})}useFetchingMode(){if(this.setStreamOrFetchCalled)throw new e.NluxUsageError({source:this.constructor.name,message:"Unable to set data loading mode to fetch. Stream or fetch setter has already been called by this builder. Make sure you are not calling stream() or fetch() twice."});return this.dataExchangeMode="fetch",this.setStreamOrFetchCalled=!0,new h(this)}useStreamingMode(){return this}}exports.GptFetchAdapter=c,exports.GptStreamingAdapter=o,exports.createAdapter=t=>{if("openai/gpt"!==t)throw new e.NluxUsageError({source:"createAdapter",message:"Adapter type not supported"});return new d};

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

import{NluxUsageError as e,Observable as t}from"@nlux/nlux";import s from"openai";const a=Object.freeze({encodeMessage:async e=>({role:"user",content:e}),decodeMessage:async e=>{if(!e.choices||!e.choices[0])throw Error("Invalid payload");const t=e.choices[0].delta.content;if("string"==typeof t)return t}}),i=Object.freeze({encodeMessage:async e=>({role:"user",content:e}),decodeMessage:async e=>{if(!e.choices||!e.choices[0])throw Error("Invalid payload");const t=e.choices[0].message.content;if("string"==typeof t)return t}}),o=Object.freeze({id:"nlux-gpt-adapter",connectionType:"http",capabilities:{textChat:!0,audio:!1,fileUpload:!1,replyToSingleMessage:!1},remote:{url:"https://api.openai.com/v1/chat/completion"},inputFormats:["text"],outputFormats:["text"]});class n{constructor({initialSystemMessage:e,apiKey:t,dataExchangeMode:a,model:i}){var o;this.currentStatus="disconnected",this.dataExchangeMode="fetch",this.initialSystemMessage="Act as a helpful assistant to the user",this.currentStatus="disconnected",this.dataExchangeMode=a,this.openai=new s({apiKey:t,dangerouslyAllowBrowser:!0}),this.model=i,e&&(this.initialSystemMessage=e),"string"==typeof(o='OpenAI GPT adapter has been initialized in browser mode using option "dangerouslyAllowBrowser". This is not recommended for production use. We recommend that you implement a server-side proxy and configure a customized adapter for it. Read more at https://help.openai.com/en/articles/5112595-best-practices-for-api-key-safety')?console.warn(`[nlux-openai] ${o}`):(console.warn("[nlux-openai] Debug:"),console.warn(o))}get id(){return this.info.id}get info(){return o}get status(){return this.currentStatus}async decode(e){const{decodeMessage:t}=this.config;return t(e)}async encode(e){const{encodeMessage:t}=this.config;return t(e)}}class r extends n{constructor({apiKey:e,model:t,initialSystemMessage:s}){super({apiKey:e,model:t,initialSystemMessage:s,dataExchangeMode:"stream"}),void 0!==s&&s.length>0&&(this.initialSystemMessage=s)}get config(){return a}send(s){if("string"!=typeof s||0===s.length)throw new e({source:this.constructor.name,message:"Cannot send empty messages"});const a=new t,i=this.initialSystemMessage?[{role:"system",content:this.initialSystemMessage}]:[];return i.push({role:"user",content:s}),this.openai.chat.completions.create({stream:!0,model:this.model,messages:i}).then((async e=>{let t=e[Symbol.asyncIterator](),s=await t.next();for(;!s.done;){const e=s.value,i=await this.decode(e);void 0!==i&&a.next(i),s=await t.next()}a.complete()})),a}}class l{constructor(e){this.apiKey=null,this.dataExchangeMode="stream",this.initialSystemMessage=null,this.model="gpt-4",this.setApiKeyCalled=!1,this.setInitialSystemMessageCalled=!1,this.setModelCalled=!1,this.setStreamOrFetchCalled=!1,e&&(this.apiKey=e.apiKey,this.dataExchangeMode=e.dataExchangeMode,this.initialSystemMessage=e.initialSystemMessage,this.model=e.model,this.setApiKeyCalled=e.setApiKeyCalled,this.setInitialSystemMessageCalled=e.setInitialSystemMessageCalled,this.setModelCalled=e.setModelCalled,this.setStreamOrFetchCalled=e.setStreamOrFetchCalled)}withApiKey(t){if(this.setApiKeyCalled)throw new e({source:this.constructor.name,message:"Unable to set API key. API key setter has already been called by this builder. Make sure you are not calling withApiKey() twice."});return this.apiKey=t,this.setApiKeyCalled=!0,this}withInitialSystemMessage(t){if(this.setInitialSystemMessageCalled)throw new e({source:this.constructor.name,message:"Unable to set initial system message. Initial system message setter has already been called by this builder. Make sure you are not calling withInitialSystemMessage() twice."});return this.initialSystemMessage=t??null,this.setInitialSystemMessageCalled=!0,this}withModel(t){if(this.setModelCalled)throw new e({source:this.constructor.name,message:"Unable to set model. Model setter has already been called by this builder. Make sure you are not calling withModel() twice."});return this.model=t,this.setModelCalled=!0,this}}class c extends l{constructor(e){super(e),this.dataExchangeMode="stream"}create(){if(!this.apiKey)throw new e({source:this.constructor.name,message:"Unable to create ChatGPT adapter. API key is not set. You should call useApiKey() on instance to set the API key, orpass the API key as an option with useAdapter() hook."});return new r({apiKey:this.apiKey,model:this.model,initialSystemMessage:this.initialSystemMessage??void 0})}useFetchingMode(){throw new e({source:this.constructor.name,message:"Unable to set data loading mode to fetch. This builder is configured to use the stream API. You cannot change it to use the fetch API."})}useStreamingMode(){throw new e({source:this.constructor.name,message:"Unable to set data loading mode to stream. This builder is already configured to use the stream API."})}}const d=t=>{if("openai/gpt"!==t)throw new e({source:"createAdapter",message:"Adapter type not supported"});return new c};class h extends n{constructor({apiKey:e,model:t,initialSystemMessage:s}){super({apiKey:e,model:t,initialSystemMessage:s,dataExchangeMode:"fetch"}),void 0!==s&&s.length>0&&(this.initialSystemMessage=s)}get config(){return i}send(s){if("string"!=typeof s||0===s.length)throw new e({source:this.constructor.name,message:"Cannot send empty messages"});const a=new t,i=this.initialSystemMessage?[{role:"system",content:this.initialSystemMessage}]:[];return i.push({role:"user",content:s}),this.openai.chat.completions.create({stream:!1,model:this.model,messages:i}).then((async e=>{const t=await this.decode(e);void 0!==t&&a.next(t),a.complete()})),a}}export{h as GptFetchAdapter,r as GptStreamingAdapter,d as createAdapter};
import{NluxUsageError as e,Observable as t}from"@nlux/nlux";import s from"openai";const a=Object.freeze({encodeMessage:async e=>({role:"user",content:e}),decodeMessage:async e=>{if(!e.choices||!e.choices[0])throw Error("Invalid payload");const t=e.choices[0].delta.content;if("string"==typeof t)return t}}),i=Object.freeze({encodeMessage:async e=>({role:"user",content:e}),decodeMessage:async e=>{if(!e.choices||!e.choices[0])throw Error("Invalid payload");const t=e.choices[0].message.content;if("string"==typeof t)return t}}),n=Object.freeze({id:"nlux-gpt-adapter",connectionType:"http",capabilities:{textChat:!0,audio:!1,fileUpload:!1,replyToSingleMessage:!1},remote:{url:"https://api.openai.com/v1/chat/completion"},inputFormats:["text"],outputFormats:["text"]});class o{constructor({initialSystemMessage:e,apiKey:t,dataExchangeMode:a,model:i}){var n;this.currentStatus="disconnected",this.dataExchangeMode="fetch",this.initialSystemMessage="Act as a helpful assistant to the user",this.currentStatus="disconnected",this.dataExchangeMode=a,this.openai=new s({apiKey:t,dangerouslyAllowBrowser:!0}),this.model=i,e&&(this.initialSystemMessage=e),"string"==typeof(n='OpenAI GPT adapter has been initialized in browser mode using option "dangerouslyAllowBrowser". This is not recommended for production use. We recommend that you implement a server-side proxy and configure a customized adapter for it. Read more at https://help.openai.com/en/articles/5112595-best-practices-for-api-key-safety')?console.warn(`[nlux-openai] ${n}`):(console.warn("[nlux-openai] warn:"),console.warn(n))}get id(){return this.info.id}get info(){return n}get status(){return this.currentStatus}async decode(e){const{decodeMessage:t}=this.config;return t(e)}async encode(e){const{encodeMessage:t}=this.config;return t(e)}}class r extends o{constructor({apiKey:e,model:t,initialSystemMessage:s}){super({apiKey:e,model:t,initialSystemMessage:s,dataExchangeMode:"stream"}),void 0!==s&&s.length>0&&(this.initialSystemMessage=s)}get config(){return a}send(s){if("string"!=typeof s||0===s.length)throw new e({source:this.constructor.name,message:"Cannot send empty messages"});const a=new t,i=this.initialSystemMessage?[{role:"system",content:this.initialSystemMessage}]:[];return i.push({role:"user",content:s}),this.openai.chat.completions.create({stream:!0,model:this.model,messages:i}).then((async e=>{let t=e[Symbol.asyncIterator](),s=await t.next();for(;!s.done;){const e=s.value,i=await this.decode(e);void 0!==i&&a.next(i),s=await t.next()}a.complete()})).catch((t=>{a.error(new e({source:this.constructor.name,message:t.message}))})),a}}class l{constructor(e){this.apiKey=null,this.dataExchangeMode="stream",this.initialSystemMessage=null,this.model="gpt-4",this.setApiKeyCalled=!1,this.setInitialSystemMessageCalled=!1,this.setModelCalled=!1,this.setStreamOrFetchCalled=!1,e&&(this.apiKey=e.apiKey,this.dataExchangeMode=e.dataExchangeMode,this.initialSystemMessage=e.initialSystemMessage,this.model=e.model,this.setApiKeyCalled=e.setApiKeyCalled,this.setInitialSystemMessageCalled=e.setInitialSystemMessageCalled,this.setModelCalled=e.setModelCalled,this.setStreamOrFetchCalled=e.setStreamOrFetchCalled)}withApiKey(t){if(this.setApiKeyCalled)throw new e({source:this.constructor.name,message:"Unable to set API key. API key setter has already been called by this builder. Make sure you are not calling withApiKey() twice."});return this.apiKey=t,this.setApiKeyCalled=!0,this}withInitialSystemMessage(t){if(this.setInitialSystemMessageCalled)throw new e({source:this.constructor.name,message:"Unable to set initial system message. Initial system message setter has already been called by this builder. Make sure you are not calling withInitialSystemMessage() twice."});return this.initialSystemMessage=t??null,this.setInitialSystemMessageCalled=!0,this}withModel(t){if(this.setModelCalled)throw new e({source:this.constructor.name,message:"Unable to set model. Model setter has already been called by this builder. Make sure you are not calling withModel() twice."});return this.model=t,this.setModelCalled=!0,this}}const c=e=>{if("object"==typeof e&&null!==e){if("invalid_api_key"===e.code)return"NX-NT-002";if(e.message?.toLowerCase().includes("connection error"))return"NX-NT-001"}return null};class h extends o{constructor({apiKey:e,model:t,initialSystemMessage:s}){super({apiKey:e,model:t,initialSystemMessage:s,dataExchangeMode:"fetch"}),void 0!==s&&s.length>0&&(this.initialSystemMessage=s)}get config(){return i}send(t){return new Promise(((s,a)=>{if("string"!=typeof t||0===t.length)throw new e({source:this.constructor.name,message:"Cannot send empty messages"});const i=this.initialSystemMessage?[{role:"system",content:this.initialSystemMessage}]:[];i.push({role:"user",content:t}),this.openai.chat.completions.create({stream:!1,model:this.model,messages:i}).then((async t=>{const i=await this.decode(t);"string"!=typeof i?a(new e({source:this.constructor.name,message:"Unable to decode response from OpenAI"})):s(i)})).catch((t=>{a(new e({source:this.constructor.name,message:t.message,exceptionId:c(t)??void 0}))}))}))}}class d extends l{constructor(e){super(e)}create(){if(!this.apiKey)throw new e({source:this.constructor.name,message:"Unable to create ChatGPT adapter. API key is not set. You should call useApiKey() on instance to set the API key, orpass the API key as an option with useAdapter() hook."});return new h({apiKey:this.apiKey,model:this.model,initialSystemMessage:this.initialSystemMessage??void 0})}useFetchingMode(){return this}useStreamingMode(){if(this.setStreamOrFetchCalled)throw new e({source:this.constructor.name,message:"Unable to set data loading mode to stream. Stream or fetch setter has already been called by this builder. Make sure you are not calling stream() or fetch() twice."});return this.dataExchangeMode="stream",this.setStreamOrFetchCalled=!0,new m(this)}}class m extends l{constructor(e){super(e)}create(){if(!this.apiKey)throw new e({source:this.constructor.name,message:"Unable to create ChatGPT adapter. API key is not set. You should call useApiKey() on instance to set the API key, orpass the API key as an option with useAdapter() hook."});return new r({apiKey:this.apiKey,model:this.model,initialSystemMessage:this.initialSystemMessage??void 0})}useFetchingMode(){if(this.setStreamOrFetchCalled)throw new e({source:this.constructor.name,message:"Unable to set data loading mode to fetch. Stream or fetch setter has already been called by this builder. Make sure you are not calling stream() or fetch() twice."});return this.dataExchangeMode="fetch",this.setStreamOrFetchCalled=!0,new d(this)}useStreamingMode(){return this}}const u=t=>{if("openai/gpt"!==t)throw new e({source:"createAdapter",message:"Adapter type not supported"});return new m};export{h as GptFetchAdapter,r as GptStreamingAdapter,u as createAdapter};

@@ -24,3 +24,3 @@ import { ISseAdapter, AdapterStatus, AdapterConfig, AdapterInfo, Message, Observable, AdapterBuilder, Adapter } from '@nlux/nlux';

encode(message: Message): Promise<OutboundPayload>;
abstract send(message: Message): Observable<Message>;
abstract send(message: Message): Observable<Message> | Promise<Message>;
}

@@ -38,3 +38,3 @@

}>;
send(message: Message): Observable<Message>;
send(message: Message): Promise<Message>;
}

@@ -73,12 +73,5 @@

declare class OpenAIGptBuilder extends OpenAIGptAbstractBuilder {
constructor();
create(): GptFetchAdapter | GptStreamingAdapter;
useFetchingMode(): OpenAIGptAbstractBuilder;
useStreamingMode(): OpenAIGptAbstractBuilder;
}
type ProvidedAdapterType = 'openai/gpt';
declare const createAdapter: (adapterType: ProvidedAdapterType) => OpenAIGptBuilder;
declare const createAdapter: (adapterType: ProvidedAdapterType) => OpenAIGptAbstractBuilder;
export { GptFetchAdapter, GptStreamingAdapter, type OpenAIChatModel, createAdapter };
{
"name": "@nlux/openai",
"version": "0.1.9-beta",
"version": "0.1.10-beta",
"description": "The OpenAI adapters for NLUX, the javascript library for building conversational AI interfaces.",

@@ -57,3 +57,3 @@ "keywords": [

"peerDependencies": {
"@nlux/nlux": "0.1.9-beta"
"@nlux/nlux": "0.1.10-beta"
},

@@ -60,0 +60,0 @@ "main": "index.js",

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

!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@nlux/nlux"),require("openai")):"function"==typeof define&&define.amd?define(["exports","@nlux/nlux","openai"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["@nlux/openai"]={},e.nlux,e.OpenAI)}(this,(function(e,t,s){"use strict";const a=Object.freeze({encodeMessage:async e=>({role:"user",content:e}),decodeMessage:async e=>{if(!e.choices||!e.choices[0])throw Error("Invalid payload");const t=e.choices[0].delta.content;if("string"==typeof t)return t}}),i=Object.freeze({encodeMessage:async e=>({role:"user",content:e}),decodeMessage:async e=>{if(!e.choices||!e.choices[0])throw Error("Invalid payload");const t=e.choices[0].message.content;if("string"==typeof t)return t}}),o=Object.freeze({id:"nlux-gpt-adapter",connectionType:"http",capabilities:{textChat:!0,audio:!1,fileUpload:!1,replyToSingleMessage:!1},remote:{url:"https://api.openai.com/v1/chat/completion"},inputFormats:["text"],outputFormats:["text"]});class n{constructor({initialSystemMessage:e,apiKey:t,dataExchangeMode:a,model:i}){var o;this.currentStatus="disconnected",this.dataExchangeMode="fetch",this.initialSystemMessage="Act as a helpful assistant to the user",this.currentStatus="disconnected",this.dataExchangeMode=a,this.openai=new s({apiKey:t,dangerouslyAllowBrowser:!0}),this.model=i,e&&(this.initialSystemMessage=e),"string"==typeof(o='OpenAI GPT adapter has been initialized in browser mode using option "dangerouslyAllowBrowser". This is not recommended for production use. We recommend that you implement a server-side proxy and configure a customized adapter for it. Read more at https://help.openai.com/en/articles/5112595-best-practices-for-api-key-safety')?console.warn(`[nlux-openai] ${o}`):(console.warn("[nlux-openai] Debug:"),console.warn(o))}get id(){return this.info.id}get info(){return o}get status(){return this.currentStatus}async decode(e){const{decodeMessage:t}=this.config;return t(e)}async encode(e){const{encodeMessage:t}=this.config;return t(e)}}class r extends n{constructor({apiKey:e,model:t,initialSystemMessage:s}){super({apiKey:e,model:t,initialSystemMessage:s,dataExchangeMode:"stream"}),void 0!==s&&s.length>0&&(this.initialSystemMessage=s)}get config(){return a}send(e){if("string"!=typeof e||0===e.length)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot send empty messages"});const s=new t.Observable,a=this.initialSystemMessage?[{role:"system",content:this.initialSystemMessage}]:[];return a.push({role:"user",content:e}),this.openai.chat.completions.create({stream:!0,model:this.model,messages:a}).then((async e=>{let t=e[Symbol.asyncIterator](),a=await t.next();for(;!a.done;){const e=a.value,i=await this.decode(e);void 0!==i&&s.next(i),a=await t.next()}s.complete()})),s}}class l{constructor(e){this.apiKey=null,this.dataExchangeMode="stream",this.initialSystemMessage=null,this.model="gpt-4",this.setApiKeyCalled=!1,this.setInitialSystemMessageCalled=!1,this.setModelCalled=!1,this.setStreamOrFetchCalled=!1,e&&(this.apiKey=e.apiKey,this.dataExchangeMode=e.dataExchangeMode,this.initialSystemMessage=e.initialSystemMessage,this.model=e.model,this.setApiKeyCalled=e.setApiKeyCalled,this.setInitialSystemMessageCalled=e.setInitialSystemMessageCalled,this.setModelCalled=e.setModelCalled,this.setStreamOrFetchCalled=e.setStreamOrFetchCalled)}withApiKey(e){if(this.setApiKeyCalled)throw new t.NluxUsageError({source:this.constructor.name,message:"Unable to set API key. API key setter has already been called by this builder. Make sure you are not calling withApiKey() twice."});return this.apiKey=e,this.setApiKeyCalled=!0,this}withInitialSystemMessage(e){if(this.setInitialSystemMessageCalled)throw new t.NluxUsageError({source:this.constructor.name,message:"Unable to set initial system message. Initial system message setter has already been called by this builder. Make sure you are not calling withInitialSystemMessage() twice."});return this.initialSystemMessage=e??null,this.setInitialSystemMessageCalled=!0,this}withModel(e){if(this.setModelCalled)throw new t.NluxUsageError({source:this.constructor.name,message:"Unable to set model. Model setter has already been called by this builder. Make sure you are not calling withModel() twice."});return this.model=e,this.setModelCalled=!0,this}}class c extends l{constructor(e){super(e),this.dataExchangeMode="stream"}create(){if(!this.apiKey)throw new t.NluxUsageError({source:this.constructor.name,message:"Unable to create ChatGPT adapter. API key is not set. You should call useApiKey() on instance to set the API key, orpass the API key as an option with useAdapter() hook."});return new r({apiKey:this.apiKey,model:this.model,initialSystemMessage:this.initialSystemMessage??void 0})}useFetchingMode(){throw new t.NluxUsageError({source:this.constructor.name,message:"Unable to set data loading mode to fetch. This builder is configured to use the stream API. You cannot change it to use the fetch API."})}useStreamingMode(){throw new t.NluxUsageError({source:this.constructor.name,message:"Unable to set data loading mode to stream. This builder is already configured to use the stream API."})}}e.GptFetchAdapter=class extends n{constructor({apiKey:e,model:t,initialSystemMessage:s}){super({apiKey:e,model:t,initialSystemMessage:s,dataExchangeMode:"fetch"}),void 0!==s&&s.length>0&&(this.initialSystemMessage=s)}get config(){return i}send(e){if("string"!=typeof e||0===e.length)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot send empty messages"});const s=new t.Observable,a=this.initialSystemMessage?[{role:"system",content:this.initialSystemMessage}]:[];return a.push({role:"user",content:e}),this.openai.chat.completions.create({stream:!1,model:this.model,messages:a}).then((async e=>{const t=await this.decode(e);void 0!==t&&s.next(t),s.complete()})),s}},e.GptStreamingAdapter=r,e.createAdapter=e=>{if("openai/gpt"!==e)throw new t.NluxUsageError({source:"createAdapter",message:"Adapter type not supported"});return new c}}));
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@nlux/nlux"),require("openai")):"function"==typeof define&&define.amd?define(["exports","@nlux/nlux","openai"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["@nlux/openai"]={},e.nlux,e.OpenAI)}(this,(function(e,t,s){"use strict";const a=Object.freeze({encodeMessage:async e=>({role:"user",content:e}),decodeMessage:async e=>{if(!e.choices||!e.choices[0])throw Error("Invalid payload");const t=e.choices[0].delta.content;if("string"==typeof t)return t}}),i=Object.freeze({encodeMessage:async e=>({role:"user",content:e}),decodeMessage:async e=>{if(!e.choices||!e.choices[0])throw Error("Invalid payload");const t=e.choices[0].message.content;if("string"==typeof t)return t}}),o=Object.freeze({id:"nlux-gpt-adapter",connectionType:"http",capabilities:{textChat:!0,audio:!1,fileUpload:!1,replyToSingleMessage:!1},remote:{url:"https://api.openai.com/v1/chat/completion"},inputFormats:["text"],outputFormats:["text"]});class n{constructor({initialSystemMessage:e,apiKey:t,dataExchangeMode:a,model:i}){var o;this.currentStatus="disconnected",this.dataExchangeMode="fetch",this.initialSystemMessage="Act as a helpful assistant to the user",this.currentStatus="disconnected",this.dataExchangeMode=a,this.openai=new s({apiKey:t,dangerouslyAllowBrowser:!0}),this.model=i,e&&(this.initialSystemMessage=e),"string"==typeof(o='OpenAI GPT adapter has been initialized in browser mode using option "dangerouslyAllowBrowser". This is not recommended for production use. We recommend that you implement a server-side proxy and configure a customized adapter for it. Read more at https://help.openai.com/en/articles/5112595-best-practices-for-api-key-safety')?console.warn(`[nlux-openai] ${o}`):(console.warn("[nlux-openai] warn:"),console.warn(o))}get id(){return this.info.id}get info(){return o}get status(){return this.currentStatus}async decode(e){const{decodeMessage:t}=this.config;return t(e)}async encode(e){const{encodeMessage:t}=this.config;return t(e)}}class r extends n{constructor({apiKey:e,model:t,initialSystemMessage:s}){super({apiKey:e,model:t,initialSystemMessage:s,dataExchangeMode:"stream"}),void 0!==s&&s.length>0&&(this.initialSystemMessage=s)}get config(){return a}send(e){if("string"!=typeof e||0===e.length)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot send empty messages"});const s=new t.Observable,a=this.initialSystemMessage?[{role:"system",content:this.initialSystemMessage}]:[];return a.push({role:"user",content:e}),this.openai.chat.completions.create({stream:!0,model:this.model,messages:a}).then((async e=>{let t=e[Symbol.asyncIterator](),a=await t.next();for(;!a.done;){const e=a.value,i=await this.decode(e);void 0!==i&&s.next(i),a=await t.next()}s.complete()})).catch((e=>{s.error(new t.NluxUsageError({source:this.constructor.name,message:e.message}))})),s}}class l{constructor(e){this.apiKey=null,this.dataExchangeMode="stream",this.initialSystemMessage=null,this.model="gpt-4",this.setApiKeyCalled=!1,this.setInitialSystemMessageCalled=!1,this.setModelCalled=!1,this.setStreamOrFetchCalled=!1,e&&(this.apiKey=e.apiKey,this.dataExchangeMode=e.dataExchangeMode,this.initialSystemMessage=e.initialSystemMessage,this.model=e.model,this.setApiKeyCalled=e.setApiKeyCalled,this.setInitialSystemMessageCalled=e.setInitialSystemMessageCalled,this.setModelCalled=e.setModelCalled,this.setStreamOrFetchCalled=e.setStreamOrFetchCalled)}withApiKey(e){if(this.setApiKeyCalled)throw new t.NluxUsageError({source:this.constructor.name,message:"Unable to set API key. API key setter has already been called by this builder. Make sure you are not calling withApiKey() twice."});return this.apiKey=e,this.setApiKeyCalled=!0,this}withInitialSystemMessage(e){if(this.setInitialSystemMessageCalled)throw new t.NluxUsageError({source:this.constructor.name,message:"Unable to set initial system message. Initial system message setter has already been called by this builder. Make sure you are not calling withInitialSystemMessage() twice."});return this.initialSystemMessage=e??null,this.setInitialSystemMessageCalled=!0,this}withModel(e){if(this.setModelCalled)throw new t.NluxUsageError({source:this.constructor.name,message:"Unable to set model. Model setter has already been called by this builder. Make sure you are not calling withModel() twice."});return this.model=e,this.setModelCalled=!0,this}}const c=e=>{if("object"==typeof e&&null!==e){if("invalid_api_key"===e.code)return"NX-NT-002";if(e.message?.toLowerCase().includes("connection error"))return"NX-NT-001"}return null};class h extends n{constructor({apiKey:e,model:t,initialSystemMessage:s}){super({apiKey:e,model:t,initialSystemMessage:s,dataExchangeMode:"fetch"}),void 0!==s&&s.length>0&&(this.initialSystemMessage=s)}get config(){return i}send(e){return new Promise(((s,a)=>{if("string"!=typeof e||0===e.length)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot send empty messages"});const i=this.initialSystemMessage?[{role:"system",content:this.initialSystemMessage}]:[];i.push({role:"user",content:e}),this.openai.chat.completions.create({stream:!1,model:this.model,messages:i}).then((async e=>{const i=await this.decode(e);"string"!=typeof i?a(new t.NluxUsageError({source:this.constructor.name,message:"Unable to decode response from OpenAI"})):s(i)})).catch((e=>{a(new t.NluxUsageError({source:this.constructor.name,message:e.message,exceptionId:c(e)??void 0}))}))}))}}class d extends l{constructor(e){super(e)}create(){if(!this.apiKey)throw new t.NluxUsageError({source:this.constructor.name,message:"Unable to create ChatGPT adapter. API key is not set. You should call useApiKey() on instance to set the API key, orpass the API key as an option with useAdapter() hook."});return new h({apiKey:this.apiKey,model:this.model,initialSystemMessage:this.initialSystemMessage??void 0})}useFetchingMode(){return this}useStreamingMode(){if(this.setStreamOrFetchCalled)throw new t.NluxUsageError({source:this.constructor.name,message:"Unable to set data loading mode to stream. Stream or fetch setter has already been called by this builder. Make sure you are not calling stream() or fetch() twice."});return this.dataExchangeMode="stream",this.setStreamOrFetchCalled=!0,new u(this)}}class u extends l{constructor(e){super(e)}create(){if(!this.apiKey)throw new t.NluxUsageError({source:this.constructor.name,message:"Unable to create ChatGPT adapter. API key is not set. You should call useApiKey() on instance to set the API key, orpass the API key as an option with useAdapter() hook."});return new r({apiKey:this.apiKey,model:this.model,initialSystemMessage:this.initialSystemMessage??void 0})}useFetchingMode(){if(this.setStreamOrFetchCalled)throw new t.NluxUsageError({source:this.constructor.name,message:"Unable to set data loading mode to fetch. Stream or fetch setter has already been called by this builder. Make sure you are not calling stream() or fetch() twice."});return this.dataExchangeMode="fetch",this.setStreamOrFetchCalled=!0,new d(this)}useStreamingMode(){return this}}e.GptFetchAdapter=h,e.GptStreamingAdapter=r,e.createAdapter=e=>{if("openai/gpt"!==e)throw new t.NluxUsageError({source:"createAdapter",message:"Adapter type not supported"});return new u}}));
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