使用Spring AI调用AI模型

简介

Spring AI是Spring推出的用于快速集成AI功能。该项目从著名的Python项目(如LangChain和LlamaIndex)中汲取灵感,经过多个版本迭代,逐渐集成了OpenAI、Google、Amazon、Baidu等大语言模型平台。 Spring AI支持将模型数据结构化输出、RAG(Retrieval-Augmented Generation)、支持对接所有主要的向量数据库(如:Apache Cassanra、Redis、Elasticsearch、PostgreSQL等),允许模型请求客户端工具和函数,支持以下模型类型:

  • Chat Completion
  • Embedding
  • Text to Image
  • Audio Transcription
  • Text to Speech
  • Moderation

部分例子

添加依赖

Spring AI 1.0 M6之后的版本已经可以直接从中央仓库下载了,添加如下BOM

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.ai</groupId>
                <artifactId>spring-ai-bom</artifactId>
                <version>1.0.0-M6</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <!-- openAI 服务 -->
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
        </dependency>

        <!-- 百度千帆 服务 -->
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-qianfan-spring-boot-starter</artifactId>
        </dependency>
    </dependencies>

对接Chat Model

@RestController
public class ChatController {
    private final OpenAiChatModel chatModel;

    @Autowired
    public ChatController(OpenAiChatModel chatModel) {
        this.chatModel = chatModel;
    }

    @GetMapping("/ai/generate")
    public Map generate(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        return Map.of("generation", this.chatModel.call(message));
    }

    @GetMapping("/ai/generateStream")
    public Flux<ChatResponse> generateStream(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        Prompt prompt = new Prompt(new UserMessage(message));
        return this.chatModel.stream(prompt);
    }

    
    @GetMapping("/ai/tool/calling")
    public Map toolCalling(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        //目前会循环调用工具方法, 应该跟deepseek自身有关 https://api-docs.deepseek.com/zh-cn/guides/function_calling
        String response = ChatClient.create(chatModel).prompt(message).tools(new IdiomsTools()).call().content();
        return Map.of("generation", response);
    }

}

application.properties需要配置对应api key 信息


spring.ai.openai.api-key=youkey
spring.ai.openai.base-url=https://api.deepseek.com
spring.ai.openai.chat.options.model=deepseek-chat

# 千帆
spring.ai.qianfan.api-key=you api key
spring.ai.qianfan.secret-key=you secret key

使用Advisors API增强AI交互

@RestController
@RequestMapping("/qianfan/advisor")
public class QianfanAdvisorController {

    private final QianFanChatModel chatModel;
    private final ChatClient chatClient;

    @Autowired
    public QianfanAdvisorController(QianFanChatModel chatModel) {
        this.chatModel = chatModel;
        this.chatClient = ChatClient.builder(chatModel)
                .defaultAdvisors(
                        new MessageChatMemoryAdvisor(new InMemoryChatMemory()) // chat-memory advisor
                )
                .build();
    }
    @GetMapping("/ai/generate")
    public Map generate(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {

        return Map.of("generation", this.chatClient.prompt().advisors(new LoggingAdvisor()).user(message).call().content());
    }

}

上面示例中通过InMemroyChatMemory实现每次会话过程将之前的上下文一起代入每次会话中。

对接阿里大模型

Spring AI Alibaba 开源项目基于 Spring AI 构建,是阿里云通义系列模型及服务在 Java AI 应用开发领域的最佳实践,提供高层次的 AI API 抽象与云原生基础设施集成方案,帮助开发者快速构建 AI 应用。

更多功能和例子详见:

Spring AI Alibaba文档: https://java2ai.com/

Spring AI 文档:https://docs.spring.io/spring-ai/reference/

AI原生IDE:https://juejin.cn/trae-x-2025?inviteCode=CT3QzvcRw5mEvHsyK9H6QzHLkmE4Cjks&utm_campaign=liebian_invite

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值