- AI
- Aggiornamento QUASAR
This commit is contained in:
@@ -21,6 +21,7 @@ const globalTables = require('../tools/globalTables');
|
||||
|
||||
const { ObjectId } = require('mongodb');
|
||||
|
||||
const OpenAI = require("openai");
|
||||
|
||||
router.post('/getlist', authenticate_noerror, async function (req, res, next) {
|
||||
|
||||
@@ -43,4 +44,66 @@ router.post('/getlist', authenticate_noerror, async function (req, res, next) {
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
async function getDeepSeekResponse(prompt, options) {
|
||||
try {
|
||||
|
||||
const deepseek = new OpenAI({
|
||||
baseURL: 'https://api.deepseek.com/v1', // Verifica il percorso esatto dalle API
|
||||
apiKey: process.env.DS_API_KEY, // Mai hardcodare la chiave!
|
||||
defaultHeaders: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
if (!options.withexplain) {
|
||||
prompt = prompt + '\n' + 'Ritornami solo il risultato, senza spiegazione.'
|
||||
}
|
||||
if (options.outputType) {
|
||||
prompt = prompt + '\n' + options.outputType;
|
||||
}
|
||||
|
||||
const completion = await deepseek.chat.completions.create({
|
||||
model: options.model || "deepseek-chat",
|
||||
messages: [
|
||||
{ role: "system", content: options.contestsystem || "" },
|
||||
{ role: "user", content: prompt }
|
||||
],
|
||||
temperature: options.temp || 0.3,
|
||||
max_tokens: options.max_tokens || 1000,
|
||||
stream: options.stream || false,
|
||||
});
|
||||
|
||||
if (!completion || !completion.choices || completion.choices.length === 0) {
|
||||
throw new Error('Invalid response from DeepSeek API');
|
||||
}
|
||||
|
||||
return completion.choices[0];
|
||||
} catch (error) {
|
||||
console.error('DeepSeek Error:', error.response?.data || error.message);
|
||||
throw new Error('Failed to get AI response');
|
||||
}
|
||||
}
|
||||
|
||||
// Endpoint per DeepSeek
|
||||
router.post('/ds', authenticate, async (req, res) => {
|
||||
try {
|
||||
let prompt = req.body.prompt;
|
||||
let options = req.body.options;
|
||||
|
||||
const choice = await getDeepSeekResponse(prompt, options);
|
||||
|
||||
return res.send({
|
||||
code: server_constants.RIS_CODE_OK,
|
||||
choice,
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('DeepSeek API Error:', error.response?.data || error.message);
|
||||
return res.send({ code: server_constants.RIS_CODE_ERR, error: 'Errore nella chiamata a DeepSeek: ' + error.response?.data || error.message });
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
module.exports = router;
|
||||
|
||||
Reference in New Issue
Block a user