Compare commits

...

9 Commits

Author SHA1 Message Date
sweep-ai[bot] 1bc0d544e8 Merge main into sweep/json-support_2 2023-08-15 16:04:07 +00:00
sweep-ai[bot] 9a0bb2b1d0 Updated packages/core/src/indices/list/ListIndex.ts 2023-08-14 17:50:29 +00:00
sweep-ai[bot] d8bad8224b Updated packages/core/src/ResponseSynthesizer.ts 2023-08-14 17:48:41 +00:00
sweep-ai[bot] 29a9f98905 Updated packages/core/src/NodeParser.ts 2023-08-14 17:47:17 +00:00
sweep-ai[bot] fc9aec005d Modified JsonDocument to store JSON data as an object and stringify it in getText method 2023-08-14 17:46:27 +00:00
sweep-ai[bot] 50ec4487ad Modified the fromDocuments method to handle JsonDocument instances 2023-08-14 17:40:51 +00:00
sweep-ai[bot] 4d5b4fabde Modified synthesize method to handle nodes generated from JSON data 2023-08-14 17:39:45 +00:00
sweep-ai[bot] 8e4d564cbe Modified getNodesFromDocument to handle JsonDocument instances 2023-08-14 17:39:09 +00:00
sweep-ai[bot] 507126e0b6 Added JsonDocument class to handle JSON data 2023-08-14 17:38:17 +00:00
4 changed files with 62 additions and 9 deletions
+26
View File
@@ -0,0 +1,26 @@
import { Document } from './Document';
/**
* JsonDocument is a class that extends the Document class to handle JSON data.
*/
export class JsonDocument extends Document {
jsonData: Object;
/**
* Constructor for the JsonDocument class.
* @param {Object} jsonData - The JSON data for the document.
*/
constructor(jsonData: Object) {
super();
this.jsonData = jsonData;
}
/**
* Overrides the getText method of the Document class to return the JSON data.
* @returns {string} The JSON data of the document.
*/
getText(): string {
return JSON.stringify(this.jsonData);
}
}
+11 -3
View File
@@ -1,4 +1,5 @@
import { Document, NodeRelationship, TextNode } from "./Node";
import { JsonDocument } from "./JsonDocument";
import { SentenceSplitter } from "./TextSplitter";
import { DEFAULT_CHUNK_OVERLAP, DEFAULT_CHUNK_SIZE } from "./constants";
@@ -27,18 +28,25 @@ export function getTextSplitsFromDocument(
* @returns An array of nodes.
*/
export function getNodesFromDocument(
document: Document,
document: Document | JsonDocument,
textSplitter: SentenceSplitter,
includeMetadata: boolean = true,
includePrevNextRel: boolean = true
) {
let nodes: TextNode[] = [];
const textSplits = getTextSplitsFromDocument(document, textSplitter);
let textSplits;
if (document instanceof JsonDocument) {
// Parse the JSON data and generate text splits
const jsonData = JSON.parse(document.getText());
textSplits = Array.isArray(jsonData) ? jsonData : [jsonData];
} else {
textSplits = getTextSplitsFromDocument(document, textSplitter);
}
textSplits.forEach((textSplit) => {
const node = new TextNode({
text: textSplit,
text: typeof textSplit === 'object' ? JSON.stringify(textSplit) : textSplit,
metadata: includeMetadata ? document.metadata : {},
});
node.relationships[NodeRelationship.SOURCE] = document.asRelatedNodeInfo();
+10 -4
View File
@@ -33,7 +33,7 @@ interface BaseResponseBuilder {
*/
getResponse(
query: string,
textChunks: string[],
textChunks: (string | Object)[],
parentEvent?: Event,
prevResponse?: string
): Promise<string>;
@@ -295,9 +295,15 @@ export class ResponseSynthesizer {
}
async synthesize(query: string, nodes: NodeWithScore[], parentEvent?: Event) {
let textChunks: string[] = nodes.map((node) =>
node.node.getContent(MetadataMode.NONE)
);
let textChunks: string[] = nodes.map((node) => {
if (node.node instanceof JsonDocument) {
// Parse the JSON data and generate text chunks
const jsonData = node.node.getContent(MetadataMode.NONE);
return typeof jsonData === 'string' ? JSON.parse(jsonData) : jsonData;
} else {
return node.node.getContent(MetadataMode.NONE);
}
});
const response = await this.responseBuilder.getResponse(
query,
textChunks,
+15 -2
View File
@@ -114,7 +114,7 @@ export class ListIndex extends BaseIndex<IndexList> {
}
static async fromDocuments(
documents: Document[],
documents: (Document | JsonDocument)[],
args: {
storageContext?: StorageContext;
serviceContext?: ServiceContext;
@@ -130,7 +130,17 @@ export class ListIndex extends BaseIndex<IndexList> {
docStore.setDocumentHash(doc.id_, doc.hash);
}
const nodes = serviceContext.nodeParser.getNodesFromDocuments(documents);
let nodes = [];
for (const doc of documents) {
if (doc instanceof JsonDocument) {
// Handle JsonDocument instances
const jsonData = JSON.parse(doc.getText());
nodes = nodes.concat(Array.isArray(jsonData) ? jsonData : [jsonData]);
} else {
nodes = nodes.concat(serviceContext.nodeParser.getNodesFromDocuments([doc]));
}
}
const index = await ListIndex.init({
nodes,
storageContext,
@@ -138,6 +148,9 @@ export class ListIndex extends BaseIndex<IndexList> {
});
return index;
}
});
return index;
}
asRetriever(options?: { mode: ListRetrieverMode }): BaseRetriever {
const { mode = ListRetrieverMode.DEFAULT } = options ?? {};