Compare commits

...

3 Commits

Author SHA1 Message Date
Cohee 75372ad0cc Use Map for caches instead of objects 2024-04-23 16:15:54 +03:00
Cohee d1f292f462
Merge pull request #2122 from joenunezb/fix-informaticai-missing-choices-message
Fix: Handle InformaticAI Endpoint response without message in response payload
2024-04-23 14:07:27 +03:00
joenunezb 890cf81627 Fix: InformaticAI response without message in choices 2024-04-23 03:56:50 -07:00
3 changed files with 20 additions and 10 deletions

View File

@ -287,8 +287,10 @@ async function synchronizeChat(batchSize = 5) {
}
}
// Cache object for storing hash values
const hashCache = {};
/**
* @type {Map<string, number>} Cache object for storing hash values
*/
const hashCache = new Map();
/**
* Gets the hash value for a given string
@ -297,15 +299,15 @@ const hashCache = {};
*/
function getStringHash(str) {
// Check if the hash is already in the cache
if (Object.hasOwn(hashCache, str)) {
return hashCache[str];
if (hashCache.has(str)) {
return hashCache.get(str);
}
// Calculate the hash value
const hash = calculateHash(str);
// Store the hash in the cache
hashCache[str] = hash;
hashCache.set(str, hash);
return hash;
}

View File

@ -325,7 +325,7 @@ router.post('/generate', jsonParser, async function (request, response) {
// Map InfermaticAI response to OAI completions format
if (apiType === TEXTGEN_TYPES.INFERMATICAI) {
data['choices'] = (data?.choices || []).map(choice => ({ text: choice.message.content }));
data['choices'] = (data?.choices || []).map(choice => ({ text: choice?.message?.content || choice.text }));
}
return response.send(data);

View File

@ -5,7 +5,10 @@ const TASK = 'text-classification';
const router = express.Router();
const cacheObject = {};
/**
* @type {Map<string, object>} Cache for classification results
*/
const cacheObject = new Map();
router.post('/labels', jsonParser, async (req, res) => {
try {
@ -23,15 +26,20 @@ router.post('/', jsonParser, async (req, res) => {
try {
const { text } = req.body;
/**
* Get classification result for a given text
* @param {string} text Text to classify
* @returns {Promise<object>} Classification result
*/
async function getResult(text) {
if (Object.hasOwn(cacheObject, text)) {
return cacheObject[text];
if (cacheObject.has(text)) {
return cacheObject.get(text);
} else {
const module = await import('../transformers.mjs');
const pipe = await module.default.getPipeline(TASK);
const result = await pipe(text, { topk: 5 });
result.sort((a, b) => b.score - a.score);
cacheObject[text] = result;
cacheObject.set(text, result);
return result;
}
}