Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Projection;
import com.datastax.astra.client.core.query.Sort;
import com.datastax.astra.client.core.rerank.RerankServiceOptions;
import com.datastax.astra.client.core.rerank.RerankedResult;
import com.datastax.astra.client.core.vector.DataAPIVector;
import com.datastax.astra.internal.command.AbstractCursor;
Expand Down Expand Up @@ -155,6 +156,20 @@ public CollectionFindAndRerankCursor<T, R> project(Projection... newProjection)
return newCursor;
}

/**
* Creates a new {@link CollectionFindAndRerankCursor} with an updated reranking services
*
* @param rerankService
* the new projection to apply
* @return a new {@link CollectionFindAndRerankCursor} instance with the specified reranking service
*/
public CollectionFindAndRerankCursor<T, R> rerankService(RerankServiceOptions rerankService) {
checkIdleState();
CollectionFindAndRerankCursor<T, R> newCursor = this.clone();
newCursor.options.rerankService(rerankService);
return newCursor;
}

/**
* Creates a new {@link CollectionFindAndRerankCursor} with a specified sort order.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import com.datastax.astra.client.core.options.BaseOptions;
import com.datastax.astra.client.core.query.Projection;
import com.datastax.astra.client.core.query.Sort;
import com.datastax.astra.client.core.rerank.RerankProvider;
import com.datastax.astra.client.core.rerank.RerankServiceOptions;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
Expand Down Expand Up @@ -71,6 +73,11 @@ public class CollectionFindAndRerankOptions extends BaseOptions<CollectionFindAn
*/
String rerankQuery;

/**
* Allowed definition of custom reranking
*/
RerankServiceOptions rerank;

/**
* Options for hybrid projection
*/
Expand Down Expand Up @@ -100,6 +107,7 @@ public CollectionFindAndRerankOptions(CollectionFindAndRerankOptions other) {
this.hybridLimits = other.hybridLimits;
this.limit = other.limit;
this.rerankOn = other.rerankOn;
this.rerank = other.rerank;
this.rerankQuery = other.rerankQuery;
this.includeScores = other.includeScores;
this.includeSortVector = other.includeSortVector;
Expand Down Expand Up @@ -212,6 +220,19 @@ public CollectionFindAndRerankOptions rerankOn(String rerankOn) {
return this;
}

/**
* Return a copy of this object with a new RerankServiceOptions setting.
* It allows to define the reranking model at query time.
*
* @param rerankService value for rerankService options
*
* @return current command
*/
public CollectionFindAndRerankOptions rerankService(RerankServiceOptions rerankService) {
this.rerank = rerankService;
return this;
}

/**
* Add a rerankQuery clause in the find block
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ public CollectionDefinition vector(int dimension, @NonNull SimilarityMetric func
* @return self reference
*/
public CollectionDefinition vectorize(String provider, String modeName) {
return vectorize(provider, modeName, null);
return vectorize(provider, modeName, (String) null);
}

/**
Expand All @@ -313,6 +313,18 @@ public CollectionDefinition vectorize(String provider, String modeName, String s
return this;
}

/**
* Enable Vectorization within the collection.
*
* @param provider provider Name (LLM)
* @param modeName mode name
* @param parameters expected parameters for vectorize
* @return self reference
*/
public CollectionDefinition vectorize(String provider, String modeName, Map<String, Object> parameters) {
return vectorize(provider, modeName, null, parameters);
}

/**
* Enable Vectorization within the collection.
*
Expand All @@ -328,6 +340,8 @@ public CollectionDefinition vectorize(String provider, String modeName, String s
return this;
}



// ---------------------
// Lexical options
// ---------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import com.datastax.astra.client.core.vector.VectorOptions;
import com.datastax.astra.client.collections.commands.cursor.CollectionFindAndRerankCursor;
import com.datastax.astra.client.databases.commands.results.FindRerankingProvidersResult;
import com.datastax.astra.client.exceptions.DataAPIException;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.*;
Expand All @@ -65,6 +66,7 @@
import static com.datastax.astra.client.core.lexical.AnalyzerTypes.STANDARD;
import static com.datastax.astra.client.core.lexical.AnalyzerTypes.WHITESPACE;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;

/**
* Abstract integration tests for findAndRerank, lexical search, and hybrid search.
Expand Down Expand Up @@ -651,4 +653,89 @@ void should_replaceOne_withVectorizeSort() {
assertThat(doc).isPresent();
assertThat(doc.get().getString("quote")).isEqualTo("Math is beautiful");
}

// ========== Reranker Service Mutation ==========

@Test
@Order(26)
void should_mutateRerankService_andVerifyChanges() {
if (skipIfNoRerankingKey()) return;

String collectionName = "c_rerank_mutation";
getDatabase().dropCollection(collectionName);

// Create collection with initial rerank service configuration
RerankServiceOptions initialRerankService = new RerankServiceOptions()
.modelName("nvidia/llama-3.2-nv-rerankqa-1b-v2")
.provider("nvidia");

CollectionRerankOptions initialRerankOptions = new CollectionRerankOptions()
.enabled(true)
.service(initialRerankService);

VectorServiceOptions vectorService = new VectorServiceOptions()
.provider("nvidia")
.modelName("NV-Embed-QA");

VectorOptions vectorOptions = new VectorOptions()
.dimension(1024)
.metric(SimilarityMetric.COSINE.getValue())
.service(vectorService);

CollectionDefinition initialDef = new CollectionDefinition()
.vector(vectorOptions)
.lexical(new LexicalOptions().enabled(true).analyzer(new Analyzer(STANDARD)))
.rerank(initialRerankOptions);

Collection<Document> col = getDatabase().createCollection(collectionName, initialDef);
assertThat(col).isNotNull();
assertThat(col.getDefinition().getRerank()).isNotNull();
assertThat(col.getDefinition().getRerank().getService().getModelName())
.isEqualTo("nvidia/llama-3.2-nv-rerankqa-1b-v2");

// Insert test documents
col.insertMany(List.of(
new Document().id("m1").put("text", "Artificial intelligence transforms technology").vectorize("Artificial intelligence transforms technology").lexical("Artificial intelligence transforms technology"),
new Document().id("m2").put("text", "Machine learning enables predictions").vectorize("Machine learning enables predictions").lexical("Machine learning enables predictions"),
new Document().id("m3").put("text", "Deep learning powers neural networks").vectorize("Deep learning powers neural networks").lexical("Deep learning powers neural networks")),
new CollectionInsertManyOptions().chunkSize(3));

// Perform findAndRerank with initial configuration
CollectionFindAndRerankOptions options = baseFindAndRerankOptions()
.sort(Sort.hybrid(new Hybrid("artificial intelligence and machine learning")))
.hybridLimits(10)
.limit(3);

List<RerankedResult<Document>> initialResults = col.findAndRerank(options).toList();
assertThat(initialResults).isNotEmpty();
log.info("Initial rerank results count: {}", initialResults.size());

// Mutate the rerank service by creating a new collection definition
// Note: In practice, mutation would involve updating collection settings if supported
// For this test, we verify that different rerank configurations can be applied
RerankServiceOptions validUpdatedRerankService = new RerankServiceOptions()
.modelName("nvidia/llama-3.2-nv-rerankqa-1b-v2")
.provider("nvidia"); // Add custom parameters

CollectionFindAndRerankOptions mutatedRerankOptions = new CollectionFindAndRerankOptions()
.rerankService(validUpdatedRerankService);

List<RerankedResult<Document>> updatedResults = col.findAndRerank(mutatedRerankOptions).toList();
assertThat(updatedResults).isNotEmpty();
log.info("Updated rerank results count: {}", initialResults.size());

CollectionFindAndRerankOptions mutatedInvalidRerankOptions = new CollectionFindAndRerankOptions()
.rerankService(new RerankServiceOptions().modelName("bla").provider("ble"));

try {
col.findAndRerank(mutatedInvalidRerankOptions).toList();
fail();
} catch (DataAPIException dataAPIException) {
// Should fail
}

// Cleanup
getDatabase().dropCollection(collectionName);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ public class CollectionCloneSettings {
@Builder.Default
private final int insertThreadPoolSize = 10;

/**
* Number of parallel threads for reading from source collection.
* More threads = faster reading through parallel skip/limit queries.
* Default: 5
*/
@Builder.Default
private final int readThreadPoolSize = 5;

/**
* Maximum time in seconds to wait for all insertions to complete.
* Default: 300 seconds (5 minutes)
Expand Down