Connecting your enterprise Data and APIs with AI Models。

跨 AI 提供商的可移植 API 支持聊天、文本转图像和嵌入模型。支持同步和流式 API 选项。还可访问特定于模型的功能。
支持所有主要AI 模型提供商,例如 Anthropic、OpenAI、Microsoft、Amazon、Google 和 Ollama。支持的模型类型包括:聊天完成,嵌入,文本转图像,音频转录,文本转语音,适度。 最新预览版也已经支持了DeepSeek模型。
结构化输出- 将 AI 模型输出映射到 POJO。
用于 AI 模型和向量存储的 Spring Boot 自动配置和启动器。
用于数据工程的文档提取ETL 框架。
ChatClient API - 用于与 AI 聊天模型通信的流畅 API,惯用语类似于 WebClient 和 RestClient API。
Spring Boot3.x框架构建的,环境准备如下:
JDK 17 或更高版本 Maven 或 Gradle 构建工具 DeepSeek API Key(可通过官网注册获取),申请地址:https://platform.deepseek.com/usage
spring-ai-openai starter:伪装成 OpenAI,DeepSeek 提供了 OpenAI 兼容模式。 spring-ai-ollama-spring-boot-starter:通过 Ollama 本地部署一个 DeepSeek R1 蒸馏版。
deepseek-chat(V3):适用于聊天机器人、智能客服、内容生成等,能够理解和生成日常对话内容。 deepseek-reasoner(R1):专为复杂推理任务设计,适合解决需要深度逻辑分析和推理的问题。
Maven依赖
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-openai-spring-boot-starter</artifactId></dependency></dependencies>
配置
# 必填项spring.ai.openai.api-key=you-apikeyspring.ai.openai.base-url=https://api.deepseek.com# 模型选择(示例使用对话模型)spring.ai.openai.chat.options.model=deepseek-chat
编码
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.*;@RestController@RequestMapping("/v1/api/chat")public class ChatAiController {@Autowiredprivate DeepSeekClient deepSeekClient;@PostMappingpublic String chat(@RequestBody String message) {return deepSeekClient.chatCompletion(message).getOutput().getContent();}@GetMapping(value = "/stream", produces = "text/event-stream")public Flux<String> chatStream(@RequestParam String message) {return deepSeekClient.chatFluxCompletion(message).map(response -> response.getOutput().getContent());}}
Ollama部署集成DeepSeek蒸馏模型,参考文章:
Maven依赖添加
<dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-ollama-spring-boot-starter</artifactId></dependency>
配置
spring.ai.ollama.base-url=http://localhost:11434spring.ai.ollama.chat.model=deepseek-r1:1.5b # 与本地模型名称对应
编码
@RestController@RequestMapping("/v1/api/chat")public class ChatAiOllamaController {@Autowiredprivate DeepSeekClient deepSeekClient;@GetMapping("/stream/ollama")public ResponseEntity<Flux<String>> chatStream(@RequestParam(value = "message") String message) {try {// 调用 deepSeekClient 生成响应,并以 Flux<String>(响应流)形式返回Flux<String> response = deepSeekClient.chatFluxCompletionOllama(message);return ResponseEntity.ok(response);} catch (Exception e) {return ResponseEntity.badRequest().build();}}}
https://github.com/alibaba/spring-ai-alibaba/blob/main/README-zh.md
Spring Cloud Alibaba AI
https://docs.spring.io/spring-ai/reference/api/chatclient.html
Spring AI

欢迎微信扫描二维码,关注我的公众号~~
最后修改时间:2025-02-17 13:11:34
文章转载自迷三张,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




