
從零開始掌握Reddit獲取API密鑰與數(shù)據(jù)分析
spring-ai-openai-spring-boot-starter
0.8.1
在 application.yml
中設(shè)置 DeepSeek 的 OpenAI 式端點(diǎn)和 API 密鑰:
spring:
ai:
openai:
base-url: https://api.deepseek.com/v1
api-key: sk-your-deepseek-key-here
chat.options:
model: deepseek-chat
在代碼中通過 ChatClient 調(diào)用 DeepSeek 的 API:
@RestController
@RequestMapping("/ai")
@Slf4j
public class ChatController {
private final ChatClient chatClient;
public ChatController(ChatClient.Builder chatClientBuilder) {
this.chatClient = chatClientBuilder.build();
}
@GetMapping("/chat")
public String generate(@RequestParam(value = "message") String message) {
log.info("Generating response");
return chatClient.prompt(message).call().content();
}
}
通過在 Prompt
調(diào)用中添加請求選項(xiàng),可以覆蓋默認(rèn)配置,例如設(shè)置模型和溫度:
ChatResponse response = chatModel.call(
new Prompt(
"Generate the names of 5 famous pirates.",
OpenAiChatOptions.builder()
.withModel("deepseek-chat")
.withTemperature(0.4)
.build()
));
如果想在本地或內(nèi)網(wǎng)服務(wù)器上部署 DeepSeek,可以采用 Ollama 平臺(tái)。
從官方網(wǎng)站下載并安裝 Ollama:https://ollama.com
通過 Ollama 下載 DeepSeek 模型:
ollama pull deepseek-r1:1.5b
ollama list deepseek
更多版本可以在 https://ollama.com/library/deepseek-r1 查看。
在項(xiàng)目的 pom.xml
文件中添加以下依賴:
org.springframework.ai
spring-ai-ollama-spring-boot-starter
0.8.1
在 application.yml
中設(shè)置本地模型名稱對(duì)應(yīng)的配置:
spring:
ai:
ollama:
base-url: http://localhost:11434
chat:
model: deepseek-r1:1.5b
@RestController
@RequestMapping("/ai")
public class ChatController {
private final ChatClient chatClient;
public ChatController(ChatClient.Builder chatClient) {
this.chatClient = chatClient.build();
}
@GetMapping("/chat")
public ResponseEntity<Flux> chat(@RequestParam(value = "message") String message) {
try {
Flux response = chatClient.prompt(message).stream().content();
return ResponseEntity.ok(response);
} catch (Exception e) {
return ResponseEntity.badRequest().build();
}
}
}
通過 Spring AI 集成 DeepSeek,無論是偽裝成 OpenAI 還是通過 Ollama 本地部署,都是非常簡單且靈活的選擇。Spring AI 的設(shè)計(jì)使得企業(yè)可以在不同的 AI 服務(wù)之間輕松切換,從而提升了系統(tǒng)的適應(yīng)性和擴(kuò)展性。
問:Spring AI 如何簡化 AI 模型集成?
問:DeepSeek 的 OpenAI 兼容模式有什么優(yōu)勢?
問:如何在本地部署 DeepSeek 模型?
問:Spring AI 的抽象設(shè)計(jì)如何幫助企業(yè)?
問:如何確保 API 密鑰的安全性?
對(duì)比大模型API的內(nèi)容創(chuàng)意新穎性、情感共鳴力、商業(yè)轉(zhuǎn)化潛力
一鍵對(duì)比試用API 限時(shí)免費(fèi)