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)点击搜索即可

相关推荐
FPGA-ADDA3 分钟前
第四篇:嵌入式系统常用通信接口详解(I2C、SPI、UART、RS232/485、CAN、USB)
人工智能·单片机·嵌入式硬件·fpga开发·信息与通信
智算菩萨6 分钟前
【How Far Are We From AGI】7 AGI的七重奏——从实验室到现实世界的应用图景与文明展望
论文阅读·人工智能·ai·agi·感知
若水不如远方11 分钟前
分布式一致性(六):拥抱可用性 —— 最终一致性与 Gossip 协议
分布式·后端·算法
lianghanwu199914 分钟前
深入解析 Apache Kafka:从核心原理到实战进阶指南
后端
想不到一个好的ID15 分钟前
Claude Code 初学者必看指南
前端·后端
一招定胜负15 分钟前
从 TXT 到 CSV 再到 Flask 部署:语音转写 AI 总结全流程实战
人工智能
数字供应链安全产品选型25 分钟前
#AI原生安全,Gartner 点名之后:AIST 技术正在进入深水区
大数据·人工智能
liukuang11026 分钟前
阿里Q3财报:全栈AI驱动下的价值重构
人工智能·重构
landuochong20035 分钟前
claude增加自动化日历提醒功能,并同步到iphone日历
人工智能·iphone·claudecode
我爱娃哈哈38 分钟前
SpringBoot + Redis Stream + 消费组:替代 Kafka 轻量级消息队列,低延迟高吞吐
后端