|
|
|
|
|
const axios = require("axios"); |
|
|
|
|
|
const sessions = new Map(); |
|
|
const SESSION_TIMEOUT = 2 * 60 * 1000; |
|
|
|
|
|
const models = { |
|
|
sleepy: "312b3ce9-62dc-4e94-9a40-fb8f60c93ea1", |
|
|
sarah: "4f2edc8d-2992-427f-93c2-849fc2d956de", |
|
|
myra: "deb86452-e27f-47d0-959d-98bdf53fac16", |
|
|
aiko: "c8bfad14-8529-4bf2-a359-b5dd067f2b3b" |
|
|
}; |
|
|
|
|
|
async function hammerAI(userId, message, model) { |
|
|
const url = "https://www.hammerai.com/api/cloud/chat"; |
|
|
const modelId = models[model.toLowerCase()] || model; |
|
|
|
|
|
if (!sessions.has(userId)) { |
|
|
sessions.set(userId, { |
|
|
messages: [ |
|
|
{ |
|
|
role: "system", |
|
|
content: "You are an AI chat assistant. Respond politely and helpfully to the user.", |
|
|
}, |
|
|
], |
|
|
timeout: null, |
|
|
}); |
|
|
} |
|
|
|
|
|
const session = sessions.get(userId); |
|
|
session.messages.push({ role: "user", content: message }); |
|
|
|
|
|
if (session.timeout) clearTimeout(session.timeout); |
|
|
session.timeout = setTimeout(() => { |
|
|
sessions.delete(userId); |
|
|
}, SESSION_TIMEOUT); |
|
|
|
|
|
const payload = { |
|
|
authorId: "b081fd42-5c88-460a-8ccd-d801e882faf7", |
|
|
userId: "", |
|
|
licenseKey: "", |
|
|
generateChat: { |
|
|
quantizationKey: "vllm-mistralai/Mistral-Nemo-Instruct-2407", |
|
|
messages: session.messages, |
|
|
temperature: 0.8, |
|
|
topP: 0.9, |
|
|
topK: 30, |
|
|
nPredict: 256, |
|
|
repetitionPenalty: 1.1, |
|
|
contextSize: 4096, |
|
|
mlock: true, |
|
|
characterId: modelId, |
|
|
}, |
|
|
}; |
|
|
|
|
|
try { |
|
|
const { data } = await axios.post(url, payload, { |
|
|
headers: { |
|
|
"Content-Type": "text/plain;charset=UTF-8", |
|
|
"User-Agent": "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Mobile Safari/537.36", |
|
|
Origin: "https://www.hammerai.com", |
|
|
Referer: `https://www.hammerai.com/chat/${modelId}`, |
|
|
}, |
|
|
}); |
|
|
|
|
|
session.messages.push({ role: "assistant", content: data }); |
|
|
return data; |
|
|
} catch (err) { |
|
|
return `Error: ${err.response?.status || err.message}`; |
|
|
} |
|
|
} |
|
|
|
|
|
const handler = async (req, res) => { |
|
|
try { |
|
|
const { chat, model, key } = req.query; |
|
|
|
|
|
if (!chat) { |
|
|
return res.status(400).json({ |
|
|
success: false, |
|
|
error: 'Missing required parameter: chat' |
|
|
}); |
|
|
} |
|
|
|
|
|
if (!model) { |
|
|
return res.status(400).json({ |
|
|
success: false, |
|
|
error: 'Missing required parameter: model', |
|
|
availableModels: Object.keys(models) |
|
|
}); |
|
|
} |
|
|
|
|
|
if (!models[model.toLowerCase()]) { |
|
|
return res.status(400).json({ |
|
|
success: false, |
|
|
error: 'Invalid model', |
|
|
availableModels: Object.keys(models) |
|
|
}); |
|
|
} |
|
|
|
|
|
if (!key) { |
|
|
return res.status(400).json({ |
|
|
success: false, |
|
|
error: 'Missing required parameter: key' |
|
|
}); |
|
|
} |
|
|
|
|
|
const userId = key; |
|
|
const result = await hammerAI(userId, chat, model); |
|
|
|
|
|
res.json({ |
|
|
author: "Herza", |
|
|
success: true, |
|
|
model: model, |
|
|
response: result |
|
|
}); |
|
|
|
|
|
} catch (error) { |
|
|
res.status(500).json({ |
|
|
success: false, |
|
|
error: error.message |
|
|
}); |
|
|
} |
|
|
}; |
|
|
|
|
|
module.exports = { |
|
|
name: 'HammerAI Chat', |
|
|
description: 'Generate responses using HammerAI with different character models', |
|
|
type: 'GET', |
|
|
routes: ['api/AI/hammerAI'], |
|
|
tags: ['ai', 'hammerai', 'chat'], |
|
|
main: ['AI'], |
|
|
parameters: ['chat', 'model', 'key'], |
|
|
enabled: true, |
|
|
handler |
|
|
}; |