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 小时前
Python实战:PyQt5 + OpenCV + Dlib 打造AI桌面应用
人工智能·python·qt
风娜2 小时前
IT 疑难杂症诊疗室:从现象到根因的系统排查方法论
人工智能·github
碧海银沙音频科技研究院2 小时前
炬芯ATS3089X手表的HFP通话不自动路由到设备BT的问题解决
网络·人工智能·嵌入式硬件·算法
承渊政道2 小时前
【从零开始大模型开发与微调:基于PyTorch与ChatGLM】(开源大模型ChatGLM高级定制化应用实战)
人工智能·pytorch·python·llm·模型微调·glm
howdoyoudo2026062 小时前
AI审计手记 #28:从“暂停训练”到“切断联网”——AI安全控制手段的极限升级
大数据·人工智能·安全·ai·语言模型
colman wang3 小时前
人工智能课程(配套AIMA(Artificial Intelligence: A Modern Approach))
人工智能
LinXunFeng7 小时前
Draw Things 换装 LoRA 实战:用 Silo 秒装,同一提示词对比出图
人工智能·开源·aigc
xiaohaiAIgeo7 小时前
【2026年】通风柜自动门安全吗?联锁逻辑与防夹设计
人工智能·安全·科普知识
像风一样自由20207 小时前
52.Docker部署大模型应用从本地开发到服务化运行
运维·人工智能·docker·容器·大模型·智能体