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_components
  ai:
    openai:
      api-key: #自己创建的密钥
      base-url: https://api.siliconflow.cn
      chat:
        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)点击搜索即可

相关推荐
watersink2 分钟前
MCP 协议与 Skill 开发架构培训文档
人工智能·架构
做萤石二次开发的哈哈4 分钟前
AI 陪护机器人硬件如何接入萤石ERTC 实现实时通话?
人工智能·音视频·实时音视频·萤石开放平台
Luhui Dev9 分钟前
Anthropic 的 Claude Code 翻车经验
人工智能·luhuidev
DataX_ruby8211 分钟前
2026年数据中台厂商市场份额分析
大数据·人工智能·数据治理·数据中台
Luchang-Li12 分钟前
GPU传输带宽等信息监控nvidia-smi
人工智能·gpu·监控·性能·带宽
冬奇Lab15 分钟前
Skill 平台的五个深坑:企业 AI 能力体系的质量治理
人工智能·agent
码农小白AI16 分钟前
生鲜农产品来料验收提质,IACheck AI 报告文档审核比对农残兽残合格证书
人工智能
禹亮科技17 分钟前
上海临港100㎡大型跨国会议室音视频集成方案(思科Webex+思必驰AI音频)
人工智能·音视频·思必驰吸顶麦·禹亮科技
海兰20 分钟前
【web应用】Excel 项目数据自动化分析系统(AI 驱动分析)详细设计与部署指南(附源代码)
前端·人工智能·自动化·excel