ARTICLE DETAIL

资讯详情

深耕网站视觉设计与运营推广的一线实战洞察。

spring boot4集成spring AI 2

spring boot4集成spring AI 2 1、maven依赖dependencygroupIdorg.springframework.ai/groupIdartifactIdspring-ai-starter-model-openai/artifactIdversion2.0.1/versionscopecompile/scope/dependency2、yml配置文件spring: ai: openai: base-url: https://api.deepseek.com api-key: xxxxx chat: temperature:0.3model: deepseek-v4-flash3、新建AiConfig配置类Configuration public class AiConfig{Bean public ChatClient chatClient(ChatClient.Builder builder){returnbuilder .defaultSystem(你是一个物联网系统助手负责把自然语言转成结构化查询参数不直连数据库。).build();}}4、测试运行代码package com.example.web_service.tj.base_data.controller;importjakarta.annotation.Resource;importorg.springframework.ai.chat.client.ChatClient;importorg.springframework.context.annotation.Lazy;importorg.springframework.http.MediaType;importorg.springframework.web.bind.annotation.*;importreactor.core.publisher.Flux;importjava.util.Map;RestController RequestMapping(/aitest)public class TestController{Resource Lazy private ChatClient chatClient;PostMapping(/ask)public String ask(RequestBody MapString,Stringparams){returnchatClient.prompt().user(params.get(text)).call().content();}GetMapping(value/stream, producesMediaType.TEXT_EVENT_STREAM_VALUE)public FluxStringstream(RequestParam(text)String text){returnchatClient.prompt().user(text).stream().content();}}5、如果要打通业务则做如下修改1定义业务类和实现类public interface IProjectTagQueryService{//获取查询结果 MapString, BigDecimalgetProjectTagsDatas(ListStringparams);}package com.example.web_service.tj.ai.service.impl;importcom.example.web_service.system.service.IIotDbService;importcom.example.web_service.system.service.ISystemTenantService;importcom.example.web_service.tj.ai.dto.ProjectTagsQueryDTO;importcom.example.web_service.tj.ai.service.IProjectTagQueryService;importcom.example.web_service.tj.project_config.entity.PcProjectVariableEntity;importcom.example.web_service.tj.project_config.service.IPcProjectVariableService;importcom.example.web_service.utils.AuthUserUtils;importcom.example.web_service.utils.BusinessException;importjakarta.annotation.Resource;importlombok.extern.slf4j.Slf4j;importorg.springframework.ai.tool.annotation.Tool;importorg.springframework.ai.tool.annotation.ToolParam;importorg.springframework.context.annotation.Lazy;importorg.springframework.stereotype.Service;importorg.springframework.util.CollectionUtils;importjava.math.BigDecimal;importjava.util.List;importjava.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 MapString, BigDecimalgetProjectTagsDatas(ToolParam(description点位/变量列表如[var1,var2,var3])ListStringparams){//下面是你的具体业务 if(CollectionUtils.isEmpty(params)){throw new BusinessException(请告知我需要查询的点位);}ListStringtagCodesparams.stream().map(String::trim).toList();ListPcProjectVariableEntitylistprojectVariableService.getBaseProjectVariableListByTagCodes(tagCodes);if(tagCodes.size()!list.size()){throw new BusinessException(点位编码存在不正确的);}StringtenantCodesystemTenantService.getTenantCodeByCurrentUser();returniotDbService.getTagLastValueByCodes(tenantCode,tagCodes);}}重点是ToolParam和Tool(description “查询1个或多个点位/变量的当前数据”)2AiConfig配置类做如下修改Configuration public class AiConfig{Bean public ChatClient chatClient(ChatClient.Builder builder, IProjectTagQueryService projectTagQueryService){returnbuilder .defaultSystem( 你是一个物联网数据查询助手支持以下能力 ---## 能力1查询1个或者多个点位/变量的当前值触发词点位、变量、当前值 参数 - tagCodes: 点位列表ListString如[forward,reverse,start]---).defaultTools(projectTagQueryService)//带入业务接口/类(对象).build();}}
返回列表