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

相关推荐
YongGit44 分钟前
OpenClaw 本地 AI 助手完全指南:飞书接入 + 远程部署实战
人工智能
想用offer打牌2 小时前
高并发下如何保证接口的幂等性
后端·面试·状态机
程序员鱼皮2 小时前
斯坦福大学竟然开了个 AI 编程课?!我已经学上了
人工智能·ai编程
爱勇宝3 小时前
2026一人公司生存指南:用AI大模型,90天跑出你的第一条现金流
前端·后端·架构
golang学习记3 小时前
Go 并发编程:原子操作(Atomics)完全指南
后端
星浩AI3 小时前
Skill 的核心要素与渐进式加载架构——如何设计一个生产可用的 Skill?
人工智能·agent
树獭非懒3 小时前
告别繁琐多端开发:DivKit 带你玩转 Server-Driven UI!
android·前端·人工智能
阿尔的代码屋3 小时前
[大模型实战 07] 基于 LlamaIndex ReAct 框架手搓全自动博客监控 Agent
人工智能·python
小小小怪兽3 小时前
🔨聊一聊Skills
人工智能·agent
穿过生命散发芬芳3 小时前
OpenClaw:开启OpenCloudOS 操作系统智能运维初体验
人工智能·aigc