Spring Boot接入Deep Seek的API
1,首先进入deepseek的官网:DeepSeek | 深度求索,单击右上角的API开放平台。
2,单击API keys,创建一个API,创建完成务必复制!!不然关掉之后会看不看api key!!!
DeepSeek Chat :: Spring AI Reference
创建一个SpringWeb项目,勾选AI下面的Open-Ai,创建成功后就可以进行下一步。
首先把application.property改成application.yml,将以下信息复制到yml里,记得把api-key替换成你自己的!
spring:ai:openai:api-key: api-keybase-url: https://api.deepseek.comchat:options:model: deepseek-chat
编写配置类:
@Configuration
public class ChatConfig {@Beanpublic ChatClient chatClient(OpenAiChatModel openAiChatModel) {return ChatClient.builder(openAiChatModel).build();}
}
编写controller
@RestController
@RequestMapping("/ai")
public class ChatController {private final ChatClient chatClient;public ChatController(ChatClient chatClient) {this.chatClient = chatClient;}@GetMapping("/chat")public String generate(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {return chatClient.prompt().user(message).call().content();}
}
启动项目,测试请求
搞定!
备注:pom.xml
<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><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-bom</artifactId><version>${spring-ai.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><annotationProcessorPaths><path><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></path></annotationProcessorPaths></configuration></plugin><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build>