|
|
|
@ -2,10 +2,11 @@ package com.supervision.config;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.http.HttpUtil;
|
|
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import lombok.Data;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.ai.document.Document;
|
|
|
|
|
import org.springframework.ai.embedding.*;
|
|
|
|
|
import org.springframework.core.SpringProperties;
|
|
|
|
|
import org.springframework.util.Assert;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
@ -13,9 +14,9 @@ import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class VectorEmbeddingClient extends AbstractEmbeddingClient {
|
|
|
|
|
|
|
|
|
|
private final Logger logger = LoggerFactory.getLogger(getClass());
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public List<Double> embed(Document document) {
|
|
|
|
@ -30,17 +31,13 @@ public class VectorEmbeddingClient extends AbstractEmbeddingClient {
|
|
|
|
|
@Override
|
|
|
|
|
public EmbeddingResponse call(EmbeddingRequest request) {
|
|
|
|
|
Assert.notEmpty(request.getInstructions(), "At least one text is required!");
|
|
|
|
|
if (request.getInstructions().size() != 1) {
|
|
|
|
|
logger.warn(
|
|
|
|
|
"Ollama Embedding does not support batch embedding. Will make multiple API calls to embed(Document)");
|
|
|
|
|
}
|
|
|
|
|
List<List<Double>> embeddingList = new ArrayList<>();
|
|
|
|
|
String embeddingUrl = SpringProperties.getProperty("embeddingUrl");
|
|
|
|
|
for (String inputContent : request.getInstructions()) {
|
|
|
|
|
// TODO 这里需要吧inputContent转化为向量数据
|
|
|
|
|
String post = HttpUtil.post("http://192.168.10.42:8000/embeddings/", JSONUtil.toJsonStr(Map.of("text", inputContent)));
|
|
|
|
|
String o = JSONUtil.parseObj(post).getStr("embeddings");
|
|
|
|
|
List<Double> embedding = JSONUtil.toList(o, Double.class);
|
|
|
|
|
embeddingList.add(embedding);
|
|
|
|
|
// 这里需要吧inputContent转化为向量数据
|
|
|
|
|
String post = HttpUtil.post(embeddingUrl, JSONUtil.toJsonStr(Map.of("text", inputContent)));
|
|
|
|
|
EmbeddingData bean = JSONUtil.toBean(post, EmbeddingData.class);
|
|
|
|
|
embeddingList.add(bean.embeddings);
|
|
|
|
|
}
|
|
|
|
|
var indexCounter = new AtomicInteger(0);
|
|
|
|
|
List<Embedding> embeddings = embeddingList.stream()
|
|
|
|
@ -48,4 +45,9 @@ public class VectorEmbeddingClient extends AbstractEmbeddingClient {
|
|
|
|
|
.toList();
|
|
|
|
|
return new EmbeddingResponse(embeddings);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Data
|
|
|
|
|
private static class EmbeddingData {
|
|
|
|
|
private List<Double> embeddings;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|