20250719 引入ES作为向量数据库

This commit is contained in:
liangjinglin 2025-07-19 16:33:11 +08:00
parent 0ee73fd3de
commit c17113c9be
4 changed files with 93 additions and 13 deletions

58
pom.xml
View File

@ -7,19 +7,18 @@
<parent> <parent>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId> <artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.5</version> <version>3.3.6</version>
<relativePath/> <relativePath/>
</parent> </parent>
<groupId>com.example</groupId> <groupId>com.example</groupId>
<artifactId>spring-ai-demo</artifactId> <artifactId>spring-ai-demo</artifactId>
<version>1.0.0</version> <version>1.1.0</version>
<name>spring-ai-demo</name> <name>spring-ai-demo</name>
<description>Spring AI Demo Project</description> <description>Spring AI Demo Project</description>
<properties> <properties>
<java.version>17</java.version> <java.version>17</java.version>
<spring-ai.version>0.8.1</spring-ai.version>
</properties> </properties>
<dependencies> <dependencies>
@ -35,27 +34,53 @@
<artifactId>spring-boot-starter-thymeleaf</artifactId> <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency> </dependency>
<dependency> <!-- <dependency>-->
<groupId>org.springframework.ai</groupId> <!-- <groupId>org.springframework.ai</groupId>-->
<artifactId>spring-ai-starter-model-openai</artifactId> <!-- <artifactId>spring-ai-starter-model-openai</artifactId>-->
</dependency> <!-- </dependency>-->
<dependency> <dependency>
<groupId>org.springframework.ai</groupId> <groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-deepseek</artifactId> <artifactId>spring-ai-starter-model-deepseek</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-zhipuai</artifactId>
</dependency>
<!-- Elasticsearch Vector Store -->
<dependency> <dependency>
<groupId>org.springframework.ai</groupId> <groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-vector-store-elasticsearch</artifactId> <artifactId>spring-ai-starter-vector-store-elasticsearch</artifactId>
</dependency> </dependency>
<!-- 对于Spring Boot 3.3.6 + Spring AI 1.0.0显式指定Elasticsearch版本 -->
<dependency> <dependency>
<groupId>co.elastic.clients</groupId> <groupId>co.elastic.clients</groupId>
<artifactId>elasticsearch-java</artifactId> <artifactId>elasticsearch-java</artifactId>
<version>8.13.3</version> <version>8.13.4</version>
</dependency> </dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>8.13.4</version>
</dependency>
<!-- Jackson Core 和 Databind for JSON processing -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<!-- Spring Boot DevTools (开发工具) --> <!-- Spring Boot DevTools (开发工具) -->
<dependency> <dependency>
@ -93,6 +118,13 @@
<type>pom</type> <type>pom</type>
<scope>import</scope> <scope>import</scope>
</dependency> </dependency>
<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-bom</artifactId>
<version>1.0.0.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>
@ -116,6 +148,14 @@
</build> </build>
<repositories> <repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository> <repository>
<id>spring-snapshots</id> <id>spring-snapshots</id>
<name>Spring Snapshots</name> <name>Spring Snapshots</name>
@ -137,4 +177,4 @@
</repository> </repository>
</repositories> </repositories>
</project> </project>

View File

@ -0,0 +1,21 @@
package com.example.springaidemo.controller;
import com.example.springaidemo.service.VectorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/RagController")
public class RagController {
@Autowired
private VectorService vectorService;
@RequestMapping("/storeVector")
public String storeVector() {
vectorService.storeVector();
return "success";
}
}

View File

@ -1,12 +1,29 @@
package com.example.springaidemo.service; package com.example.springaidemo.service;
import org.springframework.ai.document.Document;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.VectorStore; import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.ai.vectorstore.elasticsearch.ElasticsearchVectorStore;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class VectorService { public class VectorService {
@Autowired @Autowired VectorStore vectorStore;
private VectorStore vectorStore;
public void storeVector(){
List<Document> documents = List.of(
new Document("Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!"),
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."));
// Add the documents to Elasticsearch
vectorStore.add(documents);
// Retrieve documents similar to a query
List<Document> results = this.vectorStore.similaritySearch(SearchRequest.builder().query("Spring").topK(5).build());
}
} }

View File

@ -8,6 +8,8 @@ spring:
ai: ai:
deepseek: deepseek:
api-key: sk-3043bb4777404970a22c7544dd30aaa2 api-key: sk-3043bb4777404970a22c7544dd30aaa2
zhipuai:
api-key: 73f440ddeafc47ba94ed66e35fbd63d7.VmlulRZ4BMWexncF
openai: openai:
api-key: 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 api-key: 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
# 如果您有代理服务,可以修改为代理地址 # 如果您有代理服务,可以修改为代理地址
@ -23,7 +25,7 @@ spring:
read-timeout: 60000 # 60秒读取超时 read-timeout: 60000 # 60秒读取超时
vectorstore: vectorstore:
elasticsearch: elasticsearch:
initialize-schema: true initialize-schema: false
index-name: custom-index index-name: custom-index
dimensions: 1536 dimensions: 1536
similarity: cosine similarity: cosine
@ -34,7 +36,7 @@ spring:
# model: ${OLLAMA_MODEL:llama2} # model: ${OLLAMA_MODEL:llama2}
server: server:
port: 8080 port: 8009
logging: logging:
level: level: