memory / services /llm.js
gcharanteja
feat: add curation and vector services, enhance README with local development and configuration details
cf2a24d
Raw
History Blame Contribute Delete
1.81 kB
import OpenAI from "openai";
import fs from "node:fs";
import path from "node:path";
// ── CONFIG LOADING ────────────────────────────────────────────
const DATA_DIR = process.env.DATA_DIR || "/data";
const CONFIG_PATH = path.join(DATA_DIR, "config.json");
function loadConfig() {
try {
if (!fs.existsSync(CONFIG_PATH)) {
console.error(`Config file not found at ${CONFIG_PATH}`);
process.exit(1);
}
const config = JSON.parse(fs.readFileSync(CONFIG_PATH, "utf8"));
return config;
} catch (error) {
console.error(`Failed to load config from ${CONFIG_PATH}:`, error.message);
process.exit(1);
}
}
const config = loadConfig();
// ── OPENAI CONFIG ────────────────────────────────────────────
const openai = new OpenAI({
apiKey: process.env.NVIDIA_API_KEY,
baseURL: "https://integrate.api.nvidia.com/v1",
});
const OPENAI_MODEL = config.OPENAI_MODEL || "stepfun-ai/step-3.5-flash";
// ── STEP 1: Ask OpenAI to extract structured knowledge ────────
async function askOpenAI(rawText, prompt) {
const completion = await openai.chat.completions.create({
model: OPENAI_MODEL,
messages: [
{
role: "system",
content:
"You are a JSON extraction engine. Return ONLY a valid JSON object. No markdown, no code fences, no text before or after. Your entire response must be parseable JSON.",
},
{
role: "user",
content: prompt,
},
],
temperature: 0.05,
max_tokens: 4096,
});
return completion.choices[0]?.message?.content || "";
}
export { openai, OPENAI_MODEL, askOpenAI };