refactored EmbeddingService
This commit is contained in:
@@ -22,37 +22,62 @@ public class EmbeddingService {
|
||||
}
|
||||
|
||||
public void vectorizeData(List<ServiceItem> items, ServiceType serviceType, Long connectionId) {
|
||||
|
||||
List<Document> mappedResponse = new ArrayList<>();
|
||||
|
||||
List<Document> documents = new ArrayList<>();
|
||||
for (ServiceItem item : items) {
|
||||
documents.add(toDocument(item, serviceType, connectionId));
|
||||
}
|
||||
vectorStore.add(documents);
|
||||
}
|
||||
|
||||
private Document toDocument(ServiceItem item, ServiceType serviceType, Long connectionId) {
|
||||
String extraData = buildExtraData(item.extraData());
|
||||
Map<String, Object> metadata = buildMetadata(item, serviceType, connectionId, extraData);
|
||||
String content = buildContent(item, extraData);
|
||||
return new Document(connectionId + ":" + item.id(), content, metadata);
|
||||
}
|
||||
|
||||
private String buildExtraData(Map<String, Object> extraData) {
|
||||
if (extraData == null) {
|
||||
return "";
|
||||
}
|
||||
StringBuilder data = new StringBuilder();
|
||||
|
||||
if (item.extraData() != null) {
|
||||
for (Map.Entry<String, Object> entry : item.extraData().entrySet()) {
|
||||
for (Map.Entry<String, Object> entry : extraData.entrySet()) {
|
||||
if (entry.getValue() == null || entry.getValue().toString().isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
if (!data.isEmpty()) {
|
||||
data.append(" \n");
|
||||
}
|
||||
data.append(entry.getKey());
|
||||
data.append(": ");
|
||||
data.append(entry.getValue());
|
||||
data.append(entry.getKey()).append(": ").append(entry.getValue());
|
||||
}
|
||||
return data.toString();
|
||||
}
|
||||
|
||||
private Map<String, Object> buildMetadata(ServiceItem item, ServiceType serviceType,
|
||||
Long connectionId, String extraData) {
|
||||
Map<String, Object> metadata = new HashMap<>();
|
||||
metadata.put("connectionId", connectionId);
|
||||
metadata.put("serviceType", serviceType.name());
|
||||
metadata.put("itemId", item.id());
|
||||
metadata.put("title", item.title());
|
||||
if (item.description() != null && !item.description().isEmpty()) {
|
||||
metadata.put("description", item.description());
|
||||
metadata.put("extraData", data.toString());
|
||||
|
||||
mappedResponse.add(new Document(connectionId + ":" + item.id(), "Title: " + item.title()
|
||||
+ (item.description() != null ? "\n Description: " + item.description() : "")
|
||||
+ (!data.isEmpty() ? "\n Extradata: " + data.toString() : ""), metadata));
|
||||
}
|
||||
vectorStore.add(mappedResponse);
|
||||
if (!extraData.isEmpty()) {
|
||||
metadata.put("extraData", extraData);
|
||||
}
|
||||
return metadata;
|
||||
}
|
||||
|
||||
private String buildContent(ServiceItem item, String extraData) {
|
||||
StringBuilder content = new StringBuilder("Title: ").append(item.title());
|
||||
if (item.description() != null && !item.description().isEmpty()) {
|
||||
content.append("\n Description: ").append(item.description());
|
||||
}
|
||||
if (!extraData.isEmpty()) {
|
||||
content.append("\n Extradata: ").append(extraData);
|
||||
}
|
||||
return content.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user