Skip to content

Commit 9c86f3b

Browse files
Jigsawcopybara-github
authored andcommitted
Internal change
GitOrigin-RevId: 37c382118924f7f7f3a16f3fec1cc171c07a5e6e
1 parent 30af703 commit 9c86f3b

2 files changed

Lines changed: 76 additions & 8 deletions

File tree

src/tasks/summarization_subtasks/topics.test.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ import { GroupedSummaryStats } from "../../stats_util";
1717
import { CommentWithVoteTallies } from "../../types";
1818
import { TopicsSummary } from "./topics";
1919

20+
// Mock the model response. This mock needs to be set up to return response specific for each test.
21+
let mockCommonGroundSummary: jest.SpyInstance;
22+
let mockDifferencesSummary: jest.SpyInstance;
23+
2024
const TEST_COMMENTS: CommentWithVoteTallies[] = [
2125
{
2226
id: "1",
@@ -57,7 +61,22 @@ const TEST_COMMENTS: CommentWithVoteTallies[] = [
5761
];
5862

5963
describe("TopicsSummaryTest", () => {
64+
beforeEach(() => {
65+
mockCommonGroundSummary = jest.spyOn(TopicsSummary.prototype, "getCommonGroundSummary");
66+
mockDifferencesSummary = jest.spyOn(TopicsSummary.prototype, "getDifferencesOfOpinionSummary");
67+
});
68+
69+
afterEach(() => {
70+
mockCommonGroundSummary.mockRestore();
71+
mockDifferencesSummary.mockRestore();
72+
});
6073
it("should create a properly formatted topics summary", async () => {
74+
// Mock the LLM calls
75+
mockCommonGroundSummary.mockReturnValue(Promise.resolve("Some points of common ground..."));
76+
mockDifferencesSummary.mockReturnValue(
77+
Promise.resolve("Areas of disagreement between groups...")
78+
);
79+
6180
expect(
6281
await new TopicsSummary(
6382
new GroupedSummaryStats(TEST_COMMENTS),
@@ -73,13 +92,13 @@ This topic included 2 subtopics.
7392
7493
#### Subtopic A.1 (2 comments)
7594
76-
Common ground: Some points of common ground...
95+
Common ground between groups: Some points of common ground...
7796
7897
Differences of opinion: Areas of disagreement between groups...
7998
8099
#### Subtopic A.2 (1 comments)
81100
82-
Common ground: Some points of common ground...
101+
Common ground between groups: Some points of common ground...
83102
84103
Differences of opinion: Areas of disagreement between groups...
85104
@@ -90,7 +109,7 @@ This topic included 1 subtopic.
90109
91110
#### Subtopic B.1 (1 comments)
92111
93-
Common ground: Some points of common ground...
112+
Common ground between groups: Some points of common ground...
94113
95114
Differences of opinion: Areas of disagreement between groups...
96115

src/tasks/summarization_subtasks/topics.ts

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,14 @@
1515
// Functions for different ways to summarize Comment and Vote data.
1616

1717
import { RecursiveSummary, resolvePromisesInParallel } from "./recursive_summarization";
18-
import { TopicStats, GroupedSummaryStats } from "../../stats_util";
18+
import { TopicStats, GroupedSummaryStats, GroupStats } from "../../stats_util";
19+
import { getPrompt } from "../../sensemaker_utils";
20+
import { Comment } from "../../types";
21+
import { commentCitation } from "../../validation/grounding";
22+
23+
const commonGroundInstructions = `Here are several comments sharing different opinions. Your job is to summarize these comments. Do not pretend that you hold any of these opinions. You are not a participant in this discussion. Participants in this conversation have been clustered into opinion groups. These opinion groups mostly approve of these comments. Write a concise summary of these comments that is at least one sentence and at most three sentences long. The summary should be substantiated, detailed and informative: include specific findings, requests, proposals, action items and examples, grounded in the comments. Refer to the people who made these comments as participants, not commenters. Do not talk about how strongly they approve of these comments. Use complete sentences. Do not use the passive voice. Do not use ambiguous pronouns. Be clear. Do not generate bullet points or special formatting. Do not yap.`;
24+
25+
const differencesOfOpinionInstructions = `Here are several comments which generated disagreement. Your job is summarize the ideas contained in the comments. Do not pretend that you hold any of these opinions. You are not a participant in this discussion. Write a concise summary of these comments that is at least one sentence and at most three sentences long. Refer to the people who made these comments as participants, not commenters. Do not talk about how strongly they disagree on these comments. Use complete sentences. Do not use the passive voice. Do not use ambiguous pronouns. Be clear. Do not generate bullet points or special formatting. The summary should not imply that these views were agreed on by all participants. Your output should begin in the form "There was low consensus". Do not pretend that these comments were written by different people. For each sentence use a unique phrase to indicate that there was low consensus on the topic, and do not present each comment as an alternative idea. Do not yap.`;
1926

2027
export class TopicsSummary extends RecursiveSummary<GroupedSummaryStats> {
2128
async getSummary() {
@@ -90,17 +97,59 @@ ${subtopicsSummaryText}
9097
async getSubtopicSummary(subtopicStat: TopicStats): Promise<string> {
9198
const sectionTitle: string = `${subtopicStat.name} (${subtopicStat.commentCount} comments)`;
9299

93-
// For now, these are mocked out...
94-
const commonGroundSummary = "Some points of common ground...";
95-
const differencesSummary = "Areas of disagreement between groups...";
100+
const groupStats = this.input.getStatsByGroup();
101+
const groupNames = groupStats.map((stat: GroupStats) => {
102+
return stat.name;
103+
});
104+
105+
const commonGroundSummary = await this.getCommonGroundSummary();
106+
const differencesSummary = await this.getDifferencesOfOpinionSummary(groupNames);
96107

97108
return Promise.resolve(
98109
`#### ${sectionTitle}
99110
100-
Common ground: ${commonGroundSummary}
111+
Common ground between groups: ${commonGroundSummary}
101112
102113
Differences of opinion: ${differencesSummary}
103114
`
104115
);
105116
}
117+
118+
/**
119+
* Summarizes the comments on which there was the strongest agreement between groups.
120+
* @returns a two sentence description of similarities and differences.
121+
*/
122+
async getCommonGroundSummary(): Promise<string> {
123+
const commonGroundComments = this.input.getCommonGroundComments();
124+
return this.model.generateText(
125+
getPrompt(
126+
commonGroundInstructions,
127+
commonGroundComments.map((comment: Comment): string => comment.text)
128+
)
129+
);
130+
}
131+
132+
/**
133+
* Summarizes the comments on which there was the strongest disagreement between groups.
134+
* @returns a two sentence description of similarities and differences.
135+
*/
136+
async getDifferencesOfOpinionSummary(groupNames: string[]): Promise<string> {
137+
const topDisagreeCommentsAcrossGroups =
138+
this.input.getDifferencesBetweenGroupsComments(groupNames);
139+
return this.model.generateText(
140+
getPrompt(
141+
differencesOfOpinionInstructions,
142+
topDisagreeCommentsAcrossGroups.map((comment: Comment) => comment.text)
143+
)
144+
);
145+
}
146+
147+
/**
148+
* Create citations for comments in the format of "[12, 43, 56]"
149+
* @param comments the comments to use for citations
150+
* @returns the formatted citations
151+
*/
152+
private getCommentCitations(comments: Comment[]): string {
153+
return "[" + comments.map((comment) => commentCitation(comment)).join(", ") + "]";
154+
}
106155
}

0 commit comments

Comments
 (0)