added classes for vectorization, similarity search and summarization
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
package com.vaessl.app.ai;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.springframework.ai.document.Document;
|
||||
import org.springframework.ai.vectorstore.VectorStore;
|
||||
import org.springframework.stereotype.Service;
|
||||
import com.vaessl.app.shared.ServiceItem;
|
||||
import com.vaessl.app.shared.ServiceType;
|
||||
|
||||
@Service
|
||||
public class EmbeddingService {
|
||||
|
||||
private static final float THRESHOLD = 0.7f;
|
||||
private static final int LIMIT = 2;
|
||||
private final VectorStore vectorStore;
|
||||
|
||||
public EmbeddingService(VectorStore vectorStore) {
|
||||
this.vectorStore = vectorStore;
|
||||
}
|
||||
|
||||
public void vectorizeData(List<ServiceItem> responses, ServiceType serviceType,
|
||||
Long connectionId) {
|
||||
|
||||
List<Document> mappedResponse = new ArrayList<>();
|
||||
|
||||
for (ServiceItem response : responses) {
|
||||
|
||||
StringBuilder data = new StringBuilder();
|
||||
|
||||
if (response.extraData() != null) {
|
||||
for (Map.Entry<String, Object> entry : response.extraData().entrySet()) {
|
||||
if (!data.isEmpty()) {
|
||||
data.append(" \n");
|
||||
}
|
||||
data.append(entry.getKey());
|
||||
data.append(": ");
|
||||
data.append(entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
Map<String, Object> metadata = new HashMap<>();
|
||||
metadata.put("connectionId", connectionId);
|
||||
metadata.put("serviceType", serviceType.name());
|
||||
metadata.put("id", response.id());
|
||||
metadata.put("title", response.title());
|
||||
metadata.put("description", response.description());
|
||||
metadata.put("extraData", data.toString());
|
||||
|
||||
mappedResponse.add(new Document(connectionId + ":" + response.id(), "Title: "
|
||||
+ response.title()
|
||||
+ (response.description() != null ? "\n Description: " + response.description()
|
||||
: "")
|
||||
+ (!data.isEmpty() ? "\n Extradata: " + data.toString() : ""), metadata));
|
||||
}
|
||||
vectorStore.add(mappedResponse);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.vaessl.app.search;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Component;
|
||||
import com.vaessl.app.shared.ServiceItem;
|
||||
import com.vaessl.app.shared.ServiceType;
|
||||
|
||||
@Component
|
||||
public class HomeboxAiSearchProvider implements SearchProvider {
|
||||
|
||||
@Override
|
||||
public ServiceType getServiceType() {
|
||||
return ServiceType.HOMEBOX;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<ServiceItem> getSearchResults(SearchRequest request, Pageable pageable) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'getSearchResults'");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.vaessl.app.search;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class HomeboxSyncService {
|
||||
|
||||
}
|
||||
@@ -4,11 +4,11 @@ import java.util.List;
|
||||
import org.springframework.data.domain.Page;
|
||||
|
||||
public record PagedSearchResponse<T>(List<T> content, int page, int pageSize, long totalElements,
|
||||
boolean first, boolean last, String sort) {
|
||||
boolean first, boolean last, String sort, String summary) {
|
||||
|
||||
public static <T> PagedSearchResponse<T> from(Page<T> pageResult) {
|
||||
return new PagedSearchResponse<>(pageResult.getContent(), pageResult.getNumber(),
|
||||
pageResult.getSize(), pageResult.getTotalElements(), pageResult.isFirst(),
|
||||
pageResult.isLast(), pageResult.getSort().toString());
|
||||
pageResult.isLast(), pageResult.getSort().toString(), null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,5 +5,5 @@ import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
public record SearchRequest(@NotBlank String appUrl, @NotBlank String username, String query,
|
||||
@NotNull ServiceType serviceType) {
|
||||
@NotNull ServiceType serviceType, boolean aiSearch) {
|
||||
}
|
||||
|
||||
@@ -29,7 +29,8 @@ class HomeboxSearchProviderTest {
|
||||
|
||||
when(mockRepo.findByAppUrlAndUsername(MOCK_URL, MOCK_USER)).thenReturn(null);
|
||||
|
||||
SearchRequest request = new SearchRequest(MOCK_URL, MOCK_USER, "test query", HOMEBOX);
|
||||
SearchRequest request =
|
||||
new SearchRequest(MOCK_URL, MOCK_USER, "test query", HOMEBOX, false);
|
||||
Pageable pageable = PageRequest.of(0, 10);
|
||||
assertThrows(ConnectionNotFoundException.class,
|
||||
() -> provider.getSearchResults(request, pageable));
|
||||
|
||||
@@ -104,7 +104,8 @@ class SearchControllerTest {
|
||||
"appUrl": "http://irrelevant",
|
||||
"query": "Item",
|
||||
"serviceType": "HOMEBOX",
|
||||
"username": "irrelevant"
|
||||
"username": "irrelevant",
|
||||
"aiSearch": false
|
||||
}
|
||||
""")).andExpect(status().isUnauthorized());
|
||||
}
|
||||
@@ -117,22 +118,23 @@ class SearchControllerTest {
|
||||
}
|
||||
|
||||
private String searchRequestBody(WireMockRuntimeInfo wm, String serviceType) {
|
||||
return searchRequestBody(wm.getHttpBaseUrl(), serviceType);
|
||||
return searchRequestBody(wm.getHttpBaseUrl(), serviceType, false);
|
||||
}
|
||||
|
||||
private String searchRequestBody(String serviceType) {
|
||||
return searchRequestBody("http://irrelevant", serviceType);
|
||||
return searchRequestBody("http://irrelevant", serviceType, false);
|
||||
}
|
||||
|
||||
private String searchRequestBody(String appUrl, String serviceType) {
|
||||
private String searchRequestBody(String appUrl, String serviceType, boolean aiSearch) {
|
||||
return """
|
||||
{
|
||||
"appUrl": "%s",
|
||||
"query": "Item",
|
||||
"serviceType": "%s",
|
||||
"username": "%s"
|
||||
"username": "%s",
|
||||
"aiSearch": "%b"
|
||||
}
|
||||
""".formatted(appUrl, serviceType, MOCK_USER);
|
||||
""".formatted(appUrl, serviceType, MOCK_USER, aiSearch);
|
||||
}
|
||||
|
||||
private String connectionRequestBody(WireMockRuntimeInfo wm) {
|
||||
|
||||
Reference in New Issue
Block a user