Skip to content

Commit

Permalink
Workaround: use QueryArgs.param or dialect depending on the Quarkus v…
Browse files Browse the repository at this point in the history
…ersion
  • Loading branch information
jmartisk committed Sep 17, 2024
1 parent 0b9c339 commit 4271b7c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.quarkiverse.langchain4j.redis.runtime.RedisEmbeddingStoreConfig;
import io.quarkiverse.langchain4j.redis.runtime.RedisEmbeddingStoreRecorder;
import io.quarkus.arc.deployment.SyntheticBeanBuildItem;
import io.quarkus.builder.Version;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.ExecutionTime;
Expand Down Expand Up @@ -57,6 +58,11 @@ public void createBean(
.add("value", clientName)
.build();
}

// FIXME: after updating to Quarkus 3.15, this workaround can be removed
int minorVersion = Integer.parseInt(Version.getVersion().split("\\.")[1]);
boolean workaround_specifyDialectAsParameter = minorVersion < 13;

beanProducer.produce(SyntheticBeanBuildItem
.configure(REDIS_EMBEDDING_STORE)
.types(ClassType.create(EmbeddingStore.class),
Expand All @@ -67,7 +73,7 @@ public void createBean(
.scope(ApplicationScoped.class)
.addInjectionPoint(ClassType.create(DotName.createSimple(ReactiveRedisDataSource.class)),
redisClientQualifier)
.createWith(recorder.embeddingStoreFunction(config, clientName))
.createWith(recorder.embeddingStoreFunction(config, clientName, workaround_specifyDialectAsParameter))
.done());
embeddingStoreProducer.produce(new EmbeddingStoreBuildItem());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,20 @@ public class RedisEmbeddingStore implements EmbeddingStore<TextSegment> {
private final boolean indexCreated;
private boolean warnedAboutWrongDimension = false;

// FIXME: this can be removed after upgrading to Quarkus 3.15
private final boolean workaround_specifyDialectAsParameter;

private static final String SCORE_FIELD_NAME = "vector_score";

public static Builder builder() {
return new Builder();
}

public RedisEmbeddingStore(ReactiveRedisDataSource ds, RedisSchema schema) {
public RedisEmbeddingStore(ReactiveRedisDataSource ds, RedisSchema schema, boolean workaround_specifyDialectAsParameter) {
this.ds = ds;
this.schema = schema;
this.indexCreated = createIndexIfDoesNotExist();
this.workaround_specifyDialectAsParameter = workaround_specifyDialectAsParameter;
}

private boolean createIndexIfDoesNotExist() {
Expand Down Expand Up @@ -174,9 +178,15 @@ public EmbeddingSearchResult<TextSegment> search(EmbeddingSearchRequest request)
schema.getVectorFieldName(), SCORE_FIELD_NAME);
QueryArgs args = new QueryArgs()
.sortByAscending(SCORE_FIELD_NAME)
.param("DIALECT", "2")
.param("BLOB", request.queryEmbedding().vector());

if (workaround_specifyDialectAsParameter) {
args = args.param("DIALECT", "2");
} else {
// FIXME: always do just this after upgrading to Quarkus 3.15
args = args.dialect(2);
}

Uni<SearchQueryResponse> search = ds.search()
.ftSearch(schema.getIndexName(), query, args);
SearchQueryResponse response = search.await().indefinitely();
Expand Down Expand Up @@ -239,6 +249,7 @@ public static class Builder {
private ReactiveRedisDataSource redisClient;

private RedisSchema schema;
private boolean workaround_specifyDialectAsParameter;

public Builder dataSource(ReactiveRedisDataSource client) {
this.redisClient = client;
Expand All @@ -250,8 +261,13 @@ public Builder schema(RedisSchema schema) {
return this;
}

public Builder workaround_specifyDialectAsParameter(boolean workaround_specifyDialectAsParameter) {
this.workaround_specifyDialectAsParameter = workaround_specifyDialectAsParameter;
return this;
}

public RedisEmbeddingStore build() {
return new RedisEmbeddingStore(redisClient, schema);
return new RedisEmbeddingStore(redisClient, schema, workaround_specifyDialectAsParameter);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public class RedisEmbeddingStoreRecorder {

public Function<SyntheticCreationalContext<RedisEmbeddingStore>, RedisEmbeddingStore> embeddingStoreFunction(
RedisEmbeddingStoreConfig config, String clientName) {
RedisEmbeddingStoreConfig config, String clientName, boolean workaround_specifyDialectAsParameter) {
return new Function<>() {
@Override
public RedisEmbeddingStore apply(SyntheticCreationalContext<RedisEmbeddingStore> context) {
Expand All @@ -41,6 +41,7 @@ public RedisEmbeddingStore apply(SyntheticCreationalContext<RedisEmbeddingStore>
.metricType(config.distanceMetric())
.build();
builder.schema(schema);
builder.workaround_specifyDialectAsParameter(workaround_specifyDialectAsParameter);

return builder.build();
}
Expand Down

0 comments on commit 4271b7c

Please sign in to comment.