Skip to content

Commit 32eb37b

Browse files
committed
Simplify Java FactChecker
1 parent d81f448 commit 32eb37b

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

examples-java/src/main/java/com/embabel/example/factchecker/FactChecker.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.embabel.agent.api.common.OperationContext;
2424
import com.embabel.agent.api.common.TransformationActionContext;
2525
import com.embabel.agent.api.common.workflow.control.ResultList;
26+
import com.embabel.agent.api.common.workflow.control.ScatterGatherBuilder;
2627
import com.embabel.agent.api.common.workflow.multimodel.ConsensusBuilder;
2728
import com.embabel.agent.core.CoreToolGroups;
2829
import com.embabel.agent.domain.io.UserInput;
@@ -36,7 +37,6 @@
3637

3738
import java.util.List;
3839
import java.util.Map;
39-
import java.util.stream.Collectors;
4040
import java.util.stream.Stream;
4141

4242
/**
@@ -57,14 +57,14 @@ record FactCheck(
5757
double confidence,
5858
@JsonPropertyDescription("reasoning for your scoring")
5959
String reasoning,
60+
@JsonPropertyDescription("Source of the fact checks, typically a LLM model")
61+
String source,
6062
List<InternetResource> links
6163
) {
6264
}
6365

6466
record FactChecks(
65-
List<FactCheck> checks,
66-
@JsonPropertyDescription("Source of the fact checks, typically a LLM model")
67-
String source
67+
List<FactCheck> checks
6868
) {
6969
}
7070

@@ -132,17 +132,18 @@ FactChecks runAndConsolidateFactChecks(
132132
var llmFactChecks = properties.models().stream()
133133
.flatMap(model -> factCheckWithSingleLlm(model, distinctFactualAssertions, context))
134134
.toList();
135-
return ConsensusBuilder
135+
return ScatterGatherBuilder
136136
.returning(FactChecks.class)
137-
.sourcedFrom(llmFactChecks)
138-
.withConsensusBy(this::reconcileFactChecks)
137+
.fromElements(FactCheck.class)
138+
.generatedBy(llmFactChecks)
139+
.consolidatedBy(this::reconcileFactChecks)
139140
.asSubProcess(context);
140141
}
141142

142143
/**
143144
* Fact-check the distinct factual assertions using a given LLM
144145
*/
145-
private Stream<Supplier<FactChecks>> factCheckWithSingleLlm(
146+
private Stream<Supplier<FactCheck>> factCheckWithSingleLlm(
146147
String model,
147148
DistinctFactualAssertions distinctFactualAssertions,
148149
OperationContext context) {
@@ -156,16 +157,18 @@ private Stream<Supplier<FactChecks>> factCheckWithSingleLlm(
156157
Given the following assertion, check if it is true or false and explain why in %d words
157158
Express your confidence in your determination as a number between 0 and 1.
158159
Use web tools so you can cite information to support your conclusion.
160+
Use '%s' for the source field.
159161
160162
ASSERTION TO CHECK:
161163
%s
162164
""".formatted(
163165
properties.reasoningWordCount(),
166+
model,
164167
assertion
165168
),
166169
FactCheck.class
167170
)).stream()
168-
.map(check -> () -> new FactChecks(List.of(check), model));
171+
.map(check -> () -> check);
169172
}
170173

171174
private Stream<Supplier<DistinctFactualAssertions>> factualAssertionExtractors(UserInput userInput, ActionContext actionContext) {
@@ -224,15 +227,15 @@ private DistinctFactualAssertions consolidateFactualAssertions(
224227
* Use the best LLM to reconcile the fact checks into a single list.
225228
*/
226229
private FactChecks reconcileFactChecks(
227-
TransformationActionContext<ResultList<FactChecks>, FactChecks> context) {
230+
TransformationActionContext<ResultList<FactCheck>, FactChecks> context) {
228231
var formattedFactChecks = new StringBuilder();
229232
for (var factCheck : context.getInput().getResults()) {
230233
formattedFactChecks.append("Source: ").append(factCheck.source()).append("\n");
231-
formattedFactChecks.append("- ").append(factCheck.checks().stream()
232-
.map(check ->
233-
String.format("%s (%B with confidence %.2f)\nReasoning: %s\nLinks:\n%s",
234-
check.assertion(), check.isTrue(), check.confidence(), check.reasoning(), check.links()))
235-
.collect(Collectors.joining("\n- ")))
234+
formattedFactChecks.append("- ").append(
235+
String.format("%s (%B with confidence %.2f from source '%s')\nReasoning: %s\nLinks:\n%s",
236+
factCheck.assertion(), factCheck.isTrue(),
237+
factCheck.confidence(), factCheck.source(),
238+
factCheck.reasoning(), factCheck.links()))
236239
.append("\n\n");
237240
}
238241
return context.ai()

0 commit comments

Comments
 (0)