72 lines
2.4 KiB
Java
72 lines
2.4 KiB
Java
package com.luqj.djl.utils;
|
|
|
|
import ai.djl.Device;
|
|
import ai.djl.huggingface.translator.TextEmbeddingTranslatorFactory;
|
|
import ai.djl.inference.Predictor;
|
|
import ai.djl.repository.zoo.Criteria;
|
|
import ai.djl.repository.zoo.ZooModel;
|
|
import ai.djl.training.util.ProgressBar;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import java.nio.file.Paths;
|
|
|
|
@Slf4j
|
|
public class EmbeddingModelSingleton {
|
|
|
|
private static volatile Criteria<String, float[]> criteriaInstance;
|
|
private static volatile ZooModel<String, float[]> model;
|
|
|
|
private EmbeddingModelSingleton() {
|
|
// 私有构造函数,防止外部实例化
|
|
}
|
|
|
|
public static float[] embed(String text) {
|
|
try {
|
|
try (Predictor<String, float[]> predictor = model.newPredictor()) {
|
|
return predictor.predict(text);
|
|
}
|
|
} catch (Exception e) {
|
|
log.error(e.getMessage());
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
|
|
public static double cosine(float[] a, float[] b) {
|
|
double dot = 0, na = 0, nb = 0;
|
|
for (int i = 0; i < a.length; i++) {
|
|
dot += a[i] * b[i];
|
|
na += a[i] * a[i];
|
|
nb += b[i] * b[i];
|
|
}
|
|
return dot / (Math.sqrt(na) * Math.sqrt(nb) + 1e-8);
|
|
}
|
|
|
|
/**
|
|
* 获取 Criteria 单例
|
|
*/
|
|
public static Criteria<String, float[]> getCriteria(String path) {
|
|
if (criteriaInstance == null) {
|
|
synchronized (EmbeddingModelSingleton.class) {
|
|
if (criteriaInstance == null) {
|
|
criteriaInstance = Criteria.builder()
|
|
.setTypes(String.class, float[].class)
|
|
.optModelPath(Paths.get(path))
|
|
// .optModelUrls("djl://ai.djl.huggingface.pytorch/princeton-nlp/unsup-simcse-roberta-base")
|
|
.optEngine("PyTorch")
|
|
.optDevice(Device.cpu())
|
|
.optTranslatorFactory(new TextEmbeddingTranslatorFactory())
|
|
.optModelName("sup-simcse-bert-base-uncased.pt")
|
|
.optProgress(new ProgressBar())
|
|
.build();
|
|
try {
|
|
model = criteriaInstance.loadModel();
|
|
} catch (Exception e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return criteriaInstance;
|
|
}
|
|
}
|