松果

本节将指导您设置松果VectorStore以存储文档嵌入并执行相似性搜索。

松果是什么?

松果是一款流行的基于云的向量数据库,可让您高效存储和搜索向量。

先决条件

  1. 松果账户:在开始之前,请注册一个松果账户

  2. 松果项目:注册后,创建一个新项目、一个索引,并生成一个 API 密钥。您将需要这些详细信息来进行配置。

  3. OpenAI账户:在OpenAI 注册并在API 密钥中生成令牌。

配置

要设置PineconeVectorStore,请从您的松果账户收集以下详细信息:

  • 松果 API 密钥

  • 松果环境

  • 松果项目 ID

  • 松果索引名称

  • 松果命名空间

此信息可在松果 UI 门户中找到。

在设置嵌入时,选择1536的向量维度。这与 OpenAI 的模型text-embedding-ada-002的维度匹配,我们将在本指南中使用该模型。

此外,您需要提供您的 OpenAI API 密钥。将其设置为环境变量,如下所示:

export SPRING_AI_OPENAI_API_KEY='您的_OpenAI_API_Key'

仓库

要获取 Spring AI 构件,请声明 Spring 快照仓库:

<repository>
	<id>spring-snapshots</id>
	<name>Spring Snapshots</name>
	<url>https://repo.spring.io/snapshot</url>
	<releases>
		<enabled>false</enabled>
	</releases>
</repository>

依赖项

将这些依赖项添加到您的项目中:

  • OpenAI:用于计算嵌入。

<dependency>
	<groupId>org.springframework.ai</groupId>
	<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
  • Pinecone

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-pinecone</artifactId>
</dependency>
请参阅依赖管理部分,将Spring AI BOM添加到您的构建文件中。
Sure, here's your translated HTML snippet with the English text replaced by Chinese:

示例代码

为了在你的应用中配置Pinecone,你可以使用以下设置:

@Bean
public PineconeVectorStoreConfig pineconeVectorStoreConfig() {

    return PineconeVectorStoreConfig.builder()
        .withApiKey(<PINECONE_API_KEY>)
        .withEnvironment("gcp-starter")
        .withProjectId("89309e6")
        .withIndexName("spring-ai-test-index")
        .withNamespace("") // 免费版不支持命名空间。
        .build();
}

通过将Spring Boot OpenAI starter添加到你的项目中,与OpenAI的嵌入式技术集成。这为你提供了Embeddings客户端的实现:

@Bean
public VectorStore vectorStore(PineconeVectorStoreConfig config, EmbeddingClient embeddingClient) {
    return new PineconeVectorStore(config, embeddingClient);
}

在你的主代码中,创建一些文档:

List<Document> documents = List.of(
	new Document("Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!", Map.of("meta1", "meta1")),
	new Document("世界很大,救赎就在拐角处"),
	new Document("你向前走,面对过去,然后转身面向未来", Map.of("meta2", "meta2")));

将文档添加到Pinecone中:

vectorStore.add(List.of(document));

最后,检索与查询相似的文档:

List<Document> results = vectorStore.similaritySearch(SearchRequest.query("Spring").withTopK(5));

如果一切顺利,你应该可以检索到包含文本“Spring AI rocks!!”的文档。