@hazeljs/rag 0.2.0-alpha.1

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.
Files changed (379) hide show
  1. package/LICENSE +192 -0
  2. package/README.md +504 -0
  3. package/dist/__tests__/graph/community-detector.test.d.ts +2 -0
  4. package/dist/__tests__/graph/community-detector.test.d.ts.map +1 -0
  5. package/dist/__tests__/graph/community-detector.test.js +87 -0
  6. package/dist/__tests__/graph/community-detector.test.js.map +1 -0
  7. package/dist/__tests__/graph/community-summarizer.test.d.ts +2 -0
  8. package/dist/__tests__/graph/community-summarizer.test.d.ts.map +1 -0
  9. package/dist/__tests__/graph/community-summarizer.test.js +131 -0
  10. package/dist/__tests__/graph/community-summarizer.test.js.map +1 -0
  11. package/dist/__tests__/graph/entity-extractor.test.d.ts +2 -0
  12. package/dist/__tests__/graph/entity-extractor.test.d.ts.map +1 -0
  13. package/dist/__tests__/graph/entity-extractor.test.js +129 -0
  14. package/dist/__tests__/graph/entity-extractor.test.js.map +1 -0
  15. package/dist/__tests__/graph/graph-rag-pipeline.test.d.ts +2 -0
  16. package/dist/__tests__/graph/graph-rag-pipeline.test.d.ts.map +1 -0
  17. package/dist/__tests__/graph/graph-rag-pipeline.test.js +158 -0
  18. package/dist/__tests__/graph/graph-rag-pipeline.test.js.map +1 -0
  19. package/dist/__tests__/graph/knowledge-graph.test.d.ts +2 -0
  20. package/dist/__tests__/graph/knowledge-graph.test.d.ts.map +1 -0
  21. package/dist/__tests__/graph/knowledge-graph.test.js +208 -0
  22. package/dist/__tests__/graph/knowledge-graph.test.js.map +1 -0
  23. package/dist/__tests__/loaders/base.loader.test.d.ts +2 -0
  24. package/dist/__tests__/loaders/base.loader.test.d.ts.map +1 -0
  25. package/dist/__tests__/loaders/base.loader.test.js +114 -0
  26. package/dist/__tests__/loaders/base.loader.test.js.map +1 -0
  27. package/dist/__tests__/loaders/csv-file.loader.test.d.ts +2 -0
  28. package/dist/__tests__/loaders/csv-file.loader.test.d.ts.map +1 -0
  29. package/dist/__tests__/loaders/csv-file.loader.test.js +98 -0
  30. package/dist/__tests__/loaders/csv-file.loader.test.js.map +1 -0
  31. package/dist/__tests__/loaders/directory.loader.test.d.ts +2 -0
  32. package/dist/__tests__/loaders/directory.loader.test.d.ts.map +1 -0
  33. package/dist/__tests__/loaders/directory.loader.test.js +154 -0
  34. package/dist/__tests__/loaders/directory.loader.test.js.map +1 -0
  35. package/dist/__tests__/loaders/html-file.loader.test.d.ts +2 -0
  36. package/dist/__tests__/loaders/html-file.loader.test.d.ts.map +1 -0
  37. package/dist/__tests__/loaders/html-file.loader.test.js +93 -0
  38. package/dist/__tests__/loaders/html-file.loader.test.js.map +1 -0
  39. package/dist/__tests__/loaders/json-file.loader.test.d.ts +2 -0
  40. package/dist/__tests__/loaders/json-file.loader.test.d.ts.map +1 -0
  41. package/dist/__tests__/loaders/json-file.loader.test.js +84 -0
  42. package/dist/__tests__/loaders/json-file.loader.test.js.map +1 -0
  43. package/dist/__tests__/loaders/markdown-file.loader.test.d.ts +2 -0
  44. package/dist/__tests__/loaders/markdown-file.loader.test.d.ts.map +1 -0
  45. package/dist/__tests__/loaders/markdown-file.loader.test.js +83 -0
  46. package/dist/__tests__/loaders/markdown-file.loader.test.js.map +1 -0
  47. package/dist/__tests__/loaders/text-file.loader.test.d.ts +2 -0
  48. package/dist/__tests__/loaders/text-file.loader.test.d.ts.map +1 -0
  49. package/dist/__tests__/loaders/text-file.loader.test.js +50 -0
  50. package/dist/__tests__/loaders/text-file.loader.test.js.map +1 -0
  51. package/dist/__tests__/rag-pipeline.test.d.ts +2 -0
  52. package/dist/__tests__/rag-pipeline.test.d.ts.map +1 -0
  53. package/dist/__tests__/rag-pipeline.test.js +210 -0
  54. package/dist/__tests__/rag-pipeline.test.js.map +1 -0
  55. package/dist/__tests__/retrieval/bm25.test.d.ts +2 -0
  56. package/dist/__tests__/retrieval/bm25.test.d.ts.map +1 -0
  57. package/dist/__tests__/retrieval/bm25.test.js +86 -0
  58. package/dist/__tests__/retrieval/bm25.test.js.map +1 -0
  59. package/dist/__tests__/retrieval/hybrid-search.test.d.ts +2 -0
  60. package/dist/__tests__/retrieval/hybrid-search.test.d.ts.map +1 -0
  61. package/dist/__tests__/retrieval/hybrid-search.test.js +85 -0
  62. package/dist/__tests__/retrieval/hybrid-search.test.js.map +1 -0
  63. package/dist/__tests__/retrieval/multi-query.test.d.ts +2 -0
  64. package/dist/__tests__/retrieval/multi-query.test.d.ts.map +1 -0
  65. package/dist/__tests__/retrieval/multi-query.test.js +90 -0
  66. package/dist/__tests__/retrieval/multi-query.test.js.map +1 -0
  67. package/dist/__tests__/text-splitters/recursive-text-splitter.test.d.ts +2 -0
  68. package/dist/__tests__/text-splitters/recursive-text-splitter.test.d.ts.map +1 -0
  69. package/dist/__tests__/text-splitters/recursive-text-splitter.test.js +97 -0
  70. package/dist/__tests__/text-splitters/recursive-text-splitter.test.js.map +1 -0
  71. package/dist/__tests__/utils/similarity.test.d.ts +2 -0
  72. package/dist/__tests__/utils/similarity.test.d.ts.map +1 -0
  73. package/dist/__tests__/utils/similarity.test.js +47 -0
  74. package/dist/__tests__/utils/similarity.test.js.map +1 -0
  75. package/dist/agentic/agentic-rag.service.d.ts +49 -0
  76. package/dist/agentic/agentic-rag.service.d.ts.map +1 -0
  77. package/dist/agentic/agentic-rag.service.js +149 -0
  78. package/dist/agentic/agentic-rag.service.js.map +1 -0
  79. package/dist/agentic/decorators/active-learning.decorator.d.ts +19 -0
  80. package/dist/agentic/decorators/active-learning.decorator.d.ts.map +1 -0
  81. package/dist/agentic/decorators/active-learning.decorator.js +98 -0
  82. package/dist/agentic/decorators/active-learning.decorator.js.map +1 -0
  83. package/dist/agentic/decorators/adaptive-retrieval.decorator.d.ts +17 -0
  84. package/dist/agentic/decorators/adaptive-retrieval.decorator.d.ts.map +1 -0
  85. package/dist/agentic/decorators/adaptive-retrieval.decorator.js +103 -0
  86. package/dist/agentic/decorators/adaptive-retrieval.decorator.js.map +1 -0
  87. package/dist/agentic/decorators/cached.decorator.d.ts +18 -0
  88. package/dist/agentic/decorators/cached.decorator.d.ts.map +1 -0
  89. package/dist/agentic/decorators/cached.decorator.js +93 -0
  90. package/dist/agentic/decorators/cached.decorator.js.map +1 -0
  91. package/dist/agentic/decorators/context-aware.decorator.d.ts +16 -0
  92. package/dist/agentic/decorators/context-aware.decorator.d.ts.map +1 -0
  93. package/dist/agentic/decorators/context-aware.decorator.js +169 -0
  94. package/dist/agentic/decorators/context-aware.decorator.js.map +1 -0
  95. package/dist/agentic/decorators/corrective-rag.decorator.d.ts +16 -0
  96. package/dist/agentic/decorators/corrective-rag.decorator.d.ts.map +1 -0
  97. package/dist/agentic/decorators/corrective-rag.decorator.js +142 -0
  98. package/dist/agentic/decorators/corrective-rag.decorator.js.map +1 -0
  99. package/dist/agentic/decorators/hyde.decorator.d.ts +15 -0
  100. package/dist/agentic/decorators/hyde.decorator.d.ts.map +1 -0
  101. package/dist/agentic/decorators/hyde.decorator.js +91 -0
  102. package/dist/agentic/decorators/hyde.decorator.js.map +1 -0
  103. package/dist/agentic/decorators/index.d.ts +16 -0
  104. package/dist/agentic/decorators/index.d.ts.map +1 -0
  105. package/dist/agentic/decorators/index.js +32 -0
  106. package/dist/agentic/decorators/index.js.map +1 -0
  107. package/dist/agentic/decorators/multi-hop.decorator.d.ts +15 -0
  108. package/dist/agentic/decorators/multi-hop.decorator.d.ts.map +1 -0
  109. package/dist/agentic/decorators/multi-hop.decorator.js +109 -0
  110. package/dist/agentic/decorators/multi-hop.decorator.js.map +1 -0
  111. package/dist/agentic/decorators/query-planner.decorator.d.ts +20 -0
  112. package/dist/agentic/decorators/query-planner.decorator.d.ts.map +1 -0
  113. package/dist/agentic/decorators/query-planner.decorator.js +213 -0
  114. package/dist/agentic/decorators/query-planner.decorator.js.map +1 -0
  115. package/dist/agentic/decorators/query-rewriter.decorator.d.ts +16 -0
  116. package/dist/agentic/decorators/query-rewriter.decorator.d.ts.map +1 -0
  117. package/dist/agentic/decorators/query-rewriter.decorator.js +143 -0
  118. package/dist/agentic/decorators/query-rewriter.decorator.js.map +1 -0
  119. package/dist/agentic/decorators/self-reflective.decorator.d.ts +20 -0
  120. package/dist/agentic/decorators/self-reflective.decorator.d.ts.map +1 -0
  121. package/dist/agentic/decorators/self-reflective.decorator.js +189 -0
  122. package/dist/agentic/decorators/self-reflective.decorator.js.map +1 -0
  123. package/dist/agentic/decorators/source-verification.decorator.d.ts +15 -0
  124. package/dist/agentic/decorators/source-verification.decorator.d.ts.map +1 -0
  125. package/dist/agentic/decorators/source-verification.decorator.js +121 -0
  126. package/dist/agentic/decorators/source-verification.decorator.js.map +1 -0
  127. package/dist/agentic/index.d.ts +9 -0
  128. package/dist/agentic/index.d.ts.map +1 -0
  129. package/dist/agentic/index.js +25 -0
  130. package/dist/agentic/index.js.map +1 -0
  131. package/dist/agentic/types.d.ts +210 -0
  132. package/dist/agentic/types.d.ts.map +1 -0
  133. package/dist/agentic/types.js +7 -0
  134. package/dist/agentic/types.js.map +1 -0
  135. package/dist/decorators/embeddable.decorator.d.ts +31 -0
  136. package/dist/decorators/embeddable.decorator.d.ts.map +1 -0
  137. package/dist/decorators/embeddable.decorator.js +44 -0
  138. package/dist/decorators/embeddable.decorator.js.map +1 -0
  139. package/dist/decorators/rag.decorator.d.ts +58 -0
  140. package/dist/decorators/rag.decorator.d.ts.map +1 -0
  141. package/dist/decorators/rag.decorator.js +78 -0
  142. package/dist/decorators/rag.decorator.js.map +1 -0
  143. package/dist/decorators/semantic-search.decorator.d.ts +69 -0
  144. package/dist/decorators/semantic-search.decorator.d.ts.map +1 -0
  145. package/dist/decorators/semantic-search.decorator.js +116 -0
  146. package/dist/decorators/semantic-search.decorator.js.map +1 -0
  147. package/dist/embeddings/cohere-embeddings.d.ts +33 -0
  148. package/dist/embeddings/cohere-embeddings.d.ts.map +1 -0
  149. package/dist/embeddings/cohere-embeddings.js +91 -0
  150. package/dist/embeddings/cohere-embeddings.js.map +1 -0
  151. package/dist/embeddings/openai-embeddings.d.ts +21 -0
  152. package/dist/embeddings/openai-embeddings.d.ts.map +1 -0
  153. package/dist/embeddings/openai-embeddings.js +53 -0
  154. package/dist/embeddings/openai-embeddings.js.map +1 -0
  155. package/dist/graph/community-detector.d.ts +45 -0
  156. package/dist/graph/community-detector.d.ts.map +1 -0
  157. package/dist/graph/community-detector.js +153 -0
  158. package/dist/graph/community-detector.js.map +1 -0
  159. package/dist/graph/community-summarizer.d.ts +41 -0
  160. package/dist/graph/community-summarizer.d.ts.map +1 -0
  161. package/dist/graph/community-summarizer.js +119 -0
  162. package/dist/graph/community-summarizer.js.map +1 -0
  163. package/dist/graph/entity-extractor.d.ts +47 -0
  164. package/dist/graph/entity-extractor.d.ts.map +1 -0
  165. package/dist/graph/entity-extractor.js +224 -0
  166. package/dist/graph/entity-extractor.js.map +1 -0
  167. package/dist/graph/graph-rag-pipeline.d.ts +83 -0
  168. package/dist/graph/graph-rag-pipeline.d.ts.map +1 -0
  169. package/dist/graph/graph-rag-pipeline.js +390 -0
  170. package/dist/graph/graph-rag-pipeline.js.map +1 -0
  171. package/dist/graph/graph.types.d.ts +186 -0
  172. package/dist/graph/graph.types.d.ts.map +1 -0
  173. package/dist/graph/graph.types.js +20 -0
  174. package/dist/graph/graph.types.js.map +1 -0
  175. package/dist/graph/index.d.ts +15 -0
  176. package/dist/graph/index.d.ts.map +1 -0
  177. package/dist/graph/index.js +31 -0
  178. package/dist/graph/index.js.map +1 -0
  179. package/dist/graph/knowledge-graph.d.ts +57 -0
  180. package/dist/graph/knowledge-graph.d.ts.map +1 -0
  181. package/dist/graph/knowledge-graph.js +198 -0
  182. package/dist/graph/knowledge-graph.js.map +1 -0
  183. package/dist/index.d.ts +29 -0
  184. package/dist/index.d.ts.map +1 -0
  185. package/dist/index.js +58 -0
  186. package/dist/index.js.map +1 -0
  187. package/dist/loaders/base.loader.d.ts +108 -0
  188. package/dist/loaders/base.loader.d.ts.map +1 -0
  189. package/dist/loaders/base.loader.js +123 -0
  190. package/dist/loaders/base.loader.js.map +1 -0
  191. package/dist/loaders/csv-file.loader.d.ts +61 -0
  192. package/dist/loaders/csv-file.loader.d.ts.map +1 -0
  193. package/dist/loaders/csv-file.loader.js +162 -0
  194. package/dist/loaders/csv-file.loader.js.map +1 -0
  195. package/dist/loaders/directory.loader.d.ts +67 -0
  196. package/dist/loaders/directory.loader.d.ts.map +1 -0
  197. package/dist/loaders/directory.loader.js +163 -0
  198. package/dist/loaders/directory.loader.js.map +1 -0
  199. package/dist/loaders/docx.loader.d.ts +52 -0
  200. package/dist/loaders/docx.loader.d.ts.map +1 -0
  201. package/dist/loaders/docx.loader.js +110 -0
  202. package/dist/loaders/docx.loader.js.map +1 -0
  203. package/dist/loaders/github.loader.d.ts +114 -0
  204. package/dist/loaders/github.loader.d.ts.map +1 -0
  205. package/dist/loaders/github.loader.js +217 -0
  206. package/dist/loaders/github.loader.js.map +1 -0
  207. package/dist/loaders/html-file.loader.d.ts +55 -0
  208. package/dist/loaders/html-file.loader.d.ts.map +1 -0
  209. package/dist/loaders/html-file.loader.js +170 -0
  210. package/dist/loaders/html-file.loader.js.map +1 -0
  211. package/dist/loaders/index.d.ts +52 -0
  212. package/dist/loaders/index.d.ts.map +1 -0
  213. package/dist/loaders/index.js +61 -0
  214. package/dist/loaders/index.js.map +1 -0
  215. package/dist/loaders/json-file.loader.d.ts +51 -0
  216. package/dist/loaders/json-file.loader.d.ts.map +1 -0
  217. package/dist/loaders/json-file.loader.js +100 -0
  218. package/dist/loaders/json-file.loader.js.map +1 -0
  219. package/dist/loaders/markdown-file.loader.d.ts +61 -0
  220. package/dist/loaders/markdown-file.loader.d.ts.map +1 -0
  221. package/dist/loaders/markdown-file.loader.js +148 -0
  222. package/dist/loaders/markdown-file.loader.js.map +1 -0
  223. package/dist/loaders/pdf.loader.d.ts +64 -0
  224. package/dist/loaders/pdf.loader.d.ts.map +1 -0
  225. package/dist/loaders/pdf.loader.js +163 -0
  226. package/dist/loaders/pdf.loader.js.map +1 -0
  227. package/dist/loaders/text-file.loader.d.ts +39 -0
  228. package/dist/loaders/text-file.loader.d.ts.map +1 -0
  229. package/dist/loaders/text-file.loader.js +69 -0
  230. package/dist/loaders/text-file.loader.js.map +1 -0
  231. package/dist/loaders/web.loader.d.ts +87 -0
  232. package/dist/loaders/web.loader.d.ts.map +1 -0
  233. package/dist/loaders/web.loader.js +194 -0
  234. package/dist/loaders/web.loader.js.map +1 -0
  235. package/dist/loaders/youtube-transcript.loader.d.ts +92 -0
  236. package/dist/loaders/youtube-transcript.loader.d.ts.map +1 -0
  237. package/dist/loaders/youtube-transcript.loader.js +254 -0
  238. package/dist/loaders/youtube-transcript.loader.js.map +1 -0
  239. package/dist/memory/index.d.ts +11 -0
  240. package/dist/memory/index.d.ts.map +1 -0
  241. package/dist/memory/index.js +31 -0
  242. package/dist/memory/index.js.map +1 -0
  243. package/dist/memory/memory-manager.d.ts +96 -0
  244. package/dist/memory/memory-manager.d.ts.map +1 -0
  245. package/dist/memory/memory-manager.js +369 -0
  246. package/dist/memory/memory-manager.js.map +1 -0
  247. package/dist/memory/memory-store.interface.d.ts +73 -0
  248. package/dist/memory/memory-store.interface.d.ts.map +1 -0
  249. package/dist/memory/memory-store.interface.js +6 -0
  250. package/dist/memory/memory-store.interface.js.map +1 -0
  251. package/dist/memory/stores/buffer-memory.d.ts +47 -0
  252. package/dist/memory/stores/buffer-memory.d.ts.map +1 -0
  253. package/dist/memory/stores/buffer-memory.js +280 -0
  254. package/dist/memory/stores/buffer-memory.js.map +1 -0
  255. package/dist/memory/stores/hybrid-memory.d.ts +49 -0
  256. package/dist/memory/stores/hybrid-memory.d.ts.map +1 -0
  257. package/dist/memory/stores/hybrid-memory.js +194 -0
  258. package/dist/memory/stores/hybrid-memory.js.map +1 -0
  259. package/dist/memory/stores/vector-memory.d.ts +48 -0
  260. package/dist/memory/stores/vector-memory.d.ts.map +1 -0
  261. package/dist/memory/stores/vector-memory.js +312 -0
  262. package/dist/memory/stores/vector-memory.js.map +1 -0
  263. package/dist/memory/types.d.ts +119 -0
  264. package/dist/memory/types.d.ts.map +1 -0
  265. package/dist/memory/types.js +18 -0
  266. package/dist/memory/types.js.map +1 -0
  267. package/dist/prompts/agentic/adaptive-retrieval.prompt.d.ts +8 -0
  268. package/dist/prompts/agentic/adaptive-retrieval.prompt.d.ts.map +1 -0
  269. package/dist/prompts/agentic/adaptive-retrieval.prompt.js +27 -0
  270. package/dist/prompts/agentic/adaptive-retrieval.prompt.js.map +1 -0
  271. package/dist/prompts/agentic/corrective-rag.prompt.d.ts +9 -0
  272. package/dist/prompts/agentic/corrective-rag.prompt.d.ts.map +1 -0
  273. package/dist/prompts/agentic/corrective-rag.prompt.js +23 -0
  274. package/dist/prompts/agentic/corrective-rag.prompt.js.map +1 -0
  275. package/dist/prompts/agentic/hyde.prompt.d.ts +9 -0
  276. package/dist/prompts/agentic/hyde.prompt.d.ts.map +1 -0
  277. package/dist/prompts/agentic/hyde.prompt.js +18 -0
  278. package/dist/prompts/agentic/hyde.prompt.js.map +1 -0
  279. package/dist/prompts/agentic/multi-hop.prompt.d.ts +15 -0
  280. package/dist/prompts/agentic/multi-hop.prompt.d.ts.map +1 -0
  281. package/dist/prompts/agentic/multi-hop.prompt.js +38 -0
  282. package/dist/prompts/agentic/multi-hop.prompt.js.map +1 -0
  283. package/dist/prompts/agentic/query-planner.prompt.d.ts +8 -0
  284. package/dist/prompts/agentic/query-planner.prompt.d.ts.map +1 -0
  285. package/dist/prompts/agentic/query-planner.prompt.js +30 -0
  286. package/dist/prompts/agentic/query-planner.prompt.js.map +1 -0
  287. package/dist/prompts/agentic/query-rewriter.prompt.d.ts +10 -0
  288. package/dist/prompts/agentic/query-rewriter.prompt.d.ts.map +1 -0
  289. package/dist/prompts/agentic/query-rewriter.prompt.js +17 -0
  290. package/dist/prompts/agentic/query-rewriter.prompt.js.map +1 -0
  291. package/dist/prompts/agentic/self-reflective-improve.prompt.d.ts +10 -0
  292. package/dist/prompts/agentic/self-reflective-improve.prompt.d.ts.map +1 -0
  293. package/dist/prompts/agentic/self-reflective-improve.prompt.js +24 -0
  294. package/dist/prompts/agentic/self-reflective-improve.prompt.js.map +1 -0
  295. package/dist/prompts/agentic/self-reflective.prompt.d.ts +9 -0
  296. package/dist/prompts/agentic/self-reflective.prompt.d.ts.map +1 -0
  297. package/dist/prompts/agentic/self-reflective.prompt.js +32 -0
  298. package/dist/prompts/agentic/self-reflective.prompt.js.map +1 -0
  299. package/dist/prompts/community-summary.prompt.d.ts +9 -0
  300. package/dist/prompts/community-summary.prompt.d.ts.map +1 -0
  301. package/dist/prompts/community-summary.prompt.js +30 -0
  302. package/dist/prompts/community-summary.prompt.js.map +1 -0
  303. package/dist/prompts/entity-extraction.prompt.d.ts +10 -0
  304. package/dist/prompts/entity-extraction.prompt.d.ts.map +1 -0
  305. package/dist/prompts/entity-extraction.prompt.js +39 -0
  306. package/dist/prompts/entity-extraction.prompt.js.map +1 -0
  307. package/dist/prompts/graph-search.prompt.d.ts +10 -0
  308. package/dist/prompts/graph-search.prompt.d.ts.map +1 -0
  309. package/dist/prompts/graph-search.prompt.js +23 -0
  310. package/dist/prompts/graph-search.prompt.js.map +1 -0
  311. package/dist/prompts/index.d.ts +13 -0
  312. package/dist/prompts/index.d.ts.map +1 -0
  313. package/dist/prompts/index.js +29 -0
  314. package/dist/prompts/index.js.map +1 -0
  315. package/dist/prompts/rag-answer.prompt.d.ts +9 -0
  316. package/dist/prompts/rag-answer.prompt.d.ts.map +1 -0
  317. package/dist/prompts/rag-answer.prompt.js +20 -0
  318. package/dist/prompts/rag-answer.prompt.js.map +1 -0
  319. package/dist/rag-pipeline-with-memory.d.ts +68 -0
  320. package/dist/rag-pipeline-with-memory.d.ts.map +1 -0
  321. package/dist/rag-pipeline-with-memory.js +186 -0
  322. package/dist/rag-pipeline-with-memory.js.map +1 -0
  323. package/dist/rag-pipeline.d.ts +59 -0
  324. package/dist/rag-pipeline.d.ts.map +1 -0
  325. package/dist/rag-pipeline.js +181 -0
  326. package/dist/rag-pipeline.js.map +1 -0
  327. package/dist/rag.module.d.ts +26 -0
  328. package/dist/rag.module.d.ts.map +1 -0
  329. package/dist/rag.module.js +40 -0
  330. package/dist/rag.module.js.map +1 -0
  331. package/dist/rag.service.d.ts +96 -0
  332. package/dist/rag.service.d.ts.map +1 -0
  333. package/dist/rag.service.js +173 -0
  334. package/dist/rag.service.js.map +1 -0
  335. package/dist/retrieval/bm25.d.ts +57 -0
  336. package/dist/retrieval/bm25.d.ts.map +1 -0
  337. package/dist/retrieval/bm25.js +106 -0
  338. package/dist/retrieval/bm25.js.map +1 -0
  339. package/dist/retrieval/hybrid-search.d.ts +48 -0
  340. package/dist/retrieval/hybrid-search.d.ts.map +1 -0
  341. package/dist/retrieval/hybrid-search.js +123 -0
  342. package/dist/retrieval/hybrid-search.js.map +1 -0
  343. package/dist/retrieval/multi-query.d.ts +38 -0
  344. package/dist/retrieval/multi-query.d.ts.map +1 -0
  345. package/dist/retrieval/multi-query.js +135 -0
  346. package/dist/retrieval/multi-query.js.map +1 -0
  347. package/dist/text-splitters/recursive-text-splitter.d.ts +21 -0
  348. package/dist/text-splitters/recursive-text-splitter.d.ts.map +1 -0
  349. package/dist/text-splitters/recursive-text-splitter.js +95 -0
  350. package/dist/text-splitters/recursive-text-splitter.js.map +1 -0
  351. package/dist/types/index.d.ts +144 -0
  352. package/dist/types/index.d.ts.map +1 -0
  353. package/dist/types/index.js +16 -0
  354. package/dist/types/index.js.map +1 -0
  355. package/dist/utils/similarity.d.ts +16 -0
  356. package/dist/utils/similarity.d.ts.map +1 -0
  357. package/dist/utils/similarity.js +58 -0
  358. package/dist/utils/similarity.js.map +1 -0
  359. package/dist/vector-stores/chroma.store.d.ts +42 -0
  360. package/dist/vector-stores/chroma.store.d.ts.map +1 -0
  361. package/dist/vector-stores/chroma.store.js +242 -0
  362. package/dist/vector-stores/chroma.store.js.map +1 -0
  363. package/dist/vector-stores/memory-vector-store.d.ts +20 -0
  364. package/dist/vector-stores/memory-vector-store.d.ts.map +1 -0
  365. package/dist/vector-stores/memory-vector-store.js +94 -0
  366. package/dist/vector-stores/memory-vector-store.js.map +1 -0
  367. package/dist/vector-stores/pinecone.store.d.ts +34 -0
  368. package/dist/vector-stores/pinecone.store.d.ts.map +1 -0
  369. package/dist/vector-stores/pinecone.store.js +146 -0
  370. package/dist/vector-stores/pinecone.store.js.map +1 -0
  371. package/dist/vector-stores/qdrant.store.d.ts +33 -0
  372. package/dist/vector-stores/qdrant.store.d.ts.map +1 -0
  373. package/dist/vector-stores/qdrant.store.js +174 -0
  374. package/dist/vector-stores/qdrant.store.js.map +1 -0
  375. package/dist/vector-stores/weaviate.store.d.ts +37 -0
  376. package/dist/vector-stores/weaviate.store.d.ts.map +1 -0
  377. package/dist/vector-stores/weaviate.store.js +226 -0
  378. package/dist/vector-stores/weaviate.store.js.map +1 -0
  379. package/package.json +146 -0
@@ -0,0 +1,153 @@
1
+ "use strict";
2
+ /**
3
+ * CommunityDetector
4
+ *
5
+ * Detects communities (clusters of closely-related entities) in the knowledge
6
+ * graph using the **Label Propagation Algorithm (LPA)**.
7
+ *
8
+ * Why LPA?
9
+ * - O(V + E) per iteration → scales well to thousands of entities
10
+ * - No parameters to tune (unlike Louvain's resolution parameter)
11
+ * - Converges in ~5–10 iterations for most real-world graphs
12
+ *
13
+ * Algorithm:
14
+ * 1. Assign each entity its own unique community label.
15
+ * 2. Shuffle entity iteration order (breaks symmetry for better convergence).
16
+ * 3. Each entity adopts the most frequent label among its neighbours,
17
+ * weighted by relationship strength.
18
+ * 4. Repeat until labels stabilise or max iterations reached.
19
+ * 5. Groups entities that share the same label into communities.
20
+ *
21
+ * Large-community splitting:
22
+ * If a community exceeds `maxCommunitySize`, it is recursively split by
23
+ * running LPA on the sub-graph of its members only.
24
+ */
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.CommunityDetector = void 0;
27
+ class CommunityDetector {
28
+ constructor(config = {}) {
29
+ this.maxIterations = config.maxIterations ?? 10;
30
+ this.maxCommunitySize = config.maxCommunitySize ?? 15;
31
+ }
32
+ /**
33
+ * Run community detection on the full graph.
34
+ * Returns a flat list of GraphCommunity objects (level 0 = leaf communities).
35
+ */
36
+ detect(graph) {
37
+ const entityIds = [...graph.entities.keys()];
38
+ if (entityIds.length === 0)
39
+ return [];
40
+ // Build weight map: entityId → [{ neighbourId, weight }]
41
+ const weightedAdj = this.buildWeightedAdjacency(graph);
42
+ const labels = this.runLPA(entityIds, weightedAdj);
43
+ const rawCommunities = this.groupByLabel(labels);
44
+ // Split oversized communities
45
+ const finalCommunities = this.splitOversized(rawCommunities, graph, 0);
46
+ return finalCommunities.map((memberIds, idx) => ({
47
+ id: `community_${idx}`,
48
+ entityIds: memberIds,
49
+ level: 0,
50
+ }));
51
+ }
52
+ // ── Private ───────────────────────────────────────────────────────────────
53
+ buildWeightedAdjacency(graph) {
54
+ const adj = new Map();
55
+ for (const entityId of graph.entities.keys()) {
56
+ adj.set(entityId, []);
57
+ }
58
+ for (const rel of graph.relationships.values()) {
59
+ const srcList = adj.get(rel.sourceId) ?? [];
60
+ const tgtList = adj.get(rel.targetId) ?? [];
61
+ srcList.push({ id: rel.targetId, weight: rel.weight });
62
+ tgtList.push({ id: rel.sourceId, weight: rel.weight });
63
+ adj.set(rel.sourceId, srcList);
64
+ adj.set(rel.targetId, tgtList);
65
+ }
66
+ return adj;
67
+ }
68
+ runLPA(entityIds, adj) {
69
+ // Initialise: each entity is its own community
70
+ const labels = new Map();
71
+ for (const id of entityIds)
72
+ labels.set(id, id);
73
+ for (let iter = 0; iter < this.maxIterations; iter++) {
74
+ const order = this.shuffle([...entityIds]);
75
+ let changed = false;
76
+ for (const entityId of order) {
77
+ const neighbours = adj.get(entityId) ?? [];
78
+ if (neighbours.length === 0)
79
+ continue;
80
+ // Tally label weights from neighbours
81
+ const labelWeights = new Map();
82
+ for (const { id, weight } of neighbours) {
83
+ const neighbourLabel = labels.get(id);
84
+ labelWeights.set(neighbourLabel, (labelWeights.get(neighbourLabel) ?? 0) + weight);
85
+ }
86
+ // Choose the label with the highest total weight
87
+ let bestLabel = labels.get(entityId);
88
+ let bestWeight = 0;
89
+ for (const [label, weight] of labelWeights.entries()) {
90
+ if (weight > bestWeight) {
91
+ bestWeight = weight;
92
+ bestLabel = label;
93
+ }
94
+ }
95
+ if (bestLabel !== labels.get(entityId)) {
96
+ labels.set(entityId, bestLabel);
97
+ changed = true;
98
+ }
99
+ }
100
+ if (!changed)
101
+ break;
102
+ }
103
+ return labels;
104
+ }
105
+ groupByLabel(labels) {
106
+ const groups = new Map();
107
+ for (const [entityId, label] of labels.entries()) {
108
+ const group = groups.get(label) ?? [];
109
+ group.push(entityId);
110
+ groups.set(label, group);
111
+ }
112
+ return [...groups.values()];
113
+ }
114
+ splitOversized(communities, graph, level) {
115
+ const result = [];
116
+ for (const community of communities) {
117
+ if (community.length <= this.maxCommunitySize || level >= 3) {
118
+ result.push(community);
119
+ continue;
120
+ }
121
+ // Build sub-graph adjacency for just this community's members
122
+ const memberSet = new Set(community);
123
+ const subAdj = new Map();
124
+ for (const id of community)
125
+ subAdj.set(id, []);
126
+ for (const rel of graph.relationships.values()) {
127
+ if (!memberSet.has(rel.sourceId) || !memberSet.has(rel.targetId))
128
+ continue;
129
+ subAdj.get(rel.sourceId).push({ id: rel.targetId, weight: rel.weight });
130
+ subAdj.get(rel.targetId).push({ id: rel.sourceId, weight: rel.weight });
131
+ }
132
+ const subLabels = this.runLPA(community, subAdj);
133
+ const subCommunities = this.groupByLabel(subLabels);
134
+ // Only split if we actually produced smaller groups
135
+ if (subCommunities.length > 1) {
136
+ result.push(...this.splitOversized(subCommunities, graph, level + 1));
137
+ }
138
+ else {
139
+ result.push(community);
140
+ }
141
+ }
142
+ return result;
143
+ }
144
+ shuffle(arr) {
145
+ for (let i = arr.length - 1; i > 0; i--) {
146
+ const j = Math.floor(Math.random() * (i + 1));
147
+ [arr[i], arr[j]] = [arr[j], arr[i]];
148
+ }
149
+ return arr;
150
+ }
151
+ }
152
+ exports.CommunityDetector = CommunityDetector;
153
+ //# sourceMappingURL=community-detector.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"community-detector.js","sourceRoot":"","sources":["../../src/graph/community-detector.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;;;AAUH,MAAa,iBAAiB;IAI5B,YAAY,SAAkC,EAAE;QAC9C,IAAI,CAAC,aAAa,GAAG,MAAM,CAAC,aAAa,IAAI,EAAE,CAAC;QAChD,IAAI,CAAC,gBAAgB,GAAG,MAAM,CAAC,gBAAgB,IAAI,EAAE,CAAC;IACxD,CAAC;IAED;;;OAGG;IACH,MAAM,CAAC,KAAiB;QACtB,MAAM,SAAS,GAAG,CAAC,GAAG,KAAK,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC;QAC7C,IAAI,SAAS,CAAC,MAAM,KAAK,CAAC;YAAE,OAAO,EAAE,CAAC;QAEtC,yDAAyD;QACzD,MAAM,WAAW,GAAG,IAAI,CAAC,sBAAsB,CAAC,KAAK,CAAC,CAAC;QAEvD,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,WAAW,CAAC,CAAC;QACnD,MAAM,cAAc,GAAG,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC;QAEjD,8BAA8B;QAC9B,MAAM,gBAAgB,GAAG,IAAI,CAAC,cAAc,CAAC,cAAc,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC;QAEvE,OAAO,gBAAgB,CAAC,GAAG,CAAC,CAAC,SAAS,EAAE,GAAG,EAAE,EAAE,CAAC,CAAC;YAC/C,EAAE,EAAE,aAAa,GAAG,EAAE;YACtB,SAAS,EAAE,SAAS;YACpB,KAAK,EAAE,CAAC;SACT,CAAC,CAAC,CAAC;IACN,CAAC;IAED,6EAA6E;IAErE,sBAAsB,CAC5B,KAAiB;QAEjB,MAAM,GAAG,GAAG,IAAI,GAAG,EAAiD,CAA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@ -0,0 +1,41 @@
1
+ /**
2
+ * CommunitySummarizer
3
+ *
4
+ * Generates LLM-powered community reports for each detected community.
5
+ * Each report contains:
6
+ * - A one-line title capturing the community's central theme
7
+ * - A 2-4 paragraph narrative summary
8
+ * - Bullet-point key findings
9
+ * - An importance rating 1–10
10
+ *
11
+ * These reports are the foundation of GraphRAG's **global search** mode:
12
+ * instead of searching raw text, the system searches community reports,
13
+ * which capture cross-document themes that are invisible to chunk-level
14
+ * vector search.
15
+ *
16
+ * Performance note: reports are generated in parallel (Promise.all) with
17
+ * a configurable concurrency limit so we don't overwhelm the LLM rate limit.
18
+ */
19
+ import type { GraphCommunity, CommunityReport } from './graph.types';
20
+ import type { GraphStore } from './knowledge-graph';
21
+ import '../prompts/community-summary.prompt';
22
+ export interface CommunitySummarizerConfig {
23
+ llm: (prompt: string) => Promise<string>;
24
+ /** Max concurrent LLM calls. Default: 3. */
25
+ concurrency?: number;
26
+ }
27
+ export declare class CommunitySummarizer {
28
+ private readonly llm;
29
+ private readonly concurrency;
30
+ constructor(config: CommunitySummarizerConfig);
31
+ /**
32
+ * Generate a CommunityReport for every community in `communities`.
33
+ * Returns the reports in the same order as the input array.
34
+ */
35
+ summarizeAll(communities: GraphCommunity[], graph: GraphStore): Promise<CommunityReport[]>;
36
+ private summarizeCommunity;
37
+ private buildSummaryPrompt;
38
+ private parseReport;
39
+ private buildFallbackReport;
40
+ }
41
+ //# sourceMappingURL=community-summarizer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"community-summarizer.d.ts","sourceRoot":"","sources":["../../src/graph/community-summarizer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AACrE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAEpD,OAAO,qCAAqC,CAAC;AAG7C,MAAM,WAAW,yBAAyB;IACxC,GAAG,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;IACzC,4CAA4C;IAC5C,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,qBAAa,mBAAmB;IAC9B,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAsC;IAC1D,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAS;gBAEzB,MAAM,EAAE,yBAAyB;IAK7C;;;OAGG;IACG,YAAY,CAAC,WAAW,EAAE,cAAc,EAAE,EAAE,KAAK,EAAE,UAAU,GAAG,OAAO,CAAC,eAAe,EAAE,CAAC;YAkBlF,kBAAkB;IAyChC,OAAO,CAAC,kBAAkB;IAkB1B,OAAO,CAAC,WAAW;IA2BnB,OAAO,CAAC,mBAAmB;CAU5B"}
@@ -0,0 +1,119 @@
1
+ "use strict";
2
+ /**
3
+ * CommunitySummarizer
4
+ *
5
+ * Generates LLM-powered community reports for each detected community.
6
+ * Each report contains:
7
+ * - A one-line title capturing the community's central theme
8
+ * - A 2-4 paragraph narrative summary
9
+ * - Bullet-point key findings
10
+ * - An importance rating 1–10
11
+ *
12
+ * These reports are the foundation of GraphRAG's **global search** mode:
13
+ * instead of searching raw text, the system searches community reports,
14
+ * which capture cross-document themes that are invisible to chunk-level
15
+ * vector search.
16
+ *
17
+ * Performance note: reports are generated in parallel (Promise.all) with
18
+ * a configurable concurrency limit so we don't overwhelm the LLM rate limit.
19
+ */
20
+ Object.defineProperty(exports, "__esModule", { value: true });
21
+ exports.CommunitySummarizer = void 0;
22
+ const prompts_1 = require("@hazeljs/prompts");
23
+ require("../prompts/community-summary.prompt");
24
+ const community_summary_prompt_1 = require("../prompts/community-summary.prompt");
25
+ class CommunitySummarizer {
26
+ constructor(config) {
27
+ this.llm = config.llm;
28
+ this.concurrency = config.concurrency ?? 3;
29
+ }
30
+ /**
31
+ * Generate a CommunityReport for every community in `communities`.
32
+ * Returns the reports in the same order as the input array.
33
+ */
34
+ async summarizeAll(communities, graph) {
35
+ if (communities.length === 0)
36
+ return [];
37
+ const results = [];
38
+ // Process in batches respecting concurrency limit
39
+ for (let i = 0; i < communities.length; i += this.concurrency) {
40
+ const batch = communities.slice(i, i + this.concurrency);
41
+ const batchResults = await Promise.all(batch.map((community) => this.summarizeCommunity(community, graph)));
42
+ results.push(...batchResults);
43
+ }
44
+ return results;
45
+ }
46
+ // ── Private ───────────────────────────────────────────────────────────────
47
+ async summarizeCommunity(community, graph) {
48
+ const entities = community.entityIds
49
+ .map((id) => graph.entities.get(id))
50
+ .filter((e) => e !== undefined);
51
+ const relDescriptions = [];
52
+ for (const rel of graph.relationships.values()) {
53
+ if (community.entityIds.includes(rel.sourceId) &&
54
+ community.entityIds.includes(rel.targetId)) {
55
+ const src = graph.entities.get(rel.sourceId);
56
+ const tgt = graph.entities.get(rel.targetId);
57
+ if (src && tgt) {
58
+ relDescriptions.push(`${src.name} ${rel.type} ${tgt.name}: ${rel.description} (weight: ${rel.weight})`);
59
+ }
60
+ }
61
+ }
62
+ const entityDescriptions = entities.map((e) => `- ${e.name} [${e.type}]: ${e.description}`);
63
+ const prompt = this.buildSummaryPrompt('', entityDescriptions, relDescriptions);
64
+ try {
65
+ const raw = await this.llm(prompt);
66
+ return this.parseReport(community.id, community.entityIds, raw);
67
+ }
68
+ catch {
69
+ // Fallback: build a simple report without LLM
70
+ return this.buildFallbackReport(community, entities.map((e) => e.name));
71
+ }
72
+ }
73
+ buildSummaryPrompt(_communityId, entityDescriptions, relDescriptions) {
74
+ const relText = relDescriptions.length > 0
75
+ ? relDescriptions.join('\n')
76
+ : '(no intra-community relationships found)';
77
+ return prompts_1.PromptRegistry.get(community_summary_prompt_1.COMMUNITY_SUMMARY_KEY).render({
78
+ entityDescriptions: entityDescriptions.join('\n'),
79
+ relDescriptions: relText,
80
+ });
81
+ }
82
+ parseReport(communityId, entityIds, raw) {
83
+ const cleaned = raw
84
+ .replace(/```json\s*/gi, '')
85
+ .replace(/```\s*/g, '')
86
+ .trim();
87
+ let parsed;
88
+ try {
89
+ parsed = JSON.parse(cleaned);
90
+ }
91
+ catch {
92
+ const match = cleaned.match(/\{[\s\S]*\}/);
93
+ if (!match)
94
+ throw new Error('No JSON in community summary response');
95
+ parsed = JSON.parse(match[0]);
96
+ }
97
+ const obj = parsed;
98
+ return {
99
+ communityId,
100
+ title: String(obj.title ?? 'Untitled Community'),
101
+ summary: String(obj.summary ?? ''),
102
+ findings: Array.isArray(obj.findings) ? obj.findings.map(String) : [],
103
+ rating: Math.min(10, Math.max(1, Number(obj.rating) || 5)),
104
+ entityIds,
105
+ };
106
+ }
107
+ buildFallbackReport(community, entityNames) {
108
+ return {
109
+ communityId: community.id,
110
+ title: `Community: ${entityNames.slice(0, 3).join(', ')}${entityNames.length > 3 ? '...' : ''}`,
111
+ summary: `This community contains ${entityNames.length} related entities: ${entityNames.join(', ')}.`,
112
+ findings: entityNames.map((name) => `Contains entity: ${name}`),
113
+ rating: 5,
114
+ entityIds: community.entityIds,
115
+ };
116
+ }
117
+ }
118
+ exports.CommunitySummarizer = CommunitySummarizer;
119
+ //# sourceMappingURL=community-summarizer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"community-summarizer.js","sourceRoot":"","sources":["../../src/graph/community-summarizer.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;GAiBG;;;AAIH,8CAAkD;AAClD,+CAA6C;AAC7C,kFAA4E;AAQ5E,MAAa,mBAAmB;IAI9B,YAAY,MAAiC;QAC3C,IAAI,CAAC,GAAG,GAAG,MAAM,CAAC,GAAG,CAAC;QACtB,IAAI,CAAC,WAAW,GAAG,MAAM,CAAC,WAAW,IAAI,CAAC,CAAC;IAC7C,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,YAAY,CAAC,WAA6B,EAAE,KAAiB;QACjE,IAAI,WAAW,CAAC,MAAM,KAAK,CAAC;YAAE,OAAO,EAAE,CAAC;QAExC,MAAM,OAAO,GAAsB,EAAE,CAAC;QACtC,kDAAkD;QAClD,KAAK,IA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AAW,EAAE,SAAS,CAAC,EAAE;YACzB,KAAK,EAAE,cAAc,WAAW,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,WAAW,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,EAAE;YAC/F,OAAO,EAAE,2BAA2B,WAAW,CAAC,MAAM,sBAAsB,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG;YACrG,QAAQ,EAAE,WAAW,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,oBAAoB,IAAI,EAAE,CAAC;YAC/D,MAAM,EAAE,CAAC;YACT,SAAS,EAAE,SAAS,CAAC,SAAS;SAC/B,CAAC;IACJ,CAAC;CACF;AA/HD,kDA+HC"}
@@ -0,0 +1,47 @@
1
+ /**
2
+ * EntityExtractor
3
+ *
4
+ * Uses an LLM to extract (entity, type, description) nodes and
5
+ * (source, type, target, description, weight) relationship edges from raw text.
6
+ *
7
+ * The extractor:
8
+ * 1. Splits the input into chunks of `chunkSize` characters.
9
+ * 2. Sends each chunk to the LLM with a structured JSON extraction prompt.
10
+ * 3. Parses and validates the JSON response.
11
+ * 4. Deduplicates entities by normalized name (case-insensitive).
12
+ * 5. Merges relationships that reference the same entity pair+type.
13
+ *
14
+ * LLM output contract — the model MUST return valid JSON matching:
15
+ * {
16
+ * "entities": [{ "name": string, "type": EntityType, "description": string }],
17
+ * "relationships": [{ "source": string, "target": string, "type": RelType, "description": string, "weight": 1-10 }]
18
+ * }
19
+ */
20
+ import type { ExtractionResult } from './graph.types';
21
+ import '../prompts/entity-extraction.prompt';
22
+ export interface EntityExtractorConfig {
23
+ llm: (prompt: string) => Promise<string>;
24
+ chunkSize?: number;
25
+ }
26
+ export declare class EntityExtractor {
27
+ private readonly llm;
28
+ private readonly chunkSize;
29
+ constructor(config: EntityExtractorConfig);
30
+ /**
31
+ * Extract entities and relationships from an array of text chunks.
32
+ * Returns a merged ExtractionResult with deduplicated entities.
33
+ */
34
+ extract(texts: string[], _sourceDocId: string): Promise<ExtractionResult>;
35
+ private extractFromSegment;
36
+ private buildExtractionPrompt;
37
+ private parseResponse;
38
+ private normalizeEntityType;
39
+ private normalizeRelType;
40
+ /**
41
+ * Merge multiple extraction results, deduplicating entities by
42
+ * normalized (lower-case, trimmed) name and combining descriptions.
43
+ */
44
+ private mergeResults;
45
+ private splitText;
46
+ }
47
+ //# sourceMappingURL=entity-extractor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"entity-extractor.d.ts","sourceRoot":"","sources":["../../src/graph/entity-extractor.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAgC,MAAM,eAAe,CAAC;AAEpF,OAAO,qCAAqC,CAAC;AA8B7C,MAAM,WAAW,qBAAqB;IACpC,GAAG,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;IACzC,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,qBAAa,eAAe;IAC1B,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAsC;IAC1D,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAS;gBAEvB,MAAM,EAAE,qBAAqB;IAKzC;;;OAGG;IACG,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC;YAuBjE,kBAAkB;IAMhC,OAAO,CAAC,qBAAqB;IAU7B,OAAO,CAAC,aAAa;IA0DrB,OAAO,CAAC,mBAAmB;IAK3B,OAAO,CAAC,gBAAgB;IAKxB;;;OAGG;IACH,OAAO,CAAC,YAAY;IAyEpB,OAAO,CAAC,SAAS;CAmBlB"}
@@ -0,0 +1,224 @@
1
+ "use strict";
2
+ /**
3
+ * EntityExtractor
4
+ *
5
+ * Uses an LLM to extract (entity, type, description) nodes and
6
+ * (source, type, target, description, weight) relationship edges from raw text.
7
+ *
8
+ * The extractor:
9
+ * 1. Splits the input into chunks of `chunkSize` characters.
10
+ * 2. Sends each chunk to the LLM with a structured JSON extraction prompt.
11
+ * 3. Parses and validates the JSON response.
12
+ * 4. Deduplicates entities by normalized name (case-insensitive).
13
+ * 5. Merges relationships that reference the same entity pair+type.
14
+ *
15
+ * LLM output contract — the model MUST return valid JSON matching:
16
+ * {
17
+ * "entities": [{ "name": string, "type": EntityType, "description": string }],
18
+ * "relationships": [{ "source": string, "target": string, "type": RelType, "description": string, "weight": 1-10 }]
19
+ * }
20
+ */
21
+ Object.defineProperty(exports, "__esModule", { value: true });
22
+ exports.EntityExtractor = void 0;
23
+ const prompts_1 = require("@hazeljs/prompts");
24
+ require("../prompts/entity-extraction.prompt");
25
+ const entity_extraction_prompt_1 = require("../prompts/entity-extraction.prompt");
26
+ const ENTITY_TYPES = [
27
+ 'CONCEPT',
28
+ 'TECHNOLOGY',
29
+ 'PERSON',
30
+ 'ORGANIZATION',
31
+ 'PROCESS',
32
+ 'FEATURE',
33
+ 'EVENT',
34
+ 'LOCATION',
35
+ 'OTHER',
36
+ ];
37
+ const RELATIONSHIP_TYPES = [
38
+ 'USES',
39
+ 'IMPLEMENTS',
40
+ 'CREATED_BY',
41
+ 'PART_OF',
42
+ 'DEPENDS_ON',
43
+ 'RELATED_TO',
44
+ 'EXTENDS',
45
+ 'CONFIGURES',
46
+ 'TRIGGERS',
47
+ 'PRODUCES',
48
+ 'REPLACES',
49
+ 'OTHER',
50
+ ];
51
+ class EntityExtractor {
52
+ constructor(config) {
53
+ this.llm = config.llm;
54
+ this.chunkSize = config.chunkSize ?? 2000;
55
+ }
56
+ /**
57
+ * Extract entities and relationships from an array of text chunks.
58
+ * Returns a merged ExtractionResult with deduplicated entities.
59
+ */
60
+ async extract(texts, _sourceDocId) {
61
+ const allResults = [];
62
+ for (const text of texts) {
63
+ // Further split if text exceeds chunkSize
64
+ const segments = this.splitText(text, this.chunkSize);
65
+ for (const segment of segments) {
66
+ try {
67
+ const result = await this.extractFromSegment(segment);
68
+ allResults.push(result);
69
+ }
70
+ catch {
71
+ // Silently skip unparseable LLM responses — the graph will still be
72
+ // populated from other chunks.
73
+ }
74
+ }
75
+ }
76
+ return this.mergeResults(allResults);
77
+ }
78
+ // ── Private helpers ───────────────────────────────────────────────────────
79
+ async extractFromSegment(text) {
80
+ const prompt = this.buildExtractionPrompt(text);
81
+ const raw = await this.llm(prompt);
82
+ return this.parseResponse(raw);
83
+ }
84
+ buildExtractionPrompt(text) {
85
+ return prompts_1.PromptRegistry.get(entity_extraction_prompt_1.ENTITY_EXTRACTION_KEY).render({
86
+ text,
87
+ entityTypes: ENTITY_TYPES.join(', '),
88
+ relationshipTypes: RELATIONSHIP_TYPES.join(', '),
89
+ });
90
+ }
91
+ parseResponse(raw) {
92
+ // Strip markdown code fences if present
93
+ const cleaned = raw
94
+ .replace(/```json\s*/gi, '')
95
+ .replace(/```\s*/g, '')
96
+ .trim();
97
+ let parsed;
98
+ try {
99
+ parsed = JSON.parse(cleaned);
100
+ }
101
+ catch {
102
+ // Try to extract JSON object from the response using a regex
103
+ const match = cleaned.match(/\{[\s\S]*\}/);
104
+ if (!match)
105
+ throw new Error('No JSON object found in LLM response');
106
+ parsed = JSON.parse(match[0]);
107
+ }
108
+ if (typeof parsed !== 'object' ||
109
+ parsed === null ||
110
+ !Array.isArray(parsed.entities) ||
111
+ !Array.isArray(parsed.relationships)) {
112
+ throw new Error('Invalid extraction response shape');
113
+ }
114
+ const raw_ = parsed;
115
+ return {
116
+ entities: raw_.entities
117
+ .filter((e) => e.name && e.type && e.description)
118
+ .map((e) => ({
119
+ name: String(e.name).trim(),
120
+ type: this.normalizeEntityType(String(e.type)),
121
+ description: String(e.description).trim(),
122
+ })),
123
+ relationships: raw_.relationships
124
+ .filter((r) => r.source && r.target && r.type && r.description)
125
+ .map((r) => ({
126
+ source: String(r.source).trim(),
127
+ target: String(r.target).trim(),
128
+ type: this.normalizeRelType(String(r.type)),
129
+ description: String(r.description).trim(),
130
+ weight: Math.min(10, Math.max(1, Number(r.weight) || 5)),
131
+ })),
132
+ };
133
+ }
134
+ normalizeEntityType(raw) {
135
+ const upper = raw.toUpperCase();
136
+ return ENTITY_TYPES.includes(upper) ? upper : 'OTHER';
137
+ }
138
+ normalizeRelType(raw) {
139
+ const upper = raw.toUpperCase();
140
+ return RELATIONSHIP_TYPES.includes(upper) ? upper : 'OTHER';
141
+ }
142
+ /**
143
+ * Merge multiple extraction results, deduplicating entities by
144
+ * normalized (lower-case, trimmed) name and combining descriptions.
145
+ */
146
+ mergeResults(results) {
147
+ // Deduplicate entities
148
+ const entityMap = new Map();
149
+ for (const result of results) {
150
+ for (const entity of result.entities) {
151
+ const key = entity.name.toLowerCase().trim();
152
+ const existing = entityMap.get(key);
153
+ if (existing) {
154
+ // Merge descriptions when they add new information
155
+ if (!existing.description.includes(entity.description)) {
156
+ existing.description = `${existing.description}. ${entity.description}`;
157
+ }
158
+ }
159
+ else {
160
+ entityMap.set(key, { ...entity });
161
+ }
162
+ }
163
+ }
164
+ // Build a canonical name map (normalized → canonical)
165
+ const canonicalName = new Map();
166
+ for (const [key, entity] of entityMap.entries()) {
167
+ canonicalName.set(key, entity.name);
168
+ }
169
+ // Deduplicate and normalise relationships
170
+ const relMap = new Map();
171
+ for (const result of results) {
172
+ for (const rel of result.relationships) {
173
+ const srcKey = rel.source.toLowerCase().trim();
174
+ const tgtKey = rel.target.toLowerCase().trim();
175
+ const srcCanon = canonicalName.get(srcKey) ?? rel.source;
176
+ const tgtCanon = canonicalName.get(tgtKey) ?? rel.target;
177
+ // Only keep relationships between known entities
178
+ if (!canonicalName.has(srcKey) || !canonicalName.has(tgtKey))
179
+ continue;
180
+ const relKey = `${srcCanon}::${tgtCanon}::${rel.type}`;
181
+ const existing = relMap.get(relKey);
182
+ if (existing) {
183
+ existing.weight = Math.max(existing.weight, rel.weight);
184
+ if (!existing.description.includes(rel.description)) {
185
+ existing.description = `${existing.description}. ${rel.description}`;
186
+ }
187
+ }
188
+ else {
189
+ relMap.set(relKey, {
190
+ source: srcCanon,
191
+ target: tgtCanon,
192
+ type: rel.type,
193
+ description: rel.description,
194
+ weight: rel.weight,
195
+ });
196
+ }
197
+ }
198
+ }
199
+ return {
200
+ entities: [...entityMap.values()],
201
+ relationships: [...relMap.values()],
202
+ };
203
+ }
204
+ splitText(text, maxLen) {
205
+ if (text.length <= maxLen)
206
+ return [text];
207
+ const chunks = [];
208
+ let start = 0;
209
+ while (start < text.length) {
210
+ let end = Math.min(start + maxLen, text.length);
211
+ // Try to break at a paragraph boundary
212
+ if (end < text.length) {
213
+ const breakAt = text.lastIndexOf('\n\n', end);
214
+ if (breakAt > start + maxLen * 0.5)
215
+ end = breakAt + 2;
216
+ }
217
+ chunks.push(text.slice(start, end).trim());
218
+ start = end;
219
+ }
220
+ return chunks.filter((c) => c.length > 0);
221
+ }
222
+ }
223
+ exports.EntityExtractor = EntityExtractor;
224
+ //# sourceMappingURL=entity-extractor.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"entity-extractor.js","sourceRoot":"","sources":["../../src/graph/entity-extractor.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;GAkBG;;;AAGH,8CAAkD;AAClD,+CAA6C;AAC7C,kFAA4E;AAE5E,MAAM,YAAY,GAAiB;IACjC,SAAS;IACT,YAAY;IACZ,QAAQ;IACR,cAAc;IACd,SAAS;IACT,SAAS;IACT,OAAO;IACP,UAAU;IACV,OAAO;CACR,CAAC;AAEF,MAAM,kBAAkB,GAAuB;IAC7C,MAAM;IACN,YAAY;IACZ,YAAY;IACZ,SAAS;IACT,YAAY;IACZ,YAAY;IACZ,SAAS;IACT,YAAY;IACZ,UAAU;IACV,UAAU;IACV,UAAU;IACV,OAAO;CACR,CAAC;AAOF,MAAa,eAAe;IAI1B,YAAY,MAA6B;QACvC,IAAI,CAAC,GAAG,GAAG,MAAM,CAAC,GAA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