111
This commit is contained in:
1 parent
68a2b4f68c
commit
7cd697187c
308 files changed
+30454
-75
No files matched your search
@@ -0,0 +1,21 @@
|
||||
package com.luqj.djl.init;
|
||||
|
||||
import com.luqj.djl.utils.EmbeddingModelSingleton;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.Arrays;
|
||||
|
||||
@Component
|
||||
public class DjlInit {
|
||||
@Value("${simcse.path:}")
|
||||
private String simcsePath;
|
||||
@PostConstruct
|
||||
private void init() throws Exception {
|
||||
// umsVideoService.deleteRecommentByQuery("cn");
|
||||
// EmbeddingModelSingleton.getCriteria(simcsePath);
|
||||
// float[] queryVector = EmbeddingModelSingleton.embed("测试用户");
|
||||
// System.out.println(Arrays.toString(queryVector));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user