Move native hints into individual modules

* Use aot.factories approach for registration
* Add tests
* final tweaks- Thanks Josh!
This commit is contained in:
Mark Pollack
2024-02-13 11:11:24 -05:00
parent ea7dce3833
commit 596f2b06c0
34 changed files with 609 additions and 207 deletions

View File

@@ -0,0 +1,47 @@
package org.springframework.ai.aot;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.commons.logging.LogFactory;
import org.junit.jupiter.api.Test;
import org.springframework.aot.hint.TypeReference;
import org.springframework.util.Assert;
import java.util.Set;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
class AiRuntimeHintsTests {
@JsonInclude
static class TestApi {
static class FooBar {
}
record Foo(@JsonProperty("name") String name) {
}
@JsonInclude
enum Bar {
A, B
}
}
@Test
void discoverRelevantClasses() throws Exception {
var classes = AiRuntimeHints.findJsonAnnotatedClassesInPackage(TestApi.class);
var included = Set.of(TestApi.Bar.class, TestApi.Foo.class)
.stream()
.map(t -> TypeReference.of(t.getName()))
.collect(Collectors.toSet());
LogFactory.getLog(getClass()).info(classes);
Assert.state(classes.containsAll(included), "there should be all of the enumerated classes. ");
}
}

View File

@@ -0,0 +1,19 @@
package org.springframework.ai.aot;
import org.junit.jupiter.api.Test;
import org.springframework.aot.hint.RuntimeHints;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.aot.hint.predicate.RuntimeHintsPredicates.resource;
class KnuddelsRuntimeHintsTest {
@Test
void knuddels() {
var runtimeHints = new RuntimeHints();
var knuddels = new KnuddelsRuntimeHints();
knuddels.registerHints(runtimeHints, null);
assertThat(runtimeHints).matches(resource().forResource("com/knuddels/jtokkit/cl100k_base.tiktoken"));
}
}

View File

@@ -0,0 +1,19 @@
package org.springframework.ai.aot;
import org.junit.jupiter.api.Test;
import org.springframework.aot.hint.RuntimeHints;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.aot.hint.predicate.RuntimeHintsPredicates.resource;
class SpringAiCoreRuntimeHintsTest {
@Test
void core() {
var runtimeHints = new RuntimeHints();
var knuddels = new SpringAiCoreRuntimeHints();
knuddels.registerHints(runtimeHints, null);
assertThat(runtimeHints).matches(resource().forResource("embedding/embedding-model-dimensions.properties"));
}
}