searchService.js 998 B

1234567891011121314151617181920212223242526272829303132
  1. import { config } from "../config/index.js";
  2. import { embedTexts } from "./ollamaClient.js";
  3. import { qdrant } from "./qdrantClient.js";
  4. export async function searchDocs({ query, topK, embedRole = "query", minScore, collectionName = config.qdrant.collection }) {
  5. const [vector] = await embedTexts([query], { role: embedRole });
  6. let result;
  7. try {
  8. result = await qdrant.search(collectionName, {
  9. vector,
  10. limit: topK ?? config.rag.topK,
  11. score_threshold: minScore ?? config.rag.minScore,
  12. with_payload: true,
  13. with_vector: false
  14. });
  15. } catch (err) {
  16. const status = err?.status ?? err?.statusCode ?? err?.response?.status ?? null;
  17. if (Number(status) === 404) return [];
  18. throw err;
  19. }
  20. return (result ?? [])
  21. .map((r) => ({
  22. score: r.score,
  23. id: r.id,
  24. text: r.payload?.text ?? "",
  25. source: r.payload?.source ?? null,
  26. chunkIndex: r.payload?.chunkIndex ?? null,
  27. metadata: r.payload?.metadata ?? null
  28. }));
  29. }