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();
    }
}
相关推荐
TMT星球3 分钟前
度小满与香港科技大学联合发布轩辕4.5金融大模型
人工智能·金融
GJGCY4 分钟前
金融AI Agent安全如何评估?数据出站、身份映射、参数绑定与审计追踪机制
人工智能·经验分享·安全·ai·金融·自动化·智能体
甲维斯7 分钟前
Codex,Zcode,KimiCode电脑操作能力对比!
人工智能·游戏开发
ckjr00711 分钟前
手机里的AI功能越来越多,如何让它真正提高日常使用效率?
人工智能·智能手机
美狐美颜SDK开放平台12 分钟前
开发直播APP、电商短视频与视频社交平台时,能否共用一套美颜SDK?
android·图像处理·人工智能·音视频·直播美颜sdk
youzuos13 分钟前
大模型面试别背术语:用一次故障讲清工程能力:工程拆解与验证
人工智能
小蒜学长13 分钟前
基于SpringBoot+Vue的云南地区扶贫助农系统的设计与实现(代码+数据库+LW)
java·vue.js·spring boot·后端·乡村振兴
财迅通Ai19 分钟前
MLCC业务爆发式增长,带动昀冢科技三季度扭亏为盈
大数据·人工智能·科技·昀冢科技
RuoyiOffice20 分钟前
Hyper-V+Kubernetes 搭建实战:保留 Windows,构建三节点 Linux 容器集群
linux·spring boot·kubernetes·containerd·hyper-v·微服务部署·ruoyi office
AI日报派送佬20 分钟前
2026年11月02日 AI 行业日报|商汤开源 Loop-9B、Pika 发布 H3 Flex 音视频模型、Meta× 傅利叶推出人形机器人 Max
人工智能·微软·开源·音视频·openai·1024程序员节·ai日报