詹学伟
詹学伟
Published on 2025-06-08 / 23 Visits
0
0

AI智能体流式对话

一、添加依赖

        <!--流式输出-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>dev.langchain4j</groupId>
            <artifactId>langchain4j-reactor</artifactId>
        </dependency>

二、配置流式输出模型

langchain4j:
  # 阿里白练通义千问
  community:
    dashscope:
      streaming-chat-model:
        api-key: sk-09c7b571687b46d5a2e25a03fbddxxxx
        model-name: qwen-plus

三、修改XueweiAgent

package com.zhan.chat.assistant;

import dev.langchain4j.service.MemoryId;
import dev.langchain4j.service.SystemMessage;
import dev.langchain4j.service.UserMessage;
import dev.langchain4j.service.spring.AiService;
import dev.langchain4j.service.spring.AiServiceWiringMode;
import reactor.core.publisher.Flux;

/**
 * @author zhanxuewei
 */
@AiService(
        wiringMode = AiServiceWiringMode.EXPLICIT,
        // chatModel = "qwenChatModel",
        streamingChatModel = "qwenStreamingChatModel",
        chatMemoryProvider = "chatMemoryProviderXuewei",
        tools = "appointmentTools",
        contentRetriever = "contentRetrieverXuewei"
        //contentRetriever = "contentRetrieverPincone"
)
public interface XueweiAgent {
    @SystemMessage(fromResource = "xuewei-prompt-template.txt")
    Flux<String> chat(@MemoryId Long memoryId, @UserMessage String userMessage);
}

四、修改XueweiController

package com.zhan.chat.controller;

import com.zhan.chat.assistant.XueweiAgent;
import com.zhan.chat.bean.ChatForm;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@Tag(name = "智能医生助手")
@RestController
@RequestMapping("/doctor/assistant")
public class XueweiController {

    @Autowired
    private XueweiAgent xueweiAgent;

    @Operation(summary = "对话")
    @PostMapping(value = "/chat",produces = "text/stream;charset=utf-8")
    public Flux<String> chat(@RequestBody ChatForm chatForm) {
        return xueweiAgent.chat(chatForm.getMemoryId(), chatForm.getMessage());
    }
}

五、测试


Comment