当前位置:   article > 正文

Spring Boot集成AI问答助手(极简无需显卡)

Spring Boot集成AI问答助手(极简无需显卡)

最近在做毕业设计,是SpringBoot+Vue的论坛系统,老师说最好加上一点创新点,思考了一下决定加一个RAG构建的智能问答助手。这里我们使用LLM+Langchain的方式对大模型进行微调,采用通义千问的API来充当LLM的部分,无需显卡,非常适合配置一般的电脑,那么如何最简单的在SpringBoot里集成呢?

首先下载Langchain-chatchat的整合包

为了减少时间成本,这里直接参照B站大神分享的整合包:
无需显卡Langchain-chatchat接入通义千问大模型
按照视频里的操作下载完成之后申请通义千问API的key并配置完毕就可以进行下一步了

在SpringBoot里集成

首先启动整合包
启动完成之后看见如下界面,红框部分是整合包的API文档
在这里插入图片描述
进入API文档地址:
在这里插入图片描述
找到这个与知识库对话的API,接下来将他封装进SpringBoot,这里我们使用Spring WebClient构造请求体并访问。
首先在pom文件里添加WebClient相关:

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
  • 1
  • 2
  • 3
  • 4

Hutool工具类:

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.16</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

然后在SpringBoot里新建一个工具类:

@Component
@Slf4j
public class LangChainSDK {

    private final WebClient webClient;

    public LangChainSDK() {
        // 初始化 WebClient 配置
        this.webClient = WebClient.builder()
                .baseUrl("http://127.0.0.1:7861") // 设置基础URL
                .build();
    }

    public Mono<String> chatWithAI(String content) {
        try {
            Map<String, Object> body = new HashMap<>();
            body.put("query", content); //问题内容
            body.put("knowledge_base_name", "yourModelName"); //知识库名字
            body.put("top_k", 3);
            body.put("score_threshold", 2); //0-2 模型匹配值
            body.put("stream", false);
            body.put("model_name", "qwen-api"); //模型名字
            body.put("temperature", 0.7); //默认
            body.put("max_tokens", 0);
            body.put("prompt_name", "default");

            return webClient.post()
                    .uri("/chat/knowledge_base_chat")
                    .contentType(MediaType.APPLICATION_JSON)
                    .bodyValue(body)
                    .retrieve()
                    .bodyToMono(String.class)
                    .map(response -> {
                        //现在返回值的类型是String类型的JSON----data:{answer:"xxx"}
                        String jsonStr = response.replace("data:", "");
                        //转成真JSON
                        JSONObject jsonObject = JSONUtil.parseObj(jsonStr);
                        //只要answer
                        return jsonObject.getStr("answer");
                    })
                    .doOnError(error -> {
                        log.error("LangChain Chat Error: {}", error.getMessage());
                    });
        } catch (Exception e) {
            log.error("Exception occurred: {}", e.getMessage());
            throw new ServiceException(ResponseEnum.LANG_CHAIN_CHAT_ERROR);
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

body.put("knowledge_base_name", "yourModelName"); //知识库名字这里面的知识库名字替换成你自己的
throw new ServiceException(ResponseEnum.LANG_CHAIN_CHAT_ERROR);这里是我自己封装的全局异常类,可以替换成你们自己的,或者直接throw一个RunTimeException

调用

接下来就可以调用了,我在Service层调用如下:

@Service
public class AIServiceImpl implements AIService {

    @Resource
    private LangChainSDK langChainSDK;


    @Override
    public String chatWithAI(String content) {
        return langChainSDK.chatWithAI(content).block();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

content是你的想问的问题

在controller里调用:

@RestController
@RequestMapping("/ai")
@Api(tags = "AI助手模块")
public class AIController {

    @Resource
    private AIService aiService;

    @PostMapping("/chat")
    @ApiOperation(value = "和本地知识库对话")
    public R<?> chatWithAI(@RequestParam String content) {
        return new R<>().success(ResponseEnum.SUCCESS, aiService.chatWithAI(content));
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

运行结果

ApiFox调试结果:
在这里插入图片描述
成功整合

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/838003
推荐阅读
相关标签
  

闽ICP备14008679号