Skip to content

Commit

Permalink
feat: make the json mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
Angular2Guy committed Mar 11, 2024
1 parent cde54a7 commit 0fc7997
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,16 @@

public interface OpenLibraryClient extends Function<OpenLibraryClient.Request, OpenLibraryClient.Response> {
@JsonIgnoreProperties(ignoreUnknown = true)
record Book(@JsonProperty(value= "author_name", required = false) List<String> authorName) {}
record Book(@JsonProperty(value= "author_name", required = false) List<String> authorName,
@JsonProperty(value= "language", required = false) List<String> languages,
@JsonProperty(value= "publish_date", required = false) List<String> publishDates,
@JsonProperty(value= "publisher", required = false) List<String> publishers,
String title, String type,
@JsonProperty(value= "subject", required = false) List<String> subjects,
@JsonProperty(value= "place", required = false) List<String> places,
@JsonProperty(value= "time", required = false) List<String> times,
@JsonProperty(value= "person", required = false) List<String> 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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";
Expand All @@ -91,7 +92,7 @@ public String functionCall(String question) {
}
var query = String.format(this.promptStr, jsonStr, question);
int aiCallCounter = 0;
var responseRef = new AtomicReference<String>("false");
var responseRef = new AtomicReference<Response>(new Response(0L, 0L, false, List.of()));
List<Tool> myToolsList = List.of();
while (aiCallCounter < 3 && myToolsList.isEmpty()) {
aiCallCounter += 1;
Expand All @@ -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();
}
Expand Down

0 comments on commit 0fc7997

Please sign in to comment.