langchain4j整合milvus实现RAG
依赖
<dependencyManagement>
<!--langchain包管理-->
<dependencies>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-bom</artifactId>
<version>1.0.0-beta3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--整合阿里百炼-->
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-community-bom</artifactId>
<version>1.0.0-beta3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-community-dashscope-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-spring-boot-starter</artifactId>
</dependency>
<!--流式输出-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-reactor</artifactId>
</dependency>
<!--整合milvus-->
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-milvus-spring-boot-starter</artifactId>
<version>1.0.0-beta3</version>
</dependency>
</dependencies>
配置文件
langchain4j:
community:
dashscope:
chat-model:
api-key: ${ALI_API_KEY}
model-name: qwen-max
embedding-model:
api-key: ${ALI_API_KEY}
model-name: text-embedding-v3
streaming-chat-model:
api-key: ${ALI_API_KEY}
model-name: qwen-max
milvus:
host: 127.0.0.1
port: 19530
dimension: 1024
auto-flush-on-insert: true
collection-name: default
初始化数据
@Autowired
private EmbeddingModel embeddingModel;
@Autowired
private EmbeddingStore embeddingStore;
@Test
void test(){
Document document = ClassPathDocumentLoader.loadDocument("test01.txt",new TextDocumentParser());
EmbeddingStoreIngestor
.builder()
.embeddingStore(store)
.embeddingModel(embeddingModel)
.build()
.ingest(document);
}
配置类
@Configuration
public class AIConfig {
@Autowired
private EmbeddingModel embeddingModel;
@Autowired
private MilvusEmbeddingStore store;
@Bean
public ContentRetriever contentRetriever(){
EmbeddingStoreContentRetriever retriever = EmbeddingStoreContentRetriever.builder()
.embeddingModel(embeddingModel)
.embeddingStore(store)
.maxResults(1)
.build();
return retriever;
}
}
AiService
@AiService(wiringMode = AiServiceWiringMode.EXPLICIT,
chatModel = "qwenChatModel",
streamingChatModel = "qwenStreamingChatModel",
contentRetriever = "contentRetriever")
public interface Assistant {
Flux<String> chat(String message);
}
controller
@RestController
@RequestMapping("chat")
public class ChatController {
@Autowired
private Assistant assistant;
@GetMapping(value = "stream/{message}",produces = "text/stream;charset=utf-8")
public Flux<String> stream(@PathVariable String message){
return assistant.chat(message);
}
}