Skip to content
LangChain4j 文档版本 1.22.0 源快照 413a86f5 更新于 2026-10-14

Couchbase

https://www.couchbase.com/

Maven 依赖

xml
<dependency>
    <groupId>dev.langchain4j</groupId>
    <artifactId>langchain4j-couchbase</artifactId>
    <version>1.22.0-beta32</version>
</dependency>

API

  • CouchbaseEmbeddingStore

示例

Couchbase 嵌入存储

Couchbase 的 langchain4j 集成将每个嵌入存储在单独的文档中,并使用 FTS 向量索引对存储的向量执行查询。目前,它支持存储嵌入及其元数据,以及移除 嵌入。在本教程撰写之时,尚不支持按元数据过滤被向量搜索选中的嵌入。请注意,该嵌入存储集成仍在活跃开发中,且其自带的默认配置不建议用于生产环境。

连接到 Couchbase 集群

可用一个 builder 类来初始化 couchbase 嵌入存储。初始化时以下参数为必填:

  • 集群连接字符串
  • 集群用户名
  • 集群密码
  • 用于存储嵌入的 bucket 名称
  • 用于存储嵌入的 scope 名称
  • 用于存储嵌入的 collection 名称
  • 嵌入存储所使用的 FTS 向量索引名称
  • 待存储向量的维度(长度)

下面这段示例代码演示了如何初始化一个连接到本地运行的 Couchbase 服务器的嵌入存储:

java
CouchbaseEmbeddingStore embeddingStore = CouchbaseEmbeddingStore.builder()
        .clusterUrl("localhost:8091")
        .username("Administrator")
        .password("password")
        .bucketName("langchain4j")
        .scopeName("_default")
        .collectionName("_default")
        .searchIndexName("test")
        .dimensions(512)
        .build();

该示例源码使用 testcontainers 库启动一个专属于它的 Couchbase 服务器:

java
CouchbaseContainer couchbaseContainer =
        new CouchbaseContainer(DockerImageName.parse("couchbase:enterprise").asCompatibleSubstituteFor("couchbase/server"))
                .withCredentials("Administrator", "password")
                .withBucket(testBucketDefinition)
                .withStartupTimeout(Duration.ofMinutes(1));

CouchbaseEmbeddingStore embeddingStore = CouchbaseEmbeddingStore.builder()
        .clusterUrl(couchbaseContainer.getConnectionString())
        .username(couchbaseContainer.getUsername())
        .password(couchbaseContainer.getPassword())
        .bucketName(testBucketDefinition.getName())
        .scopeName("_default")
        .collectionName("_default")
        .searchIndexName("test")
        .dimensions(384)
        .build();

向量索引

该嵌入存储使用一个 FTS 向量索引来执行向量相似度查找。若提供了一个在集群上不存在的向量索引名称,存储会尝试 基于所提供的初始化设置创建一个带默认配置的新索引。建议手动审阅所创建索引的设置,并按具体用例加以调整。关于向量搜索与 FTS 索引配置的更多信息,可在 Couchbase 文档中找到。

嵌入文档

该集成会自动为所有存储的嵌入分配唯一的 UUID 标识符。下面是一个嵌入文档示例(为便于阅读,向量字段值已截断):

json
{
  "id": "f4831648-07ca-4c77-a031-75acb6c1cf2f",
  "vector": [
    ...
    0.037255168,
    -0.001608681
  ],
  "text": "text",
  "metadata": {
    "some": "value"
  },
  "score": 0
}

这些嵌入由开发者选定的嵌入模型生成,得到的向量值因模型而异。

将嵌入存入 Couchbase

用嵌入模型生成的嵌入可通过 CouchbaseEmbeddingStore 类的 add 和 addAll 方法存入 couchbase:

java
EmbeddingModel embeddingModel = new AllMiniLmL6V2EmbeddingModel();

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

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

Thread.sleep(1000); // to be sure that embeddings were persisted

查询相关嵌入

在向存储中添加了若干嵌入之后,可用一个查询向量在存储中查找与它相关的嵌入。 这里,我们用嵌入模型为短语 "what is your favorite sport?" 生成一个向量。所获得的 向量随后被用来在数据库中查找最相关的答案:

java
Embedding queryEmbedding = embeddingModel.embed("What is your favourite sport?").content();
EmbeddingSearchRequest searchRequest = EmbeddingSearchRequest.builder()
        .queryEmbedding(queryEmbedding)
        .maxResults(1)
        .build();
EmbeddingSearchResult<TextSegment> searchResult = embeddingStore.search(searchRequest);
EmbeddingMatch<TextSegment> embeddingMatch = searchResult.matches().get(0);

随后,被选中答案的相关性分数与文本会被打印到应用输出:

java
System.out.println(embeddingMatch.score()); // 0.81442887
System.out.println(embeddingMatch.embedded().text()); // I like football.

删除嵌入

Couchbase 嵌入存储也支持按标识符移除嵌入,例如:

java
embeddingStore.remove(embeddingMatch.id())

或者,移除所有嵌入:

java
embeddingStore.removeAll();

BytesRouter 是社区翻译站点,所有译文仅供参考,一切以官方英文原文为准。