Skip to content

Commit 967e622

Browse files
authored
If chunks only is enabled the sousrces are get from the object metadata of the response
1 parent 45638ed commit 967e622

1 file changed

Lines changed: 65 additions & 7 deletions

File tree

src/app/knowledge-bases/modals/modal-preview-knowledge-base/modal-preview-knowledge-base.component.ts

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -706,13 +706,15 @@ export class ModalPreviewKnowledgeBaseComponent extends PricingBaseComponent imp
706706
this.logger.log("[MODAL-PREVIEW-KB] ask gpt preview qa: ", this.qa)
707707
this.logger.log("ask gpt preview this.qa?.content_chunks: ", this.qa?.content_chunks);
708708
this.logger.log("ask gpt preview this.qa?.chunks: ", this.qa?.chunks);
709-
if (this.qa?.content_chunks) {
710-
this.contentChunks = this.qa?.content_chunks
711-
} else if (this.qa?.chunks) {
712-
this.contentChunks = this.qa?.chunks
713-
}
709+
// if (this.qa?.content_chunks) {
710+
// this.contentChunks = this.qa?.content_chunks
711+
// } else if (this.qa?.chunks) {
712+
// this.contentChunks = this.qa?.chunks
713+
// }
714714

715-
this.contentSources = this.extractAllSources(response);
715+
// this.contentSources = this.extractAllSources(response);
716+
this.contentChunks = this.extractContentChunks(response);
717+
this.contentSources = this.extractSourcesForPreview(response);
716718
this.logger.log("ask gpt preview contentChunks: ", this.contentChunks);
717719
this.logger.log("ask gpt preview contentSources: ", this.contentSources);
718720
// this.logger.log("ask gpt preview response: ", response, startTime, endTime, this.responseTime);
@@ -973,13 +975,40 @@ export class ModalPreviewKnowledgeBaseComponent extends PricingBaseComponent imp
973975
}
974976
}
975977

978+
/** Chunk list: `content_chunks` (default) or `chunks` when chunks-only mode. */
979+
private extractContentChunks(response: any): string[] {
980+
if (!response) {
981+
return [];
982+
}
983+
if (Array.isArray(response.chunks) && response.chunks.length) {
984+
return response.chunks;
985+
}
986+
if (Array.isArray(response.content_chunks) && response.content_chunks.length) {
987+
return response.content_chunks;
988+
}
989+
return [];
990+
}
991+
992+
/** Fonti per risposta non-stream (include metadata quando chunks-only). */
993+
private extractSourcesForPreview(response: any): { value: string; isUrl: boolean }[] {
994+
if (!response) {
995+
return [];
996+
}
997+
if (this.chunkOnly && Array.isArray(response.metadata)) {
998+
return this.extractSourcesFromMetadata(response.metadata);
999+
}
1000+
return this.extractAllSources(response);
1001+
}
1002+
9761003
/**
9771004
* Estrae tutte le fonti da response.sources (array) o response.source (stringa).
9781005
* Supporta items come stringa o oggetto con .url, .href, .source.
9791006
*/
9801007
private extractAllSources(response: any): { value: string; isUrl: boolean }[] {
9811008
const out: { value: string; isUrl: boolean }[] = [];
982-
if (!response) return out;
1009+
if (!response) {
1010+
return out;
1011+
}
9831012
if (response.sources && Array.isArray(response.sources)) {
9841013
for (const item of response.sources) {
9851014
const value = typeof item === 'string' ? item : String(item?.url ?? item?.href ?? item?.source ?? item ?? '');
@@ -996,6 +1025,35 @@ export class ModalPreviewKnowledgeBaseComponent extends PricingBaseComponent imp
9961025
return out;
9971026
}
9981027

1028+
private extractSourcesFromMetadata(metadata: any[]): { value: string; isUrl: boolean }[] {
1029+
const out: { value: string; isUrl: boolean }[] = [];
1030+
const seen = new Set<string>();
1031+
for (const item of metadata) {
1032+
if (!item) {
1033+
continue;
1034+
}
1035+
const source = String(item.source ?? '').trim();
1036+
const fileName = String(item.file_name ?? '').trim();
1037+
const type = String(item.type ?? '').toLowerCase();
1038+
let value = '';
1039+
let isUrl = false;
1040+
if (source && (type === 'url' || this.isValidURL(source))) {
1041+
value = source;
1042+
isUrl = true;
1043+
} else if (fileName) {
1044+
value = fileName;
1045+
} else if (source) {
1046+
value = source;
1047+
}
1048+
if (!value || seen.has(value)) {
1049+
continue;
1050+
}
1051+
seen.add(value);
1052+
out.push({ value, isUrl });
1053+
}
1054+
return out;
1055+
}
1056+
9991057
onTextareaInput(textarea: HTMLTextAreaElement): void {
10001058
// console.log('[MODAL-PREVIEW-KB] textarea', textarea )
10011059
const minHeight = 37;

0 commit comments

Comments
 (0)