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();
}
}