spring boot4集成spring AI 2

1、maven依赖

bash 复制代码
<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-starter-model-openai</artifactId>
    <version>2.0.1</version>
    <scope>compile</scope>
</dependency>

2、yml配置文件

bash 复制代码
spring:
  ai:
    openai:
      base-url: https://api.deepseek.com
      api-key: xxxxx
      chat:
        temperature: 0.3
        model: deepseek-v4-flash

3、新建AiConfig配置类

bash 复制代码
@Configuration
public class AiConfig {
    @Bean
    public ChatClient chatClient(ChatClient.Builder builder) {
        return builder
                .defaultSystem("你是一个物联网系统助手,负责把自然语言转成结构化查询参数,不直连数据库。")
                .build();
    }
}

4、测试运行代码

bash 复制代码
package com.example.web_service.tj.base_data.controller;

import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.util.Map;

@RestController
@RequestMapping("/aitest")
public class TestController {
    @Resource
    @Lazy
    private ChatClient chatClient;

    @PostMapping("/ask")
    public String ask(@RequestBody Map<String,String> params) {
        return chatClient.prompt()
                .user(params.get("text"))
                .call()
                .content();
    }

    @GetMapping(value = "/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> stream(@RequestParam("text") String text) {
        return chatClient.prompt()
                .user(text)
                .stream()
                .content();
    }
}

5、如果要打通业务则做如下修改

1)定义业务类和实现类

bash 复制代码
public interface IProjectTagQueryService {
    //获取查询结果
    Map<String, BigDecimal> getProjectTagsDatas(List<String> params);
}
bash 复制代码
package com.example.web_service.tj.ai.service.impl;

import com.example.web_service.system.service.IIotDbService;
import com.example.web_service.system.service.ISystemTenantService;
import com.example.web_service.tj.ai.dto.ProjectTagsQueryDTO;
import com.example.web_service.tj.ai.service.IProjectTagQueryService;
import com.example.web_service.tj.project_config.entity.PcProjectVariableEntity;
import com.example.web_service.tj.project_config.service.IPcProjectVariableService;
import com.example.web_service.utils.AuthUserUtils;
import com.example.web_service.utils.BusinessException;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class ProjectTagQueryServiceImpl implements IProjectTagQueryService {

    @Resource
    @Lazy
    private IPcProjectVariableService projectVariableService;

    @Resource
    @Lazy
    private ISystemTenantService systemTenantService;

    @Resource
    @Lazy
    private IIotDbService iotDbService;

    @Override
    @Tool(description = "查询1个或多个点位/变量的当前数据")
    public Map<String, BigDecimal> getProjectTagsDatas(@ToolParam(description = "点位/变量列表,如[var1,var2,var3]") List<String> params) {
    	//下面是你的具体业务
        if(CollectionUtils.isEmpty(params)){
            throw new BusinessException("请告知我需要查询的点位");
        }
        List<String> tagCodes=params.stream().map(String::trim).toList();
        List<PcProjectVariableEntity> list=projectVariableService.getBaseProjectVariableListByTagCodes(tagCodes);
        if(tagCodes.size()!=list.size()){
            throw new BusinessException("点位编码存在不正确的");
        }
        String tenantCode=systemTenantService.getTenantCodeByCurrentUser();
        return iotDbService.getTagLastValueByCodes(tenantCode,tagCodes);
    }
}

重点是@ToolParam和@Tool(description = "查询1个或多个点位/变量的当前数据")

2)AiConfig配置类做如下修改

bash 复制代码
@Configuration
public class AiConfig {
    @Bean
    public ChatClient chatClient(ChatClient.Builder builder, IProjectTagQueryService projectTagQueryService) {
        return builder
                .defaultSystem("""
                        你是一个物联网数据查询助手,支持以下能力:
                        ---
                        ## 能力1:查询1个或者多个点位/变量的当前值
                        触发词:点位、变量、当前值
                        参数:
                        - tagCodes: 点位列表(List<String>),如 ["forward", "reverse", "start"]
                        ---
                        """)
                .defaultTools(projectTagQueryService)//带入业务接口/类(对象)
                .build();
    }
}
相关推荐
回眸&啤酒鸭1 天前
【回眸】Minicart 电商购物车核心功能落地指南
人工智能
一隅论数智1 天前
给AI一张“业务概念地图“:本体如何从哲学走向企业智能
大数据·人工智能·经验分享·笔记·学习·学习方法·政务
AI的探索之旅1 天前
97 个 OpenCV 实例(三十):双目立体,从标定到点云
人工智能·opencv·计算机视觉
AlbertZein1 天前
Step-5-Preview 上手实测:3D 游戏、金融分析、网页设计一次跑完
人工智能·aigc
LaughingZhu1 天前
Product Hunt 每日热榜 | 2026-09-19
人工智能·深度学习·神经网络·搜索引擎·百度
美狐美颜SDK开放平台1 天前
开发直播APP时如何接入视频美颜SDK?开发流程与注意事项
android·人工智能·计算机视觉·音视频·直播美颜sdk
wukangjupingbb1 天前
智能网联汽车安全能力框架
人工智能
龙亘川1 天前
明月照湾区,智启新赛道:从顶流文旅IP盛会看智慧文旅升级路径
人工智能·智慧城市·开源软件·数据可视化
飞猫的边缘AI1 天前
边缘AI应用:家用AI摄像头怎么做数据训练?
人工智能·边缘计算·ai算法·边缘ai