Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn about dimension mismatch #172

Merged
merged 1 commit into from
Dec 20, 2023
Merged
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 @@ -42,6 +42,8 @@ public class RedisEmbeddingStore implements EmbeddingStore<TextSegment> {
private final ReactiveRedisDataSource ds;
private final RedisSchema schema;
private final Logger LOG = Logger.getLogger(RedisEmbeddingStore.class);
private final boolean indexCreated;
private boolean warnedAboutWrongDimension = false;

private static final String SCORE_FIELD_NAME = "vector_score";

Expand All @@ -52,10 +54,10 @@ public static Builder builder() {
public RedisEmbeddingStore(ReactiveRedisDataSource ds, RedisSchema schema) {
this.ds = ds;
this.schema = schema;
createIndexIfDoesNotExist();
this.indexCreated = createIndexIfDoesNotExist();
}

private void createIndexIfDoesNotExist() {
private boolean createIndexIfDoesNotExist() {
List<String> indexes = ds.search().ft_list()
.onFailure().invoke(t -> {
if (t.getMessage().contains("unknown command")) {
Expand All @@ -71,8 +73,10 @@ private void createIndexIfDoesNotExist() {
schema.defineFields(indexCreateArgs);
LOG.debug("Creating Redis index " + schema.getIndexName());
ds.search().ftCreate(schema.getIndexName(), indexCreateArgs).await().indefinitely();
return true;
} else {
LOG.debug("Index in Redis already exists: " + schema.getIndexName());
return false;
}
}

Expand Down Expand Up @@ -130,6 +134,14 @@ private void addAllInternal(List<String> ids, List<Embedding> embeddings, List<T
TextSegment textSegment = embedded == null ? null : embedded.get(i);
Map<String, Object> fields = new HashMap<>();
fields.put(schema.getVectorFieldName(), embedding.vector());
if (!warnedAboutWrongDimension && indexCreated && embedding.vector().length != schema.getDimension()) {
LOG.warn("Creating an embedding with dimension " + embedding.vector().length + " but the index was " +
"created with dimension " + schema.getDimension() + ". " +
"This may result in embeddings not being found when they should be. " +
"Please check the quarkus.langchain4j.redis.dimension property. " +
"This warning will be shown only once.");
warnedAboutWrongDimension = true;
}
if (textSegment != null) {
fields.put(schema.getScalarFieldName(), textSegment.text());
fields.putAll(textSegment.metadata().asMap());
Expand Down