(六) Spring AI 1.0版本 + 千问大模型+RAG
上篇文章我们大概讲了一下向量模型的知识,本篇文章,我们将会通过RAG实战的形式,来感受一下RAG。
项目准备
pom.xml
这里我们需要引入向量库和pdf相关的包
<dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-starter-vector-store-redis</artifactId></dependency><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-pdf-document-reader</artifactId></dependency>
application.yaml
spring:ai:vectorstore:redis:index-name: spring_ai_index ##向量库索引名称initialize-schema: true ##是否初始化向量索引结构prefix: "doc:" ##向量库前缀data:redis:host: 127.0.0.1 ##redis地址,我这里使用的Docker安装的Redis-stack的向量库
EmbeddingController类
@RestController
@RequestMapping(value = "/api/embedding")
public class EmbeddingController {@Autowiredprivate VectorStore vectorStore;@GetMapping("/vector/store")public List<Document> vectoreStore() {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")));vectorStore.add(documents);List<Document> results = this.vectorStore.similaritySearch(SearchRequest.builder().query("Spring").topK(5).build());return results;}@GetMapping("/vector/store/pdf")public List<Document> vectorStorePdf() throws MalformedURLException {org.springframework.core.io.Resource resource = new FileUrlResource("funcationCall.pdf");PdfDocumentReaderConfig pdfDocumentReaderConfig = PdfDocumentReaderConfig.builder().withPageExtractedTextFormatter(ExtractedTextFormatter.defaults()).withPagesPerDocument(1).build();PagePdfDocumentReader pagePdfDocumentReader = new PagePdfDocumentReader(resource, pdfDocumentReaderConfig);List<Document> documents = pagePdfDocumentReader.read();vectorStore.add(documents);List<Document> results = this.vectorStore.similaritySearch(SearchRequest.builder().query("FunctionTool").topK(5).build());return results;}}
向量库我是使用的redis-stack:7.2.0-v18。建议使用docker-desktop安装。
方法里面的查询方法是比较简单的用法,详细复杂的查询可以查看SpringAi的官网
https://docs.spring.io/spring-ai/reference/api/vectordbs/redis.html
以上就是本次分享的主要内容。