Comparing version 0.4.7 to 0.4.8
@@ -1,1 +0,1 @@ | ||
"use strict";var e=require("@nlux/nlux");const t=class t{constructor(t){if(!t.model)throw new e.NluxValidationError({source:this.constructor.name,message:'when creating the Hugging Face adapter, you must set either the model or the endpoint using the "endpoint" option!'});this.task=t.task,this.dataTransferMode=t.dataTransferMode,this.model=t.model,this.authToken=t.authToken}get config(){return{encodeMessage:e=>Promise.resolve(e),decodeMessage:e=>Promise.resolve(e)}}get id(){return""}get info(){return{id:"",connectionType:"ws",capabilities:{textChat:!0,audio:!1,fileUpload:!1,replyToSingleMessage:!1},remote:{url:""},inputFormats:["text"],outputFormats:["text"]}}get status(){return"idle"}async decode(e){if("string"==typeof e)return Promise.resolve(e);if(Array.isArray(e)){if(0===e.length)return Promise.resolve("");const t=e[0];if("object"==typeof t&&t.hasOwnProperty("generated_text"))return Promise.resolve(t.generated_text)}return""}async encode(e){return"<S> You are a helpful assistant. You keep your answers short. </S>[INST]"+e+"[/INST]"}send(t,r){if(!t)throw new e.NluxValidationError({source:this.constructor.name,message:"The first argument to the send() method must be a non-empty string"});if("stream"===this.dataTransferMode&&!r)throw new e.NluxValidationError({source:this.constructor.name,message:"The Hugging Face adapter is set to be used in streaming mode, but no observer was provided to the send() method! You should either provide an observer as a second argument to the send() NaN"});if("fetch"===this.dataTransferMode)return this.sendPull(t);this.sendStream(t,r)}async sendPull(r){if(!this.model)throw new e.NluxValidationError({source:this.constructor.name,message:'You must provide a valid model or endpoint through the "model" option when creating the Hugging Face adapter!'});const o={"Content-Type":"application/json"};this.authToken&&(o.Authorization=`Bearer ${this.authToken}`);const s="endpoint"===(e=>{const t=e.toLowerCase()||"";return t.startsWith("https://")||t.startsWith("http://")?"endpoint":"model"})(this.model)?this.model:`${t.baseUrl}/${this.model}`,n=await this.encode(r),a=await fetch(s,{headers:o,method:"POST",body:JSON.stringify({inputs:n,parameters:{max_new_tokens:500}})});return await this.decode(await a.json())}sendStream(e,t){throw new Error("Not implemented yet!")}};t.baseUrl="https://api-inference.huggingface.co/models";let r=t;class o{constructor(){this.theAuthToken=null,this.theDataTransferMode="stream",this.theModelOrEndpoint=null,this.theTask="text-generation",this.withDataTransferModeCalled=!1,this.withTaskCalled=!1}create(){if(null===this.theModelOrEndpoint)throw new e.NluxValidationError({source:this.constructor.name,message:'You must provide a model or an endpoint using the "withModel()" method to create a Hugging Face adapter!'});return new r({dataTransferMode:this.theDataTransferMode,task:this.theTask,model:this.theModelOrEndpoint,authToken:this.theAuthToken??void 0})}withAuthToken(t){if(null!==this.theAuthToken)throw new e.NluxUsageError({source:this.constructor.name,message:"Cannot set the auth token more than once"});return this.theAuthToken=t,this}withDataTransferMode(t){if(this.withDataTransferModeCalled)throw new e.NluxUsageError({source:this.constructor.name,message:"Cannot set the data loading mode more than once"});return this.theDataTransferMode=t,this.withDataTransferModeCalled=!0,this}withEndpoint(t){if(null!==this.theModelOrEndpoint)throw new e.NluxUsageError({source:this.constructor.name,message:"Cannot set the endpoint because a model or an endpoint has already been set"});return this.theModelOrEndpoint=t,this}withModel(t){if(null!==this.theModelOrEndpoint)throw new e.NluxUsageError({source:this.constructor.name,message:"Cannot set the model because a model or an endpoint has already been set"});return this.theModelOrEndpoint=t,this}withTask(t){if(this.withTaskCalled)throw new e.NluxUsageError({source:this.constructor.name,message:"Cannot set the task type more than once"});return this.theTask=t,this.withTaskCalled=!0,this}}exports.createAdapter=()=>new o; | ||
"use strict";var e=require("@nlux/nlux");const t=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},s=e=>e.ok?null:e.status>=500&&e.status<600?"NX-NT-003":e.status>=400&&e.status<500?"NX-NT-004":"NX-NT-001",o=class o{constructor(t){if(!t.model)throw new e.NluxValidationError({source:this.constructor.name,message:'when creating the Hugging Face adapter, you must set either the model or the endpoint using the "endpoint" option!'});this.options={...t}}get config(){return{encodeMessage:e=>Promise.resolve(e),decodeMessage:e=>Promise.resolve(e)}}get dataTransferMode(){return this.options.dataTransferMode}get id(){return""}get info(){return{id:"",capabilities:{textChat:!0,audio:!1,fileUpload:!1,replyToSingleMessage:!1},remote:{url:""},inputFormats:["text"],outputFormats:["text"]}}get outputFormat(){throw new Error("Method not implemented.")}get status(){return"idle"}async decode(e){if("string"==typeof e)return Promise.resolve(e);if(Array.isArray(e)){if(0===e.length)return Promise.resolve("");const t=e[0];if("object"==typeof t&&t.hasOwnProperty("generated_text"))return Promise.resolve(t.generated_text)}return""}async encode(t){const s=t,{inputPreProcessor:o}=this.options;if(o&&s){if("string"==typeof s)return o(s,null,{...this.options});e.warn("The input pre-processor function was provided, but the message is not a string! Input pre-processor will not be applied.")}return t}send(t,s){if(!t)throw new e.NluxValidationError({source:this.constructor.name,message:"The first argument to the send() method must be a non-empty string"});if("stream"===this.dataTransferMode&&!s)throw new e.NluxValidationError({source:this.constructor.name,message:"The Hugging Face adapter is set to be used in streaming mode, but no observer was provided to the send() method! You should either provide an observer as a second argument to the send() NaN"});if("fetch"===this.dataTransferMode)return this.sendFetch(t);this.sendStream(t,s)}async sendFetch(r){if(!this.options.model)throw new e.NluxValidationError({source:this.constructor.name,message:'You must provide a valid model or endpoint through the "model" option when creating the Hugging Face adapter!'});const n={"Content-Type":"application/json"};this.options.authToken&&(n.Authorization=`Bearer ${this.options.authToken}`);const a="endpoint"===(e=>{const t=e.toLowerCase()||"";return t.startsWith("https://")||t.startsWith("http://")?"endpoint":"model"})(this.options.model)?this.options.model:`${o.baseUrl}/${this.options.model}`;let i;try{const e=await this.encode(r);i=await fetch(a,{headers:n,method:"POST",body:JSON.stringify({inputs:e,parameters:{max_new_tokens:this.options.maxNewTokens??o.defaultMaxNewTokens}})})}catch(s){throw new e.NluxError({source:this.constructor.name,message:`An error occurred while sending the message to the Hugging Face API: ${s.message}`,exceptionId:t(s)??void 0})}if(!i.ok)throw new e.NluxError({source:this.constructor.name,message:`The Hugging Face adapter received an invalid response from the server: ${i.status}`,exceptionId:s(i)??void 0});return await this.decode(await i.json())}sendStream(e,t){throw new Error("Not implemented yet!")}};o.baseUrl="https://api-inference.huggingface.co/models",o.defaultMaxNewTokens=500;let r=o;class n{constructor(){this.theAuthToken=null,this.theDataTransferMode="stream",this.theInputPreProcessor=null,this.theMaxNewTokens=null,this.theModelOrEndpoint=null,this.theSystemMessage=null,this.withDataTransferModeCalled=!1}create(){if(null===this.theModelOrEndpoint)throw new e.NluxValidationError({source:this.constructor.name,message:'You must provide a model or an endpoint using the "withModel()" method to create a Hugging Face adapter!'});return new r({dataTransferMode:this.theDataTransferMode,model:this.theModelOrEndpoint,authToken:this.theAuthToken??void 0,inputPreProcessor:this.theInputPreProcessor??void 0,maxNewTokens:this.theMaxNewTokens??void 0,systemMessage:this.theSystemMessage??void 0})}withAuthToken(t){if(null!==this.theAuthToken)throw new e.NluxUsageError({source:this.constructor.name,message:"Cannot set the auth token more than once"});return this.theAuthToken=t,this}withDataTransferMode(t){if(this.withDataTransferModeCalled)throw new e.NluxUsageError({source:this.constructor.name,message:"Cannot set the data loading mode more than once"});return this.theDataTransferMode=t,this.withDataTransferModeCalled=!0,this}withEndpoint(t){if(null!==this.theModelOrEndpoint)throw new e.NluxUsageError({source:this.constructor.name,message:"Cannot set the endpoint because a model or an endpoint has already been set"});return this.theModelOrEndpoint=t,this}withInputPreProcessor(t){if(null!==this.theInputPreProcessor)throw new e.NluxUsageError({source:this.constructor.name,message:"Cannot set the input pre-processor more than once"});return this.theInputPreProcessor=t,this}withMaxNewTokens(t){if(null!==this.theMaxNewTokens)throw new e.NluxUsageError({source:this.constructor.name,message:"Cannot set the max new tokens more than once"});return this.theMaxNewTokens=t,this}withModel(t){if(null!==this.theModelOrEndpoint)throw new e.NluxUsageError({source:this.constructor.name,message:"Cannot set the model because a model or an endpoint has already been set"});return this.theModelOrEndpoint=t,this}withSystemMessage(t){if(null!==this.theSystemMessage)throw new e.NluxUsageError({source:this.constructor.name,message:"Cannot set the system message more than once"});return this.theSystemMessage=t,this}}exports.createAdapter=()=>new n,exports.llama2InputPreProcessor=(e,t,s)=>`<s>[INST] <<SYS>> ${s?.systemMessage??"You are a helpful assistant. You keep your answers short."} <</SYS>></s><s>[INST] ${e} [/INST]`; |
@@ -1,1 +0,1 @@ | ||
import{NluxValidationError as e,NluxUsageError as t}from"@nlux/nlux";const s=class t{constructor(t){if(!t.model)throw new e({source:this.constructor.name,message:'when creating the Hugging Face adapter, you must set either the model or the endpoint using the "endpoint" option!'});this.task=t.task,this.dataTransferMode=t.dataTransferMode,this.model=t.model,this.authToken=t.authToken}get config(){return{encodeMessage:e=>Promise.resolve(e),decodeMessage:e=>Promise.resolve(e)}}get id(){return""}get info(){return{id:"",connectionType:"ws",capabilities:{textChat:!0,audio:!1,fileUpload:!1,replyToSingleMessage:!1},remote:{url:""},inputFormats:["text"],outputFormats:["text"]}}get status(){return"idle"}async decode(e){if("string"==typeof e)return Promise.resolve(e);if(Array.isArray(e)){if(0===e.length)return Promise.resolve("");const t=e[0];if("object"==typeof t&&t.hasOwnProperty("generated_text"))return Promise.resolve(t.generated_text)}return""}async encode(e){return"<S> You are a helpful assistant. You keep your answers short. </S>[INST]"+e+"[/INST]"}send(t,s){if(!t)throw new e({source:this.constructor.name,message:"The first argument to the send() method must be a non-empty string"});if("stream"===this.dataTransferMode&&!s)throw new e({source:this.constructor.name,message:"The Hugging Face adapter is set to be used in streaming mode, but no observer was provided to the send() method! You should either provide an observer as a second argument to the send() NaN"});if("fetch"===this.dataTransferMode)return this.sendPull(t);this.sendStream(t,s)}async sendPull(s){if(!this.model)throw new e({source:this.constructor.name,message:'You must provide a valid model or endpoint through the "model" option when creating the Hugging Face adapter!'});const o={"Content-Type":"application/json"};this.authToken&&(o.Authorization=`Bearer ${this.authToken}`);const n="endpoint"===(e=>{const t=e.toLowerCase()||"";return t.startsWith("https://")||t.startsWith("http://")?"endpoint":"model"})(this.model)?this.model:`${t.baseUrl}/${this.model}`,r=await this.encode(s),a=await fetch(n,{headers:o,method:"POST",body:JSON.stringify({inputs:r,parameters:{max_new_tokens:500}})});return await this.decode(await a.json())}sendStream(e,t){throw new Error("Not implemented yet!")}};s.baseUrl="https://api-inference.huggingface.co/models";let o=s;class n{constructor(){this.theAuthToken=null,this.theDataTransferMode="stream",this.theModelOrEndpoint=null,this.theTask="text-generation",this.withDataTransferModeCalled=!1,this.withTaskCalled=!1}create(){if(null===this.theModelOrEndpoint)throw new e({source:this.constructor.name,message:'You must provide a model or an endpoint using the "withModel()" method to create a Hugging Face adapter!'});return new o({dataTransferMode:this.theDataTransferMode,task:this.theTask,model:this.theModelOrEndpoint,authToken:this.theAuthToken??void 0})}withAuthToken(e){if(null!==this.theAuthToken)throw new t({source:this.constructor.name,message:"Cannot set the auth token more than once"});return this.theAuthToken=e,this}withDataTransferMode(e){if(this.withDataTransferModeCalled)throw new t({source:this.constructor.name,message:"Cannot set the data loading mode more than once"});return this.theDataTransferMode=e,this.withDataTransferModeCalled=!0,this}withEndpoint(e){if(null!==this.theModelOrEndpoint)throw new t({source:this.constructor.name,message:"Cannot set the endpoint because a model or an endpoint has already been set"});return this.theModelOrEndpoint=e,this}withModel(e){if(null!==this.theModelOrEndpoint)throw new t({source:this.constructor.name,message:"Cannot set the model because a model or an endpoint has already been set"});return this.theModelOrEndpoint=e,this}withTask(e){if(this.withTaskCalled)throw new t({source:this.constructor.name,message:"Cannot set the task type more than once"});return this.theTask=e,this.withTaskCalled=!0,this}}const r=()=>new n;export{r as createAdapter}; | ||
import{NluxValidationError as e,warn as t,NluxError as s,NluxUsageError as o}from"@nlux/nlux";const n=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},r=e=>e.ok?null:e.status>=500&&e.status<600?"NX-NT-003":e.status>=400&&e.status<500?"NX-NT-004":"NX-NT-001",a=class o{constructor(t){if(!t.model)throw new e({source:this.constructor.name,message:'when creating the Hugging Face adapter, you must set either the model or the endpoint using the "endpoint" option!'});this.options={...t}}get config(){return{encodeMessage:e=>Promise.resolve(e),decodeMessage:e=>Promise.resolve(e)}}get dataTransferMode(){return this.options.dataTransferMode}get id(){return""}get info(){return{id:"",capabilities:{textChat:!0,audio:!1,fileUpload:!1,replyToSingleMessage:!1},remote:{url:""},inputFormats:["text"],outputFormats:["text"]}}get outputFormat(){throw new Error("Method not implemented.")}get status(){return"idle"}async decode(e){if("string"==typeof e)return Promise.resolve(e);if(Array.isArray(e)){if(0===e.length)return Promise.resolve("");const t=e[0];if("object"==typeof t&&t.hasOwnProperty("generated_text"))return Promise.resolve(t.generated_text)}return""}async encode(e){const s=e,{inputPreProcessor:o}=this.options;if(o&&s){if("string"==typeof s)return o(s,null,{...this.options});t("The input pre-processor function was provided, but the message is not a string! Input pre-processor will not be applied.")}return e}send(t,s){if(!t)throw new e({source:this.constructor.name,message:"The first argument to the send() method must be a non-empty string"});if("stream"===this.dataTransferMode&&!s)throw new e({source:this.constructor.name,message:"The Hugging Face adapter is set to be used in streaming mode, but no observer was provided to the send() method! You should either provide an observer as a second argument to the send() NaN"});if("fetch"===this.dataTransferMode)return this.sendFetch(t);this.sendStream(t,s)}async sendFetch(t){if(!this.options.model)throw new e({source:this.constructor.name,message:'You must provide a valid model or endpoint through the "model" option when creating the Hugging Face adapter!'});const a={"Content-Type":"application/json"};this.options.authToken&&(a.Authorization=`Bearer ${this.options.authToken}`);const i="endpoint"===(e=>{const t=e.toLowerCase()||"";return t.startsWith("https://")||t.startsWith("http://")?"endpoint":"model"})(this.options.model)?this.options.model:`${o.baseUrl}/${this.options.model}`;let h;try{const e=await this.encode(t);h=await fetch(i,{headers:a,method:"POST",body:JSON.stringify({inputs:e,parameters:{max_new_tokens:this.options.maxNewTokens??o.defaultMaxNewTokens}})})}catch(e){throw new s({source:this.constructor.name,message:`An error occurred while sending the message to the Hugging Face API: ${e.message}`,exceptionId:n(e)??void 0})}if(!h.ok)throw new s({source:this.constructor.name,message:`The Hugging Face adapter received an invalid response from the server: ${h.status}`,exceptionId:r(h)??void 0});return await this.decode(await h.json())}sendStream(e,t){throw new Error("Not implemented yet!")}};a.baseUrl="https://api-inference.huggingface.co/models",a.defaultMaxNewTokens=500;let i=a;class h{constructor(){this.theAuthToken=null,this.theDataTransferMode="stream",this.theInputPreProcessor=null,this.theMaxNewTokens=null,this.theModelOrEndpoint=null,this.theSystemMessage=null,this.withDataTransferModeCalled=!1}create(){if(null===this.theModelOrEndpoint)throw new e({source:this.constructor.name,message:'You must provide a model or an endpoint using the "withModel()" method to create a Hugging Face adapter!'});return new i({dataTransferMode:this.theDataTransferMode,model:this.theModelOrEndpoint,authToken:this.theAuthToken??void 0,inputPreProcessor:this.theInputPreProcessor??void 0,maxNewTokens:this.theMaxNewTokens??void 0,systemMessage:this.theSystemMessage??void 0})}withAuthToken(e){if(null!==this.theAuthToken)throw new o({source:this.constructor.name,message:"Cannot set the auth token more than once"});return this.theAuthToken=e,this}withDataTransferMode(e){if(this.withDataTransferModeCalled)throw new o({source:this.constructor.name,message:"Cannot set the data loading mode more than once"});return this.theDataTransferMode=e,this.withDataTransferModeCalled=!0,this}withEndpoint(e){if(null!==this.theModelOrEndpoint)throw new o({source:this.constructor.name,message:"Cannot set the endpoint because a model or an endpoint has already been set"});return this.theModelOrEndpoint=e,this}withInputPreProcessor(e){if(null!==this.theInputPreProcessor)throw new o({source:this.constructor.name,message:"Cannot set the input pre-processor more than once"});return this.theInputPreProcessor=e,this}withMaxNewTokens(e){if(null!==this.theMaxNewTokens)throw new o({source:this.constructor.name,message:"Cannot set the max new tokens more than once"});return this.theMaxNewTokens=e,this}withModel(e){if(null!==this.theModelOrEndpoint)throw new o({source:this.constructor.name,message:"Cannot set the model because a model or an endpoint has already been set"});return this.theModelOrEndpoint=e,this}withSystemMessage(e){if(null!==this.theSystemMessage)throw new o({source:this.constructor.name,message:"Cannot set the system message more than once"});return this.theSystemMessage=e,this}}const u=()=>new h,d=(e,t,s)=>`<s>[INST] <<SYS>> ${s?.systemMessage??"You are a helpful assistant. You keep your answers short."} <</SYS>></s><s>[INST] ${e} [/INST]`;export{u as createAdapter,d as llama2InputPreProcessor}; |
72
hf.d.ts
@@ -1,6 +0,16 @@ | ||
import { AdapterBuilder as AdapterBuilder$1, NluxAdapter } from '@nlux/nlux'; | ||
import { DataTransferMode, AdapterBuilder, StandardAdapter } from '@nlux/nlux'; | ||
export { Adapter, DataTransferMode, StandardAdapter } from '@nlux/nlux'; | ||
type InferenceTask = 'text-generation' | 'text2text-generation'; | ||
type DataTransferMode = 'stream' | 'fetch'; | ||
interface AdapterBuilder extends AdapterBuilder$1<any, any> { | ||
type HfAdapterOptions = { | ||
dataTransferMode: DataTransferMode; | ||
model: string; | ||
authToken?: string; | ||
inputPreProcessor?: HfInputPreProcessor; | ||
maxNewTokens?: number; | ||
systemMessage?: string; | ||
}; | ||
type HfInputPreProcessor = (input: string, memorizedConversation: Record<string, string> | null, adapterOptions: Readonly<HfAdapterOptions>) => string; | ||
interface HfAdapterBuilder extends AdapterBuilder<any, any> { | ||
/** | ||
@@ -11,5 +21,5 @@ * Create a new Hugging Face Inference API adapter. | ||
* | ||
* @returns {NluxAdapter} | ||
* @returns {StandardAdapter} | ||
*/ | ||
create(): NluxAdapter<any, any>; | ||
create(): StandardAdapter<any, any>; | ||
/** | ||
@@ -27,7 +37,7 @@ * The authorization token to use for Hugging Face Inference API. | ||
*/ | ||
withAuthToken(authToken: string): AdapterBuilder; | ||
withAuthToken(authToken: string): HfAdapterBuilder; | ||
/** | ||
* Instruct the adapter to connect to API and load data either in streaming mode or in pull mode. | ||
* Instruct the adapter to connect to API and load data either in streaming mode or in fetch mode. | ||
* The `stream` mode would use protocols such as websockets or server-side events, and Nlux will display data as | ||
* it's being generated by the server. The `pull` mode would use a single request to fetch data, and the response | ||
* it's being generated by the server. The `fetch` mode would use a single request to fetch data, and the response | ||
* would only be displayed once the entire message is loaded. | ||
@@ -39,3 +49,3 @@ * | ||
*/ | ||
withDataTransferMode(mode: DataTransferMode): AdapterBuilder; | ||
withDataTransferMode(mode: DataTransferMode): HfAdapterBuilder; | ||
/** | ||
@@ -51,4 +61,24 @@ * The endpoint to use for Hugging Face Inference API. | ||
*/ | ||
withEndpoint(endpoint: string): AdapterBuilder; | ||
withEndpoint(endpoint: string): HfAdapterBuilder; | ||
/** | ||
* This function will be called before sending the input to the Hugging Face Inference API. | ||
* It receives the input that the user has typed in the UI, and it should return the input that will be sent to the | ||
* API. This is useful if you want to preprocess the input before sending it to the API. | ||
* | ||
* @optional | ||
* @param {(input: any) => any} inputPreProcessor | ||
* @returns {HfAdapterBuilder} | ||
*/ | ||
withInputPreProcessor(inputPreProcessor: HfInputPreProcessor): HfAdapterBuilder; | ||
/** | ||
* The maximum number of new tokens that can be generated by the Hugging Face Inference API. | ||
* This is useful if you want to limit the number of tokens that can be generated by the API. | ||
* | ||
* @optional | ||
* @default 500 | ||
* @param {number} maxNewTokens | ||
* @returns {HfAdapterBuilder} | ||
*/ | ||
withMaxNewTokens(maxNewTokens: number): HfAdapterBuilder; | ||
/** | ||
* The model or the endpoint to use for Hugging Face Inference API. | ||
@@ -62,18 +92,18 @@ * You should provide either a model or an endpoint, but not both. | ||
*/ | ||
withModel(model: string): AdapterBuilder; | ||
withModel(model: string): HfAdapterBuilder; | ||
/** | ||
* The task to use for Hugging Face Inference API. | ||
* Each model exposed by Hugging Face Inference API supports a specific task, such a text generation, translation, | ||
* classification, image generation, etc. | ||
* The initial system to send to the Hugging Face Inference API. | ||
* This will be used during the pre-processing step to construct the payload that will be sent to the API. | ||
* | ||
* Nlux currently supports text-related tasks: `text-generation` and `text2text-generation`. | ||
* | ||
* @param {InferenceTask} task | ||
* @optional | ||
* @param {string} message | ||
* @returns {AdapterBuilder} | ||
*/ | ||
withTask(task: InferenceTask): AdapterBuilder; | ||
withSystemMessage(message: string): HfAdapterBuilder; | ||
} | ||
declare const createAdapter: () => AdapterBuilder; | ||
declare const createAdapter: () => HfAdapterBuilder; | ||
export { type AdapterBuilder, type DataTransferMode, type InferenceTask, createAdapter }; | ||
declare const llama2InputPreProcessor: HfInputPreProcessor; | ||
export { type HfAdapterBuilder, type HfAdapterOptions, type HfInputPreProcessor, createAdapter, llama2InputPreProcessor }; |
{ | ||
"name": "@nlux/hf", | ||
"version": "0.4.7", | ||
"version": "0.4.8", | ||
"description": "The Hugging Face adapters for NLUX, the javascript library for building conversational AI interfaces.", | ||
@@ -55,3 +55,3 @@ "keywords": [ | ||
"dependencies": { | ||
"@nlux/nlux": "0.4.7" | ||
"@nlux/nlux": "0.4.8" | ||
}, | ||
@@ -58,0 +58,0 @@ "main": "index.js", |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@nlux/nlux")):"function"==typeof define&&define.amd?define(["exports","@nlux/nlux"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["@nlux/hf"]={},e.nlux)}(this,(function(e,t){"use strict";const o=class e{constructor(e){if(!e.model)throw new t.NluxValidationError({source:this.constructor.name,message:'when creating the Hugging Face adapter, you must set either the model or the endpoint using the "endpoint" option!'});this.task=e.task,this.dataTransferMode=e.dataTransferMode,this.model=e.model,this.authToken=e.authToken}get config(){return{encodeMessage:e=>Promise.resolve(e),decodeMessage:e=>Promise.resolve(e)}}get id(){return""}get info(){return{id:"",connectionType:"ws",capabilities:{textChat:!0,audio:!1,fileUpload:!1,replyToSingleMessage:!1},remote:{url:""},inputFormats:["text"],outputFormats:["text"]}}get status(){return"idle"}async decode(e){if("string"==typeof e)return Promise.resolve(e);if(Array.isArray(e)){if(0===e.length)return Promise.resolve("");const t=e[0];if("object"==typeof t&&t.hasOwnProperty("generated_text"))return Promise.resolve(t.generated_text)}return""}async encode(e){return"<S> You are a helpful assistant. You keep your answers short. </S>[INST]"+e+"[/INST]"}send(e,o){if(!e)throw new t.NluxValidationError({source:this.constructor.name,message:"The first argument to the send() method must be a non-empty string"});if("stream"===this.dataTransferMode&&!o)throw new t.NluxValidationError({source:this.constructor.name,message:"The Hugging Face adapter is set to be used in streaming mode, but no observer was provided to the send() method! You should either provide an observer as a second argument to the send() NaN"});if("fetch"===this.dataTransferMode)return this.sendPull(e);this.sendStream(e,o)}async sendPull(o){if(!this.model)throw new t.NluxValidationError({source:this.constructor.name,message:'You must provide a valid model or endpoint through the "model" option when creating the Hugging Face adapter!'});const n={"Content-Type":"application/json"};this.authToken&&(n.Authorization=`Bearer ${this.authToken}`);const r="endpoint"===(e=>{const t=e.toLowerCase()||"";return t.startsWith("https://")||t.startsWith("http://")?"endpoint":"model"})(this.model)?this.model:`${e.baseUrl}/${this.model}`,s=await this.encode(o),a=await fetch(r,{headers:n,method:"POST",body:JSON.stringify({inputs:s,parameters:{max_new_tokens:500}})});return await this.decode(await a.json())}sendStream(e,t){throw new Error("Not implemented yet!")}};o.baseUrl="https://api-inference.huggingface.co/models";let n=o;class r{constructor(){this.theAuthToken=null,this.theDataTransferMode="stream",this.theModelOrEndpoint=null,this.theTask="text-generation",this.withDataTransferModeCalled=!1,this.withTaskCalled=!1}create(){if(null===this.theModelOrEndpoint)throw new t.NluxValidationError({source:this.constructor.name,message:'You must provide a model or an endpoint using the "withModel()" method to create a Hugging Face adapter!'});return new n({dataTransferMode:this.theDataTransferMode,task:this.theTask,model:this.theModelOrEndpoint,authToken:this.theAuthToken??void 0})}withAuthToken(e){if(null!==this.theAuthToken)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot set the auth token more than once"});return this.theAuthToken=e,this}withDataTransferMode(e){if(this.withDataTransferModeCalled)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot set the data loading mode more than once"});return this.theDataTransferMode=e,this.withDataTransferModeCalled=!0,this}withEndpoint(e){if(null!==this.theModelOrEndpoint)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot set the endpoint because a model or an endpoint has already been set"});return this.theModelOrEndpoint=e,this}withModel(e){if(null!==this.theModelOrEndpoint)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot set the model because a model or an endpoint has already been set"});return this.theModelOrEndpoint=e,this}withTask(e){if(this.withTaskCalled)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot set the task type more than once"});return this.theTask=e,this.withTaskCalled=!0,this}}e.createAdapter=()=>new r})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@nlux/nlux")):"function"==typeof define&&define.amd?define(["exports","@nlux/nlux"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["@nlux/hf"]={},e.nlux)}(this,(function(e,t){"use strict";const s=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},o=e=>e.ok?null:e.status>=500&&e.status<600?"NX-NT-003":e.status>=400&&e.status<500?"NX-NT-004":"NX-NT-001",r=class e{constructor(e){if(!e.model)throw new t.NluxValidationError({source:this.constructor.name,message:'when creating the Hugging Face adapter, you must set either the model or the endpoint using the "endpoint" option!'});this.options={...e}}get config(){return{encodeMessage:e=>Promise.resolve(e),decodeMessage:e=>Promise.resolve(e)}}get dataTransferMode(){return this.options.dataTransferMode}get id(){return""}get info(){return{id:"",capabilities:{textChat:!0,audio:!1,fileUpload:!1,replyToSingleMessage:!1},remote:{url:""},inputFormats:["text"],outputFormats:["text"]}}get outputFormat(){throw new Error("Method not implemented.")}get status(){return"idle"}async decode(e){if("string"==typeof e)return Promise.resolve(e);if(Array.isArray(e)){if(0===e.length)return Promise.resolve("");const t=e[0];if("object"==typeof t&&t.hasOwnProperty("generated_text"))return Promise.resolve(t.generated_text)}return""}async encode(e){const s=e,{inputPreProcessor:o}=this.options;if(o&&s){if("string"==typeof s)return o(s,null,{...this.options});t.warn("The input pre-processor function was provided, but the message is not a string! Input pre-processor will not be applied.")}return e}send(e,s){if(!e)throw new t.NluxValidationError({source:this.constructor.name,message:"The first argument to the send() method must be a non-empty string"});if("stream"===this.dataTransferMode&&!s)throw new t.NluxValidationError({source:this.constructor.name,message:"The Hugging Face adapter is set to be used in streaming mode, but no observer was provided to the send() method! You should either provide an observer as a second argument to the send() NaN"});if("fetch"===this.dataTransferMode)return this.sendFetch(e);this.sendStream(e,s)}async sendFetch(r){if(!this.options.model)throw new t.NluxValidationError({source:this.constructor.name,message:'You must provide a valid model or endpoint through the "model" option when creating the Hugging Face adapter!'});const n={"Content-Type":"application/json"};this.options.authToken&&(n.Authorization=`Bearer ${this.options.authToken}`);const a="endpoint"===(e=>{const t=e.toLowerCase()||"";return t.startsWith("https://")||t.startsWith("http://")?"endpoint":"model"})(this.options.model)?this.options.model:`${e.baseUrl}/${this.options.model}`;let i;try{const t=await this.encode(r);i=await fetch(a,{headers:n,method:"POST",body:JSON.stringify({inputs:t,parameters:{max_new_tokens:this.options.maxNewTokens??e.defaultMaxNewTokens}})})}catch(e){throw new t.NluxError({source:this.constructor.name,message:`An error occurred while sending the message to the Hugging Face API: ${e.message}`,exceptionId:s(e)??void 0})}if(!i.ok)throw new t.NluxError({source:this.constructor.name,message:`The Hugging Face adapter received an invalid response from the server: ${i.status}`,exceptionId:o(i)??void 0});return await this.decode(await i.json())}sendStream(e,t){throw new Error("Not implemented yet!")}};r.baseUrl="https://api-inference.huggingface.co/models",r.defaultMaxNewTokens=500;let n=r;class a{constructor(){this.theAuthToken=null,this.theDataTransferMode="stream",this.theInputPreProcessor=null,this.theMaxNewTokens=null,this.theModelOrEndpoint=null,this.theSystemMessage=null,this.withDataTransferModeCalled=!1}create(){if(null===this.theModelOrEndpoint)throw new t.NluxValidationError({source:this.constructor.name,message:'You must provide a model or an endpoint using the "withModel()" method to create a Hugging Face adapter!'});return new n({dataTransferMode:this.theDataTransferMode,model:this.theModelOrEndpoint,authToken:this.theAuthToken??void 0,inputPreProcessor:this.theInputPreProcessor??void 0,maxNewTokens:this.theMaxNewTokens??void 0,systemMessage:this.theSystemMessage??void 0})}withAuthToken(e){if(null!==this.theAuthToken)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot set the auth token more than once"});return this.theAuthToken=e,this}withDataTransferMode(e){if(this.withDataTransferModeCalled)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot set the data loading mode more than once"});return this.theDataTransferMode=e,this.withDataTransferModeCalled=!0,this}withEndpoint(e){if(null!==this.theModelOrEndpoint)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot set the endpoint because a model or an endpoint has already been set"});return this.theModelOrEndpoint=e,this}withInputPreProcessor(e){if(null!==this.theInputPreProcessor)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot set the input pre-processor more than once"});return this.theInputPreProcessor=e,this}withMaxNewTokens(e){if(null!==this.theMaxNewTokens)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot set the max new tokens more than once"});return this.theMaxNewTokens=e,this}withModel(e){if(null!==this.theModelOrEndpoint)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot set the model because a model or an endpoint has already been set"});return this.theModelOrEndpoint=e,this}withSystemMessage(e){if(null!==this.theSystemMessage)throw new t.NluxUsageError({source:this.constructor.name,message:"Cannot set the system message more than once"});return this.theSystemMessage=e,this}}e.createAdapter=()=>new a,e.llama2InputPreProcessor=(e,t,s)=>`<s>[INST] <<SYS>> ${s?.systemMessage??"You are a helpful assistant. You keep your answers short."} <</SYS>></s><s>[INST] ${e} [/INST]`})); |
23423
166
Updated@nlux/nlux@0.4.8