Pinecone
本部分将指导您设置 Pinecone VectorStore
以存储文档嵌入并执行相似性搜索。
This section walks you through setting up the Pinecone VectorStore
to store document embeddings and perform similarity searches.
Prerequisites
-
Pinecone Account: Before you start, sign up for a Pinecone account.
-
Pinecone Project: Once registered, create a new project, an index, and generate an API key. You’ll need these details for configuration.
-
OpenAI Account: Create an account at OpenAI Signup and generate the token at API Keys.
Configuration
为了设置 PineconeVectorStore
,请从你的 Pinecone 账户中收集以下详细信息:
To set up PineconeVectorStore
, gather the following details from your Pinecone account:
-
Pinecone API Key
-
Pinecone Environment
-
Pinecone Project ID
-
Pinecone Index Name
-
Pinecone Namespace
在 Pinecone UI 门户网站中可以看到这些信息。 This information is available to you in the Pinecone UI portal. |
在设置嵌入时,选择 1536
的矢量维度。这匹配了 OpenAI 模型 text-embedding-ada-002
的维数,我们将在本指南中使用此模型。
When setting up embeddings, select a vector dimension of 1536
. This matches the dimensionality of OpenAI’s model text-embedding-ada-002
, which we’ll be using for this guide.
此外,你需要提供你的 OpenAI API 秘钥。像这样将其设置为环境变量:
Additionally, you’ll need to provide your OpenAI API Key. Set it as an environment variable like so:
export SPRING_AI_OPENAI_API_KEY='Your_OpenAI_API_Key'
Repository
为了获取 Spring AI 构件,声明 Spring 快照储存库:
To acquire Spring AI artifacts, declare the Spring Snapshot repository:
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
Dependencies
将这些依赖项添加到你的项目中:
Add these dependencies to your project:
-
OpenAI: Required for calculating embeddings.
<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>
|
Refer to the Dependency Management section to add the Spring AI BOM to your build file. |
Sample Code
为了在你的应用程序中配置 Pinecone,你可以使用以下设置:
To configure Pinecone in your application, you can use the following setup:
@Bean
public PineconeVectorStoreConfig pineconeVectorStoreConfig() {
return PineconeVectorStoreConfig.builder()
.withApiKey(<PINECONE_API_KEY>)
.withEnvironment("gcp-starter")
.withProjectId("89309e6")
.withIndexName("spring-ai-test-index")
.withNamespace("") // the free tier doesn't support namespaces.
.build();
}
通过向你的项目中添加 Spring Boot OpenAI 启动器来与 OpenAI 的嵌入集成。这为你提供了嵌入客户端的实现:
Integrate with OpenAI’s embeddings by adding the Spring Boot OpenAI starter to your project. This provides you with an implementation of the Embeddings client:
@Bean
public VectorStore vectorStore(PineconeVectorStoreConfig config, EmbeddingClient embeddingClient) {
return new PineconeVectorStore(config, embeddingClient);
}
在你的主代码中,创建一些文档:
In your main code, create some documents:
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("The World is Big and Salvation Lurks Around the Corner"),
new Document("You walk forward facing the past and you turn back toward the future.", Map.of("meta2", "meta2")));
将这些文档添加到 Pinecone:
Add the documents to Pinecone:
vectorStore.add(List.of(document));
最后,检索与查询类似的文档:
And finally, retrieve documents similar to a query:
List<Document> results = vectorStore.similaritySearch(SearchRequest.query("Spring").withTopK(5));
如果一切都顺利,你应该检索包含文本 “Spring AI rocks!!” 的文档。
If all goes well, you should retrieve the document containing the text "Spring AI rocks!!".