代码提交
This commit is contained in:
1 parent
96bfdacee5
commit
067f602732
1 file changed
+48
-21
@@ -168,33 +168,60 @@ function parseKnowledgeBaseChunk(jsonText: string): KnowledgeBaseChunk {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function flattenDocs(value: unknown): Record<string, unknown>[] {
|
/**
|
||||||
if (Array.isArray(value)) {
|
* Mirrors GeneralQA's `mapDocsToSources` behavior: `docs` may be an array,
|
||||||
return value.flatMap((item) => flattenDocs(item));
|
* a single document, or a plain string. The previous adapter only accepted
|
||||||
|
* objects and consequently dropped the first stream payload whenever the
|
||||||
|
* knowledge-base service returned a string document.
|
||||||
|
*/
|
||||||
|
function normalizeSources(value: KnowledgeBaseChunk["docs"]): RagSource[] {
|
||||||
|
const docs = Array.isArray(value) ? value : value ? [value] : [];
|
||||||
|
return docs.flatMap((item, index): RagSource[] => {
|
||||||
|
if (typeof item === "string") {
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
title: `参考资料 ${index + 1}`,
|
||||||
|
type: "知识库",
|
||||||
|
snippet: item,
|
||||||
|
content: item,
|
||||||
|
raw: { content: item },
|
||||||
|
},
|
||||||
|
];
|
||||||
}
|
}
|
||||||
if (!value || typeof value !== "object") return [];
|
if (!item || typeof item !== "object") return [];
|
||||||
const record = value as Record<string, unknown>;
|
|
||||||
if ("docs" in record) return flattenDocs(record.docs);
|
|
||||||
return [record];
|
|
||||||
}
|
|
||||||
|
|
||||||
function normalizeSources(value: unknown): RagSource[] {
|
const record = item as Record<string, unknown>;
|
||||||
return flattenDocs(value).map((item, index) => {
|
const title = String(
|
||||||
const title = String(item.m_title ?? item.title ?? item.source ?? `参考资料 ${index + 1}`);
|
record.title ?? record.m_title ?? record.source ?? `参考资料 ${index + 1}`,
|
||||||
const content = String(item.m_content ?? item.content ?? item.snippet ?? "");
|
);
|
||||||
const source = item.source1 ?? item.source;
|
const content = String(
|
||||||
const rawUrl = item.url ?? item.link ?? item.href;
|
record.content ?? record.m_content ?? record.snippet ?? record.url ?? "",
|
||||||
return {
|
);
|
||||||
id: String(item.recUuid ?? item.rec_uuid ?? item.uuid ?? item.id ?? "") || undefined,
|
const source = record.source ?? record.source1 ?? record.title ?? record.m_title;
|
||||||
|
const rawUrl = record.url ?? record.link ?? record.href;
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
id:
|
||||||
|
String(record.recUuid ?? record.rec_uuid ?? record.uuid ?? record.id ?? "") ||
|
||||||
|
undefined,
|
||||||
title,
|
title,
|
||||||
type: String(item.source1 ?? item.type ?? "知识库"),
|
type: String(record.source1 ?? record.type ?? "知识库"),
|
||||||
snippet: content,
|
snippet: content,
|
||||||
content,
|
content,
|
||||||
source: typeof source === "string" ? source : undefined,
|
source: typeof source === "string" ? source : undefined,
|
||||||
time: typeof item.m_publish === "string" ? item.m_publish : undefined,
|
time:
|
||||||
url: typeof rawUrl === "string" && /^https?:\/\//i.test(rawUrl) ? rawUrl : undefined,
|
typeof record.m_publish === "string"
|
||||||
raw: item,
|
? record.m_publish
|
||||||
};
|
: typeof record.time === "string"
|
||||||
|
? record.time
|
||||||
|
: undefined,
|
||||||
|
url:
|
||||||
|
typeof rawUrl === "string" && /^https?:\/\//i.test(rawUrl)
|
||||||
|
? rawUrl
|
||||||
|
: undefined,
|
||||||
|
raw: record,
|
||||||
|
},
|
||||||
|
];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user