快速开始

10 阅读1分钟

第一步:环境准备

  • jdk21
  • gradle 8.7
  • idea

第二步:快速创建工程

访问:spring.io/projects/sp…

点击链接,调整初始工程界面,初始完成生成spring-ai-demo工程,自动包含spring ai相关依赖,天然带有openAi的依赖,如果不需要可以后面再配置文件中移除,比如接入deepseek

第三步:集成deepseek

plugins {
	id 'java'
	id 'org.springframework.boot' version '3.5.9'
	id 'io.spring.dependency-management' version '1.1.7'
}

group = 'spring.ai'
version = '0.0.1-SNAPSHOT'
description = 'Spring AI , getting started example, using Open AI'

java {
	toolchain {
		languageVersion = JavaLanguageVersion.of(21)
	}
}

repositories {
	mavenCentral()
	maven { url 'https://repo.spring.io/milestone' }
	maven { url 'https://repo.spring.io/snapshot' }
	maven {
		name = 'Central Portal Snapshots'
		url = 'https://central.sonatype.com/repository/maven-snapshots/'
	}
}

ext {
	set('springAiVersion', "1.1.2")
}

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-web'
	implementation 'org.springframework.ai:spring-ai-starter-model-deepseek'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
	testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

dependencyManagement {
	imports {
		mavenBom "org.springframework.ai:spring-ai-bom:${springAiVersion}"
	}
}

tasks.named('test') {
	useJUnitPlatform()
}

spring:
  application:
    name: spring-ai-demo
  ai:
    deepseek:
          api-key: {deepseek的key}
server:
  port: 9090


package spring.ai;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/hello")
public class ChatController01 {

    private final ChatClient chatClient;

    public ChatController01(ChatClient.Builder chatClientBuilder) {
        this.chatClient = chatClientBuilder.build();
    }

    @GetMapping("/ai")
    String generation(String userInput) {
        return this.chatClient.prompt()
                .user(userInput)
                .call()
                .content();
    }
}

第四步:启动验证

至此完成了Spring Ai 集成deepseek的工作