-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bb47bb1
commit 0d40c10
Showing
7 changed files
with
177 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 18 additions & 2 deletions
20
labs/tools/src/main/java/com/thomasvitale/ai/spring/Tools.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,38 @@ | ||
package com.thomasvitale.ai.spring; | ||
|
||
import com.thomasvitale.ai.spring.api.tools.Tool; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.List; | ||
|
||
@Component | ||
public class Tools { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(Tools.class); | ||
|
||
private final BookService bookService; | ||
|
||
Tools(BookService bookService) { | ||
this.bookService = bookService; | ||
} | ||
|
||
@Tool("Get the list of books written by the given author available in the library") | ||
public List<BookService.Book> booksByAuthor(String author) { | ||
return bookService.getBooksByAuthor(new BookService.Author(author)); | ||
public List<BookService.Book> booksByAuthor(BookService.Author author) { | ||
logger.info("Getting books by author: {}", author); | ||
return bookService.getBooksByAuthor(author); | ||
} | ||
|
||
@Tool("Get the list of books written by the given authors available in the library") | ||
public List<BookService.Book> booksByAuthors(List<String> authors) { | ||
logger.info("Getting books by authors: {}", String.join(", ", authors)); | ||
return bookService.getBooksByAuthor(authors.stream().map(BookService.Author::new).toList()); | ||
} | ||
|
||
@Tool("Welcome users to the library") | ||
public void welcome() { | ||
logger.info("Welcoming users to the library"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
labs/tools/src/test/java/com/thomasvitale/ai/spring/ToolsTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.thomasvitale.ai.spring; | ||
|
||
import com.thomasvitale.ai.spring.api.tools.method.MethodToolCallbackResolver; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class ToolsTests { | ||
|
||
String value1 = """ | ||
{ | ||
"author": { | ||
"name" : "J.R.R. Tolkien" | ||
} | ||
} | ||
"""; | ||
|
||
String value2 = """ | ||
{ | ||
"authors" : ["J.R.R. Tolkien", "Philip Pullman"] | ||
} | ||
"""; | ||
|
||
@Test | ||
void nonStaticMethod() { | ||
var object = new Tools(new BookService()); | ||
|
||
var functionCallbacks = MethodToolCallbackResolver.builder() | ||
.target(object) | ||
.build() | ||
.getToolCallbacks(); | ||
|
||
var booksByAuthor = Stream.of(functionCallbacks) | ||
.filter(func -> func.getName().equals("booksByAuthor")) | ||
.findFirst() | ||
.orElseThrow(); | ||
|
||
String response1 = booksByAuthor.call(value1); | ||
|
||
assertThat(response1).isNotEmpty(); | ||
assertThat(response1) | ||
.containsIgnoringWhitespaces("The Hobbit") | ||
.containsIgnoringWhitespaces("The Lord of The Rings") | ||
.containsIgnoringWhitespaces("The Silmarillion"); | ||
|
||
var booksByAuthors = Stream.of(functionCallbacks) | ||
.filter(func -> func.getName().equals("booksByAuthors")) | ||
.findFirst() | ||
.orElseThrow(); | ||
|
||
String response2 = booksByAuthors.call(value2); | ||
|
||
assertThat(response2).isNotEmpty(); | ||
} | ||
|
||
@Test | ||
void noArgsNoReturnMethod() { | ||
var object = new Tools(new BookService()); | ||
|
||
var functionCallbacks = MethodToolCallbackResolver.builder() | ||
.target(object) | ||
.build() | ||
.getToolCallbacks(); | ||
|
||
var welcome = Stream.of(functionCallbacks) | ||
.filter(func -> func.getName().equals("welcome")) | ||
.findFirst() | ||
.orElseThrow(); | ||
|
||
String response = welcome.call("{}"); | ||
|
||
assertThat(response).isEqualTo("Done"); | ||
} | ||
|
||
} |