spring boot4集成spring AI 2

发布时间:2026/11/5 18:12:40
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();}}

关于本文作者

来自尧图内容编辑团队

尧图内容编辑团队 内容团队

尧图内容编辑团队

本文由尧图网络内容编辑团队执笔。团队由资深项目经理、前端工程师与设计师组成,所有内容均来自亲手交付的真实项目,先讲清问题、再给出可落地的解法。尧图深耕北京网站建设十年,服务过京华建材集团、智造科技等各行业客户,把一线经验沉淀为可复用的行业观察。

  • 十年建站经验,覆盖建材、制造、服务、文创等
  • 项目经理把关选题与事实准确性
  • 工程师与设计师联合撰写专业细节
  • 统一编辑规范,保证文风与排版一致
  • 每月复盘转化数据,迭代选题方向

延伸阅读

相关资讯与近期热门内容

深度阅读推荐

建站决策前值得细读的三篇

网站改版的5个关键决策
2024-08-12

网站改版的5个关键决策

什么时候该改版、改到什么程度、如何避免流量掉光,京华建材集团改版复盘给出答案。

获取专属建站方案

看完文章,把您的行业与预算告诉我们,免费获取一份量身定制的官网建设方案与报价。

立即免费咨询