
Spring AI 宣布接入 DeepSeek !! 原创
DeepSeek,作为深度求索公司推出的杰出大模型,代表了国产技术的辉煌。掌握如何运用 DeepSeek 大模型至关重要。接下来,我们将探讨如何基于DeepSeek 大模型和 Spring AI 开发 AI Agent 智能体应用。
1、Spring AI 集成 DeepSeek 大模型
第一、DeepSeek 大模型介绍
DeepSeek 近日推出了两款新大模型:
1.DeepSeek V 系列,专注于对话功能,其模型命名为 deepseek-chat。
2.DeepSeek R 系列,主要致力于推理能力,模型名称为 deepseek-reasoner。
若想了解这些模型的发布和演进历程,可以查阅 DeepSeek 官方的更新日志。
https://api-docs.deepseek.com/zh-cn/updates
第二、Spring AI 集成 DeepSeek 大模型
DeepSeek AI 推出了开源的 DeepSeek V3 模型,该模型在推理和问题解决方面具有领先的技术实力而著称。
Spring AI 通过复用现成的 OpenAI 客户端实现了与 DeepSeek AI 的集成。首先,您需要申请 DeepSeek API 的密钥,设置基础 URL,并从支持的模型中选择一个进行使用。
第三、Spring AI 集成前准备
1.创建 API 密钥:访问地址:https://api-docs.deepseek.com/zh-cn/ 创建 API 密钥。使用 Spring AI 项目中的 spring.ai.openai.api-key 属性对其进行配置。
2.设置 DeepSeek 基本 URL:将 spring.ai.openai.base-url 属性设置为 api.deepseek.com。
3.选择 DeepSeek 模型:使用属性 spring.ai.openai.chat.model=<model name> 指定模型。有关可用选项,请参阅支持的型号。
2、Spring AI 集成 DeepSeek 实例
第一步、引入依赖
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
第二步、配置
spring:
ai:
openai:
api-key: sk-xxx // 填写自己申请的key
base-url: https://api.deepseek.com
chat:
options:
model: deepseek-chat
第三步、聊天 Agent 智能体案例
package com.ivy.controller;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import java.util.Map;
@RestController
public class ChatController {
private final OpenAiChatModel chatModel;
public ChatController(OpenAiChatModel chatModel) {
this.chatModel = chatModel;
}
@GetMapping("/ai/generate")
public Map<String, String> 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);
}
}
3、Spring AI 集成 DeepSeek 总结
Spring AI 与 DeepSeek 大模型的集成过程相当简便,同时支持了阻塞和流式聊天模式。关于 DeepSeek 大模型的函数调用、角色定义以及结构化输出等方面,与之前文章中介绍的内容保持一致,没有特别的差异。因此,这里不再重复说明。对于感兴趣的朋友,可以查阅本专栏的其他文章以获取更多信息。
本文转载自公众号玄姐聊AGI 作者:玄姐
