searchChat.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import { config } from "../src/config/index.js";
  2. import { embedTexts } from "../src/services/ollamaClient.js";
  3. import { qdrant } from "../src/services/qdrantClient.js";
  4. function isRefusalText(text) {
  5. const t = String(text ?? "").toLowerCase();
  6. return (
  7. t.includes("desculpe") &&
  8. (t.includes("não posso") || t.includes("nao posso") || t.includes("não posso fornecer") || t.includes("nao posso fornecer"))
  9. );
  10. }
  11. export async function searchDocs({ query, topK }) {
  12. const collectionName = config.qdrant.collection;
  13. const [vector] = await embedTexts([query]);
  14. const result = await qdrant.search(collectionName, {
  15. vector,
  16. limit: topK ?? config.rag.topK,
  17. score_threshold: config.rag.minScore,
  18. with_payload: true,
  19. with_vector: false
  20. });
  21. return (result ?? [])
  22. .map((r) => ({
  23. score: r.score,
  24. id: r.id,
  25. text: r.payload?.text ?? "",
  26. source: r.payload?.source ?? null,
  27. chunkIndex: r.payload?.chunkIndex ?? null,
  28. metadata: r.payload?.metadata ?? null
  29. }))
  30. .filter((h) => !isRefusalText(h.text));
  31. }