code cleanup/hardening
This commit is contained in:
@@ -5,9 +5,9 @@ const {getEnv, OLLAMA, PORTS} = require('@nexusai/shared');
|
|||||||
const app = express();
|
const app = express();
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
|
||||||
const PORT = getEnv('PORT', PORTS.EMBEDDING); // Default to 3003 if PORT is not set
|
const PORT = getEnv('PORT', PORTS.EMBEDDING);
|
||||||
const OLLAMA_URL = getEnv('OLLAMA_URL', OLLAMA.DEFAULT_URL); // URL for Ollama API
|
const OLLAMA_URL = getEnv('OLLAMA_URL', OLLAMA.DEFAULT_URL);
|
||||||
const EMBED_MODEL = getEnv('EMBEDDING_MODEL', OLLAMA.EMBED_MODEL); // Ollama model for embeddings
|
const EMBED_MODEL = getEnv('EMBEDDING_MODEL', OLLAMA.EMBED_MODEL);
|
||||||
|
|
||||||
//OLLAMA embedding helper function
|
//OLLAMA embedding helper function
|
||||||
async function embedText(text) {
|
async function embedText(text) {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
const {getDB} = require('../db');
|
const {getDB} = require('../db');
|
||||||
const { EPISODIC, getEnv, SERVICES, parseRow, formatEpisodeText } = require('@nexusai/shared');
|
const { EPISODIC, getEnv, SERVICES, parseRow, formatEpisodeText, SUMMARIES } = require('@nexusai/shared');
|
||||||
const semantic = require('../semantic');
|
const semantic = require('../semantic');
|
||||||
const { extractAndStoreEntities } = require('../entities/extraction')
|
const { extractAndStoreEntities } = require('../entities/extraction')
|
||||||
|
|
||||||
@@ -207,7 +207,7 @@ async function getEpisodeEmbedding(userMessage, aiResponse){
|
|||||||
return data.embedding;
|
return data.embedding;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getEpisodesByProject(projectId, limit = 200) {
|
function getEpisodesByProject(projectId, limit = SUMMARIES.MAX_PROJECT_EPISODE_LIMIT) {
|
||||||
const db = getDB();
|
const db = getDB();
|
||||||
return db.prepare(`
|
return db.prepare(`
|
||||||
SELECT e.* FROM episodes e
|
SELECT e.* FROM episodes e
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
const { SERVICES, getEnv } = require('@nexusai/shared');
|
const { SERVICES, getEnv, SUMMARIES } = require('@nexusai/shared');
|
||||||
const {
|
const {
|
||||||
getSessionSummariesForProject,
|
getSessionSummariesForProject,
|
||||||
getProjectOverviewSummary,
|
getProjectOverviewSummary,
|
||||||
@@ -12,7 +12,7 @@ const {
|
|||||||
const EXTRACTION_URL = getEnv('EXTRACTION_URL', 'http://localhost:11434');
|
const EXTRACTION_URL = getEnv('EXTRACTION_URL', 'http://localhost:11434');
|
||||||
const EXTRACTION_MODEL = getEnv('EXTRACTION_MODEL', 'qwen2.5:3b');
|
const EXTRACTION_MODEL = getEnv('EXTRACTION_MODEL', 'qwen2.5:3b');
|
||||||
|
|
||||||
const MAX_SUMMARY_CHARS = 8000; // generous ceiling before we truncate input
|
const MAX_SUMMARY_CHARS = SUMMARIES.MAX_SUMMARY_CHARS; // generous ceiling before we truncate input
|
||||||
|
|
||||||
function buildProjectSummaryPrompt(projectName, sessionSummaries) {
|
function buildProjectSummaryPrompt(projectName, sessionSummaries) {
|
||||||
let summaryBlock = sessionSummaries
|
let summaryBlock = sessionSummaries
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
require ('dotenv').config();
|
require ('dotenv').config();
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const {getEnv, PORTS, SERVICES, ORCHESTRATION} = require('@nexusai/shared');
|
const {getEnv, PORTS, SERVICES, ORCHESTRATION, logger} = require('@nexusai/shared');
|
||||||
|
|
||||||
/**** ROUTERS *** */
|
/**** ROUTERS *** */
|
||||||
const chatRouter = require('./routes/chat');
|
const chatRouter = require('./routes/chat');
|
||||||
@@ -53,5 +53,5 @@ app.use('/summaries', summariesRouter)
|
|||||||
|
|
||||||
/******* Start the server ************/
|
/******* Start the server ************/
|
||||||
app.listen(PORT, () => {
|
app.listen(PORT, () => {
|
||||||
console.log(`Orchestration Service is running on port ${PORT}`);
|
logger.info(`Orchestration Service is running on port ${PORT}`);
|
||||||
});
|
});
|
||||||
@@ -26,7 +26,7 @@ const ORCHESTRATION = {
|
|||||||
SEMANTIC_LIMIT: 5,
|
SEMANTIC_LIMIT: 5,
|
||||||
SCORE_THRESHOLD: 0.5,
|
SCORE_THRESHOLD: 0.5,
|
||||||
ENTITIES_LIMIT: 5,
|
ENTITIES_LIMIT: 5,
|
||||||
ENTITIES_THRESHOLD: 0.6,
|
ENTITIES_THRESHOLD: 0.55,
|
||||||
TEMPERATURE: 0.7,
|
TEMPERATURE: 0.7,
|
||||||
CORS_ORIGIN: 'http://localhost:5173',
|
CORS_ORIGIN: 'http://localhost:5173',
|
||||||
SYSTEM_PROMPT: `You are a helpful, context-aware AI assistant. You have access to memories of past conversations with the user. Use them to provide consistent, personalised responses.`
|
SYSTEM_PROMPT: `You are a helpful, context-aware AI assistant. You have access to memories of past conversations with the user. Use them to provide consistent, personalised responses.`
|
||||||
@@ -73,11 +73,14 @@ const SUMMARIES = {
|
|||||||
THRESHOLD_TOKENS: 200, //trigger summary when session hits this many tokens
|
THRESHOLD_TOKENS: 200, //trigger summary when session hits this many tokens
|
||||||
MAX_SUMMARY_TOKENS: 800, //if existing summary exceeds this, create new instead of update
|
MAX_SUMMARY_TOKENS: 800, //if existing summary exceeds this, create new instead of update
|
||||||
MIN_EPISODES_SINCE: 5, // don't resummarize until N new episodes since last summary
|
MIN_EPISODES_SINCE: 5, // don't resummarize until N new episodes since last summary
|
||||||
|
MAX_SUMMARY_CHARS: 8000, // max chars to include from recent episodes when generating summary (to control prompt size)
|
||||||
|
MAX_PROJECT_EPISODE_LIMIT: 200, // max number of episodes to consider from the entire project when generating summary (to control prompt size)
|
||||||
}
|
}
|
||||||
|
|
||||||
const ENTITIES = {
|
const ENTITIES = {
|
||||||
TEMPERATURE: 0.1,
|
TEMPERATURE: 0.1, // Low temperature, more precise extraction, less creative
|
||||||
NUM_PREDICT: 1024,
|
NUM_PREDICT: 1024, // Max tokens to consider for entity extraction (e.g. recent conversation)
|
||||||
|
THRESHOLD: 0.55, // Minimum confidence score for an extracted entity to be included in the results
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
const {getEnv} = require('./config/env');
|
const {getEnv} = require('./config/env');
|
||||||
const {QDRANT, COLLECTIONS, EPISODIC, SERVICES, OLLAMA, PORTS, LLAMACPP, INFERENCE_DEFAULTS, SQLITE, ORCHESTRATION, SUMMARIES, ENTITIES } = require('./config/constants');
|
const {QDRANT, COLLECTIONS, EPISODIC, SERVICES, OLLAMA, PORTS, LLAMACPP, INFERENCE_DEFAULTS, SQLITE, ORCHESTRATION, SUMMARIES, ENTITIES } = require('./config/constants');
|
||||||
const {parseRow, formatEpisodeText} = require('./utils')
|
const {parseRow, formatEpisodeText} = require('./utils')
|
||||||
|
const {logger} = require('./utils/logger');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
getEnv,
|
getEnv,
|
||||||
@@ -18,4 +19,5 @@ module.exports = {
|
|||||||
formatEpisodeText,
|
formatEpisodeText,
|
||||||
SUMMARIES,
|
SUMMARIES,
|
||||||
ENTITIES,
|
ENTITIES,
|
||||||
|
logger,
|
||||||
};
|
};
|
||||||
12
packages/shared/src/utils/logger.js
Normal file
12
packages/shared/src/utils/logger.js
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
const LEVELS = { error: 0, warn: 1, info: 2, debug: 3 };
|
||||||
|
|
||||||
|
const current = LEVELS[process.env.LOG_LEVEL?.toLowerCase()] ?? LEVELS.info;
|
||||||
|
|
||||||
|
const logger = {
|
||||||
|
error: (...args) => current >= LEVELS.error && console.error('[ERROR]', ...args),
|
||||||
|
warn: (...args) => current >= LEVELS.warn && console.warn( '[WARN]', ...args),
|
||||||
|
info: (...args) => current >= LEVELS.info && console.log( '[INFO]', ...args),
|
||||||
|
debug: (...args) => current >= LEVELS.debug && console.log( '[DEBUG]', ...args),
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = logger;
|
||||||
Reference in New Issue
Block a user