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

相关推荐
全靠bug跑4 分钟前
Spring Cloud OpenFeign 实战三部曲:快速集成 · 连接池优化 · 客户端抽取
java·spring boot·openfeign
北邮刘老师7 分钟前
【智能体互联协议解析】北邮ACPs协议和代码与智能体互联AIP标准的关系
人工智能·大模型·智能体·智能体互联网
亚马逊云开发者18 分钟前
使用Amazon Q Developer CLI快速构建市场分析智能体
人工智能
Coding茶水间23 分钟前
基于深度学习的非机动车头盔检测系统演示与介绍(YOLOv12/v11/v8/v5模型+Pyqt5界面+训练代码+数据集)
图像处理·人工智能·深度学习·yolo·目标检测·机器学习·计算机视觉
Rose sait33 分钟前
【环境配置】Linux配置虚拟环境pytorch
linux·人工智能·python
福客AI智能客服38 分钟前
从被动响应到主动赋能:家具行业客服机器人的革新路径
大数据·人工智能
北城以北88881 小时前
Spring定时任务与Spring MVC拦截器
spring boot·spring·mvc
司南OpenCompass1 小时前
衡量AI真实科研能力!司南科学智能评测上线
人工智能·多模态模型·大模型评测·司南评测
Victor3561 小时前
Netty(20)如何实现基于Netty的WebSocket服务器?
后端
缘不易1 小时前
Springboot 整合JustAuth实现gitee授权登录
spring boot·后端·gitee