Files
Keep/keep-notes/test-openrouter.mjs

52 lines
1.7 KiB
JavaScript

import sqlite3 from 'sqlite3';
import fetch from 'node-fetch';
const db = new sqlite3.Database('./prisma/keep-notes.db');
db.all("SELECT * FROM SystemConfig", async (err, rows) => {
if (err) throw err;
const config = rows.reduce((acc, row) => ({ ...acc, [row.key]: row.value }), {});
const apiKey = config.CUSTOM_OPENAI_API_KEY;
const baseUrl = config.CUSTOM_OPENAI_BASE_URL || 'https://openrouter.ai/api/v1/';
const model = config.AI_MODEL_TAGS;
console.log("Using model:", model);
console.log("Base URL:", baseUrl);
const payload = {
model: model,
messages: [
{ role: "system", content: "Tu es l'Assistant IA de Keep Notes." },
{ role: "user", content: "hello ..." },
{ role: "assistant", content: "La configuration du Model Context Protocol..." },
{ role: "user", content: "dis moi comment utiliser le MCP" }
]
};
try {
const url = baseUrl.endsWith('/') ? `${baseUrl}chat/completions` : `${baseUrl}/chat/completions`;
const res = await fetch(url, {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json',
'HTTP-Referer': 'http://localhost:3000', // OpenRouter requirement
'X-Title': 'Keep Notes', // OpenRouter requirement
},
body: JSON.stringify(payload)
});
const data = await res.json();
if (!res.ok) {
console.error("OPENROUTER ERROR:");
console.error(JSON.stringify(data, null, 2));
} else {
console.log("OPENROUTER SUCCESS:");
console.log(data.choices[0].message.content.substring(0, 50));
}
} catch (error) {
console.error("FETCH ERROR:", error);
}
});