import java.util.List;
import java.util.Map;
import org.springframework.ai.transformers.TransformersEmbeddingClient;
public class LocalEmbeddingDemo {
public static void main(String[] args) throws Exception {
TransformersEmbeddingClient embeddingClient = new TransformersEmbeddingClient();
embeddingClient.setTokenizerResource("file:/llm/bge-zh/tokenizer.json");
embeddingClient.setModelResource("file:/llm/bge-zh/model.onnx");
//ModelOutputName在使用optimum-cli命令转换模型时会输出在控制台
embeddingClient.setModelOutputName("token_embeddings");
embeddingClient.setResourceCacheDirectory("/tmp/cache");
embeddingClient.setTokenizerOptions(Map.of("padding", "true"));
embeddingClient.afterPropertiesSet();
List<List<Double>> embeddings = embeddingClient.embed(List.of("Hello world", "World is big"));
System.out.println(embeddings.get(0).size());
}
}
Spring AI Text Embedding
于 2024-06-20 21:47:04 首次发布