当前位置: 首页 > news >正文

Java AI开发入门02-创建项目

创建新项目

填写项目信息

这里OpenAi 与Ollama二者选一个就行,我这里选择OpenAi。

项目结构

依赖文件

如下是pom文件,因为这里没用到ollama所以把这部分引入注释掉

<?xml version="1.0" encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>4.0.5</version><relativePath/><!-- lookup parent from repository --></parent><groupId>com.ai</groupId><artifactId>spring-ai-demo</artifactId><version>0.0.1-SNAPSHOT</version><name>spring-ai-demo</name><description>spring-ai-demo</description><url/><licenses><license/></licenses><developers><developer/></developers><scm><connection/><developerConnection/><tag/><url/></scm><properties><java.version>17</java.version><spring-ai.version>2.0.0-M4</spring-ai.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-webmvc</artifactId></dependency><!-- <dependency>--><!-- <groupId>org.springframework.ai</groupId>--><!-- <artifactId>spring-ai-starter-model-ollama</artifactId>--><!-- </dependency>--><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-starter-model-openai</artifactId></dependency><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-webmvc-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-bom</artifactId><version>${spring-ai.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><executions><execution><id>default-compile</id><phase>compile</phase><goals><goal>compile</goal></goals><configuration><annotationProcessorPaths><path><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></path></annotationProcessorPaths></configuration></execution><execution><id>default-testCompile</id><phase>test-compile</phase><goals><goal>testCompile</goal></goals><configuration><annotationProcessorPaths><path><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></path></annotationProcessorPaths></configuration></execution></executions></plugin></plugins></build></project>

配置文件

server.port=8066 server.servlet.context-path=/spring-ai-demo spring.application.name=spring-ai-demo # OpenAI API Key(必填):你的 OpenAI API Key,可以从 OpenAI Platform 获取 spring.ai.openai.api-key=your-api-key-here # OpenAI API ?? URL spring.ai.openai.base-url=https://dashscope.aliyuncs.com/compatible-mode # 模型及版本 spring.ai.openai.chat.options.model=qwen-max spring.ai.openai.chat.options.temperature=0.8

配置客户端

packagecom.ai.config;importorg.springframework.ai.chat.client.ChatClient;importorg.springframework.ai.openai.OpenAiChatModel;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;@ConfigurationpublicclassCommonConfiguration{@BeanpublicChatClientchatClient(OpenAiChatModelmodel){returnChatClient.builder(model).defaultSystem("你的名字叫暴龙战士,请你以暴龙战士的身份和语气回答我的问题").build();}}

接口

packagecom.ai.controller;importlombok.RequiredArgsConstructor;importorg.springframework.ai.chat.client.ChatClient;importorg.springframework.web.bind.annotation.*;importjava.util.Map;@RestController@RequestMapping("/test")publicclassTestController{// 核心对象:ChatClient,Spring AI自动配置,直接注入即可privatefinalChatClientchatClient;// 构造注入ChatClient(也可用@Autowired)publicTestController(ChatClientchatClient){this.chatClient=chatClient;}/** * 基础AI对话接口 * @param message 用户提问,默认值:给我讲一个Spring相关的笑话 * @return 封装AI回答的Map */@RequestMapping("/chat")publicMap<String,String>chat(@RequestParam(defaultValue="给我讲一个Spring相关的笑话")Stringmessage){// 核心代码:一行实现AI调用,获取返回内容StringaiAnswer=chatClient.prompt().user(message)// 设置用户提问.call()// 同步调用,一次性返回结果.content();// 提取AI回答的文本内容returnMap.of("user_message",message,"ai_answer",aiAnswer);}}

接口调用测试


http://www.cnnetsun.cn/news/1945695.html

相关文章:

  • Makefile里的‘秘密武器’:巧用MAKECMDGOALS变量实现一键编译、测试、清理
  • 以国货为潮,赴时代之约
  • 在电脑上玩Switch游戏:Ryujinx模拟器完整指南与实用技巧
  • STM32F405RG电机项目HardFault排查记:从168MHz降到84MHz的实战避坑
  • 台积电2026年Q1财报亮眼:收入增长、毛利率提升,AI芯片成关键驱动力
  • 编译问题-make update-api-Api 小结
  • **RISC-V架构下的高效嵌入式开发:从指令集到裸机编程的实战解析**在当前国产化芯片浪潮中,*8RIS
  • 终极Windows更新修复指南:Reset Windows Update Tool一键解决方案
  • 小白程序员必收藏!超全面大模型学习路线,从入门到实战,轻松提升竞争力
  • FPGA新手避坑:用Verilog实现边沿检测时,为什么你的仿真波形总是不对?
  • 【软件质量与测试】实验四: 面向对象测试
  • 3步搭建Windows AirPlay 2接收器:让Windows电脑变身苹果投屏中心
  • 2025届毕业生推荐的十大AI辅助写作助手解析与推荐
  • 2026届最火的AI学术网站实测分析
  • 5步解锁游戏手柄的全部潜力:用AntiMicroX让任何PC游戏完美适配手柄
  • 如何让Axure RP说中文:3分钟完成界面汉化的完整指南
  • [Spark] 图解Job、Stage、Task的生成逻辑与实战推演
  • 【离线部署实战】—— 攻克PyInstaller依赖地狱的完整指南
  • CANoe Panel控件实战:从开关到仪表盘的12种控件配置详解(附避坑指南)
  • 如何用InceptionTime实现时间序列分类的突破性性能
  • Vivado物理优化实战:如何用phys_opt_design命令提升时序性能(附完整Tcl脚本)
  • 如何用Hourglass倒计时器彻底改变你的时间管理方式:终极Windows时间管理工具完整指南
  • 如何在PC上玩Switch游戏:Ryujinx模拟器终极入门指南
  • 异构计算时代的安防底座:基于 Docker 的 X86/ARM 与 GPU/NPU 混合部署架构解析
  • 用OpenCV+ROS搞定无人机视觉定位:手把手教你配置vision_to_mavros节点
  • 从PQ到WFQ:深入解析华为设备队列调度技术的演进与实战
  • 周红伟:雷军对得起营销大师吗?雷军回应被贴上“营销大师”标签:表面在夸我,实际在黑我,去年很长一段时间不愿直播
  • Cal.com 开源五年后转向闭源,只为保护客户数据安全!
  • 从二维照片到三维世界:MicMac摄影测量软件完全指南
  • 海思3516a OSD水印实战:从字体渲染到视频叠加全链路解析