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

spring boot 整合AI教程

1、pom.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>springbootai</groupId><artifactId>springbootai</artifactId><version>1.0-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.5.0</version></parent><dependencyManagement><dependencies><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-bom</artifactId><version>1.0.0</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><!-- Spring Boot Web Starter --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Spring AI OpenAI Starter --><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-starter-model-openai</artifactId></dependency><dependency><groupId>org.apache.httpcomponents.client5</groupId><artifactId>httpclient5</artifactId></dependency></dependencies></project>

2、        application.yml配置

spring:http:client:factory: http_componentsai:openai:api-key: #自己创建的密钥base-url: https://api.siliconflow.cnchat:options:model: deepseek-ai/DeepSeek-R1-Distill-Qwen-7B
logging:level:root: debug

3、启动类编写

package com.spring.ai;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication(scanBasePackages = {"com.spring.controller"})
public class SpringAiApplication {public static void main(String[] args) {SpringApplication.run(SpringAiApplication.class, args);}}

4、controller 编写

package com.spring.controller;import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.*;
@RestController
@CrossOrigin(origins = "*")public class SpringAiController {private ChatClient chatClient;public SpringAiController(ChatClient.Builder chatClientBuilder) {this.chatClient = chatClientBuilder.build();}@PostMapping ("/ai")public String test(String input) {return chatClient.prompt().user(input).call().content();}}

5、客户端访问html编写。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){$("button").click(function(){var aiinpputidvalue=$("#aiinpputid").val()$("#showwait").show()	$("#showtext").hide()$.post("http://localhost:8080/ai",{input:aiinpputidvalue,},function(data,status){$("#showwait").hide()	$("#showtext").html(data)$("#showtext").show()//alert("数据: \n" + data + "\n状态: " + status);});});});</script>
</head>
<body>
<input id="aiinpputid"><button>搜索</button>
<div id="showwait"style="display:none" >正在查询请等待</div>
<div id="showtext"></div>
</body>
</html>

6、测试方法直接打开第五步html文件,

(1)文本框输入自己想要练习的对话

 (2)点击搜索即可

http://www.xdnf.cn/news/1430929.html

相关文章:

  • 解析ELK(filebeat+logstash+elasticsearch+kibana)日志系统原理以及k8s集群日志采集过程
  • Unity学习----【数据持久化】二进制数据(五)--由Excel自动生成数据结构类与二进制文件
  • 【常见的几款棋牌室计时软件】佳易王棋牌室计时计费软件大众版,佳易王棋牌室计时计费高级版,两款软件有何不同,适配不同场景需求#软件具体教程详解
  • react+taro的使用整理
  • 将 .vcproj 文件转换为 .pro 文件
  • 企业DevOps的安全与合规关键:三大主流DevOps平台能力对比
  • 认识⼯作区、暂存区、版本库
  • Wireshark笔记-DHCP两步交互流程与数据解析
  • 简单爬一个小说页面 HTML 的title和内容
  • 基于STM32单片机智能家居wifi远程监控系统机智云app设计
  • Zookeeper分布式锁原理
  • 域名备案成功后怎么还显示没有注册
  • 基于vue3和springboot框架集成websocket
  • springboot项目使用websocket功能,使用了nginx反向代理后连接失败问题解决
  • DASK shuffle任务图分析
  • ansible循环
  • 零依赖每月工作计划备忘录:高效管理你的每一天
  • TSMC-1987《Convergence Theory for Fuzzy c-Means: Counterexamples and Repairs》
  • 电动车动力电池自动点焊机|深圳比斯特自动化
  • 证明有理数集不是完备的度量空间
  • SpringBoot 整合 RabbitMQ 的完美实践
  • 【代码随想录day 22】 力扣 40.组合总和II
  • Elasticsearch 深分页限制与解决方案
  • 计算机Python毕业设计推荐:基于Django+Vue用户评论挖掘旅游系统
  • 深度学习——基于卷积神经网络实现食物图像分类之(保存最优模型)
  • 前缀和之距离和
  • 架构设计:AIGC 新规下 UGC 平台内容审核防火墙的构建
  • 【XR技术概念科普】什么是注视点渲染(Foveated Rendering)?为什么Vision Pro离不开它?
  • A股大盘数据-20250902分析
  • 深入浅出 RabbitMQ-消息可靠性投递