|
16 | 16 |
|
17 | 17 | package org.springframework.ai.embedding; |
18 | 18 |
|
| 19 | +import org.springframework.aot.hint.RuntimeHints; |
| 20 | +import org.springframework.aot.hint.RuntimeHintsRegistrar; |
| 21 | +import org.springframework.context.annotation.ImportRuntimeHints; |
| 22 | +import org.springframework.core.io.ClassPathResource; |
| 23 | +import org.springframework.core.io.Resource; |
| 24 | +import org.springframework.util.Assert; |
| 25 | + |
19 | 26 | import java.io.IOException; |
20 | 27 | import java.util.Map; |
21 | 28 | import java.util.Properties; |
22 | 29 | import java.util.concurrent.atomic.AtomicInteger; |
23 | 30 | import java.util.stream.Collectors; |
24 | 31 |
|
25 | | -import org.springframework.core.io.DefaultResourceLoader; |
26 | | - |
27 | 32 | /** |
28 | 33 | * Abstract implementation of the {@link EmbeddingModel} interface that provides |
29 | 34 | * dimensions calculation caching. |
30 | 35 | * |
31 | 36 | * @author Christian Tzolov |
| 37 | + * @author Josh Long |
32 | 38 | */ |
| 39 | +@ImportRuntimeHints(AbstractEmbeddingModel.Hints.class) |
33 | 40 | public abstract class AbstractEmbeddingModel implements EmbeddingModel { |
34 | 41 |
|
| 42 | + private static final Resource EMBEDDING_MODEL_DIMENSIONS_PROPERTIES = new ClassPathResource( |
| 43 | + "/embedding/embedding-model-dimensions.properties"); |
| 44 | + |
35 | 45 | private static final Map<String, Integer> KNOWN_EMBEDDING_DIMENSIONS = loadKnownModelDimensions(); |
36 | 46 |
|
37 | | - /** |
38 | | - * Default constructor. |
39 | | - */ |
40 | | - public AbstractEmbeddingModel() { |
| 47 | + static class Hints implements RuntimeHintsRegistrar { |
| 48 | + |
| 49 | + @Override |
| 50 | + public void registerHints(RuntimeHints hints, ClassLoader classLoader) { |
| 51 | + hints.resources().registerResource(EMBEDDING_MODEL_DIMENSIONS_PROPERTIES); |
| 52 | + } |
| 53 | + |
41 | 54 | } |
42 | 55 |
|
43 | 56 | /** |
@@ -69,10 +82,13 @@ public static int dimensions(EmbeddingModel embeddingModel, String modelName, St |
69 | 82 |
|
70 | 83 | private static Map<String, Integer> loadKnownModelDimensions() { |
71 | 84 | try { |
72 | | - Properties properties = new Properties(); |
73 | | - properties.load(new DefaultResourceLoader() |
74 | | - .getResource("classpath:/embedding/embedding-model-dimensions.properties") |
75 | | - .getInputStream()); |
| 85 | + var resource = EMBEDDING_MODEL_DIMENSIONS_PROPERTIES; |
| 86 | + Assert.notNull(resource, "the embedding dimensions must be non-null"); |
| 87 | + Assert.state(resource.exists(), "the embedding dimensions properties file must exist"); |
| 88 | + var properties = new Properties(); |
| 89 | + try (var in = resource.getInputStream()) { |
| 90 | + properties.load(in); |
| 91 | + } |
76 | 92 | return properties.entrySet() |
77 | 93 | .stream() |
78 | 94 | .collect(Collectors.toMap(e -> e.getKey().toString(), e -> Integer.parseInt(e.getValue().toString()))); |
|
0 commit comments