|
|
@ -5,7 +5,9 @@ import cn.hutool.http.HttpResponse; |
|
|
|
import cn.hutool.json.JSONUtil; |
|
|
|
import cn.hutool.json.JSONUtil; |
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
|
|
|
|
import com.alibaba.fastjson.serializer.SerializerFeature; |
|
|
|
import com.google.common.collect.Lists; |
|
|
|
import com.google.common.collect.Lists; |
|
|
|
|
|
|
|
import com.hnac.gglm.bigmodel.BigModelConstants; |
|
|
|
import com.hnac.gglm.bigmodel.configuration.BigModelInvokeApi; |
|
|
|
import com.hnac.gglm.bigmodel.configuration.BigModelInvokeApi; |
|
|
|
import com.hnac.gglm.bigmodel.utils.RequestClientUtil; |
|
|
|
import com.hnac.gglm.bigmodel.utils.RequestClientUtil; |
|
|
|
import com.hnac.hzinfo.exception.HzServiceException; |
|
|
|
import com.hnac.hzinfo.exception.HzServiceException; |
|
|
@ -30,6 +32,7 @@ import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.springblade.core.tool.api.ResultCode; |
|
|
|
import org.springblade.core.tool.api.ResultCode; |
|
|
|
import org.springblade.core.tool.utils.BeanUtil; |
|
|
|
import org.springblade.core.tool.utils.BeanUtil; |
|
|
|
import org.springblade.core.tool.utils.Func; |
|
|
|
import org.springblade.core.tool.utils.Func; |
|
|
|
|
|
|
|
import org.springblade.system.user.entity.User; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
|
|
|
@ -118,8 +121,12 @@ public class WeaviateService { |
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
public Boolean saveBatch(List entities,String className, Map<String,String> attrsMap) { |
|
|
|
public Boolean saveBatch(List entities,String className, Map<String,String> attrsMap) { |
|
|
|
|
|
|
|
Optional modelId = entities.stream().filter(entity -> { |
|
|
|
|
|
|
|
JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(entity)); |
|
|
|
|
|
|
|
return "1442295246932828161".equals(jsonObject.getString("modelId")); |
|
|
|
|
|
|
|
}).findFirst(); |
|
|
|
// 查询Weaviate 4.7.0表是否存在 若不存则新建表
|
|
|
|
// 查询Weaviate 4.7.0表是否存在 若不存则新建表
|
|
|
|
Result<Boolean> existResult = weaviateClient.schema().exists().withClassName("Hzn_lm_" + className).run(); |
|
|
|
Result<Boolean> existResult = weaviateClient.schema().exists().withClassName(BigModelConstants.PREFIX + "_" + className).run(); |
|
|
|
if(existResult.hasErrors() || !existResult.getResult()) { |
|
|
|
if(existResult.hasErrors() || !existResult.getResult()) { |
|
|
|
Map<java.lang.String,Object> createTableParams = new HashMap<>(2); |
|
|
|
Map<java.lang.String,Object> createTableParams = new HashMap<>(2); |
|
|
|
Map<String,String> deleteTableParams = new HashMap<>(1); |
|
|
|
Map<String,String> deleteTableParams = new HashMap<>(1); |
|
|
@ -134,7 +141,13 @@ public class WeaviateService { |
|
|
|
Map<String,Object> params = new HashMap<>(2); |
|
|
|
Map<String,Object> params = new HashMap<>(2); |
|
|
|
params.put("table_name", className); |
|
|
|
params.put("table_name", className); |
|
|
|
List<Map<String, Object>> data = new ArrayList<>(); |
|
|
|
List<Map<String, Object>> data = new ArrayList<>(); |
|
|
|
entities.forEach(entity -> data.add(this.getVectorData(entity,attrsMap))); |
|
|
|
entities.forEach(entity -> { |
|
|
|
|
|
|
|
// 将entity转换为Map<String,String>
|
|
|
|
|
|
|
|
JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(entity, SerializerFeature.WriteMapNullValue)); |
|
|
|
|
|
|
|
Map<String,String> map = new HashMap<>(); |
|
|
|
|
|
|
|
jsonObject.forEach((k,v) -> map.put(k,Optional.ofNullable(jsonObject.getString(k)).orElse(""))); |
|
|
|
|
|
|
|
data.add(this.getVectorData(map,attrsMap)); |
|
|
|
|
|
|
|
}); |
|
|
|
log.info("data:{}",JSON.toJSONString(data)); |
|
|
|
log.info("data:{}",JSON.toJSONString(data)); |
|
|
|
params.put("data",data); |
|
|
|
params.put("data",data); |
|
|
|
String url = gglmUrl + invokeApi.getInsertVectors(); |
|
|
|
String url = gglmUrl + invokeApi.getInsertVectors(); |
|
|
@ -142,14 +155,16 @@ public class WeaviateService { |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private Map<String,Object> getVectorData(Object entity,Map<String,String> attrsMap) { |
|
|
|
|
|
|
|
|
|
|
|
private Map<String,Object> getVectorData(Map<String,String> entity,Map<String,String> attrsMap) { |
|
|
|
Map<String,Object> result = new HashMap<>(2); |
|
|
|
Map<String,Object> result = new HashMap<>(2); |
|
|
|
result.put("object", entity); |
|
|
|
result.put("object", entity); |
|
|
|
List<Map<String,String>> vectors = new ArrayList<>(); |
|
|
|
List<Map<String,String>> vectors = new ArrayList<>(); |
|
|
|
attrsMap.forEach((k,fields) -> { |
|
|
|
attrsMap.forEach((k,fields) -> { |
|
|
|
Map<String,String> vector = new HashMap<>(); |
|
|
|
Map<String,String> vector = new HashMap<>(); |
|
|
|
vector.put("key",k); |
|
|
|
vector.put("key",k); |
|
|
|
vector.put("content", this.getFieldValue(fields, entity)); |
|
|
|
String value = Func.toStrList(",", fields).stream().map(field -> entity.get(field)).collect(Collectors.joining(" ")); |
|
|
|
|
|
|
|
vector.put("content", value); |
|
|
|
vectors.add(vector); |
|
|
|
vectors.add(vector); |
|
|
|
}); |
|
|
|
}); |
|
|
|
result.put("vector", vectors); |
|
|
|
result.put("vector", vectors); |
|
|
@ -178,7 +193,7 @@ public class WeaviateService { |
|
|
|
if(Func.isEmpty(ids) && Func.isNotEmpty(className)) { |
|
|
|
if(Func.isEmpty(ids) && Func.isNotEmpty(className)) { |
|
|
|
// 删除className
|
|
|
|
// 删除className
|
|
|
|
Map<String,String> deleteTableParams = new HashMap<>(1); |
|
|
|
Map<String,String> deleteTableParams = new HashMap<>(1); |
|
|
|
deleteTableParams.put("table_name",className.replace("Hzn_lm_","")); |
|
|
|
deleteTableParams.put("table_name",className.replace(BigModelConstants.PREFIX + "_","")); |
|
|
|
RequestClientUtil.postCall(gglmUrl + invokeApi.getDeleteTable(),deleteTableParams); |
|
|
|
RequestClientUtil.postCall(gglmUrl + invokeApi.getDeleteTable(),deleteTableParams); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
// 删除记录
|
|
|
|
// 删除记录
|
|
|
|