|
|
@ -7,6 +7,7 @@ import com.alibaba.fastjson.JSON; |
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.google.common.collect.Lists; |
|
|
|
import com.google.common.collect.Lists; |
|
|
|
import com.hnac.gglm.bigmodel.configuration.BigModelInvokeApi; |
|
|
|
import com.hnac.gglm.bigmodel.configuration.BigModelInvokeApi; |
|
|
|
|
|
|
|
import com.hnac.gglm.bigmodel.utils.RequestClientUtil; |
|
|
|
import com.hnac.hzinfo.exception.HzServiceException; |
|
|
|
import com.hnac.hzinfo.exception.HzServiceException; |
|
|
|
import io.weaviate.client.WeaviateClient; |
|
|
|
import io.weaviate.client.WeaviateClient; |
|
|
|
import io.weaviate.client.base.Result; |
|
|
|
import io.weaviate.client.base.Result; |
|
|
@ -44,6 +45,8 @@ public class WeaviateService { |
|
|
|
|
|
|
|
|
|
|
|
@Value("${gglm.vectorUrl}") |
|
|
|
@Value("${gglm.vectorUrl}") |
|
|
|
private String vectorUrl; |
|
|
|
private String vectorUrl; |
|
|
|
|
|
|
|
@Value("${gglm.url}") |
|
|
|
|
|
|
|
private String gglmUrl; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 对象保存向量数据库 |
|
|
|
* 对象保存向量数据库 |
|
|
@ -64,44 +67,74 @@ public class WeaviateService { |
|
|
|
return !result.hasErrors(); |
|
|
|
return !result.hasErrors(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
// /**
|
|
|
|
* 对象批量保存向量数据库 |
|
|
|
// * 对象批量保存向量数据库
|
|
|
|
* @param entities 保存对象列表 |
|
|
|
// * @param entities 保存对象列表
|
|
|
|
* @param className 保存表名 |
|
|
|
// * @param className 保存表名
|
|
|
|
* @param attrsMap 待计算的列信息 key-向量名 value-实体类对象属性,多个按逗号分隔 |
|
|
|
// * @param attrsMap 待计算的列信息 key-向量名 value-实体类对象属性,多个按逗号分隔
|
|
|
|
* @return 保存操作结果 |
|
|
|
// * @return 保存操作结果
|
|
|
|
*/ |
|
|
|
// */
|
|
|
|
|
|
|
|
// public Boolean saveBatch(List entities,String className, Map<String,String> attrsMap) {
|
|
|
|
|
|
|
|
// entities = entities.subList(0, 1);
|
|
|
|
|
|
|
|
// ObjectCreator creator = weaviateClient.data().creator().withClassName(className);
|
|
|
|
|
|
|
|
// List<String> vectorStrs = Lists.newArrayList();
|
|
|
|
|
|
|
|
// List<String> attrs = Lists.newArrayList();
|
|
|
|
|
|
|
|
// if(Func.isNotEmpty(attrsMap)) {
|
|
|
|
|
|
|
|
// // 格式化数据
|
|
|
|
|
|
|
|
// attrsMap.forEach((k,v) -> attrs.add(v));
|
|
|
|
|
|
|
|
// // 解析待计算的向量字段
|
|
|
|
|
|
|
|
// entities.forEach(entity -> {
|
|
|
|
|
|
|
|
// List<String> vectorStr = attrs.stream().map(fields -> this.getFieldValue(fields, entity)).filter(Func::isNotEmpty).collect(Collectors.toList());
|
|
|
|
|
|
|
|
// vectorStrs.addAll(vectorStr);
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if(Func.isNotEmpty(vectorStrs)) {
|
|
|
|
|
|
|
|
// // 若解析出来的向量存在值
|
|
|
|
|
|
|
|
// Float[] vectors = this.compute(vectorStrs);
|
|
|
|
|
|
|
|
// List<Map<String, Float[]>> vector = this.splitVector(entities.size(), attrsMap, vectors);
|
|
|
|
|
|
|
|
// for(int i = 0; i < entities.size(); i++) {
|
|
|
|
|
|
|
|
// // log.info("vector:{}",JSON.toJSONString(vector.get(i)));
|
|
|
|
|
|
|
|
// Map<String, Object> properties = this.objectToMap(entities.get(i));
|
|
|
|
|
|
|
|
// log.info("properties:{}",JSON.toJSONString(properties));
|
|
|
|
|
|
|
|
// Result<WeaviateObject> run = creator.withProperties(properties).withVectors(vector.get(i)).run();
|
|
|
|
|
|
|
|
// if(run.hasErrors()) {
|
|
|
|
|
|
|
|
// log.error("保存失败!,保存结果为:{}",JSON.toJSONString(run));
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// } else {
|
|
|
|
|
|
|
|
// entities.forEach(entity -> creator.withProperties(this.objectToMap(entity)).run());
|
|
|
|
|
|
|
|
// return true;
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return false;
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
public Boolean saveBatch(List entities,String className, Map<String,String> attrsMap) { |
|
|
|
public Boolean saveBatch(List entities,String className, Map<String,String> attrsMap) { |
|
|
|
ObjectCreator creator = weaviateClient.data().creator().withClassName(className); |
|
|
|
Map<String,String> createTableParams = new HashMap<>(1); |
|
|
|
List<String> vectorStrs = Lists.newArrayList(); |
|
|
|
createTableParams.put("table_name",className); |
|
|
|
List<String> attrs = Lists.newArrayList(); |
|
|
|
RequestClientUtil.postCall(gglmUrl + invokeApi.getCreateTable(),createTableParams); |
|
|
|
if(Func.isNotEmpty(attrsMap)) { |
|
|
|
Map<String,Object> params = new HashMap<>(2); |
|
|
|
// 格式化数据
|
|
|
|
params.put("table_name", className); |
|
|
|
attrsMap.forEach((k,v) -> attrs.add(v)); |
|
|
|
List<Map<String, Object>> data = new ArrayList<>(); |
|
|
|
// 解析待计算的向量字段
|
|
|
|
entities.forEach(entity -> data.add(this.getVectorData(entity,attrsMap))); |
|
|
|
entities.forEach(entity -> { |
|
|
|
log.info("data:{}",JSON.toJSONString(data)); |
|
|
|
List<String> vectorStr = attrs.stream().map(fields -> this.getFieldValue(fields, entity)).filter(Func::isNotEmpty).collect(Collectors.toList()); |
|
|
|
params.put("data",data); |
|
|
|
vectorStrs.addAll(vectorStr); |
|
|
|
String url = gglmUrl + invokeApi.getInsertVectors(); |
|
|
|
}); |
|
|
|
RequestClientUtil.postCall(url,params); |
|
|
|
} |
|
|
|
|
|
|
|
if(Func.isNotEmpty(vectorStrs)) { |
|
|
|
|
|
|
|
// 若解析出来的向量存在值
|
|
|
|
|
|
|
|
Float[] vectors = this.compute(vectorStrs); |
|
|
|
|
|
|
|
List<Map<String, Float[]>> vector = this.splitVector(entities.size(), attrsMap, vectors); |
|
|
|
|
|
|
|
for(int i = 0; i < entities.size(); i++) { |
|
|
|
|
|
|
|
// log.info("vector:{}",JSON.toJSONString(vector.get(i)));
|
|
|
|
|
|
|
|
Map<String, Object> properties = this.objectToMap(entities.get(i)); |
|
|
|
|
|
|
|
log.info("properties:{}",JSON.toJSONString(properties)); |
|
|
|
|
|
|
|
Result<WeaviateObject> run = creator.withProperties(properties).withVectors(vector.get(i)).run(); |
|
|
|
|
|
|
|
if(run.hasErrors()) { |
|
|
|
|
|
|
|
log.error("保存失败!,保存结果为:{}",JSON.toJSONString(run)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
entities.forEach(entity -> creator.withProperties(this.objectToMap(entity)).run()); |
|
|
|
|
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
return false; |
|
|
|
|
|
|
|
|
|
|
|
private Map<String,Object> getVectorData(Object entity,Map<String,String> attrsMap) { |
|
|
|
|
|
|
|
Map<String,Object> result = new HashMap<>(2); |
|
|
|
|
|
|
|
result.put("object", entity); |
|
|
|
|
|
|
|
List<Map<String,String>> vectors = new ArrayList<>(); |
|
|
|
|
|
|
|
attrsMap.forEach((k,fields) -> { |
|
|
|
|
|
|
|
Map<String,String> vector = new HashMap<>(); |
|
|
|
|
|
|
|
vector.put("key",k); |
|
|
|
|
|
|
|
vector.put("content", this.getFieldValue(fields, entity)); |
|
|
|
|
|
|
|
vectors.add(vector); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
result.put("vector", vectors); |
|
|
|
|
|
|
|
return result; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private Map<String,Object> objectToMap(Object object) { |
|
|
|
private Map<String,Object> objectToMap(Object object) { |
|
|
|