Skip to content

Commit

Permalink
fix: return actual source nodes with compact and refine response synt…
Browse files Browse the repository at this point in the history
…hesizer

Before we were only returning the packed text chunks in
EngineResponse.sourceNodes for this response synthesizer. This is
different than how every other synthesizer works.
  • Loading branch information
GunnarHolwerda committed Dec 7, 2024
1 parent 8386510 commit 57eb4e7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/new-cups-dress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@llamaindex/core": patch
---

The compact and refine response synthesizer (retrieved by using `getResponseSynthesizer('compact')`) has been fixed to return the original source nodes that were provided to it in its response. Previous to this it was returning the compacted text chunk documents.
31 changes: 29 additions & 2 deletions packages/core/src/response-synthesizers/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ class Refine extends BaseSynthesizer {
}
}

async getResponse(
query: MessageContent,
nodes: NodeWithScore[],
stream: true,
): Promise<AsyncIterable<EngineResponse>>;
async getResponse(
query: MessageContent,
nodes: NodeWithScore[],
stream: false,
): Promise<EngineResponse>;
async getResponse(
query: MessageContent,
nodes: NodeWithScore[],
Expand Down Expand Up @@ -197,6 +207,16 @@ class Refine extends BaseSynthesizer {
* CompactAndRefine is a slight variation of Refine that first compacts the text chunks into the smallest possible number of chunks.
*/
class CompactAndRefine extends Refine {
async getResponse(
query: MessageContent,
nodes: NodeWithScore[],
stream: true,
): Promise<AsyncIterable<EngineResponse>>;
async getResponse(
query: MessageContent,
nodes: NodeWithScore[],
stream: false,
): Promise<EngineResponse>;
async getResponse(
query: MessageContent,
nodes: NodeWithScore[],
Expand All @@ -216,17 +236,24 @@ class CompactAndRefine extends Refine {
const newTexts = this.promptHelper.repack(maxPrompt, textChunks);
const newNodes = newTexts.map((text) => new TextNode({ text }));
if (stream) {
return super.getResponse(
const streamResponse = await super.getResponse(
query,
newNodes.map((node) => ({ node })),
true,
);
return streamConverter(streamResponse, (chunk) => {
chunk.sourceNodes = nodes;
return chunk;
});
}
return super.getResponse(

const originalResponse = await super.getResponse(
query,
newNodes.map((node) => ({ node })),
false,
);
originalResponse.sourceNodes = nodes;
return originalResponse;
}
}

Expand Down

0 comments on commit 57eb4e7

Please sign in to comment.