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

相关推荐
Yao.Li10 分钟前
PVN3D ORT CUDA Custom Ops 实现与联调记录
人工智能·3d·具身智能
诺伦13 分钟前
LocalClaw 在智能制造的新机会:6部门AI+电商政策下的工厂AI升级方案
人工智能·制造
小陈工2 小时前
Python Web开发入门(十七):Vue.js与Python后端集成——让前后端真正“握手言和“
开发语言·前端·javascript·数据库·vue.js·人工智能·python
大鸡腿同学6 小时前
【成长类】《只有偏执狂才能生存》读书笔记:程序员的偏执型成长地图
后端
0xDevNull6 小时前
MySQL数据冷热分离详解
后端·mysql
墨染天姬6 小时前
【AI】端侧AIBOX可以部署哪些智能体
人工智能
AI成长日志6 小时前
【Agentic RL】1.1 什么是Agentic RL:从传统RL到智能体学习
人工智能·学习·算法
AI袋鼠帝6 小时前
OpenClaw(龙虾)最强开源对手!Github 40K Star了,又一个爆火的Agent..
后端
KevinCyao7 小时前
java视频短信接口怎么调用?SpringBoot集成视频短信及回调处理Demo
java·spring boot·音视频
2501_948114247 小时前
2026年大模型API聚合平台技术评测:企业级接入层的治理演进与星链4SAPI架构观察
大数据·人工智能·gpt·架构·claude