hugbot
Version:
Chatbot maker for HuggingFace Inference API and other AI API providers and backends.
97 lines • 6.03 kB
JavaScript
"use strict";
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _HuggingFaceTextGenClient_instances, _HuggingFaceTextGenClient_makePayload;
Object.defineProperty(exports, "__esModule", { value: true });
exports.HuggingFaceTextGenClient = void 0;
/**
* AI text generation client using Hugging Face inference API
* @link https://huggingface.co/docs/api-inference/detailed_parameters
* @param language_model string - https://huggingface.co/models
* @example "NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO";
* @param top_k number | undefined - Integer to define the top tokens considered within the sample operation to create new text.
* @param top_p number | undefined - Float to define the tokens that are within the sample operation of text generation. Add tokens in the sample for more probable to least probable until the sum of the probabilities is greater than top_p.
* @param temperature number - 0-100 The temperature of the sampling operation. 1 means regular sampling, 0 means always take the highest score, 100.0 is getting closer to uniform probability.
* @param repetition_penalty number - 0-100 The more a token is used within generation the more it is penalized to not be picked in successive generation passes.
* @param max_new_tokens number | undefined - The amount of new tokens to be generated, this does not include the input length it is a estimate of the size of generated text you want. Each new tokens slows down the request, so look for balance between response times and length of text generated.
* @param max_time number | undefined - 0-120 The amount of time in seconds that the query should take maximum. Network can cause some overhead so it will be a soft limit.
* @param return_full_text boolean - If set to False, the return results will not contain the original query making it easier for prompting.
* @param num_return_sequences number - The number of proposition you want to be returned.
* @param do_sample boolean - Whether or not to use sampling, use greedy decoding otherwise.
* @param truncate number | boolean
* @param wait_for_model boolean - If the model is not ready, wait for it instead of receiving 503. It limits the number of requests required to get your inference done. It is advised to only set this flag to true after receiving a 503 error as it will limit hanging in your application to known places.
* @param use_cache boolean - There is a cache layer on the inference API to speedup requests we have already seen. Most models can use those results as is as models are deterministic (meaning the results will be the same anyway). However if you use a non deterministic model, you can set this parameter to prevent the caching mechanism from being used resulting in a real new query.
*/
class HuggingFaceTextGenClient {
constructor(params) {
_HuggingFaceTextGenClient_instances.add(this);
this.endPoint = "https://api-inference.huggingface.co/models/";
this.languageModel = "NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO";
this.topK = undefined;
this.topP = undefined;
this.temperature = 0.7;
this.repetitionPenalty = 1.1;
this.maxNewTokens = 512;
this.maxTime = 30;
this.returnFullText = false;
this.numReturnSequences = 1;
this.doSample = false;
this.truncate = undefined;
this.waitForModel = true;
this.useCache = true;
if (params)
Object.entries(params).forEach(([key, value]) => Object.assign(this, { [key]: value }));
}
/** Send request to HuggingFace Inference API.
* @property {string} prompt - Formatted AI chat conversation string.
* @property {string} apiToken - Optional API key. */
async sendRequest(prompt, apiToken) {
try {
const payload = __classPrivateFieldGet(this, _HuggingFaceTextGenClient_instances, "m", _HuggingFaceTextGenClient_makePayload).call(this, prompt, apiToken);
const response = await fetch(this.endPoint + this.languageModel, payload);
const generatedText = await response.json();
if ("error" in generatedText)
console.error(generatedText.error);
return generatedText[0].generated_text;
}
catch (error) {
return "No response...";
}
}
}
exports.HuggingFaceTextGenClient = HuggingFaceTextGenClient;
_HuggingFaceTextGenClient_instances = new WeakSet(), _HuggingFaceTextGenClient_makePayload = function _HuggingFaceTextGenClient_makePayload(prompt, apiToken) {
const payload = {
headers: {
"Content-Type": "application/json",
"x-compute-type": "cpu+optimized",
},
method: "POST",
body: JSON.stringify({
inputs: prompt,
options: {
wait_for_model: this.waitForModel,
use_cache: this.useCache,
},
parameters: {
top_k: this.topK,
top_p: this.topP,
temperature: this.temperature,
repetition_penalty: this.repetitionPenalty,
max_new_tokens: this.maxNewTokens,
max_time: this.maxTime,
return_full_text: this.returnFullText,
num_return_sequences: this.numReturnSequences,
do_sample: this.doSample,
truncate: this.truncate,
},
}),
};
if (apiToken)
payload.headers.Authorization = `Bearer ${apiToken}`;
return payload;
};
//# sourceMappingURL=HuggingFaceTextGenClient.js.map