Skip to content

Commit

Permalink
Milvus: configurable field names (langchain4j#1852)
Browse files Browse the repository at this point in the history
  • Loading branch information
langchain4j committed Oct 10, 2024
1 parent 2bd251a commit dbfcd7c
Showing 1 changed file with 23 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,19 @@
import static dev.langchain4j.internal.Utils.getOrDefault;
import static dev.langchain4j.internal.ValidationUtils.ensureNotEmpty;
import static dev.langchain4j.internal.ValidationUtils.ensureNotNull;
import static dev.langchain4j.store.embedding.milvus.CollectionOperationsExecutor.*;
import static dev.langchain4j.store.embedding.milvus.CollectionOperationsExecutor.createCollection;
import static dev.langchain4j.store.embedding.milvus.CollectionOperationsExecutor.createIndex;
import static dev.langchain4j.store.embedding.milvus.CollectionOperationsExecutor.flush;
import static dev.langchain4j.store.embedding.milvus.CollectionOperationsExecutor.hasCollection;
import static dev.langchain4j.store.embedding.milvus.CollectionOperationsExecutor.insert;
import static dev.langchain4j.store.embedding.milvus.CollectionOperationsExecutor.loadCollectionInMemory;
import static dev.langchain4j.store.embedding.milvus.CollectionOperationsExecutor.removeForVector;
import static dev.langchain4j.store.embedding.milvus.CollectionRequestBuilder.buildSearchRequest;
import static dev.langchain4j.store.embedding.milvus.Generator.generateRandomIds;
import static dev.langchain4j.store.embedding.milvus.Mapper.*;
import static dev.langchain4j.store.embedding.milvus.Mapper.toEmbeddingMatches;
import static dev.langchain4j.store.embedding.milvus.Mapper.toMetadataJsons;
import static dev.langchain4j.store.embedding.milvus.Mapper.toScalars;
import static dev.langchain4j.store.embedding.milvus.Mapper.toVectors;
import static dev.langchain4j.store.embedding.milvus.MilvusMetadataFilterMapper.formatValues;
import static dev.langchain4j.store.embedding.milvus.MilvusMetadataFilterMapper.map;
import static io.milvus.common.clientenum.ConsistencyLevelEnum.EVENTUALLY;
Expand Down Expand Up @@ -100,10 +109,10 @@ public MilvusEmbeddingStore(
this.retrieveEmbeddingsOnSearch = getOrDefault(retrieveEmbeddingsOnSearch, false);
this.autoFlushOnInsert = getOrDefault(autoFlushOnInsert, false);
this.fieldDefinition = new FieldDefinition(
getOrDefault(idFieldName, DEFAULT_ID_FIELD_NAME),
getOrDefault(textFieldName, DEFAULT_TEXT_FIELD_NAME),
getOrDefault(metadataFiledName, DEFAULT_METADATA_FIELD_NAME),
getOrDefault(vectorFiledName, DEFAULT_VECTOR_FIELD_NAME));
getOrDefault(idFieldName, DEFAULT_ID_FIELD_NAME),
getOrDefault(textFieldName, DEFAULT_TEXT_FIELD_NAME),
getOrDefault(metadataFiledName, DEFAULT_METADATA_FIELD_NAME),
getOrDefault(vectorFiledName, DEFAULT_VECTOR_FIELD_NAME));

if (!hasCollection(this.milvusClient, this.collectionName)) {
createCollection(this.milvusClient, this.collectionName, this.fieldDefinition, ensureNotNull(dimension, "dimension"));
Expand Down Expand Up @@ -431,8 +440,8 @@ public Builder databaseName(String databaseName) {
}

/**
* @param idFieldName id field name of collection.
* Default value: id. In this case default id filed name will be used.
* @param idFieldName the name of the field where the ID of the {@link Embedding} is stored.
* Default value: "id".
* @return builder
*/
public Builder idFieldName(String idFieldName) {
Expand All @@ -441,8 +450,8 @@ public Builder idFieldName(String idFieldName) {
}

/**
* @param textFieldName text field name of collection.
* Default value: text. In this case default text field name will be used.
* @param textFieldName the name of the field where the text of the {@link TextSegment} is stored.
* Default value: "text".
* @return builder
*/
public Builder textFieldName(String textFieldName) {
Expand All @@ -451,8 +460,8 @@ public Builder textFieldName(String textFieldName) {
}

/**
* @param metadataFieldName metadata field name of database.
* Default value: metadata. In this case default metadata filed name will be used.
* @param metadataFieldName the name of the field where the {@link Metadata} of the {@link TextSegment} is stored.
* Default value: "metadata".
* @return builder
*/
public Builder metadataFieldName(String metadataFieldName) {
Expand All @@ -461,17 +470,15 @@ public Builder metadataFieldName(String metadataFieldName) {
}

/**
* @param vectorFieldName vector field name of database.
* Default value: vector. In this case default Milvus database name will be used.
* @param vectorFieldName the name of the field where the {@link Embedding} is stored.
* Default value: "vector".
* @return builder
*/
public Builder vectorFieldName(String vectorFieldName) {
this.vectorFieldName = vectorFieldName;
return this;
}



public MilvusEmbeddingStore build() {
return new MilvusEmbeddingStore(
host,
Expand Down

0 comments on commit dbfcd7c

Please sign in to comment.