Added semantic episode searching

This commit is contained in:
Storme-bit
2026-04-05 21:49:31 -07:00
parent 8765dc3c2d
commit b71005d2b1
4 changed files with 100 additions and 14 deletions

View File

@@ -0,0 +1,18 @@
const {getEnv, SERVICES } = require('@nexusai/shared')
const BASE_URL = getEnv('EMBEDDING_SERVICE_URL', SERVICES.EMBEDDING_URL);
async function embed(text) {
const res = await fetch(`${BASE_URL}/embed`, {
method: 'POST',
headers: { 'Content-Type': 'application/json'},
body: JSON.stringify({text}),
})
if (!res.ok) throw new Error(`Embedding service error: ${res.status}`);
const data = await res.json();
return data.embedding;
}
module.exports = { embed };

View File

@@ -40,9 +40,17 @@ async function createEpisode(sessionId, userMessage, aiResponse, tokenCount) {
return res.json();
}
async function getEpisodeById(episodeId) {
const res = await (`${BASE_URL}/episodes/${episodeId}`);
if (res.status === 404) return null;
if (!res.ok) throw new Error(`Failed to fetch episode: ${res.status}`);
return res.json();
}
module.exports = {
getSessionByExternalId,
createSession,
getRecentEpisodes,
createEpisode
createEpisode,
getEpisodeById
}

View File

@@ -0,0 +1,27 @@
const {getEnv, QDRANT, COLLECTIONS } = require('@nexusai/shared')
const BASE_URL = getEnv('QDrant_URL', QDRANT.DEFAULT_URL);
async function searchEpisodes( vector, {limit = 5, scoreThreshold = 0.75, sessionId } = {}) {
const body = {vector, limit, score_threshold: scoreThreshold, with_payload: true};
if (sessionId) {
body.filter = { must: [{key: 'sessionId', match: {value: sessionId} }] };
}
const res = await fetch (
`${BASE_URL}/collections/${COLLECTIONS.EPISODES}/points/search`,
{
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(body)
}
);
if (!res.ok) throw new Error(`QDrant error: ${res.status}`);
const data = await res.json();
return data.result;
}
module.exports = { searchEpisodes };