@aliyunrds/ctxdb 1.0.1-beta.2 → 1.0.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.
@@ -5,6 +5,14 @@ import { readFileSync, existsSync, statSync } from "fs";
5
5
  import { basename, extname } from "path";
6
6
  import { homedir } from "os";
7
7
  import { resolve } from "path";
8
+ import {
9
+ buildNormalizedGraphContext,
10
+ extractKnowledgeBaseIds,
11
+ extractKnowledgeChunkContent,
12
+ extractKnowledgeChunkSourceLabel,
13
+ extractKnowledgeChunkTags,
14
+ partitionKnowledgeChunks
15
+ } from "@aliyunrds/ctxdb-shared";
8
16
  var KB_COLLECTION = "/v1/knowledge/knowledge_bases";
9
17
  var DOCUMENTS = "/v1/knowledge/documents";
10
18
  var FILES = "/v1/knowledge/files";
@@ -135,13 +143,12 @@ function expandHome(p) {
135
143
  }
136
144
  function compactChunk(raw) {
137
145
  const c = raw ?? {};
138
- const content = c.content_with_weight ?? c.content ?? c.chunk ?? c.text ?? "";
139
- const doc_name = c.docnm_kwd ?? c.doc_name ?? c.docnm ?? "";
140
- const kbRaw = c.kb_id ?? c.dataset_id;
141
- const kb_id = Array.isArray(kbRaw) ? typeof kbRaw[0] === "string" ? kbRaw[0] : "" : typeof kbRaw === "string" ? kbRaw : "";
146
+ const content = extractKnowledgeChunkContent(raw);
147
+ const doc_name = extractKnowledgeChunkSourceLabel(raw);
148
+ const kb_id = extractKnowledgeBaseIds(raw)[0] ?? "";
142
149
  const docIdRaw = c.doc_id;
143
150
  const scoreRaw = c.similarity ?? c.rerank_score;
144
- const tagsRaw = c.tag_kwd;
151
+ const tags = extractKnowledgeChunkTags(raw);
145
152
  const out = {
146
153
  content,
147
154
  doc_name,
@@ -151,34 +158,35 @@ function compactChunk(raw) {
151
158
  if (typeof docIdRaw === "string" && docIdRaw.length > 0) {
152
159
  out.doc_id = docIdRaw;
153
160
  }
154
- if (Array.isArray(tagsRaw) && tagsRaw.length > 0) {
155
- out.tags = tagsRaw;
161
+ if (tags.length > 0) {
162
+ out.tags = tags;
156
163
  }
157
164
  return out;
158
165
  }
159
- function compactKbQueryResponse(raw) {
166
+ function projectKbQueryResponse(raw, projectChunk, options = {}) {
160
167
  if (!raw || typeof raw !== "object") return { chunks: [] };
161
168
  const r = raw;
162
169
  const chunksIn = Array.isArray(r.chunks) ? r.chunks : [];
170
+ const { graphChunks, documentChunks } = partitionKnowledgeChunks(chunksIn);
171
+ const graphContext = buildNormalizedGraphContext(graphChunks, {
172
+ verbose: options.verboseGraph
173
+ });
163
174
  const out = {
164
- chunks: chunksIn.map(compactChunk)
175
+ chunks: documentChunks.map(projectChunk)
165
176
  };
166
177
  if (typeof r.total === "number") out.total = r.total;
178
+ if (graphContext) out.graph_context = graphContext;
167
179
  return out;
168
180
  }
181
+ function compactKbQueryResponse(raw) {
182
+ return projectKbQueryResponse(raw, compactChunk, { verboseGraph: true });
183
+ }
169
184
  function minimalChunk(raw) {
170
185
  const c = compactChunk(raw);
171
186
  return { content: c.content, score: c.score };
172
187
  }
173
188
  function minimalKbQueryResponse(raw) {
174
- if (!raw || typeof raw !== "object") return { chunks: [] };
175
- const r = raw;
176
- const chunksIn = Array.isArray(r.chunks) ? r.chunks : [];
177
- const out = {
178
- chunks: chunksIn.map(minimalChunk)
179
- };
180
- if (typeof r.total === "number") out.total = r.total;
181
- return out;
189
+ return projectKbQueryResponse(raw, minimalChunk);
182
190
  }
183
191
 
184
192
  export {
@@ -191,8 +199,6 @@ export {
191
199
  getDocument,
192
200
  listDocuments,
193
201
  pollIngest,
194
- compactChunk,
195
202
  compactKbQueryResponse,
196
- minimalChunk,
197
203
  minimalKbQueryResponse
198
204
  };