20250720 es向量检索(引入依赖的版本等问题调试)
This commit is contained in:
parent
c17113c9be
commit
276cccf506
34
pom.xml
34
pom.xml
@ -7,7 +7,7 @@
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>3.3.6</version>
|
||||
<version>3.2.5</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
@ -19,6 +19,8 @@
|
||||
|
||||
<properties>
|
||||
<java.version>17</java.version>
|
||||
<!-- 需要用8.15.0版本,因为旧的版本KnnSearch.k方法的入参是long,但是新版改成int了 -->
|
||||
<elasticsearch.version>8.15.0</elasticsearch.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
@ -44,28 +46,37 @@
|
||||
<artifactId>spring-ai-starter-model-deepseek</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>org.springframework.ai</groupId>-->
|
||||
<!-- <artifactId>spring-ai-starter-model-zhipuai</artifactId>-->
|
||||
<!-- </dependency>-->
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.ai</groupId>
|
||||
<artifactId>spring-ai-starter-model-zhipuai</artifactId>
|
||||
<groupId>com.alibaba.cloud.ai</groupId>
|
||||
<artifactId>spring-ai-alibaba-starter-dashscope</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Elasticsearch Vector Store -->
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>org.springframework.ai</groupId>-->
|
||||
<!-- <artifactId>spring-ai-starter-vector-store-elasticsearch</artifactId>-->
|
||||
<!-- </dependency>-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.ai</groupId>
|
||||
<artifactId>spring-ai-starter-vector-store-elasticsearch</artifactId>
|
||||
<artifactId>spring-ai-elasticsearch-store</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 对于Spring Boot 3.3.6 + Spring AI 1.0.0,显式指定Elasticsearch版本 -->
|
||||
<dependency>
|
||||
<groupId>co.elastic.clients</groupId>
|
||||
<artifactId>elasticsearch-java</artifactId>
|
||||
<version>8.13.4</version>
|
||||
<version>${elasticsearch.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.elasticsearch.client</groupId>
|
||||
<artifactId>elasticsearch-rest-client</artifactId>
|
||||
<version>8.13.4</version>
|
||||
<version>${elasticsearch.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Jackson Core 和 Databind for JSON processing -->
|
||||
@ -125,6 +136,17 @@
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>co.elastic.clients</groupId>
|
||||
<artifactId>elasticsearch-java</artifactId>
|
||||
<version>${elasticsearch.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.elasticsearch.client</groupId>
|
||||
<artifactId>elasticsearch-rest-client</artifactId>
|
||||
<version>${elasticsearch.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
|
@ -0,0 +1,72 @@
|
||||
package com.example.springaidemo.config;
|
||||
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.message.BasicHeader;
|
||||
import org.elasticsearch.client.RestClient;
|
||||
import org.elasticsearch.client.RestClientBuilder;
|
||||
import org.springframework.ai.embedding.EmbeddingModel;
|
||||
import org.springframework.ai.vectorstore.VectorStore;
|
||||
import org.springframework.ai.vectorstore.elasticsearch.ElasticsearchVectorStore;
|
||||
import org.springframework.ai.vectorstore.elasticsearch.ElasticsearchVectorStoreOptions;
|
||||
import org.springframework.ai.vectorstore.elasticsearch.SimilarityFunction;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
|
||||
@Configuration
|
||||
public class ElasticVectorConfig {
|
||||
|
||||
@Value("${elasticsearch.host:154.12.80.119}")
|
||||
private String host;
|
||||
|
||||
@Value("${elasticsearch.port:9200}")
|
||||
private int port;
|
||||
|
||||
@Value("${elasticsearch.username:elastic}")
|
||||
private String username;
|
||||
|
||||
@Value("${elasticsearch.password:123456}")
|
||||
private String password;
|
||||
|
||||
@Value("${elasticsearch.index.name:custom-index}")
|
||||
private String indexName;
|
||||
|
||||
@Value("${elasticsearch.index.dimensions:1536}")
|
||||
private int dimensions;
|
||||
|
||||
@Bean
|
||||
public RestClient restClient() {
|
||||
RestClientBuilder builder = RestClient.builder(new HttpHost(host, port, null));
|
||||
|
||||
// 如果提供了用户名和密码,添加基础认证
|
||||
if (StringUtils.hasText(username) && StringUtils.hasText(password)) {
|
||||
String auth = username + ":" + password;
|
||||
String encodedAuth = Base64.getEncoder()
|
||||
.encodeToString(auth.getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
builder.setDefaultHeaders(new Header[]{
|
||||
new BasicHeader("Authorization", "Basic " + encodedAuth)
|
||||
});
|
||||
}
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public VectorStore vectorStore(RestClient restClient, EmbeddingModel dashscopeEmbeddingModel) {
|
||||
ElasticsearchVectorStoreOptions options = new ElasticsearchVectorStoreOptions();
|
||||
options.setIndexName(indexName);
|
||||
options.setSimilarity(SimilarityFunction.cosine);
|
||||
options.setDimensions(dimensions);
|
||||
|
||||
return ElasticsearchVectorStore.builder(restClient, dashscopeEmbeddingModel)
|
||||
.options(options)
|
||||
.initializeSchema(true)
|
||||
.build();
|
||||
}
|
||||
}
|
@ -3,7 +3,6 @@ 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.elasticsearch.ElasticsearchVectorStore;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@ -20,10 +19,14 @@ public class VectorService {
|
||||
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
|
||||
// Add the documents to Elasticsearch 需要注意的是elasticSearch的版本需要大于8.10.0
|
||||
vectorStore.add(documents);
|
||||
|
||||
// Retrieve documents similar to a query
|
||||
List<Document> results = this.vectorStore.similaritySearch(SearchRequest.builder().query("Spring").topK(5).build());
|
||||
SearchRequest searchRequest = SearchRequest.builder().query("Spring")
|
||||
.topK(5)
|
||||
.build();
|
||||
List<Document> results = this.vectorStore.similaritySearch(searchRequest);
|
||||
System.out.println(results);
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,11 @@ spring:
|
||||
ai:
|
||||
deepseek:
|
||||
api-key: sk-3043bb4777404970a22c7544dd30aaa2
|
||||
dashscope:
|
||||
api-key: sk-2f703a41fff0488e9b6888013d2ee58a
|
||||
chat:
|
||||
options:
|
||||
model: qwen-plus
|
||||
zhipuai:
|
||||
api-key: 73f440ddeafc47ba94ed66e35fbd63d7.VmlulRZ4BMWexncF
|
||||
openai:
|
||||
@ -29,6 +34,9 @@ spring:
|
||||
index-name: custom-index
|
||||
dimensions: 1536
|
||||
similarity: cosine
|
||||
uris: http://154.12.80.119:9200
|
||||
username: elastic
|
||||
password: 123456
|
||||
# ollama:
|
||||
# base-url: ${OLLAMA_BASE_URL:http://localhost:11434}
|
||||
# chat:
|
||||
|
Loading…
Reference in New Issue
Block a user