Skip to content

Commit 722b7fd

Browse files
yyyu-googlecopybara-github
authored andcommitted
fix: correct CitationMetadata interface. refactor nested function
PiperOrigin-RevId: 616874627
1 parent e573ce6 commit 722b7fd

File tree

6 files changed

+379
-31
lines changed

6 files changed

+379
-31
lines changed

src/functions/post_fetch_processing.ts

+33-19
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
*/
1717

1818
import {
19-
CitationSource,
20-
Content,
19+
Citation,
20+
CitationMetadata,
2121
CountTokensResponse,
2222
GenerateContentCandidate,
2323
GenerateContentResponse,
@@ -199,23 +199,14 @@ export function aggregateResponses(
199199
},
200200
} as GenerateContentCandidate;
201201
}
202-
if (response.candidates[i].citationMetadata) {
203-
if (
204-
!aggregatedResponse.candidates[i].citationMetadata?.citationSources
205-
) {
206-
aggregatedResponse.candidates[i].citationMetadata = {
207-
citationSources: [] as CitationSource[],
208-
};
209-
}
210-
211-
const existingMetadata = response.candidates[i].citationMetadata ?? {};
212-
213-
if (aggregatedResponse.candidates[i].citationMetadata) {
214-
aggregatedResponse.candidates[i].citationMetadata!.citationSources =
215-
aggregatedResponse.candidates[
216-
i
217-
].citationMetadata!.citationSources.concat(existingMetadata);
218-
}
202+
const citationMetadataAggregated: CitationMetadata | undefined =
203+
aggregateCitationMetadataForCandidate(
204+
response.candidates[i],
205+
aggregatedResponse.candidates[i]
206+
);
207+
if (citationMetadataAggregated) {
208+
aggregatedResponse.candidates[i].citationMetadata =
209+
citationMetadataAggregated;
219210
}
220211
const finishResonOfChunk = response.candidates[i].finishReason;
221212
if (finishResonOfChunk) {
@@ -260,6 +251,29 @@ export function aggregateResponses(
260251
return aggregatedResponse;
261252
}
262253

254+
function aggregateCitationMetadataForCandidate(
255+
candidateChunk: GenerateContentCandidate,
256+
aggregatedCandidate: GenerateContentCandidate
257+
): CitationMetadata | undefined {
258+
if (!candidateChunk.citationMetadata) {
259+
return;
260+
}
261+
const emptyCitationMetadata: CitationMetadata = {
262+
citations: [],
263+
};
264+
const citationMetadataAggregated: CitationMetadata =
265+
aggregatedCandidate.citationMetadata ?? emptyCitationMetadata;
266+
const citationMetadataChunk: CitationMetadata =
267+
candidateChunk.citationMetadata!;
268+
if (citationMetadataChunk.citations) {
269+
citationMetadataAggregated.citations =
270+
citationMetadataAggregated.citations!.concat(
271+
citationMetadataChunk.citations
272+
);
273+
}
274+
return citationMetadataAggregated;
275+
}
276+
263277
function aggregateGroundingMetadataForCandidate(
264278
candidateChunk: GenerateContentCandidate,
265279
aggregatedCandidate: GenerateContentCandidate

src/functions/test/functions_test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ const TEST_CANDIDATES = [
104104
finishMessage: '',
105105
safetyRatings: TEST_SAFETY_RATINGS,
106106
citationMetadata: {
107-
citationSources: [
107+
citations: [
108108
{
109109
startIndex: 367,
110110
endIndex: 491,
@@ -585,9 +585,9 @@ describe('generateContent', () => {
585585
TEST_API_ENDPOINT
586586
);
587587
expect(
588-
resp.response.candidates[0].citationMetadata?.citationSources.length
588+
resp.response.candidates[0].citationMetadata?.citations.length
589589
).toEqual(
590-
TEST_MODEL_RESPONSE.candidates[0].citationMetadata.citationSources.length
590+
TEST_MODEL_RESPONSE.candidates[0].citationMetadata.citations.length
591591
);
592592
});
593593

src/functions/test/post_fetch_processing_test.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,26 @@
1717

1818
import {
1919
AGGREGATED_RESPONSE_STREAM_RESPONSE_CHUNKS_1,
20+
AGGREGATED_RESPONSE_STREAM_RESPONSE_CHUNKS_2,
2021
STREAM_RESPONSE_CHUNKS_1,
22+
STREAM_RESPONSE_CHUNKS_2,
2123
} from './test_data';
2224
import {aggregateResponses} from '../post_fetch_processing';
2325

2426
describe('aggregateResponses', () => {
25-
it('grounding metadata in muliple chunks for multiple chandidates, should aggregate accordingly', () => {
27+
it('grounding metadata in multiple chunks for multiple candidates, should aggregate accordingly', () => {
2628
const actualResult = aggregateResponses(STREAM_RESPONSE_CHUNKS_1);
2729

2830
expect(JSON.stringify(actualResult)).toEqual(
2931
JSON.stringify(AGGREGATED_RESPONSE_STREAM_RESPONSE_CHUNKS_1)
3032
);
3133
});
34+
35+
it('citation metadata in multiple chunks for multiple candidates, should aggregate accordingly', () => {
36+
const actualResult = aggregateResponses(STREAM_RESPONSE_CHUNKS_2);
37+
38+
expect(JSON.stringify(actualResult)).toEqual(
39+
JSON.stringify(AGGREGATED_RESPONSE_STREAM_RESPONSE_CHUNKS_2)
40+
);
41+
});
3242
});

0 commit comments

Comments
 (0)