langchain4j+milvus实战

本文主要研究一下如何使用langchain4j来对接向量数据库milvus

步骤

docker运行milvus

docker run -d \
        --name milvus-standalone \
        --security-opt seccomp:unconfined \
        -e ETCD_USE_EMBED=true \
        -e ETCD_DATA_DIR=/var/lib/milvus/etcd \
        -e ETCD_CONFIG_PATH=/milvus/configs/embedEtcd.yaml \
        -e COMMON_STORAGETYPE=local \
        -v $(pwd)/volumes/milvus:/var/lib/milvus \
        -v $(pwd)/embedEtcd.yaml:/milvus/configs/embedEtcd.yaml \
        -v $(pwd)/user.yaml:/milvus/configs/user.yaml \
        -p 19530:19530 \
        -p 9091:9091 \
        -p 2379:2379 \
        --health-cmd="curl -f http://localhost:9091/healthz" \
        --health-interval=30s \
        --health-start-period=90s \
        --health-timeout=20s \
        --health-retries=3 \
        docker.1ms.run/milvusdb/milvus:v2.5.5 \
        milvus run standalone  1> /dev/null

启动之后访问http://127.0.0.1:9091/webui

在这里插入图片描述

这里需要提前创建embedEtcd.yaml

listen-client-urls: http://0.0.0.0:2379
advertise-client-urls: http://0.0.0.0:2379
quota-backend-bytes: 4294967296
auto-compaction-mode: revision
auto-compaction-retention: '1000'

user.yaml内容为空即可

pom.xml

<dependency>
    <groupId>dev.langchain4j</groupId>
    <artifactId>langchain4j-milvus</artifactId>
    <version>1.0.0-beta1</version>
</dependency>

example

public class JlamaMilvusExample {

    public static void main(String[] args) throws InterruptedException {
        EmbeddingModel embeddingModel = JlamaEmbeddingModel.builder()
                .modelName("intfloat/e5-small-v2")
                .build();

        MilvusServiceClient customMilvusClient = new MilvusServiceClient(
                ConnectParam.newBuilder()
                        .withHost("localhost")
                        .withPort(19530)
                        .build()
        );
        MilvusEmbeddingStore embeddingStore = MilvusEmbeddingStore.builder()
                .milvusClient(customMilvusClient)
                .collectionName("example_collection")      // Name of the collection
                .dimension(384)                            // Dimension of vectors
                .indexType(IndexType.FLAT)                 // Index type
                .metricType(MetricType.COSINE)             // Metric type
                .consistencyLevel(ConsistencyLevelEnum.EVENTUALLY)  // Consistency level
                .autoFlushOnInsert(true)                   // Auto flush after insert
                .idFieldName("id")                         // ID field name
                .textFieldName("text")                     // Text field name
                .metadataFieldName("metadata")             // Metadata field name
                .vectorFieldName("vector")                 // Vector field name
                .build();                                  // Build the MilvusEmbeddingStore instance

        TextSegment segment1 = TextSegment.from("I like football.");
        Embedding embedding1 = embeddingModel.embed(segment1).content();
        embeddingStore.add(embedding1, segment1);

        TimeUnit.SECONDS.sleep(60);

        TextSegment segment2 = TextSegment.from("The weather is good today.");
        Embedding embedding2 = embeddingModel.embed(segment2).content();
        embeddingStore.add(embedding2, segment2);

        TimeUnit.SECONDS.sleep(60);

        String userQuery = "What is your favourite sport?";
        Embedding queryEmbedding = embeddingModel.embed(userQuery).content();
        int maxResults = 1;
        List<EmbeddingMatch<TextSegment>> relevant = embeddingStore.findRelevant(queryEmbedding, maxResults);
        EmbeddingMatch<TextSegment> embeddingMatch = relevant.get(0);

        System.out.println("Question: " + userQuery); // What is your favourite sport?
        System.out.println("Response: " + embeddingMatch.embedded().text()); // I like football.
    }
}

最后输出

WARNING: Using incubator modules: jdk.incubator.vector
INFO  c.g.tjake.jlama.model.AbstractModel - Model type = F32, Working memory type = F32, Quantized memory type = F32
WARN  c.g.t.j.t.o.TensorOperationsProvider - Native operations not available. Consider adding 'com.github.tjake:jlama-native' to the classpath
INFO  c.g.t.j.t.o.TensorOperationsProvider - Using Panama Vector Operations (OffHeap)
Question: What is your favourite sport?
Response: I like football.

quotaAndLimits

quotaAndLimits:
  enabled: true # `true` to enable quota and limits, `false` to disable.
  # quotaCenterCollectInterval is the time interval that quotaCenter
  # collects metrics from Proxies, Query cluster and Data cluster.
  # seconds, (0 ~ 65536)
  quotaCenterCollectInterval: 3
  limits:
    allocRetryTimes: 15 # retry times when delete alloc forward data from rate limit failed
    allocWaitInterval: 1000 # retry wait duration when delete alloc forward data rate failed, in millisecond
    complexDeleteLimitEnable: false # whether complex delete check forward data by limiter
    maxCollectionNum: 65536
    maxCollectionNumPerDB: 65536 # Maximum number of collections per database.
    maxInsertSize: -1 # maximum size of a single insert request, in bytes, -1 means no limit
    maxResourceGroupNumOfQueryNode: 1024 # maximum number of resource groups of query nodes
    maxGroupSize: 10 # maximum size for one single group when doing search group by
  ddl:
    enabled: false # Whether DDL request throttling is enabled.
    # Maximum number of collection-related DDL requests per second.
    # Setting this item to 10 indicates that Milvus processes no more than 10 collection-related DDL requests per second, including collection creation requests, collection drop requests, collection load requests, and collection release requests.
    # To use this setting, set quotaAndLimits.ddl.enabled to true at the same time.
    collectionRate: -1
    # Maximum number of partition-related DDL requests per second.
    # Setting this item to 10 indicates that Milvus processes no more than 10 partition-related requests per second, including partition creation requests, partition drop requests, partition load requests, and partition release requests.
    # To use this setting, set quotaAndLimits.ddl.enabled to true at the same time.
    partitionRate: -1
    db:
      collectionRate: -1 # qps of db level , default no limit, rate for CreateCollection, DropCollection, LoadCollection, ReleaseCollection
      partitionRate: -1 # qps of db level, default no limit, rate for CreatePartition, DropPartition, LoadPartition, ReleasePartition
  indexRate:
    enabled: false # Whether index-related request throttling is enabled.
    # Maximum number of index-related requests per second.
    # Setting this item to 10 indicates that Milvus processes no more than 10 partition-related requests per second, including index creation requests and index drop requests.
    # To use this setting, set quotaAndLimits.indexRate.enabled to true at the same time.
    max: -1
    db:
      max: -1 # qps of db level, default no limit, rate for CreateIndex, DropIndex
  flushRate:
    enabled: true # Whether flush request throttling is enabled.
    # Maximum number of flush requests per second.
    # Setting this item to 10 indicates that Milvus processes no more than 10 flush requests per second.
    # To use this setting, set quotaAndLimits.flushRate.enabled to true at the same time.
    max: -1
    collection:
      max: 10 # qps, default no limit, rate for flush at collection level.
    db:
      max: -1 # qps of db level, default no limit, rate for flush
  compactionRate:
    enabled: false # Whether manual compaction request throttling is enabled.
    # Maximum number of manual-compaction requests per second.
    # Setting this item to 10 indicates that Milvus processes no more than 10 manual-compaction requests per second.
    # To use this setting, set quotaAndLimits.compaction.enabled to true at the same time.
    max: -1
    db:
      max: -1 # qps of db level, default no limit, rate for manualCompaction
  dml:
    enabled: false # Whether DML request throttling is enabled.
    insertRate:
      # Highest data insertion rate per second.
      # Setting this item to 5 indicates that Milvus only allows data insertion at the rate of 5 MB/s.
      # To use this setting, set quotaAndLimits.dml.enabled to true at the same time.
      max: -1
      db:
        max: -1 # MB/s, default no limit
      collection:
        # Highest data insertion rate per collection per second.
        # Setting this item to 5 indicates that Milvus only allows data insertion to any collection at the rate of 5 MB/s.
        # To use this setting, set quotaAndLimits.dml.enabled to true at the same time.
        max: -1
      partition:
        max: -1 # MB/s, default no limit
    upsertRate:
      max: -1 # MB/s, default no limit
      db:
        max: -1 # MB/s, default no limit
      collection:
        max: -1 # MB/s, default no limit
      partition:
        max: -1 # MB/s, default no limit
    deleteRate:
      # Highest data deletion rate per second.
      # Setting this item to 0.1 indicates that Milvus only allows data deletion at the rate of 0.1 MB/s.
      # To use this setting, set quotaAndLimits.dml.enabled to true at the same time.
      max: -1
      db:
        max: -1 # MB/s, default no limit
      collection:
        # Highest data deletion rate per second.
        # Setting this item to 0.1 indicates that Milvus only allows data deletion from any collection at the rate of 0.1 MB/s.
        # To use this setting, set quotaAndLimits.dml.enabled to true at the same time.
        max: -1
      partition:
        max: -1 # MB/s, default no limit
    bulkLoadRate:
      max: -1 # MB/s, default no limit, not support yet. TODO: limit bulkLoad rate
      db:
        max: -1 # MB/s, default no limit, not support yet. TODO: limit db bulkLoad rate
      collection:
        max: -1 # MB/s, default no limit, not support yet. TODO: limit collection bulkLoad rate
      partition:
        max: -1 # MB/s, default no limit, not support yet. TODO: limit partition bulkLoad rate
  dql:
    enabled: false # Whether DQL request throttling is enabled.
    searchRate:
      # Maximum number of vectors to search per second.
      # Setting this item to 100 indicates that Milvus only allows searching 100 vectors per second no matter whether these 100 vectors are all in one search or scattered across multiple searches.
      # To use this setting, set quotaAndLimits.dql.enabled to true at the same time.
      max: -1
      db:
        max: -1 # vps (vectors per second), default no limit
      collection:
        # Maximum number of vectors to search per collection per second.
        # Setting this item to 100 indicates that Milvus only allows searching 100 vectors per second per collection no matter whether these 100 vectors are all in one search or scattered across multiple searches.
        # To use this setting, set quotaAndLimits.dql.enabled to true at the same time.
        max: -1
      partition:
        max: -1 # vps (vectors per second), default no limit
    queryRate:
      # Maximum number of queries per second.
      # Setting this item to 100 indicates that Milvus only allows 100 queries per second.
      # To use this setting, set quotaAndLimits.dql.enabled to true at the same time.
      max: -1
      db:
        max: -1 # qps, default no limit
      collection:
        # Maximum number of queries per collection per second.
        # Setting this item to 100 indicates that Milvus only allows 100 queries per collection per second.
        # To use this setting, set quotaAndLimits.dql.enabled to true at the same time.
        max: -1
      partition:
        max: -1 # qps, default no limit
  limitWriting:
    # forceDeny false means dml requests are allowed (except for some
    # specific conditions, such as memory of nodes to water marker), true means always reject all dml requests.
    forceDeny: false
    ttProtection:
      enabled: false
      # maxTimeTickDelay indicates the backpressure for DML Operations.
      # DML rates would be reduced according to the ratio of time tick delay to maxTimeTickDelay,
      # if time tick delay is greater than maxTimeTickDelay, all DML requests would be rejected.
      # seconds
      maxTimeTickDelay: 300
    memProtection:
      # When memory usage > memoryHighWaterLevel, all dml requests would be rejected;
      # When memoryLowWaterLevel < memory usage < memoryHighWaterLevel, reduce the dml rate;
      # When memory usage < memoryLowWaterLevel, no action.
      enabled: true
      dataNodeMemoryLowWaterLevel: 0.85 # (0, 1], memoryLowWaterLevel in DataNodes
      dataNodeMemoryHighWaterLevel: 0.95 # (0, 1], memoryHighWaterLevel in DataNodes
      queryNodeMemoryLowWaterLevel: 0.85 # (0, 1], memoryLowWaterLevel in QueryNodes
      queryNodeMemoryHighWaterLevel: 0.95 # (0, 1], memoryHighWaterLevel in QueryNodes
    growingSegmentsSizeProtection:
      # No action will be taken if the growing segments size is less than the low watermark.
      # When the growing segments size exceeds the low watermark, the dml rate will be reduced,
      # but the rate will not be lower than minRateRatio * dmlRate.
      enabled: false
      minRateRatio: 0.5
      lowWaterLevel: 0.2
      highWaterLevel: 0.4
    diskProtection:
      enabled: true # When the total file size of object storage is greater than `diskQuota`, all dml requests would be rejected;
      diskQuota: -1 # MB, (0, +inf), default no limit
      diskQuotaPerDB: -1 # MB, (0, +inf), default no limit
      diskQuotaPerCollection: -1 # MB, (0, +inf), default no limit
      diskQuotaPerPartition: -1 # MB, (0, +inf), default no limit
    l0SegmentsRowCountProtection:
      enabled: false # switch to enable l0 segment row count quota
      lowWaterLevel: 30000000 # l0 segment row count quota, low water level
      highWaterLevel: 50000000 # l0 segment row count quota, high water level
    deleteBufferRowCountProtection:
      enabled: false # switch to enable delete buffer row count quota
      lowWaterLevel: 32768 # delete buffer row count quota, low water level
      highWaterLevel: 65536 # delete buffer row count quota, high water level
    deleteBufferSizeProtection:
      enabled: false # switch to enable delete buffer size quota
      lowWaterLevel: 134217728 # delete buffer size quota, low water level
      highWaterLevel: 268435456 # delete buffer size quota, high water level
  limitReading:
    # forceDeny false means dql requests are allowed (except for some
    # specific conditions, such as collection has been dropped), true means always reject all dql requests.
    forceDeny: false

注意milvus有频率控制,控制不好会报错

ERROR i.m.client.AbstractMilvusGrpcClient - FlushRequest failed, error code: 8, reason: request is rejected by grpc RateLimiter middleware, please retry later: rate limit exceeded[rate=0.1]
ERROR i.m.client.AbstractMilvusGrpcClient - FlushRequest failed! Exception:{}
io.milvus.exception.ServerException: request is rejected by grpc RateLimiter middleware, please retry later: rate limit exceeded[rate=0.1]
	at io.milvus.client.AbstractMilvusGrpcClient.handleResponse(AbstractMilvusGrpcClient.java:399)
	at io.milvus.client.AbstractMilvusGrpcClient.flush(AbstractMilvusGrpcClient.java:921)
	at io.milvus.client.MilvusServiceClient.lambda$flush$17(MilvusServiceClient.java:520)
	at io.milvus.client.MilvusServiceClient.retry(MilvusServiceClient.java:310)
	at io.milvus.client.MilvusServiceClient.flush(MilvusServiceClient.java:520)
	at dev.langchain4j.store.embedding.milvus.CollectionOperationsExecutor.flush(CollectionOperationsExecutor.java:32)
	at dev.langchain4j.store.embedding.milvus.MilvusEmbeddingStore.addAll(MilvusEmbeddingStore.java:246)
	at dev.langchain4j.store.embedding.milvus.MilvusEmbeddingStore.addInternal(MilvusEmbeddingStore.java:226)
	at dev.langchain4j.store.embedding.milvus.MilvusEmbeddingStore.add(MilvusEmbeddingStore.java:184)
	at JlamaMilvusExample.main(JlamaMilvusExample.java:63)
ERROR i.m.client.AbstractMilvusGrpcClient - FlushRequest failed, error code: 8, reason: request is rejected by grpc RateLimiter middleware, please retry later: rate limit exceeded[rate=0.1]
ERROR i.m.client.AbstractMilvusGrpcClient - FlushRequest failed! Exception:{}
io.milvus.exception.ServerException: request is rejected by grpc RateLimiter middleware, please retry later: rate limit exceeded[rate=0.1]
	at io.milvus.client.AbstractMilvusGrpcClient.handleResponse(AbstractMilvusGrpcClient.java:399)
	at io.milvus.client.AbstractMilvusGrpcClient.flush(AbstractMilvusGrpcClient.java:921)
	at io.milvus.client.MilvusServiceClient.lambda$flush$17(MilvusServiceClient.java:520)
	at io.milvus.client.MilvusServiceClient.retry(MilvusServiceClient.java:310)
	at io.milvus.client.MilvusServiceClient.flush(MilvusServiceClient.java:520)
	at dev.langchain4j.store.embedding.milvus.CollectionOperationsExecutor.flush(CollectionOperationsExecutor.java:32)
	at dev.langchain4j.store.embedding.milvus.MilvusEmbeddingStore.addAll(MilvusEmbeddingStore.java:246)
	at dev.langchain4j.store.embedding.milvus.MilvusEmbeddingStore.addInternal(MilvusEmbeddingStore.java:226)
	at dev.langchain4j.store.embedding.milvus.MilvusEmbeddingStore.add(MilvusEmbeddingStore.java:184)
	at JlamaMilvusExample.main(JlamaMilvusExample.java:63)
ERROR i.m.client.AbstractMilvusGrpcClient - FlushRequest failed, error code: 8, reason: request is rejected by grpc RateLimiter middleware, please retry later: rate limit exceeded[rate=0.1]
ERROR i.m.client.AbstractMilvusGrpcClient - FlushRequest failed! Exception:{}
io.milvus.exception.ServerException: request is rejected by grpc RateLimiter middleware, please retry later: rate limit exceeded[rate=0.1]
	at io.milvus.client.AbstractMilvusGrpcClient.handleResponse(AbstractMilvusGrpcClient.java:399)
	at io.milvus.client.AbstractMilvusGrpcClient.flush(AbstractMilvusGrpcClient.java:921)
	at io.milvus.client.MilvusServiceClient.lambda$flush$17(MilvusServiceClient.java:520)
	at io.milvus.client.MilvusServiceClient.retry(MilvusServiceClient.java:310)
	at io.milvus.client.MilvusServiceClient.flush(MilvusServiceClient.java:520)
	at dev.langchain4j.store.embedding.milvus.CollectionOperationsExecutor.flush(CollectionOperationsExecutor.java:32)
	at dev.langchain4j.store.embedding.milvus.MilvusEmbeddingStore.addAll(MilvusEmbeddingStore.java:246)
	at dev.langchain4j.store.embedding.milvus.MilvusEmbeddingStore.addInternal(MilvusEmbeddingStore.java:226)
	at dev.langchain4j.store.embedding.milvus.MilvusEmbeddingStore.add(MilvusEmbeddingStore.java:184)
	at JlamaMilvusExample.main(JlamaMilvusExample.java:63)
ERROR i.m.client.AbstractMilvusGrpcClient - FlushRequest failed, error code: 8, reason: request is rejected by grpc RateLimiter middleware, please retry later: rate limit exceeded[rate=0.1]
ERROR i.m.client.AbstractMilvusGrpcClient - FlushRequest failed! Exception:{}
io.milvus.exception.ServerException: request is rejected by grpc RateLimiter middleware, please retry later: rate limit exceeded[rate=0.1]
	at io.milvus.client.AbstractMilvusGrpcClient.handleResponse(AbstractMilvusGrpcClient.java:399)
	at io.milvus.client.AbstractMilvusGrpcClient.flush(AbstractMilvusGrpcClient.java:921)
	at io.milvus.client.MilvusServiceClient.lambda$flush$17(MilvusServiceClient.java:520)
	at io.milvus.client.MilvusServiceClient.retry(MilvusServiceClient.java:310)
	at io.milvus.client.MilvusServiceClient.flush(MilvusServiceClient.java:520)
	at dev.langchain4j.store.embedding.milvus.CollectionOperationsExecutor.flush(CollectionOperationsExecutor.java:32)
	at dev.langchain4j.store.embedding.milvus.MilvusEmbeddingStore.addAll(MilvusEmbeddingStore.java:246)
	at dev.langchain4j.store.embedding.milvus.MilvusEmbeddingStore.addInternal(MilvusEmbeddingStore.java:226)
	at dev.langchain4j.store.embedding.milvus.MilvusEmbeddingStore.add(MilvusEmbeddingStore.java:184)
	at JlamaMilvusExample.main(JlamaMilvusExample.java:63)
WARN  i.m.client.AbstractMilvusGrpcClient - Retry(4) with interval 270ms. Reason: io.milvus.exception.ServerException: request is rejected by grpc RateLimiter middleware, please retry later: rate limit exceeded[rate=0.1]
ERROR i.m.client.AbstractMilvusGrpcClient - FlushRequest failed, error code: 8, reason: request is rejected by grpc RateLimiter middleware, please retry later: rate limit exceeded[rate=0.1]
ERROR i.m.client.AbstractMilvusGrpcClient - FlushRequest failed! Exception:{}
io.milvus.exception.ServerException: request is rejected by grpc RateLimiter middleware, please retry later: rate limit exceeded[rate=0.1]
	at io.milvus.client.AbstractMilvusGrpcClient.handleResponse(AbstractMilvusGrpcClient.java:399)
	at io.milvus.client.AbstractMilvusGrpcClient.flush(AbstractMilvusGrpcClient.java:921)
	at io.milvus.client.MilvusServiceClient.lambda$flush$17(MilvusServiceClient.java:520)
	at io.milvus.client.MilvusServiceClient.retry(MilvusServiceClient.java:310)
	at io.milvus.client.MilvusServiceClient.flush(MilvusServiceClient.java:520)
	at dev.langchain4j.store.embedding.milvus.CollectionOperationsExecutor.flush(CollectionOperationsExecutor.java:32)
	at dev.langchain4j.store.embedding.milvus.MilvusEmbeddingStore.addAll(MilvusEmbeddingStore.java:246)
	at dev.langchain4j.store.embedding.milvus.MilvusEmbeddingStore.addInternal(MilvusEmbeddingStore.java:226)
	at dev.langchain4j.store.embedding.milvus.MilvusEmbeddingStore.add(MilvusEmbeddingStore.java:184)
	at JlamaMilvusExample.main(JlamaMilvusExample.java:63)
WARN  i.m.client.AbstractMilvusGrpcClient - Retry(5) with interval 810ms. Reason: io.milvus.exception.ServerException: request is rejected by grpc RateLimiter middleware, please retry later: rate limit exceeded[rate=0.1]
ERROR i.m.client.AbstractMilvusGrpcClient - FlushRequest failed, error code: 8, reason: request is rejected by grpc RateLimiter middleware, please retry later: rate limit exceeded[rate=0.1]
ERROR i.m.client.AbstractMilvusGrpcClient - FlushRequest failed! Exception:{}
io.milvus.exception.ServerException: request is rejected by grpc RateLimiter middleware, please retry later: rate limit exceeded[rate=0.1]
	at io.milvus.client.AbstractMilvusGrpcClient.handleResponse(AbstractMilvusGrpcClient.java:399)
	at io.milvus.client.AbstractMilvusGrpcClient.flush(AbstractMilvusGrpcClient.java:921)
	at io.milvus.client.MilvusServiceClient.lambda$flush$17(MilvusServiceClient.java:520)
	at io.milvus.client.MilvusServiceClient.retry(MilvusServiceClient.java:310)
	at io.milvus.client.MilvusServiceClient.flush(MilvusServiceClient.java:520)
	at dev.langchain4j.store.embedding.milvus.CollectionOperationsExecutor.flush(CollectionOperationsExecutor.java:32)
	at dev.langchain4j.store.embedding.milvus.MilvusEmbeddingStore.addAll(MilvusEmbeddingStore.java:246)
	at dev.langchain4j.store.embedding.milvus.MilvusEmbeddingStore.addInternal(MilvusEmbeddingStore.java:226)
	at dev.langchain4j.store.embedding.milvus.MilvusEmbeddingStore.add(MilvusEmbeddingStore.java:184)
	at JlamaMilvusExample.main(JlamaMilvusExample.java:63)
WARN  i.m.client.AbstractMilvusGrpcClient - Retry(6) with interval 2430ms. Reason: io.milvus.exception.ServerException: request is rejected by grpc RateLimiter middleware, please retry later: rate limit exceeded[rate=0.1]
ERROR i.m.client.AbstractMilvusGrpcClient - FlushRequest failed, error code: 8, reason: request is rejected by grpc RateLimiter middleware, please retry later: rate limit exceeded[rate=0.1]
ERROR i.m.client.AbstractMilvusGrpcClient - FlushRequest failed! Exception:{}
io.milvus.exception.ServerException: request is rejected by grpc RateLimiter middleware, please retry later: rate limit exceeded[rate=0.1]
	at io.milvus.client.AbstractMilvusGrpcClient.handleResponse(AbstractMilvusGrpcClient.java:399)
	at io.milvus.client.AbstractMilvusGrpcClient.flush(AbstractMilvusGrpcClient.java:921)
	at io.milvus.client.MilvusServiceClient.lambda$flush$17(MilvusServiceClient.java:520)
	at io.milvus.client.MilvusServiceClient.retry(MilvusServiceClient.java:310)
	at io.milvus.client.MilvusServiceClient.flush(MilvusServiceClient.java:520)
	at dev.langchain4j.store.embedding.milvus.CollectionOperationsExecutor.flush(CollectionOperationsExecutor.java:32)
	at dev.langchain4j.store.embedding.milvus.MilvusEmbeddingStore.addAll(MilvusEmbeddingStore.java:246)
	at dev.langchain4j.store.embedding.milvus.MilvusEmbeddingStore.addInternal(MilvusEmbeddingStore.java:226)
	at dev.langchain4j.store.embedding.milvus.MilvusEmbeddingStore.add(MilvusEmbeddingStore.java:184)
	at JlamaMilvusExample.main(JlamaMilvusExample.java:63)
WARN  i.m.client.AbstractMilvusGrpcClient - Retry(7) with interval 3000ms. Reason: io.milvus.exception.ServerException: request is rejected by grpc RateLimiter middleware, please retry later: rate limit exceeded[rate=0.1]

需要配置/milvus/configs/milvus.yaml,将quotaAndLimits.flushRate.collection.max调高一点,默认是0.1

小结

langchain4j提供了langchain4j-milvus用于集成对milvus的访问。

doc

<think>嗯,用户想了解如何使用langchain4j连接到Milvus数据库,并且需要示例教程。首先,我需要确认langchain4j是否支持Milvus。我记得Milvus通常是通过Java SDK连接的,而langchain4j主要集成了一些常见的数据库,比如Elasticsearch或Redis,但不确定是否有直接支持Milvus的模块。可能需要用户使用Milvus的Java客户端,然后结合langchain4j的嵌入功能和向量存储接口来实现。 接下来,我应该检查官方文档或社区资源是否有相关示例。假设没有现成的集成,可能需要手动创建VectorStore的实现类,利用Milvus的SDK进行数据操作。步骤大概包括添加依赖项、初始化Milvus客户端、实现embedding的存储和检索功能。此外,用户可能需要处理嵌入向量的生成,使用langchain4j的EmbeddingModel。 需要注意代码示例的正确性,比如依赖的版本是否兼容,Milvus的连接参数是否正确,如URI和API key。同时,要提醒用户确保Milvus服务已启动,并且正确配置了集合的索引,以便高效搜索。可能还需要提到性能优化或异常处理,比如连接超时或向量维度匹配的问题。 最后,生成相关问题时要涵盖常见的使用场景、性能优化、与其他数据库的对比,以及异常处理等方面,帮助用户全面理解集成过程。</think>以下是使用 LangChain4j 连接 Milvus 数据库的步骤说明和示例代码: --- ### 1. 添加依赖项 ```xml <dependency> <groupId>dev.langchain4j</groupId> <artifactId>langchain4j-milvus</artifactId> <version>0.25.0</version> <!-- 版本可能更新 --> </dependency> <dependency> <groupId>io.milvus</groupId> <artifactId>milvus-sdk-java</artifactId> <version>2.3.0</version> </dependency> ``` ### 2. 初始化 Milvus 客户端 ```java import io.milvus.client.MilvusServiceClient; import dev.langchain4j.store.embedding.milvus.MilvusEmbeddingStore; MilvusServiceClient milvusClient = new MilvusServiceClient( ConnectParam.newBuilder() .withUri("localhost:19530") .withToken("your_api_key") // 无认证可省略 .build() ); MilvusEmbeddingStore embeddingStore = new MilvusEmbeddingStore( milvusClient, "langchain_demo", // 集合名称 1536, // 向量维度(需匹配模型) MetricType.IP // 相似度计算方式 ); ``` ### 3. 使用示例 ```java // 创建嵌入模型 EmbeddingModel embeddingModel = new AllMiniLmL6V2EmbeddingModel(); // 生成文本向量 TextSegment textSegment = TextSegment.from("LangChain4j 集成指南"); Embedding embedding = embeddingModel.embed(textSegment.text()).content(); // 存储向量 String id = embeddingStore.add(embedding, textSegment); // 相似度搜索 List<EmbeddingMatch<TextSegment>> matches = embeddingStore.findRelevant(embedding, 10); ``` --- ### 关键参数说明 - 向量维度需与模型匹配,例如: - `all-MiniLM-L6-v2` 模型对应 $384$ 维 - OpenAI `text-embedding-3-small` 对应 $1536$ 维 - 索引建议使用 `IVF_FLAT` 或 `HNSW` 算法[^1]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值