Skip to content

Commit eb05f66

Browse files
committed
fix: handle LLM bugs
1 parent 22e9fba commit eb05f66

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

backend/src/main/java/ch/xxx/aidoclibchat/usecase/service/FunctionService.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,23 +61,33 @@ public FunctionResult functionCall(String question, ResultFormat resultFormat) {
6161
if (!this.activeProfile.contains("ollama")) {
6262
return new FunctionResult(" ", null);
6363
}
64+
var result = new FunctionResult(" ", null);
6465

65-
FunctionResult result = switch (resultFormat) {
66-
case ResultFormat.Text -> this.functionCallText(question);
67-
case ResultFormat.Json -> this.functionCallJson(question);
68-
};
66+
int i = 0;
67+
while (i < 3 && (result.jsonResult() == null && result.result() == " ")) {
68+
try {
69+
result = switch (resultFormat) {
70+
case ResultFormat.Text -> this.functionCallText(question);
71+
case ResultFormat.Json -> this.functionCallJson(question);
72+
};
73+
} catch (Exception e) {
74+
LOGGER.warn("AI Call failed.", e);
75+
}
76+
i++;
77+
}
6978
return result;
7079
}
7180

7281
private FunctionResult functionCallText(String question) {
73-
var result = this.chatClient.prompt().user(this.promptStr + question).tools(FunctionConfig.OPEN_LIBRARY_CLIENT).call()
74-
.content();
82+
var result = this.chatClient.prompt().user(this.promptStr + question).tools(FunctionConfig.OPEN_LIBRARY_CLIENT)
83+
.call().content();
7584
return new FunctionResult(result, null);
7685
}
7786

7887
private FunctionResult functionCallJson(String question) {
79-
var result = this.chatClient.prompt().user(this.promptStr + question).tools(FunctionConfig.OPEN_LIBRARY_CLIENT).call()
80-
.entity(new ParameterizedTypeReference<List<JsonResult>>() {});
88+
var result = this.chatClient.prompt().user(this.promptStr + question).tools(FunctionConfig.OPEN_LIBRARY_CLIENT)
89+
.call().entity(new ParameterizedTypeReference<List<JsonResult>>() {
90+
});
8191
return new FunctionResult(null, result);
8292
}
8393
}

frontend/src/angular/src/app/function-search/function-search.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export class FunctionSearchComponent {
113113
tap(() => (this.searching = false))
114114
)
115115
.subscribe(value => this.resultFormatControl.value === this.resultFormats[0] ?
116-
this.responseText = value.result || '' : this.responseJson = this.addToDataSource(this.mapResult(value.jsonResult || [{ author: "", books: [] }] as JsonResult[]))
116+
this.responseText = value.result?.trim() || '' : this.responseJson = this.addToDataSource(this.mapResult(value.jsonResult || [{ author: "", books: [] }] as JsonResult[]))
117117
);
118118
}
119119

0 commit comments

Comments
 (0)