Pinecone
本節將引導您設定 Pinecone VectorStore 以儲存文件嵌入並執行相似性搜尋。
Pinecone 是一個流行的基於雲的向量資料庫,它允許您高效地儲存和搜尋向量。
先決條件
-
Pinecone 賬戶:開始之前,請註冊一個 Pinecone 賬戶。
-
Pinecone 專案:註冊後,生成一個 API 金鑰並建立一個索引。您將需要這些詳細資訊進行配置。
-
EmbeddingModel例項用於計算文件嵌入。有幾個選項可用。-
如果需要,需要 EmbeddingModel 的 API 金鑰來生成
PineconeVectorStore儲存的嵌入。
-
要設定 PineconeVectorStore,請從您的 Pinecone 賬戶中收集以下詳細資訊
-
Pinecone API 金鑰
-
Pinecone 索引名稱
-
Pinecone 名稱空間
|
這些資訊可在 Pinecone UI 門戶中獲取。Pinecone 免費版不支援名稱空間。 |
自動配置
|
Spring AI 自動配置、啟動模組的工件名稱發生了重大變化。請參閱 升級說明 以獲取更多資訊。 |
Spring AI 為 Pinecone 向量儲存提供了 Spring Boot 自動配置。要啟用它,請將以下依賴項新增到專案的 Maven pom.xml 檔案中
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-vector-store-pinecone</artifactId>
</dependency>
或新增到您的 Gradle build.gradle 構建檔案中。
dependencies {
implementation 'org.springframework.ai:spring-ai-starter-vector-store-pinecone'
}
| 請參閱 依賴項管理 部分,將 Spring AI BOM 新增到您的構建檔案中。 |
| 請參閱Artifact Repositories(工件倉庫)部分,將 Maven Central 和/或 Snapshot Repositories 新增到您的構建檔案中。 |
此外,您還需要一個配置的 `EmbeddingModel` bean。有關更多資訊,請參閱EmbeddingModel部分。
以下是所需 bean 的示例
@Bean
public EmbeddingModel embeddingModel() {
// Can be any other EmbeddingModel implementation.
return new OpenAiEmbeddingModel(new OpenAiApi(System.getenv("OPENAI_API_KEY")));
}
要連線到 Pinecone,您需要提供例項的訪問詳細資訊。一個簡單的配置可以透過 Spring Boot 的 application.properties 提供,
spring.ai.vectorstore.pinecone.apiKey=<your api key>
spring.ai.vectorstore.pinecone.index-name=<your index name>
# API key if needed, e.g. OpenAI
spring.ai.openai.api.key=<api-key>
請查看向量儲存的配置引數列表,以瞭解預設值和配置選項。
現在您可以在應用程式中自動注入 Pinecone 向量儲存並使用它
@Autowired VectorStore vectorStore;
// ...
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")));
// Add the documents
vectorStore.add(documents);
// Retrieve documents similar to a query
List<Document> results = this.vectorStore.similaritySearch(SearchRequest.builder().query("Spring").topK(5).build());
配置屬性
您可以在 Spring Boot 配置中使用以下屬性來自定義 Pinecone 向量儲存。
| 財產 | 描述 | 預設值 |
|---|---|---|
|
Pinecone API 金鑰 |
- |
|
Pinecone 索引名稱 |
- |
|
Pinecone 名稱空間 |
- |
|
用於儲存原始文字內容的 Pinecone 元資料欄位名稱。 |
|
|
用於儲存計算距離的 Pinecone 元資料欄位名稱。 |
|
|
20 秒。 |
元資料過濾
您可以利用通用的、可移植的 元資料過濾器 與 Pinecone 儲存。
例如,您可以使用文字表示式語言
vectorStore.similaritySearch(
SearchRequest.builder()
.query("The World")
.topK(TOP_K)
.similarityThreshold(SIMILARITY_THRESHOLD)
.filterExpression("author in ['john', 'jill'] && article_type == 'blog'").build());
或使用 `Filter.Expression` DSL 以程式設計方式
FilterExpressionBuilder b = new FilterExpressionBuilder();
vectorStore.similaritySearch(SearchRequest.builder()
.query("The World")
.topK(TOP_K)
.similarityThreshold(SIMILARITY_THRESHOLD)
.filterExpression(b.and(
b.in("author","john", "jill"),
b.eq("article_type", "blog")).build()).build());
| 這些過濾表示式將轉換為等效的 Pinecone 過濾器。 |
手動配置
如果您更喜歡手動配置 PineconeVectorStore,您可以使用 PineconeVectorStore#Builder 來實現。
將這些依賴項新增到您的專案中
-
OpenAI:計算嵌入所需。
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
</dependency>
-
Pinecone
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-pinecone-store</artifactId>
</dependency>
| 請參閱 依賴項管理 部分,將 Spring AI BOM 新增到您的構建檔案中。 |
示例程式碼
要在應用程式中配置 Pinecone,您可以使用以下設定
@Bean
public VectorStore pineconeVectorStore(EmbeddingModel embeddingModel) {
return PineconeVectorStore.builder(embeddingModel)
.apiKey(PINECONE_API_KEY)
.indexName(PINECONE_INDEX_NAME)
.namespace(PINECONE_NAMESPACE) // the free tier doesn't support namespaces.
.contentFieldName(CUSTOM_CONTENT_FIELD_NAME) // optional field to store the original content. Defaults to `document_content`
.build();
}
在您的主程式碼中,建立一些文件。
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
vectorStore.add(documents);
最後,檢索與查詢相似的文件
List<Document> results = vectorStore.similaritySearch(SearchRequest.query("Spring").topK(5).build());
如果一切順利,您應該會檢索到包含文字“Spring AI rocks!!”的文件。
訪問原生客戶端
Pinecone 向量儲存實現透過 getNativeClient() 方法提供對底層原生 Pinecone 客戶端 (PineconeConnection) 的訪問。
PineconeVectorStore vectorStore = context.getBean(PineconeVectorStore.class);
Optional<PineconeConnection> nativeClient = vectorStore.getNativeClient();
if (nativeClient.isPresent()) {
PineconeConnection client = nativeClient.get();
// Use the native client for Pinecone-specific operations
}
原生客戶端使您能夠訪問 Pinecone 特定的功能和操作,這些功能和操作可能不會透過 VectorStore 介面公開。