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 938e8e4..79dce4e 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 @@ -17,6 +17,7 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import ch.xxx.aidoclibchat.domain.client.OpenLibraryClient; import ch.xxx.aidoclibchat.usecase.service.FunctionService; @RestController @@ -29,8 +30,8 @@ public FunctionController(FunctionService functionService) { } //example: http://localhost:8080/rest/function/books?question=show+books+of+author+kevin+rudd+with+title+avoidable+war - @GetMapping("/books") - public String postQuestion(@RequestParam(name="question", defaultValue = "") String question) { + @GetMapping(path = "/books", produces = "application/json") + public OpenLibraryClient.Response postQuestion(@RequestParam(name="question", defaultValue = "") String question) { return this.functionService.functionCall(question); } diff --git a/backend/src/main/java/ch/xxx/aidoclibchat/domain/client/OpenLibraryClient.java b/backend/src/main/java/ch/xxx/aidoclibchat/domain/client/OpenLibraryClient.java index 232576f..ba88aa0 100644 --- a/backend/src/main/java/ch/xxx/aidoclibchat/domain/client/OpenLibraryClient.java +++ b/backend/src/main/java/ch/xxx/aidoclibchat/domain/client/OpenLibraryClient.java @@ -19,7 +19,16 @@ public interface OpenLibraryClient extends Function { @JsonIgnoreProperties(ignoreUnknown = true) - record Book(@JsonProperty(value= "author_name", required = false) List authorName) {} + record Book(@JsonProperty(value= "author_name", required = false) List authorName, + @JsonProperty(value= "language", required = false) List languages, + @JsonProperty(value= "publish_date", required = false) List publishDates, + @JsonProperty(value= "publisher", required = false) List publishers, + String title, String type, + @JsonProperty(value= "subject", required = false) List subjects, + @JsonProperty(value= "place", required = false) List places, + @JsonProperty(value= "time", required = false) List times, + @JsonProperty(value= "person", required = false) List persons, + @JsonProperty(value= "ratings_average", required = false) Double ratingsAverage) {} @JsonInclude(Include.NON_NULL) @JsonClassDescription("OpenLibrary API request") record Request(@JsonProperty(required=false, value="author") @JsonPropertyDescription("The book author") String author, 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 58b5b4a..6cd967d 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 @@ -29,6 +29,7 @@ import ch.xxx.aidoclibchat.domain.client.OpenLibraryClient; import ch.xxx.aidoclibchat.domain.client.OpenLibraryClient.FunctionTool.Type; +import ch.xxx.aidoclibchat.domain.client.OpenLibraryClient.Response; @Service public class FunctionService { @@ -75,9 +76,9 @@ public FunctionService(ObjectMapper objectMapper, ChatClient chatClient, OpenLib this.openLibraryClient = openLibraryClient; } - public String functionCall(String question) { + public Response functionCall(String question) { if (!this.activeProfile.contains("ollama")) { - return "false"; + return new Response(0L, 0L, false, List.of()); } var description = "Search for books by author, title or subject."; var name = "booksearch"; @@ -91,7 +92,7 @@ public String functionCall(String question) { } var query = String.format(this.promptStr, jsonStr, question); int aiCallCounter = 0; - var responseRef = new AtomicReference("false"); + var responseRef = new AtomicReference(new Response(0L, 0L, false, List.of())); List myToolsList = List.of(); while (aiCallCounter < 3 && myToolsList.isEmpty()) { aiCallCounter += 1; @@ -118,12 +119,13 @@ public String functionCall(String question) { LOGGER.error("ChatResponse: {}", response); } } + myToolsList.forEach(myTool -> { var myRequest = new OpenLibraryClient.Request((String) myTool.toolInput().get("author"), (String) myTool.toolInput().get("title"), (String) myTool.toolInput().get("subject")); var myResponse = this.openLibraryClient.apply(myRequest); //LOGGER.info(myResponse.toString()); - responseRef.set(myResponse.toString()); + responseRef.set(myResponse); }); return responseRef.get(); }