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();
    }
}
相关推荐
星栈独行20 分钟前
自定义 UI-UX-Pro-Max 的 CSV 知识库,把 AI 生成的后台拉回行业该有的样子
前端·人工智能·ui·ux
王志来1379447300820 分钟前
场景驱动选型:4U工控机箱如何匹配多元化工业需求
大数据·人工智能·python
vivo互联网技术22 分钟前
TinySR:面向真实世界图像超分辨率的轻量级扩散模型
人工智能·算法
wangchunyu11423 分钟前
OpenHands介绍和安装说明
人工智能
xcLeigh26 分钟前
提示词模板库:建立你的专属Prompt兵器库
人工智能·大模型·prompt·提示词
边缘计算社区31 分钟前
从 Byte 到 Token,重新认识网宿科技
人工智能·科技·边缘计算
Mr数据杨34 分钟前
楼加数据分析与挖掘项目挑战实战解析 多标签文本分类与报告型任务落地
人工智能·数据分析·kaggle竞赛
2601_9623815836 分钟前
[Python人工智能] 九.gensim词向量Word2Vec安装及《庆余年》中文短文本相似度计算
人工智能·python·tensorflow·word2vec·文本相似度
独码侠39 分钟前
第02篇·30 分钟跑通 Dify:Docker 一键部署,5 步上线首个 AI 应用
人工智能·docker·容器·dify