Skip to content

Commit

Permalink
Use 0 vector to query all chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
tbolis-at-mulesoft committed Nov 7, 2024
1 parent 09b3fe3 commit cd2755a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.mule.mulechain</groupId>
<artifactId>mulechain-vectors</artifactId>
<version>0.1.78-SNAPSHOT</version>
<version>0.1.79-SNAPSHOT</version>
<packaging>mule-extension</packaging>
<name>MAC Vectors</name>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.mule.runtime.extension.api.annotation.param.MediaType.APPLICATION_JSON;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -367,18 +368,22 @@ public InputStream listSourcesFromStore(String storeName,
EmbeddingModel embeddingModel = EmbeddingModelFactory.createModel(configuration, modelParams);
EmbeddingStore<TextSegment> store = EmbeddingStoreFactory.createStore(configuration, storeName, embeddingModel.dimension());

Embedding queryEmbedding = embeddingModel.embed(".").content();
// Create a general query vector (e.g., zero vector). Zero vector is often used when you need to retrieve all
// embeddings without any specific bias.
float[] queryVector = new float[embeddingModel.dimension()];
for (int i = 0; i < embeddingModel.dimension(); i++) {
queryVector[i]=0.0f; // Zero vector
}

Embedding queryEmbedding = new Embedding(queryVector);
EmbeddingSearchRequest searchRequest = EmbeddingSearchRequest.builder()
.queryEmbedding(queryEmbedding)
.maxResults(16384)
.maxResults(10000)
.minScore(0.0)
.build();

EmbeddingSearchResult<TextSegment> searchResult = store.search(searchRequest);
List<EmbeddingMatch<TextSegment>> embeddingMatches = searchResult.matches();
String information = embeddingMatches.stream()
.map(match -> match.embedded().text())
.collect(joining("\n\n"));

JSONObject jsonObject = new JSONObject();
jsonObject.put("storeName", storeName);
Expand Down Expand Up @@ -421,6 +426,7 @@ public InputStream listSourcesFromStore(String storeName,
}

jsonObject.put("sources", JsonUtils.jsonObjectCollectionToJsonArray(sourcesJSONObjectHashMap.values()));
jsonObject.put("sourceCount", sourcesJSONObjectHashMap.size());

return toInputStream(jsonObject.toString(), StandardCharsets.UTF_8);
}
Expand Down

0 comments on commit cd2755a

Please sign in to comment.