refactoring and clean up
This commit is contained in:
@@ -2,14 +2,9 @@ const memory = require('../services/memory');
|
||||
const inference = require('../services/inference');
|
||||
const embedding = require('../services/embedding');
|
||||
const qdrant = require('../services/qdrant');
|
||||
const { ORCHESTRATION } = require('@nexusai/shared')
|
||||
|
||||
const 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.`;
|
||||
|
||||
const RECENT_EPISODE_LIMIT = 5; // Number of recent episodes to retrieve for context
|
||||
const SEMANTIC_LIMIT = 5;
|
||||
const SCORE_THRESHOLD = 0.75;
|
||||
const { RECENT_EPISODE_LIMIT, SEMANTIC_LIMIT, SCORE_THRESHOLD, SYSTEM_PROMPT } = ORCHESTRATION;
|
||||
|
||||
function buildPrompt(recentEpisodes, semanticEpisodes, userMessage) {
|
||||
const parts = [SYSTEM_PROMPT];
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
require ('dotenv').config();
|
||||
const express = require('express');
|
||||
const {getEnv} = require('@nexusai/shared');
|
||||
const {getEnv, PORTS, SERVICES, ORCHESTRATION} = require('@nexusai/shared');
|
||||
const chatRouter = require('./routes/chat');
|
||||
const sessionsRouter = require('./routes/sessions');
|
||||
const cors = require('cors');
|
||||
@@ -8,25 +8,28 @@ const cors = require('cors');
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
|
||||
const PORT = getEnv('PORT', '4000'); // Default to 4000 if PORT is not set
|
||||
|
||||
app.use(cors({
|
||||
origin: [
|
||||
getEnv('CORS_ORIGIN', 'http://localhost:5173'),
|
||||
'http://localhost:5173',
|
||||
getEnv('CORS_ORIGIN', ORCHESTRATION.CORS_ORIGIN),
|
||||
ORCHESTRATION.CORS_ORIGIN,
|
||||
],
|
||||
methods: ['GET', 'POST', 'DELETE'],
|
||||
allowedHeaders: ['Content-Type'],
|
||||
}))
|
||||
|
||||
const PORT = getEnv('PORT', PORTS.ORCHESTRATION);
|
||||
const MEMORY_URL = getEnv('MEMORY_SERVICE_URL', SERVICES.MEMORY_URL);
|
||||
const EMBEDDING_URL = getEnv('EMBEDDING_SERVICE_URL', SERVICES.EMBEDDING_URL);
|
||||
const INFERENCE_URL = getEnv('INFERENCE_SERVICE_URL', SERVICES.INFERENCE_URL);
|
||||
|
||||
// Health check endpoint
|
||||
app.get('/health', (req, res) => {
|
||||
res.json({
|
||||
service: 'Orchestration Service',
|
||||
status: 'healthy',
|
||||
memoryService: getEnv('MEMORY_SERVICE_URL', 'http://localhost:3002'),
|
||||
embeddingService: getEnv('EMBEDDING_SERVICE_URL', 'http://localhost:3003'),
|
||||
inferenceService: getEnv('INFERENCE_SERVICE_URL', 'http://localhost:3001'),
|
||||
service: 'Orchestration Service',
|
||||
status: 'healthy',
|
||||
memoryService: MEMORY_URL,
|
||||
embeddingService: EMBEDDING_URL,
|
||||
inferenceService: INFERENCE_URL,
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
const { Router } = require('express');
|
||||
const memory = require('../services/memory');
|
||||
const { EPISODIC } = require('@nexusai/shared');
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.get('/:sessionId/history', async (req, res) => {
|
||||
const { sessionId } = req.params;
|
||||
const { limit = 20, offset = 0 } = req.query;
|
||||
const { limit = EPISODIC.DEFAULT_PAGE_SIZE, offset = EPISODIC.DEFAULT_OFFSET } = req.query;
|
||||
|
||||
try {
|
||||
const session = await memory.getSessionByExternalId(sessionId);
|
||||
@@ -19,7 +20,7 @@ router.get('/:sessionId/history', async (req, res) => {
|
||||
});
|
||||
|
||||
router.get('/', async (req, res) => {
|
||||
const {limit = 20, offset = 0 } = req.query;
|
||||
const { limit = EPISODIC.DEFAULT_PAGE_SIZE, offset = EPISODIC.DEFAULT_OFFSET } = req.query;
|
||||
try {
|
||||
const sessions = await memory.getSessions(Number(limit), Number(offset));
|
||||
res.json(sessions);
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
const fetch = require('node-fetch');
|
||||
const { getEnv } = require('@nexusai/shared');
|
||||
const { getEnv, SERVICES } = require('@nexusai/shared');
|
||||
|
||||
const BASE_URL = getEnv('INFERENCE_SERVICE_URL', 'http://localhost:3001');
|
||||
const BASE_URL = getEnv('INFERENCE_SERVICE_URL', SERVICES.INFERENCE_URL);
|
||||
|
||||
async function complete(prompt, options ={}) {
|
||||
const res = await fetch(`${BASE_URL}/complete`, {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
const fetch = require('node-fetch');
|
||||
const { getEnv } = require('@nexusai/shared');
|
||||
const { getEnv, SERVICES, EPISODIC } = require('@nexusai/shared');
|
||||
|
||||
const BASE_URL = getEnv('MEMORY_SERVICE_URL', 'http://localhost:3002');
|
||||
const BASE_URL = getEnv('MEMORY_SERVICE_URL', SERVICES.MEMORY_URL);
|
||||
|
||||
//function to get session by external id, returns null if not found, throws error for other issues
|
||||
async function getSessionByExternalId(externalId) {
|
||||
@@ -24,7 +23,7 @@ async function createSession(externalId) {
|
||||
return res.json();
|
||||
}
|
||||
|
||||
async function getRecentEpisodes(sessionId, limit = 10) {
|
||||
async function getRecentEpisodes(sessionId, limit = EPISODIC.DEFAULT_SESSIONS_LIMIT) {
|
||||
const res = await fetch(`${BASE_URL}/sessions/${sessionId}/episodes?limit=${limit}`);
|
||||
if (!res.ok) throw new Error(`Failed to fetch episodes: ${res.status} ${res.statusText}`);
|
||||
return res.json();
|
||||
@@ -47,7 +46,7 @@ async function getEpisodeById(episodeId) {
|
||||
return res.json();
|
||||
}
|
||||
|
||||
async function getSessionHistory(sessionId, limit = 20, offset = 0) {
|
||||
async function getSessionHistory(sessionId, limit = EPISODIC.DEFAULT_SESSIONS_LIMIT, offset = EPISODIC.DEFAULT_OFFSET) {
|
||||
const res = await fetch(
|
||||
`${BASE_URL}/sessions/${sessionId}/episodes?limit=${limit}&offset=${offset}`
|
||||
);
|
||||
@@ -56,9 +55,9 @@ async function getSessionHistory(sessionId, limit = 20, offset = 0) {
|
||||
return res.json();
|
||||
}
|
||||
|
||||
async function getSessions(limit = 20, offset = 0) {
|
||||
async function getSessions(limit = EPISODIC.DEFAULT_SESSIONS_LIMIT, offset = EPISODIC.DEFAULT_OFFSET) {
|
||||
const res = await fetch(
|
||||
`${BASE_URL}/sessions?limit${limit}&offset=${offset}`
|
||||
`${BASE_URL}/sessions?limit=${limit}&offset=${offset}`
|
||||
);
|
||||
if (!res.ok) throw new Error(`Failed to fetch sessions: ${res.status}`);
|
||||
return res.json();
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
const {getEnv, QDRANT, COLLECTIONS } = require('@nexusai/shared')
|
||||
const {getEnv, QDRANT, COLLECTIONS, ORCHESTRATION } = require('@nexusai/shared')
|
||||
|
||||
const BASE_URL = getEnv('QDRANT_URL', QDRANT.DEFAULT_URL);
|
||||
|
||||
async function searchEpisodes( vector, {limit = 5, scoreThreshold = 0.75, sessionId } = {}) {
|
||||
async function searchEpisodes( vector, {limit = ORCHESTRATION.RECENT_EPISODE_LIMIT, scoreThreshold = ORCHESTRATION.SCORE_THRESHOLD, sessionId } = {}) {
|
||||
const body = {vector, limit, score_threshold: scoreThreshold, with_payload: true};
|
||||
|
||||
if (sessionId) {
|
||||
|
||||
Reference in New Issue
Block a user