0、确保deepeseek有余额
1、引入Maven依赖POM
<dependencyManagement> <dependencies> <dependency> <groupId>dev.langchain4j</groupId> <artifactId>langchain4j-bom</artifactId> <version>1.1.0</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>dev.langchain4j</groupId> <artifactId>langchain4j-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>dev.langchain4j</groupId> <artifactId>langchain4j-open-ai-spring-boot-starter</artifactId> <exclusions> <exclusion> <groupId>dev.langchain4j</groupId> <artifactId>langchain4j-http-client-jdk</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- for Flux<String> support --> <dependency> <groupId>dev.langchain4j</groupId> <artifactId>langchain4j-reactor</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <!-- observability --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-tracing-bridge-otel</artifactId> </dependency> <dependency> <groupId>io.opentelemetry</groupId> <artifactId>opentelemetry-exporter-zipkin</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <!-- <dependency>--> <!-- <groupId>dev.langchain4j</groupId>--> <!-- <artifactId>langchain4j-open-ai</artifactId>--> <!-- </dependency>--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/dashscope-sdk-java --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dashscope-sdk-java</artifactId> <version>2.18.3</version> </dependency> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId> <version>4.4.0</version> </dependency> </dependencies>
2、代码:
package dev.langchain4j.example;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.request.ChatRequest;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.model.openai.internal.OpenAiClient;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest()
public class ChatDemo {
@Autowired
private ChatModel chatModel;
@Test
public void testChat() {
System.out.println("----------------");
String openKey = System.getenv("OPENAI_API_KEY");
// dev.langchain4j.http.client.spring.restclient.SpringRestClientBuilderFactory,
// dev.langchain4j.http.client.jdk.JdkHttpClientBuilderFactory
/**
* 欠费:
402 Payment Required: "{"error":{"message":"Insufficient Balance","type":"unknown_error","param":null,"code":"invalid_request_error"}}"
at dev.la
*/
OpenAiChatModel model = OpenAiChatModel.builder()
.apiKey("sk-aec0******").baseUrl("https://api.deepseek.com/v1").modelName("deepseek-chat" ).build();
String response = model.chat("你是谁?");
System.out.println(response);
}
}
3、运行结果:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.4.2)
2025-07-25T11:31:35.032+08:00 INFO 7208 --- [LangChain4j Spring Boot Example] [ main] [ ] dev.langchain4j.example.ChatDemo : Starting ChatDemo using Java 17.0.12 with PID 7208 (started by MAC in o.s.b.a.e.web.EndpointLinksResolver : Exposing 14 endpoints beneath base path '/actuator'
2025-07-25T11:31:41.259+08:00 INFO 7208 --- [LangChain4j Spring Boot Example] [ main] [ ] dev.langchain4j.example.ChatDemo : Started ChatDemo in 6.698 seconds (process running for 13.372)
Java HotSpot(TM) 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
----------------
我是DeepSeek Chat,由深度求索公司创造的智能AI助手!🤖✨ 我可以帮你解答问题、提供建议、陪你聊天,甚至帮你处理各种文本和文件。无论是学习、工作,还是日常生活中的小困惑,都可以来找我聊聊!有什么我可以帮你的吗?😊
Process finished with exit code 0