Unlock DeepSeek4j 1.4: Build a Private AI Knowledge Base with Spring Boot
This guide explains why DeepSeek4j is needed, its core features, and provides step‑by‑step instructions—including dependency setup, configuration, code examples, and a complete RAG pipeline using Milvus—to help developers quickly create a private AI knowledge base with Spring Boot.
Why DeepSeek4j?
DeepSeek4J is a DeepSeek model integration framework for the Java ecosystem. Its API is concise; a single line of code can integrate DeepSeek.
Limitations of existing frameworks
Thought chain loss : The core reasoning process of R1 is completely ignored.
Incompatible response mode : Cannot handle “thinking first, conclusion later” output.
Parameter restrictions :
temperature,
top_pand other key parameters are ineffective.
Incomplete streaming : Poor user experience.
Solution
The open‑source project Pig builds on the architecture of OpenAI4J to provide an out‑of‑the‑box solution for DeepSeek – DeepSeek4j .
Enhanced support for DeepSeek’s unique thought‑chain and billing features.
Full reactive support via Project Reactor .
Spring Boot starter with auto‑configuration.
Core Features
✨ Complete preservation of thought‑chain and billing.
🚀 Reactive streaming support.
🛠 Simple and elegant API design.
📦 Ready‑to‑use Spring Boot integration for 2.x / 3.x.
💡 Built‑in debugging page.
🔍 Detailed request/response logging.
🔧 Flexible proxy configuration.
⚡ Reactive programming support.
Quick Start
Add Dependency :
<code><dependency>
<groupId>io.github.pig-mesh.ai</groupId>
<artifactId>deepseek-spring-boot-starter</artifactId>
<version>1.1.0</version>
</dependency>
</code>Configuration :
In
application.ymladd:
<code>deepseek:
api-key: your-api-key-here
base-url: https://api.deepseek.com/v1
</code>Basic Usage :
<code>@Autowired
private DeepSeekClient deepSeekClient;
@GetMapping(value = "/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<ChatCompletionResponse> chat(String prompt) {
return deepSeekClient.chatFluxCompletion(prompt);
}
</code>Advanced Configuration :
<code>ChatCompletionRequest request = ChatCompletionRequest.builder()
.model(ChatCompletionModel.DEEPSEEK_CHAT)
.addUserMessage(prompt)
.addAssistantMessage("Previous result")
.addSystemMessage("You are a professional assistant")
.maxTokens(1000)
.responseFormat()
.tools()
.build();
return deepSeekClient.chatFluxCompletion(request);
</code>Developer Egg : Double‑click
sse.htmlto open the built‑in visual debugging page that shows the evolution of the thought chain.
Building a Private Knowledge Base with DeepSeek4j
Background
DeepSeek4j provides APIs for Reasoner, Function Calling, JSON parsing, etc., simplifying DeepSeek integration.
DeepSeek does not provide a vector model, so the initial design omitted vector search.
Solution
Integrate vector capabilities by conforming to the OpenAI protocol, which adds zero extra dependencies, perfect compatibility, and standardized access.
Quick Start
Deploy Ollama models:
<code># Inference model
ollama run deepseek-r1:14b
# Vector model
ollama run bge-m3:latest
</code>Vector Database Preparation
Use Milvus either via Zilliz Cloud or Docker:
<code># Docker installation
curl -sfL https://raw.githubusercontent.com/milvus-io/milvus/master/scripts/standalone_embed.sh -o standalone_embed.sh
bash standalone_embed.sh start
</code>Ensure your network can access GitHub when using Docker.
Initialize Private Knowledge
Create a Milvus client and upload vectorized documents. Example for plain text:
<code>// Connect to Milvus server
ConnectConfig connectConfig = ConnectConfig.builder()
.uri(CLUSTER_ENDPOINT)
.token(TOKEN)
.build();
MilvusClientV2 milvusClientV2 = new MilvusClientV2(connectConfig);
</code>Embed text and insert into Milvus:
<code>@Autowired
EmbeddingClient embeddingClient;
{
String law = FileUtil.readString("/path/to/law.txt", Charset.defaultCharset());
String[] splits = StrUtil.split(law, 400);
List<JsonObject> data = new ArrayList<>();
for (String part : splits) {
List<Float> vec = embeddingClient.embed(part);
JsonArray jsonArray = new JsonArray();
for (Float v : vec) { jsonArray.add(v); }
JsonObject obj = new JsonObject();
obj.add("vector", jsonArray);
obj.addProperty("text", part);
data.add(obj);
}
InsertReq insertReq = InsertReq.builder()
.collectionName("deepseek4j_test")
.data(data)
.build();
milvusClientV2.insert(insertReq);
}
</code>Create RAG Endpoint
<code>@GetMapping(value = "/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<ChatCompletionResponse> chat(String prompt) {
List<Float> queryVec = embeddingClient.embed(prompt);
SearchReq searchReq = SearchReq.builder()
.collectionName("deepseek4j_test")
.data(Collections.singletonList(new FloatVec(queryVec)))
.outputFields(Collections.singletonList("text"))
.topK(3)
.build();
SearchResp resp = milvusClientV2.search(searchReq);
List<String> contexts = resp.getSearchResults().stream()
.flatMap(r -> r.stream())
.map(r -> r.getEntity().get("text").toString())
.collect(Collectors.toList());
ChatCompletionRequest request = ChatCompletionRequest.builder()
.model("deepseek-r1:14b")
.addUserMessage(String.format(
"Based on the user question: %s, refer to the following content: %s, and produce a final answer.",
prompt, contexts))
.build();
return deepSeekClient.chatFluxCompletion(request);
}
</code>Summary
The article introduces DeepSeek4j 1.4 and demonstrates how to use it to build a private knowledge base and enhance a RAG system, covering environment setup, vector storage, semantic retrieval, and generation.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.