Skip to content

Commit 4e149c5

Browse files
committed
feat: support DeepSeek R1 and V3 models
1 parent 89a3b66 commit 4e149c5

13 files changed

Lines changed: 300 additions & 64 deletions

File tree

src/main/cpp/llama.cpp

src/main/java/ee/carlrobert/codegpt/CodeGPTPlugin.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package ee.carlrobert.codegpt;
22

3+
import static java.io.File.separator;
34
import static java.util.Objects.requireNonNull;
45

56
import com.intellij.ide.plugins.PluginManagerCore;
67
import com.intellij.openapi.application.PathManager;
78
import com.intellij.openapi.extensions.PluginId;
89
import com.intellij.openapi.project.Project;
9-
import java.io.File;
1010
import java.nio.file.Path;
1111
import org.jetbrains.annotations.NotNull;
1212

@@ -26,18 +26,18 @@ private CodeGPTPlugin() {
2626
}
2727

2828
public static @NotNull String getPluginOptionsPath() {
29-
return PathManager.getOptionsPath() + File.separator + "CodeGPT";
29+
return PathManager.getOptionsPath() + separator + "CodeGPT";
3030
}
3131

3232
public static @NotNull String getIndexStorePath() {
33-
return getPluginOptionsPath() + File.separator + "indexes";
33+
return getPluginOptionsPath() + separator + "indexes";
3434
}
3535

3636
public static @NotNull String getLlamaSourcePath() {
37-
return getPluginBasePath() + File.separator + "llama.cpp";
37+
return getPluginBasePath() + separator + "llama.cpp";
3838
}
3939

4040
public static @NotNull String getProjectIndexStorePath(@NotNull Project project) {
41-
return getIndexStorePath() + File.separator + project.getName();
41+
return getIndexStorePath() + separator + project.getName();
4242
}
4343
}

src/main/java/ee/carlrobert/codegpt/completions/HuggingFaceModel.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ public enum HuggingFaceModel {
4646
DEEPSEEK_CODER_33B_Q5(33, 5, "deepseek-coder-33b-instruct-GGUF",
4747
"deepseek-coder-33b-instruct.Q5_K_M.gguf", 23.5),
4848

49+
DEEPSEEK_R1_1_5B_Q6(1, 6, "DeepSeek-R1-Distill-Qwen-1.5B-GGUF",
50+
"DeepSeek-R1-Distill-Qwen-1.5B-Q6_K.gguf", "bartowski", 1.89),
51+
DEEPSEEK_R1_7B_Q4(7, 4, "DeepSeek-R1-Distill-Qwen-7B-GGUF",
52+
"DeepSeek-R1-Distill-Qwen-7B-Q4_K_M.gguf", "bartowski", 4.68),
53+
DEEPSEEK_R1_7B_Q6(7, 6, "DeepSeek-R1-Distill-Qwen-7B-GGUF",
54+
"DeepSeek-R1-Distill-Qwen-7B-Q6_K.gguf", "bartowski", 6.25),
55+
DEEPSEEK_R1_14B_Q4(14, 4, "DeepSeek-R1-Distill-Qwen-14B-GGUF",
56+
"DeepSeek-R1-Distill-Qwen-14B-Q4_K_M.gguf", "bartowski", 8.99),
57+
DEEPSEEK_R1_14B_Q6(14, 6, "DeepSeek-R1-Distill-Qwen-14B-GGUF",
58+
"DeepSeek-R1-Distill-Qwen-14B-Q6_K.gguf", "bartowski", 12.12),
59+
4960
PHIND_CODE_LLAMA_34B_Q3(34, 3, "Phind-CodeLlama-34B-v2-GGUF",
5061
"phind-codellama-34b-v2.Q3_K_M.gguf"),
5162
PHIND_CODE_LLAMA_34B_Q4(34, 4, "Phind-CodeLlama-34B-v2-GGUF",

src/main/java/ee/carlrobert/codegpt/completions/llama/LlamaModel.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,20 @@ public enum LlamaModel {
6464
HuggingFaceModel.DEEPSEEK_CODER_33B_Q3,
6565
HuggingFaceModel.DEEPSEEK_CODER_33B_Q4,
6666
HuggingFaceModel.DEEPSEEK_CODER_33B_Q5)),
67+
DEEPSEEK_R1(
68+
"Deepseek R1",
69+
"DeepSeek-R1-Zero, a model trained via large-scale reinforcement learning (RL) "
70+
+ "without supervised fine-tuning (SFT) as a preliminary step, demonstrated remarkable "
71+
+ "performance on reasoning. DeepSeek-R1 achieves performance comparable to OpenAI-o1 "
72+
+ "across math, code, and reasoning tasks.",
73+
PromptTemplate.DEEPSEEK_R1,
74+
InfillPromptTemplate.DEEPSEEK_CODER,
75+
List.of(
76+
HuggingFaceModel.DEEPSEEK_R1_1_5B_Q6,
77+
HuggingFaceModel.DEEPSEEK_R1_7B_Q4,
78+
HuggingFaceModel.DEEPSEEK_R1_7B_Q6,
79+
HuggingFaceModel.DEEPSEEK_R1_14B_Q4,
80+
HuggingFaceModel.DEEPSEEK_R1_14B_Q6)),
6781
PHIND_CODE_LLAMA(
6882
"Phind Code Llama",
6983
"This model is fine-tuned from Phind-CodeLlama-34B-v1 on an additional 1.5B tokens "

src/main/java/ee/carlrobert/codegpt/completions/llama/LlamaServerAgent.java

Lines changed: 75 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ public final class LlamaServerAgent implements Disposable {
3333

3434
private static final Logger LOG = Logger.getInstance(LlamaServerAgent.class);
3535

36-
private @Nullable OSProcessHandler makeProcessHandler;
36+
private @Nullable OSProcessHandler makeSetupProcessHandler;
37+
private @Nullable OSProcessHandler makeBuildProcessHandler;
3738
private @Nullable OSProcessHandler startServerProcessHandler;
3839
private ServerProgressPanel activeServerProgressPanel;
3940
private boolean stoppedByUser;
@@ -49,11 +50,44 @@ public void startAgent(
4950
stoppedByUser = false;
5051
serverProgressPanel.displayText(
5152
CodeGPTBundle.get("llamaServerAgent.buildingProject.description"));
52-
makeProcessHandler = new OSProcessHandler(
53-
getMakeCommandLine(params));
54-
makeProcessHandler.addProcessListener(
55-
getMakeProcessListener(params, onSuccess, onServerStopped));
56-
makeProcessHandler.startNotify();
53+
54+
makeSetupProcessHandler = new OSProcessHandler(getCMakeSetupCommandLine(params));
55+
makeSetupProcessHandler.addProcessListener(new ProcessAdapter() {
56+
private final List<String> errorLines = new CopyOnWriteArrayList<>();
57+
58+
@Override
59+
public void onTextAvailable(@NotNull ProcessEvent event, @NotNull Key outputType) {
60+
if (ProcessOutputType.isStderr(outputType)) {
61+
errorLines.add(event.getText());
62+
return;
63+
}
64+
LOG.info(event.getText());
65+
}
66+
67+
@Override
68+
public void processTerminated(@NotNull ProcessEvent event) {
69+
int exitCode = event.getExitCode();
70+
LOG.info(format("CMake setup exited with code %d", exitCode));
71+
if (stoppedByUser) {
72+
onServerStopped.accept(activeServerProgressPanel);
73+
return;
74+
}
75+
if (exitCode != 0) {
76+
showServerError(String.join(",", errorLines), onServerStopped);
77+
return;
78+
}
79+
80+
try {
81+
makeBuildProcessHandler = new OSProcessHandler(getCMakeBuildCommandLine(params));
82+
makeBuildProcessHandler.addProcessListener(
83+
getMakeProcessListener(params, onSuccess, onServerStopped));
84+
makeBuildProcessHandler.startNotify();
85+
} catch (ExecutionException e) {
86+
showServerError(e.getMessage(), onServerStopped);
87+
}
88+
}
89+
});
90+
makeSetupProcessHandler.startNotify();
5791
} catch (ExecutionException e) {
5892
showServerError(e.getMessage(), onServerStopped);
5993
}
@@ -62,18 +96,18 @@ public void startAgent(
6296

6397
public void stopAgent() {
6498
stoppedByUser = true;
65-
if (makeProcessHandler != null) {
66-
makeProcessHandler.destroyProcess();
99+
if (makeSetupProcessHandler != null) {
100+
makeSetupProcessHandler.destroyProcess();
67101
}
68102
if (startServerProcessHandler != null) {
69103
startServerProcessHandler.destroyProcess();
70104
}
71105
}
72106

73107
public boolean isServerRunning() {
74-
return (makeProcessHandler != null
75-
&& makeProcessHandler.isStartNotified()
76-
&& !makeProcessHandler.isProcessTerminated())
108+
return (makeSetupProcessHandler != null
109+
&& makeSetupProcessHandler.isStartNotified()
110+
&& !makeSetupProcessHandler.isProcessTerminated())
77111
|| (startServerProcessHandler != null
78112
&& startServerProcessHandler.isStartNotified()
79113
&& !startServerProcessHandler.isProcessTerminated());
@@ -147,25 +181,14 @@ public void processTerminated(@NotNull ProcessEvent event) {
147181

148182
@Override
149183
public void onTextAvailable(@NotNull ProcessEvent event, @NotNull Key outputType) {
150-
if (ProcessOutputType.isStderr(outputType)) {
151-
errorLines.add(event.getText());
152-
}
153-
154-
if (ProcessOutputType.isStdout(outputType)) {
155-
LOG.info(event.getText());
184+
LOG.info(event.getText());
156185

157-
try {
158-
var serverMessage = objectMapper.readValue(event.getText(), LlamaServerMessage.class);
159-
// hack
160-
if ("HTTP server listening".equals(serverMessage.msg())) {
161-
LOG.info("Server up and running!");
186+
// TODO: Use proper successful boot up validation
187+
if (event.getText().contains("server is listening")) {
188+
LOG.info("Server up and running!");
162189

163-
LlamaSettings.getCurrentState().setServerPort(port);
164-
onSuccess.run();
165-
}
166-
} catch (Exception ignore) {
167-
// ignore
168-
}
190+
LlamaSettings.getCurrentState().setServerPort(port);
191+
onSuccess.run();
169192
}
170193
}
171194
};
@@ -177,20 +200,32 @@ private void showServerError(String errorText, Consumer<ServerProgressPanel> onS
177200
OverlayUtil.showClosableBalloon(errorText, MessageType.ERROR, activeServerProgressPanel);
178201
}
179202

180-
private static GeneralCommandLine getMakeCommandLine(LlamaServerStartupParams params) {
181-
GeneralCommandLine commandLine = new GeneralCommandLine().withCharset(StandardCharsets.UTF_8);
182-
commandLine.setExePath("make");
183-
commandLine.withWorkDirectory(CodeGPTPlugin.getLlamaSourcePath());
184-
commandLine.addParameters("-j");
185-
commandLine.addParameters(params.additionalBuildParameters());
186-
commandLine.withEnvironment(params.additionalEnvironmentVariables());
187-
commandLine.setRedirectErrorStream(false);
188-
return commandLine;
203+
private static GeneralCommandLine getCMakeSetupCommandLine(LlamaServerStartupParams params) {
204+
GeneralCommandLine cmakeSetupCommand = new GeneralCommandLine().withCharset(
205+
StandardCharsets.UTF_8);
206+
cmakeSetupCommand.setExePath("cmake");
207+
cmakeSetupCommand.withWorkDirectory(CodeGPTPlugin.getLlamaSourcePath());
208+
cmakeSetupCommand.addParameters("-B", "build");
209+
cmakeSetupCommand.withEnvironment(params.additionalEnvironmentVariables());
210+
cmakeSetupCommand.setRedirectErrorStream(false);
211+
return cmakeSetupCommand;
212+
}
213+
214+
private static GeneralCommandLine getCMakeBuildCommandLine(LlamaServerStartupParams params) {
215+
GeneralCommandLine cmakeBuildCommand = new GeneralCommandLine().withCharset(
216+
StandardCharsets.UTF_8);
217+
cmakeBuildCommand.setExePath("cmake");
218+
cmakeBuildCommand.withWorkDirectory(CodeGPTPlugin.getLlamaSourcePath());
219+
cmakeBuildCommand.addParameters("--build", "build", "--config", "Release", "-t", "llama-server",
220+
"-j", "4");
221+
cmakeBuildCommand.withEnvironment(params.additionalEnvironmentVariables());
222+
cmakeBuildCommand.setRedirectErrorStream(false);
223+
return cmakeBuildCommand;
189224
}
190225

191226
private GeneralCommandLine getServerCommandLine(LlamaServerStartupParams params) {
192227
GeneralCommandLine commandLine = new GeneralCommandLine().withCharset(StandardCharsets.UTF_8);
193-
commandLine.setExePath("./server");
228+
commandLine.setExePath("./build/bin/llama-server");
194229
commandLine.withWorkDirectory(CodeGPTPlugin.getLlamaSourcePath());
195230
commandLine.addParameters(
196231
"-m", params.modelPath(),
@@ -210,8 +245,8 @@ public void setActiveServerProgressPanel(
210245

211246
@Override
212247
public void dispose() {
213-
if (makeProcessHandler != null && !makeProcessHandler.isProcessTerminated()) {
214-
makeProcessHandler.destroyProcess();
248+
if (makeSetupProcessHandler != null && !makeSetupProcessHandler.isProcessTerminated()) {
249+
makeSetupProcessHandler.destroyProcess();
215250
}
216251
if (startServerProcessHandler != null && !startServerProcessHandler.isProcessTerminated()) {
217252
startServerProcessHandler.destroyProcess();

src/main/java/ee/carlrobert/codegpt/completions/llama/PromptTemplate.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import ee.carlrobert.codegpt.conversations.message.Message;
66
import java.util.List;
7+
import java.util.stream.Collectors;
78

89
public enum PromptTemplate {
910

@@ -237,6 +238,23 @@ public String buildPrompt(String systemPrompt, String userPrompt, List<Message>
237238
.toString();
238239
}
239240
},
241+
DEEPSEEK_R1("DeepSeek R1") {
242+
@Override
243+
public String buildPrompt(String systemPrompt, String userPrompt, List<Message> history) {
244+
var historyString = history.stream()
245+
.map(it -> {
246+
String response = it.getResponse();
247+
if (response.startsWith("<think>")) {
248+
response = response.replaceAll("(?s)<think>.*?</think>", "").trim();
249+
}
250+
return String.format("User:\n%s\n\nAssistant:\n%s", it.getPrompt(), response);
251+
})
252+
.collect(Collectors.joining("\n\n"));
253+
254+
return "<|begin▁of▁sentence|>%s<|User|>History:\n%s\n\nUser:\n%s<|Assistant|>"
255+
.formatted(systemPrompt, historyString, userPrompt);
256+
}
257+
},
240258
DEEPSEEK_CODER("DeepSeek Coder") {
241259
@Override
242260
public String buildPrompt(String systemPrompt, String userPrompt, List<Message> history) {

src/main/java/ee/carlrobert/codegpt/toolwindow/chat/ChatToolWindowTabPanel.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,6 @@ private ResponseMessagePanel createResponseMessagePanel(ChatCompletionParameters
225225
panel.addCopyAction(() -> CopyAction.copyToClipboard(message.getResponse()));
226226
panel.addContent(new ChatMessageResponseBody(
227227
project,
228-
true,
229228
false,
230229
message.isWebSearchIncluded(),
231230
fileContextIncluded || message.getDocumentationDetails() != null,

0 commit comments

Comments
 (0)