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 bdd6e90
Showing 1 changed file with 29 additions and 2 deletions.
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 bdd6e90

Please sign in to comment.