diff --git a/backend/src/main/java/ch/xxx/aidoclibchat/adapter/controller/FunctionController.java b/backend/src/main/java/ch/xxx/aidoclibchat/adapter/controller/FunctionController.java index df71ef0..a4e07e0 100644 --- a/backend/src/main/java/ch/xxx/aidoclibchat/adapter/controller/FunctionController.java +++ b/backend/src/main/java/ch/xxx/aidoclibchat/adapter/controller/FunctionController.java @@ -39,7 +39,7 @@ public FunctionController(FunctionService functionService) { @PostMapping(path="/books", produces = MediaType.APPLICATION_JSON_VALUE) public FunctionResult postQuestion(@RequestBody FunctionSearch functionSearch) { - return new FunctionResult(this.functionService.functionCall(functionSearch.question())); + return this.functionService.functionCall(functionSearch.question(), functionSearch.resultFormat()); } } diff --git a/backend/src/main/java/ch/xxx/aidoclibchat/domain/model/dto/FunctionResult.java b/backend/src/main/java/ch/xxx/aidoclibchat/domain/model/dto/FunctionResult.java index eac7b70..1cbc11a 100644 --- a/backend/src/main/java/ch/xxx/aidoclibchat/domain/model/dto/FunctionResult.java +++ b/backend/src/main/java/ch/xxx/aidoclibchat/domain/model/dto/FunctionResult.java @@ -12,4 +12,6 @@ */ package ch.xxx.aidoclibchat.domain.model.dto; -public record FunctionResult(String result) { } +import ch.xxx.aidoclibchat.usecase.service.FunctionService.JsonResult; + +public record FunctionResult(String result, JsonResult jsonResult) { } diff --git a/backend/src/main/java/ch/xxx/aidoclibchat/usecase/service/FunctionService.java b/backend/src/main/java/ch/xxx/aidoclibchat/usecase/service/FunctionService.java index d08846c..7487969 100644 --- a/backend/src/main/java/ch/xxx/aidoclibchat/usecase/service/FunctionService.java +++ b/backend/src/main/java/ch/xxx/aidoclibchat/usecase/service/FunctionService.java @@ -12,6 +12,8 @@ */ package ch.xxx.aidoclibchat.usecase.service; +import java.util.List; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.ai.chat.client.ChatClient; @@ -19,14 +21,28 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; + +import ch.xxx.aidoclibchat.domain.model.dto.FunctionResult; +import ch.xxx.aidoclibchat.domain.model.dto.FunctionSearch.ResultFormat; + @Service public class FunctionService { private static final Logger LOGGER = LoggerFactory.getLogger(FunctionService.class); private final ChatClient chatClient; + + @JsonPropertyOrder({ "title", "summary" }) + public record JsonBook(String title, String summary) { + } + + @JsonPropertyOrder({ "author", "books" }) + public record JsonResult(String author, List books) { + } + private final String promptStr = """ - Make sure to have a parameter when calling a function. + Make sure to have a parameter when calling a function. If no parameter is provided ask the user for the parameter. - Create a summary for each book based on the function response subject. + Create a summary for each book based on the function response subject. User Query: %s @@ -39,13 +55,27 @@ public FunctionService(Builder builder) { this.chatClient = builder.build(); } - public String functionCall(String question) { + public FunctionResult functionCall(String question, ResultFormat resultFormat) { if (!this.activeProfile.contains("ollama")) { - return ""; + return new FunctionResult(" ", null); } - var result = this.chatClient.prompt().user(this.promptStr + question).functions("openLibraryClient").call().content(); + FunctionResult result = switch (resultFormat) { + case ResultFormat.Text -> this.functionCallText(question); + case ResultFormat.Json -> this.functionCallJson(question); + }; return result; } + private FunctionResult functionCallText(String question) { + var result = this.chatClient.prompt().user(this.promptStr + question).functions("openLibraryClient").call() + .content(); + return new FunctionResult(result, null); + } + + private FunctionResult functionCallJson(String question) { + var result = this.chatClient.prompt().user(this.promptStr + question).functions("openLibraryClient").call() + .entity(JsonResult.class); + return new FunctionResult(null, result); + } } diff --git a/frontend/src/angular/src/app/function-search/function-search.component.html b/frontend/src/angular/src/app/function-search/function-search.component.html index 5b5dd4a..80b6e6b 100644 --- a/frontend/src/angular/src/app/function-search/function-search.component.html +++ b/frontend/src/angular/src/app/function-search/function-search.component.html @@ -60,7 +60,7 @@ } @else { -

{{response}}

+

{{responseText}}