native hints for pdfbox reader and embeddings support. Fixes #161

This commit is contained in:
Mark Pollack
2023-12-14 13:29:32 -05:00
parent 72af42d0d8
commit 32e2a22bfb

View File

@@ -4,21 +4,56 @@ import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.TypeReference;
import java.io.IOException;
import java.util.Set;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
public class NativeHints implements RuntimeHintsRegistrar {
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
hints.proxies().registerJdkProxy(TypeReference.of("com.theokanning.openai.OpenAiApi"));
for (var className : Set.of("com.theokanning.openai.Usage",
"com.theokanning.openai.completion.chat.ChatCompletionChoice",
"com.theokanning.openai.completion.chat.ChatCompletionRequest",
"com.theokanning.openai.completion.chat.ChatCompletionResult",
"com.theokanning.openai.completion.chat.ChatMessage"))
hints.reflection().registerType(TypeReference.of(className), MemberCategory.values());
new KnuddelsHints().registerHints(hints, classLoader);
new PdfReaderHints().registerHints(hints, classLoader);
hints.resources().registerResource(new ClassPathResource("embedding/embedding-model-dimensions.properties"));
}
static class KnuddelsHints implements RuntimeHintsRegistrar {
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
hints.resources().registerResource(new ClassPathResource("/com/knuddels/jtokkit/cl100k_base.tiktoken"));
}
}
static class PdfReaderHints implements RuntimeHintsRegistrar {
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
try {
var resolver = new PathMatchingResourcePatternResolver();
var patterns = Set.of("/org/apache/pdfbox/resources/glyphlist/zapfdingbats.txt",
"/org/apache/pdfbox/resources/glyphlist/glyphlist.txt", "/org/apache/fontbox/cmap/**",
"/org/apache/pdfbox/resources/afm/**", "/org/apache/pdfbox/resources/glyphlist/**",
"/org/apache/pdfbox/resources/icc/**", "/org/apache/pdfbox/resources/text/**",
"/org/apache/pdfbox/resources/ttf/**", "/org/apache/pdfbox/resources/version.properties");
for (var pattern : patterns)
for (var resourceMatch : resolver.getResources(pattern))
hints.resources().registerResource(resourceMatch);
}
catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}