spring-ai简单示例


spring ai主要用于提供一致的编程模型,屏蔽不同 AI 服务提供商(如 OpenAI、Azure AI、Hugging Face 等)的底层差异。

一、不使用spring-ai时直接使用RestClient调用接口

controller:

package com.yogi.controller;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestClient;

import java.util.List;


@RestController
@RequestMapping("/ai")
public class AITestController {

    @GetMapping("/debug-response")
    @Observed
    public String debugResponse() {
        try {
            RestClient restClient = RestClient.create();

            String response = restClient.post()
                    .uri("https://maas.xxxxx.com/v1/chat/completions")
                    .header("Authorization", "Bearer sk-xxxxxxxxxx")
                    .contentType(MediaType.APPLICATION_JSON)
                    .body("{\"model\": \"DeepSeek-V3.1\", \"messages\": [{\"role\": \"user\", \"content\": \"Hello\"}]}")
                    .retrieve()
                    .body(String.class);

            return "API响应: " + response;
        } catch (Exception e) {
            return "错误: " + e.getMessage();
        }
    }
}

二、使用spring ai

1、新增maven依赖

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
    <version>0.8.1</version>
</dependency>

注意我这里用的Spring AI 0.8.1 需要java17及以上版本 spring boot3.2.0及以上版本
这里我用的3.2.6,完整的pom文件如下:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.yogi</groupId>
    <artifactId>SpringAITest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>SpringAITest</name>
    <description>SpringAITest</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>17</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
            <version>0.8.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>


2、application.properties设置

注意这里的baseUrl和直接调用相比少了"/v1/chat/completions",因为spring ai会自动拼接。

spring.ai.openai.api-key=sk-xxxxxxxxxx
spring.ai.openai.base-url=https://maas.xxxxxxx.com
spring.ai.openai.chat.options.model=DeepSeek-V3.1

3、controller

package com.yogi.controller;

import org.springframework.ai.chat.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/ai")
public class AITestController {

    private final ChatClient chatClient;
    
    // 直接注入统一的 ChatClient
    @Autowired
    public AITestController(ChatClient chatClient) {
        this.chatClient = chatClient;
    }

    @GetMapping("/ask")
    public String ask(@RequestParam String question) {
        // 调用 call 方法,直接获取字符串回复
        return chatClient.call(question);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值