searchChat.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. import fs from "node:fs";
  5. function isRefusalText(text) {
  6. const t = String(text ?? "").toLowerCase();
  7. return (
  8. t.includes("desculpe") &&
  9. (t.includes("não posso") || t.includes("nao posso") || t.includes("não posso fornecer") || t.includes("nao posso fornecer"))
  10. );
  11. }
  12. export async function searchDocs({ query, topK }) {
  13. const collectionName = config.qdrant.collection;
  14. // #region debug-point C:image-rag-search-start
  15. (() => {
  16. let u = "http://127.0.0.1:7777/event";
  17. let s = "image-rag-miss";
  18. try {
  19. const e = fs.readFileSync(".dbg/image-rag-miss.env", "utf8");
  20. u = e.match(/DEBUG_SERVER_URL=(.+)/)?.[1] || u;
  21. s = e.match(/DEBUG_SESSION_ID=(.+)/)?.[1] || s;
  22. } catch {}
  23. fetch(u, {
  24. method: "POST",
  25. headers: { "content-type": "application/json" },
  26. body: JSON.stringify({
  27. sessionId: s,
  28. runId: "post",
  29. hypothesisId: "C",
  30. location: "api/chat/searchChat.js",
  31. msg: "[DEBUG] searchDocs start",
  32. data: { collection: collectionName, topK: topK ?? config.rag.topK, queryHead: String(query ?? "").slice(0, 140) },
  33. ts: Date.now()
  34. })
  35. }).catch(() => {});
  36. })();
  37. // #endregion
  38. // #region debug-point B:search-start
  39. (() => {
  40. let u = "http://127.0.0.1:7777/event";
  41. let s = "chat-502-gateway";
  42. try {
  43. const e = fs.readFileSync(".dbg/chat-502-gateway.env", "utf8");
  44. u = e.match(/DEBUG_SERVER_URL=(.+)/)?.[1] || u;
  45. s = e.match(/DEBUG_SESSION_ID=(.+)/)?.[1] || s;
  46. } catch {}
  47. fetch(u, {
  48. method: "POST",
  49. headers: { "content-type": "application/json" },
  50. body: JSON.stringify({
  51. sessionId: s,
  52. runId: "post-fix",
  53. hypothesisId: "B",
  54. location: "api/chat/searchChat.js",
  55. msg: "[DEBUG] searchDocs start",
  56. data: {
  57. collection: collectionName,
  58. topK: topK ?? config.rag.topK,
  59. queryLen: typeof query === "string" ? query.length : null,
  60. qdrantUrl: config.qdrant.url,
  61. ollamaUrl: config.ollama.url
  62. },
  63. ts: Date.now()
  64. })
  65. }).catch(() => {});
  66. })();
  67. // #endregion
  68. const [vector] = await embedTexts([query]);
  69. const result = await qdrant.search(collectionName, {
  70. vector,
  71. limit: topK ?? config.rag.topK,
  72. with_payload: true,
  73. with_vector: false
  74. });
  75. // #region debug-point C:image-rag-search-result
  76. (() => {
  77. let u = "http://127.0.0.1:7777/event";
  78. let s = "image-rag-miss";
  79. try {
  80. const e = fs.readFileSync(".dbg/image-rag-miss.env", "utf8");
  81. u = e.match(/DEBUG_SERVER_URL=(.+)/)?.[1] || u;
  82. s = e.match(/DEBUG_SESSION_ID=(.+)/)?.[1] || s;
  83. } catch {}
  84. const top = Array.isArray(result) && result.length ? result[0] : null;
  85. fetch(u, {
  86. method: "POST",
  87. headers: { "content-type": "application/json" },
  88. body: JSON.stringify({
  89. sessionId: s,
  90. runId: "post",
  91. hypothesisId: "C",
  92. location: "api/chat/searchChat.js",
  93. msg: "[DEBUG] qdrant.search result",
  94. data: {
  95. count: Array.isArray(result) ? result.length : null,
  96. topScore: top?.score ?? null,
  97. topSource: top?.payload?.source ?? null,
  98. topTextHead: typeof top?.payload?.text === "string" ? top.payload.text.slice(0, 160) : ""
  99. },
  100. ts: Date.now()
  101. })
  102. }).catch(() => {});
  103. })();
  104. // #endregion
  105. // #region debug-point B:search-result
  106. (() => {
  107. let u = "http://127.0.0.1:7777/event";
  108. let s = "chat-502-gateway";
  109. try {
  110. const e = fs.readFileSync(".dbg/chat-502-gateway.env", "utf8");
  111. u = e.match(/DEBUG_SERVER_URL=(.+)/)?.[1] || u;
  112. s = e.match(/DEBUG_SESSION_ID=(.+)/)?.[1] || s;
  113. } catch {}
  114. fetch(u, {
  115. method: "POST",
  116. headers: { "content-type": "application/json" },
  117. body: JSON.stringify({
  118. sessionId: s,
  119. runId: "post-fix",
  120. hypothesisId: "B",
  121. location: "api/chat/searchChat.js",
  122. msg: "[DEBUG] qdrant.search ok",
  123. data: { count: Array.isArray(result) ? result.length : null },
  124. ts: Date.now()
  125. })
  126. }).catch(() => {});
  127. })();
  128. // #endregion
  129. return (result ?? [])
  130. .map((r) => ({
  131. score: r.score,
  132. id: r.id,
  133. text: r.payload?.text ?? "",
  134. source: r.payload?.source ?? null,
  135. chunkIndex: r.payload?.chunkIndex ?? null,
  136. metadata: r.payload?.metadata ?? null
  137. }))
  138. .filter((h) => !isRefusalText(h.text));
  139. }