nodebb-plugin-search-agent 0.0.93 → 0.0.931
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/searchHandler.js +14 -0
- package/package.json +1 -1
package/lib/searchHandler.js
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
// ─── Token estimation helper ───────────────────────────────────────────────
|
|
2
|
+
function estimateTokens(str) {
|
|
3
|
+
// Roughly 4 chars/token for English, 2 for Hebrew/UTF-8, but 4 is safe for cost estimation
|
|
4
|
+
return Math.ceil(str.length / 4);
|
|
5
|
+
}
|
|
6
|
+
|
|
1
7
|
'use strict';
|
|
2
8
|
|
|
3
9
|
const https = require('https');
|
|
@@ -200,6 +206,7 @@ async function reRankWithAI(queryText, candidates, topicMap, apiKey, model, maxR
|
|
|
200
206
|
|
|
201
207
|
|
|
202
208
|
// Embed the query and all candidate post snippets
|
|
209
|
+
|
|
203
210
|
const { embed, embedBatch } = require('../services/embeddingService');
|
|
204
211
|
const queryEmbedding = await embed(queryText);
|
|
205
212
|
const postSnippets = candidates.map((c) => {
|
|
@@ -224,6 +231,13 @@ async function reRankWithAI(queryText, candidates, topicMap, apiKey, model, maxR
|
|
|
224
231
|
const userMessage =
|
|
225
232
|
`embedding של שאלת המשתמש: [${queryEmbedding.slice(0, 8).map(x => x.toFixed(4)).join(', ')} ...]\n\nפוסטים:\n${candidateList}`;
|
|
226
233
|
|
|
234
|
+
// --- Token count logging ---
|
|
235
|
+
const totalEmbeddingChars = queryText.length + postSnippets.reduce((sum, s) => sum + s.length, 0);
|
|
236
|
+
const embeddingTokens = estimateTokens(queryText) + postSnippets.reduce((sum, s) => sum + estimateTokens(s), 0);
|
|
237
|
+
const llmPromptTokens = estimateTokens(systemPrompt) + estimateTokens(userMessage);
|
|
238
|
+
const winston = require.main.require('winston');
|
|
239
|
+
winston.info(`[search-agent] Token usage: embedding API ≈ ${embeddingTokens} tokens, LLM prompt ≈ ${llmPromptTokens} tokens (for this search)`);
|
|
240
|
+
|
|
227
241
|
const response = await callOpenAI(apiKey, model, [
|
|
228
242
|
{ role: 'system', content: systemPrompt },
|
|
229
243
|
{ role: 'user', content: userMessage },
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nodebb-plugin-search-agent",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.931",
|
|
4
4
|
"description": "NodeBB plugin that adds a floating chat assistant to help users find relevant forum topics using TF-IDF text similarity",
|
|
5
5
|
"main": "library.js",
|
|
6
6
|
"author": "Racheli Bayfus",
|