@nlux/openai
Advanced tools
Comparing version 0.1.7-beta to 0.1.8-beta
@@ -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-gpt4-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({actAs:e,apiKey:s,timeout:a,historyDepthToInclude:i,dataExchangeMode:r}){this.actAsMessage="Act as a helpful assistant to the user",this.currentStatus="disconnected",this.dataExchangeMode="fetch",this.currentStatus="disconnected",this.historyDepthToInclude=i??2,this.timeout=a,this.dataExchangeMode=r,this.openai=new t({apiKey:s,dangerouslyAllowBrowser:!0}),e&&(this.actAsMessage=e),console.warn('GPT-4 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')}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({actAs:e,apiKey:t,timeout:s,historyDepthToInclude:a}){super({actAs:e,apiKey:t,timeout:s,historyDepthToInclude:a,dataExchangeMode:"fetch"})}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.actAsMessage?[{role:"system",content:this.actAsMessage}]:[];return a.push({role:"user",content:t}),this.openai.chat.completions.create({stream:!1,model:"gpt-4",messages:a}).then((async e=>{const t=await this.decode(e);void 0!==t&&s.next(t),s.complete()})),s}}const n=class t{constructor(e){this.apiKey=null,this.dataExchangeMode="fetch",this.historyDepth="max",this.timeout=t.defaultTimeout,this.initialSystemMessage=null,this.setApiKeyCalled=!1,this.setHistoryDepthCalled=!1,this.setInitialSystemMessageCalled=!1,this.setStreamOrFetchCalled=!1,this.setTimeOutCalled=!1,e&&(this.apiKey=e.apiKey,this.dataExchangeMode=e.dataExchangeMode,this.historyDepth=e.historyDepth,this.timeout=e.timeout,this.setApiKeyCalled=e.setApiKeyCalled,this.setHistoryDepthCalled=e.setHistoryDepthCalled,this.setStreamOrFetchCalled=e.setStreamOrFetchCalled,this.setTimeOutCalled=e.setTimeOutCalled)}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 useApiKey() twice."});return this.apiKey=t,this.setApiKeyCalled=!0,this}withHistoryDepth(t="max"){if(this.setHistoryDepthCalled)throw new e.NluxUsageError({source:this.constructor.name,message:"Unable to set history depth. History depth setter has already been called by this builder. Make sure you are not calling includeHistoryDepth() twice."});return this.historyDepth=t,this.setHistoryDepthCalled=!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}withTimeout(t){if(this.setTimeOutCalled)throw new e.NluxUsageError({source:this.constructor.name,message:"Unable to set timeout. Timeout setter has already been called by this builder. Make sure you are not calling withTimeout() twice."});return this.timeout=t,this.setTimeOutCalled=!0,this}};n.defaultTimeout=3e4;let h=n;class c extends r{constructor({actAs:e,apiKey:t,timeout:s,historyDepthToInclude:a,initialSystemMessage:i}){super({actAs:e,apiKey:t,timeout:s,historyDepthToInclude:a,dataExchangeMode:"fetch"}),this.initialSystemMessage=null,this.shouldSendInitialSystemMessage=!1,void 0!==i&&i.length>0&&(this.initialSystemMessage=i,this.shouldSendInitialSystemMessage=!0)}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.actAsMessage?[{role:"system",content:this.actAsMessage}]:[];return console.dir(this.shouldSendInitialSystemMessage),this.shouldSendInitialSystemMessage&&(a.push({role:"system",content:this.initialSystemMessage}),this.shouldSendInitialSystemMessage=!1),a.push({role:"user",content:t}),this.openai.chat.completions.create({stream:!0,model:"gpt-4",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 extends h{constructor(e){super(e),this.dataExchangeMode="stream"}create(){if(!this.apiKey)throw new e.NluxUsageError({source:this.constructor.name,message:"Unable to create GPT4 adapter. API key is not set. You should call useApiKey() on instance to set the API key."});return new c({apiKey:this.apiKey,timeout:this.timeout,historyDepthToInclude:this.historyDepth,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."})}}class u extends h{constructor(){super()}create(){if(!this.apiKey)throw new e.NluxUsageError({source:this.constructor.name,message:"Unable to create GPT4 adapter. API key is not set. You should call useApiKey() on instance to set the API key."});if("stream"===this.dataExchangeMode)throw new e.NluxUsageError({source:this.constructor.name,message:"Unable to create GPT4 adapter. This builder is configured to use the stream API because useStreamingMode() was called. You should call use the builder instance returned by useStreamingMode() to create the builder."});return new o({apiKey:this.apiKey,timeout:this.timeout,historyDepthToInclude:this.historyDepth})}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,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 l(this)}}exports.Gpt4FetchAdapter=o,exports.Gpt4StreamingAdapter=c,exports.createAdapter=t=>{if("openai/gpt4"!==t)throw new e.NluxUsageError({source:"createAdapter",message:"Adapter type not supported"});return new u}; | ||
"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}; |
@@ -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-gpt4-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({actAs:e,apiKey:t,timeout:a,historyDepthToInclude:i,dataExchangeMode:o}){this.actAsMessage="Act as a helpful assistant to the user",this.currentStatus="disconnected",this.dataExchangeMode="fetch",this.currentStatus="disconnected",this.historyDepthToInclude=i??2,this.timeout=a,this.dataExchangeMode=o,this.openai=new s({apiKey:t,dangerouslyAllowBrowser:!0}),e&&(this.actAsMessage=e),console.warn('GPT-4 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')}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({actAs:e,apiKey:t,timeout:s,historyDepthToInclude:a}){super({actAs:e,apiKey:t,timeout:s,historyDepthToInclude:a,dataExchangeMode:"fetch"})}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.actAsMessage?[{role:"system",content:this.actAsMessage}]:[];return i.push({role:"user",content:s}),this.openai.chat.completions.create({stream:!1,model:"gpt-4",messages:i}).then((async e=>{const t=await this.decode(e);void 0!==t&&a.next(t),a.complete()})),a}}const h=class t{constructor(e){this.apiKey=null,this.dataExchangeMode="fetch",this.historyDepth="max",this.timeout=t.defaultTimeout,this.initialSystemMessage=null,this.setApiKeyCalled=!1,this.setHistoryDepthCalled=!1,this.setInitialSystemMessageCalled=!1,this.setStreamOrFetchCalled=!1,this.setTimeOutCalled=!1,e&&(this.apiKey=e.apiKey,this.dataExchangeMode=e.dataExchangeMode,this.historyDepth=e.historyDepth,this.timeout=e.timeout,this.setApiKeyCalled=e.setApiKeyCalled,this.setHistoryDepthCalled=e.setHistoryDepthCalled,this.setStreamOrFetchCalled=e.setStreamOrFetchCalled,this.setTimeOutCalled=e.setTimeOutCalled)}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 useApiKey() twice."});return this.apiKey=t,this.setApiKeyCalled=!0,this}withHistoryDepth(t="max"){if(this.setHistoryDepthCalled)throw new e({source:this.constructor.name,message:"Unable to set history depth. History depth setter has already been called by this builder. Make sure you are not calling includeHistoryDepth() twice."});return this.historyDepth=t,this.setHistoryDepthCalled=!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}withTimeout(t){if(this.setTimeOutCalled)throw new e({source:this.constructor.name,message:"Unable to set timeout. Timeout setter has already been called by this builder. Make sure you are not calling withTimeout() twice."});return this.timeout=t,this.setTimeOutCalled=!0,this}};h.defaultTimeout=3e4;let c=h;class l extends n{constructor({actAs:e,apiKey:t,timeout:s,historyDepthToInclude:a,initialSystemMessage:i}){super({actAs:e,apiKey:t,timeout:s,historyDepthToInclude:a,dataExchangeMode:"fetch"}),this.initialSystemMessage=null,this.shouldSendInitialSystemMessage=!1,void 0!==i&&i.length>0&&(this.initialSystemMessage=i,this.shouldSendInitialSystemMessage=!0)}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.actAsMessage?[{role:"system",content:this.actAsMessage}]:[];return console.dir(this.shouldSendInitialSystemMessage),this.shouldSendInitialSystemMessage&&(i.push({role:"system",content:this.initialSystemMessage}),this.shouldSendInitialSystemMessage=!1),i.push({role:"user",content:s}),this.openai.chat.completions.create({stream:!0,model:"gpt-4",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 d extends c{constructor(e){super(e),this.dataExchangeMode="stream"}create(){if(!this.apiKey)throw new e({source:this.constructor.name,message:"Unable to create GPT4 adapter. API key is not set. You should call useApiKey() on instance to set the API key."});return new l({apiKey:this.apiKey,timeout:this.timeout,historyDepthToInclude:this.historyDepth,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."})}}class u extends c{constructor(){super()}create(){if(!this.apiKey)throw new e({source:this.constructor.name,message:"Unable to create GPT4 adapter. API key is not set. You should call useApiKey() on instance to set the API key."});if("stream"===this.dataExchangeMode)throw new e({source:this.constructor.name,message:"Unable to create GPT4 adapter. This builder is configured to use the stream API because useStreamingMode() was called. You should call use the builder instance returned by useStreamingMode() to create the builder."});return new r({apiKey:this.apiKey,timeout:this.timeout,historyDepthToInclude:this.historyDepth})}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,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 d(this)}}const m=t=>{if("openai/gpt4"!==t)throw new e({source:"createAdapter",message:"Adapter type not supported"});return new u};export{r as Gpt4FetchAdapter,l as Gpt4StreamingAdapter,m 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}}),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 { ISseAdapter, AdapterStatus, AdapterConfig, AdapterInfo, Message, Observable, AdapterBuilder, Adapter } from '@nlux/nlux'; | ||
import OpenAI from 'openai'; | ||
declare abstract class Gpt4AbstractAdapter<InboundPayload, OutboundPayload> implements ISseAdapter<InboundPayload, OutboundPayload> { | ||
protected actAsMessage: string | null; | ||
type OpenAIChatModel = 'gpt-4' | 'gpt-4-0613' | 'gpt-4-32k' | 'gpt-4-32k-0613' | 'gpt-3.5-turbo' | 'gpt-3.5-turbo-0613' | 'gpt-3.5-turbo-16k' | 'gpt-3.5-turbo-16k-0613'; | ||
declare abstract class GptAbstractAdapter<InboundPayload, OutboundPayload> implements ISseAdapter<InboundPayload, OutboundPayload> { | ||
protected currentStatus: AdapterStatus; | ||
protected readonly dataExchangeMode: 'stream' | 'fetch'; | ||
protected readonly historyDepthToInclude: number | 'max'; | ||
protected initialSystemMessage: string | null; | ||
protected readonly model: OpenAIChatModel; | ||
protected readonly openai: OpenAI; | ||
protected readonly timeout: number; | ||
protected constructor({ actAs, apiKey, timeout, historyDepthToInclude, dataExchangeMode, }: { | ||
actAs?: string; | ||
protected constructor({ initialSystemMessage, apiKey, dataExchangeMode, model, }: { | ||
initialSystemMessage?: string; | ||
apiKey: string; | ||
timeout: number; | ||
historyDepthToInclude: number | 'max'; | ||
dataExchangeMode: 'stream' | 'fetch'; | ||
model: OpenAIChatModel; | ||
}); | ||
@@ -27,8 +27,7 @@ abstract get config(): AdapterConfig<InboundPayload, OutboundPayload>; | ||
declare class Gpt4FetchAdapter extends Gpt4AbstractAdapter<OpenAI.Chat.Completions.ChatCompletion, OpenAI.Chat.Completions.ChatCompletionMessageParam> { | ||
constructor({ actAs, apiKey, timeout, historyDepthToInclude, }: { | ||
actAs?: string; | ||
declare class GptFetchAdapter extends GptAbstractAdapter<OpenAI.Chat.Completions.ChatCompletion, OpenAI.Chat.Completions.ChatCompletionMessageParam> { | ||
constructor({ apiKey, model, initialSystemMessage, }: { | ||
apiKey: string; | ||
timeout: number; | ||
historyDepthToInclude: number | 'max'; | ||
model: OpenAIChatModel; | ||
initialSystemMessage?: string; | ||
}); | ||
@@ -42,10 +41,6 @@ get config(): Readonly<{ | ||
declare class Gpt4StreamingAdapter extends Gpt4AbstractAdapter<OpenAI.Chat.Completions.ChatCompletionChunk, OpenAI.Chat.Completions.ChatCompletionMessageParam> { | ||
private initialSystemMessage; | ||
private shouldSendInitialSystemMessage; | ||
constructor({ actAs, apiKey, timeout, historyDepthToInclude, initialSystemMessage, }: { | ||
actAs?: string; | ||
declare class GptStreamingAdapter extends GptAbstractAdapter<OpenAI.Chat.Completions.ChatCompletionChunk, OpenAI.Chat.Completions.ChatCompletionMessageParam> { | ||
constructor({ apiKey, model, initialSystemMessage, }: { | ||
apiKey: string; | ||
timeout: number; | ||
historyDepthToInclude: number | 'max'; | ||
model: OpenAIChatModel; | ||
initialSystemMessage?: string; | ||
@@ -60,34 +55,30 @@ }); | ||
declare abstract class OpenAiGpt4AbstractBuilder implements AdapterBuilder<OpenAI.Chat.Completions.ChatCompletionChunk | OpenAI.Chat.Completions.ChatCompletion, OpenAI.Chat.Completions.ChatCompletionMessageParam> { | ||
static defaultTimeout: number; | ||
apiKey: string | null; | ||
dataExchangeMode: 'stream' | 'fetch'; | ||
historyDepth: number | 'max'; | ||
timeout: number; | ||
declare abstract class OpenAIGptAbstractBuilder implements AdapterBuilder<OpenAI.Chat.Completions.ChatCompletionChunk | OpenAI.Chat.Completions.ChatCompletion, OpenAI.Chat.Completions.ChatCompletionMessageParam> { | ||
protected apiKey: string | null; | ||
protected dataExchangeMode: 'stream' | 'fetch'; | ||
protected initialSystemMessage: string | null; | ||
protected model: OpenAIChatModel; | ||
protected setApiKeyCalled: boolean; | ||
protected setHistoryDepthCalled: boolean; | ||
protected setInitialSystemMessageCalled: boolean; | ||
protected setModelCalled: boolean; | ||
protected setStreamOrFetchCalled: boolean; | ||
protected setTimeOutCalled: boolean; | ||
protected constructor(cloneFrom?: OpenAiGpt4AbstractBuilder); | ||
abstract create(): (Gpt4FetchAdapter | Gpt4StreamingAdapter) & Adapter<any, any>; | ||
abstract useFetchingMode(): OpenAiGpt4AbstractBuilder; | ||
abstract useStreamingMode(): OpenAiGpt4AbstractBuilder; | ||
withApiKey(apiKey: string): OpenAiGpt4AbstractBuilder; | ||
withHistoryDepth(depth?: number | 'max'): OpenAiGpt4AbstractBuilder; | ||
withInitialSystemMessage(message: string): OpenAiGpt4AbstractBuilder; | ||
withTimeout(timeoutInMilliseconds: number): OpenAiGpt4AbstractBuilder; | ||
protected constructor(cloneFrom?: OpenAIGptAbstractBuilder); | ||
abstract create(): (GptFetchAdapter | GptStreamingAdapter) & Adapter<any, any>; | ||
abstract useFetchingMode(): OpenAIGptAbstractBuilder; | ||
abstract useStreamingMode(): OpenAIGptAbstractBuilder; | ||
withApiKey(apiKey: string): OpenAIGptAbstractBuilder; | ||
withInitialSystemMessage(message: string): OpenAIGptAbstractBuilder; | ||
withModel(model: OpenAIChatModel): OpenAIGptAbstractBuilder; | ||
} | ||
declare class OpenAiGpt4Builder extends OpenAiGpt4AbstractBuilder { | ||
declare class OpenAIGptBuilder extends OpenAIGptAbstractBuilder { | ||
constructor(); | ||
create(): Gpt4FetchAdapter | Gpt4StreamingAdapter; | ||
useFetchingMode(): OpenAiGpt4AbstractBuilder; | ||
useStreamingMode(): OpenAiGpt4AbstractBuilder; | ||
create(): GptFetchAdapter | GptStreamingAdapter; | ||
useFetchingMode(): OpenAIGptAbstractBuilder; | ||
useStreamingMode(): OpenAIGptAbstractBuilder; | ||
} | ||
type ProvidedAdapterType = 'openai/gpt4'; | ||
declare const createAdapter: (adapterType: ProvidedAdapterType) => OpenAiGpt4Builder; | ||
type ProvidedAdapterType = 'openai/gpt'; | ||
declare const createAdapter: (adapterType: ProvidedAdapterType) => OpenAIGptBuilder; | ||
export { Gpt4FetchAdapter, Gpt4StreamingAdapter, createAdapter }; | ||
export { GptFetchAdapter, GptStreamingAdapter, type OpenAIChatModel, createAdapter }; |
{ | ||
"name": "@nlux/openai", | ||
"version": "0.1.7-beta", | ||
"version": "0.1.8-beta", | ||
"description": "The OpenAI adapters for NLUX, the javascript library for building conversational AI interfaces.", | ||
@@ -54,3 +54,3 @@ "keywords": [ | ||
"dependencies": { | ||
"@nlux/nlux": "0.1.7-beta", | ||
"@nlux/nlux": "0.1.8-beta", | ||
"openai": "^4.11.1" | ||
@@ -57,0 +57,0 @@ }, |
@@ -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-gpt4-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({actAs:e,apiKey:t,timeout:a,historyDepthToInclude:i,dataExchangeMode:o}){this.actAsMessage="Act as a helpful assistant to the user",this.currentStatus="disconnected",this.dataExchangeMode="fetch",this.currentStatus="disconnected",this.historyDepthToInclude=i??2,this.timeout=a,this.dataExchangeMode=o,this.openai=new s({apiKey:t,dangerouslyAllowBrowser:!0}),e&&(this.actAsMessage=e),console.warn('GPT-4 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')}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 n extends r{constructor({actAs:e,apiKey:t,timeout:s,historyDepthToInclude:a}){super({actAs:e,apiKey:t,timeout:s,historyDepthToInclude:a,dataExchangeMode:"fetch"})}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.actAsMessage?[{role:"system",content:this.actAsMessage}]:[];return a.push({role:"user",content:e}),this.openai.chat.completions.create({stream:!1,model:"gpt-4",messages:a}).then((async e=>{const t=await this.decode(e);void 0!==t&&s.next(t),s.complete()})),s}}const h=class e{constructor(t){this.apiKey=null,this.dataExchangeMode="fetch",this.historyDepth="max",this.timeout=e.defaultTimeout,this.initialSystemMessage=null,this.setApiKeyCalled=!1,this.setHistoryDepthCalled=!1,this.setInitialSystemMessageCalled=!1,this.setStreamOrFetchCalled=!1,this.setTimeOutCalled=!1,t&&(this.apiKey=t.apiKey,this.dataExchangeMode=t.dataExchangeMode,this.historyDepth=t.historyDepth,this.timeout=t.timeout,this.setApiKeyCalled=t.setApiKeyCalled,this.setHistoryDepthCalled=t.setHistoryDepthCalled,this.setStreamOrFetchCalled=t.setStreamOrFetchCalled,this.setTimeOutCalled=t.setTimeOutCalled)}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 useApiKey() twice."});return this.apiKey=e,this.setApiKeyCalled=!0,this}withHistoryDepth(e="max"){if(this.setHistoryDepthCalled)throw new t.NluxUsageError({source:this.constructor.name,message:"Unable to set history depth. History depth setter has already been called by this builder. Make sure you are not calling includeHistoryDepth() twice."});return this.historyDepth=e,this.setHistoryDepthCalled=!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}withTimeout(e){if(this.setTimeOutCalled)throw new t.NluxUsageError({source:this.constructor.name,message:"Unable to set timeout. Timeout setter has already been called by this builder. Make sure you are not calling withTimeout() twice."});return this.timeout=e,this.setTimeOutCalled=!0,this}};h.defaultTimeout=3e4;let c=h;class l extends r{constructor({actAs:e,apiKey:t,timeout:s,historyDepthToInclude:a,initialSystemMessage:i}){super({actAs:e,apiKey:t,timeout:s,historyDepthToInclude:a,dataExchangeMode:"fetch"}),this.initialSystemMessage=null,this.shouldSendInitialSystemMessage=!1,void 0!==i&&i.length>0&&(this.initialSystemMessage=i,this.shouldSendInitialSystemMessage=!0)}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.actAsMessage?[{role:"system",content:this.actAsMessage}]:[];return console.dir(this.shouldSendInitialSystemMessage),this.shouldSendInitialSystemMessage&&(a.push({role:"system",content:this.initialSystemMessage}),this.shouldSendInitialSystemMessage=!1),a.push({role:"user",content:e}),this.openai.chat.completions.create({stream:!0,model:"gpt-4",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 u extends c{constructor(e){super(e),this.dataExchangeMode="stream"}create(){if(!this.apiKey)throw new t.NluxUsageError({source:this.constructor.name,message:"Unable to create GPT4 adapter. API key is not set. You should call useApiKey() on instance to set the API key."});return new l({apiKey:this.apiKey,timeout:this.timeout,historyDepthToInclude:this.historyDepth,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."})}}class d extends c{constructor(){super()}create(){if(!this.apiKey)throw new t.NluxUsageError({source:this.constructor.name,message:"Unable to create GPT4 adapter. API key is not set. You should call useApiKey() on instance to set the API key."});if("stream"===this.dataExchangeMode)throw new t.NluxUsageError({source:this.constructor.name,message:"Unable to create GPT4 adapter. This builder is configured to use the stream API because useStreamingMode() was called. You should call use the builder instance returned by useStreamingMode() to create the builder."});return new n({apiKey:this.apiKey,timeout:this.timeout,historyDepthToInclude:this.historyDepth})}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,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)}}e.Gpt4FetchAdapter=n,e.Gpt4StreamingAdapter=l,e.createAdapter=e=>{if("openai/gpt4"!==e)throw new t.NluxUsageError({source:"createAdapter",message:"Adapter type not supported"});return new d}})); | ||
!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}})); |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
23115
141
1
+ Added@types/node@18.19.65(transitive)
+ Addedopenai@4.73.0(transitive)
- Removed@types/node@18.19.67(transitive)
- Removedopenai@4.73.1(transitive)
Updated@nlux/nlux@0.1.8-beta