Skip to content

Commit

Permalink
optimize response format (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
littleli0n authored Dec 5, 2023
1 parent 38d8db1 commit d539fd5
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.intellij.openapi.wm.ToolWindowManager;
import com.zhongan.devpilot.actions.notifications.DevPilotNotification;
import com.zhongan.devpilot.constant.DefaultConst;
import com.zhongan.devpilot.constant.PromptConst;
import com.zhongan.devpilot.enums.EditorActionEnum;
import com.zhongan.devpilot.enums.SessionTypeEnum;
import com.zhongan.devpilot.gui.toolwindows.DevPilotChatToolWindowFactory;
Expand Down Expand Up @@ -89,7 +90,7 @@ protected void actionPerformed(Project project, Editor editor, String selectedTe
devPilotChatToolWindow.addClearSessionInfo();
String newPrompt = prompt.replace("{{selectedCode}}", selectedText);
if (LanguageSettingsState.getInstance().getLanguageIndex() == 1) {
newPrompt = newPrompt + "Please response in Chinese.";
newPrompt = newPrompt + PromptConst.ANSWER_IN_CHINESE;
}
devPilotChatToolWindow.syncSendAndDisplay(SessionTypeEnum.MULTI_TURN.getCode(), EditorActionEnum.getEnumByLabel(label), newPrompt,
callback, editorInfo);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/zhongan/devpilot/constant/DefaultConst.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ public class DefaultConst {

public final static int CHINESE_CONTENT_MAX_LENGTH = 1638;

public final static String DEFAULT_CODE_LANGUAGE = "java";

}
12 changes: 12 additions & 0 deletions src/main/java/com/zhongan/devpilot/constant/PromptConst.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.zhongan.devpilot.constant;

public class PromptConst {

public final static String RESPONSE_FORMAT = "You are a coding expert.\n" +
"You must obey ALL of the following rules:\n\n" +
"- quote variable name with single backtick such as `name`.\n" +
"- quote code block with triple backticks such as ```...```";

public final static String ANSWER_IN_CHINESE = "\nPlease answer in Chinese.";

}
13 changes: 5 additions & 8 deletions src/main/java/com/zhongan/devpilot/enums/EditorActionEnum.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,33 @@
public enum EditorActionEnum {
PERFORMANCE_CHECK("devpilot.action.performance.check", "Performance check in the following code",
"{{selectedCode}}\nGiving the code above, please fix any performance issues." +
"\nRemember you are very familiar with performance optimization.\n" + "Attention: generate the response using markdown format with single backticks for single lines of code or short snippets, and three backticks for quoting entire code blocks.\n"),
"\nRemember you are very familiar with performance optimization.\n"),

GENERATE_COMMENTS("devpilot.action.generate.comments", "Generate comments in the following code",
"{{selectedCode}}\nGiving the code above, please generate code comments, return code with comments."),

GENERATE_TESTS("devpilot.action.generate.tests", "Generate Tests in the following code",
"{{selectedCode}}\nGiving the code above, " +
"please help to generate JUnit test cases for it, be aware that if the code is untestable, " +
"please state it and give suggestions instead." + "Put the code in code block.\n"),
"please state it and give suggestions instead."),

FIX_THIS("devpilot.action.fix", "Fix This in the following code",
"{{selectedCode}}\nGiving the code above, please help to fix it:\n\n" +
"- Fix any typos or grammar issues.\n" +
"- Use better names as replacement to magic numbers or arbitrary acronyms.\n" +
"- Simplify the code so that it's more strait forward and easy to understand.\n" +
"- Optimize it for performance reasons.\n" +
"- Refactor it using best practice in software engineering.\n" + "\nMust only provide the code to be fixed and explain why it should be fixed.\n" +
"Attention: generate the response using markdown format with single backticks for single lines of code or short snippets, and three backticks for quoting entire code blocks.\n"),
"- Refactor it using best practice in software engineering.\n" + "\nMust only provide the code to be fixed and explain why it should be fixed.\n"),

REVIEW_CODE("devpilot.action.review", "Review code in the following code",
"{{selectedCode}}\nGiving the code above, please review the code line by line:\n\n" +
"- Think carefully, you should be extremely careful.\n" +
"- Find out if any bugs exists.\n" +
"- Reveal any bad smell in the code.\n" +
"- Give optimization or best practice suggestion.\n" +
"Attention: generate the response using markdown format with single backticks for single lines of code or short snippets, and three backticks for quoting entire code blocks.\n"),
"- Give optimization or best practice suggestion.\n"),

EXPLAIN_THIS("devpilot.action.explain", "Explain this in the following code",
"{{selectedCode}}\nGiving the code above, please explain it in detail, line by line.\n" +
"Attention: generate the response using markdown format with single backticks for single lines of code or short snippets, and three backticks for quoting entire code blocks.\n");
"{{selectedCode}}\nGiving the code above, please explain it in detail, line by line.\n");

private final String label;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.intellij.ui.components.JBScrollPane;
import com.intellij.util.ui.JBUI;
import com.zhongan.devpilot.actions.editor.popupmenu.BasicEditorAction;
import com.zhongan.devpilot.constant.PromptConst;
import com.zhongan.devpilot.enums.EditorActionEnum;
import com.zhongan.devpilot.enums.SessionTypeEnum;
import com.zhongan.devpilot.gui.toolwindows.components.ChatDisplayPanel;
Expand All @@ -26,6 +27,7 @@
import com.zhongan.devpilot.util.BalloonAlertUtils;
import com.zhongan.devpilot.util.DevPilotMessageBundle;
import com.zhongan.devpilot.util.MarkdownUtil;
import com.zhongan.devpilot.util.MessageUtil;

import java.awt.Component;
import java.awt.GridBagConstraints;
Expand Down Expand Up @@ -134,19 +136,21 @@ private void showChatContent(String content, int type, EditorActionEnum actionTy
}

private String sendMessage(Integer sessionType, String message) {
var devPilotMessage = new DevPilotMessage();
devPilotMessage.setRole("user");
devPilotMessage.setContent(message);
DevPilotMessage userMessage = MessageUtil.createUserMessage(message);
// check session type,default multi session
DevPilotChatCompletionRequest devPilotChatCompletionRequest = new DevPilotChatCompletionRequest();
SessionTypeEnum sessionTypeEnum = SessionTypeEnum.getEnumByCode(sessionType);
if (SessionTypeEnum.INDEPENDENT.equals(sessionTypeEnum)) {
// independent message can not update, just readonly
devPilotChatCompletionRequest.getMessages().add(devPilotMessage);
devPilotChatCompletionRequest.getMessages().add(userMessage);
devPilotChatCompletionRequest.getMessages().add(MessageUtil.createSystemMessage(PromptConst.RESPONSE_FORMAT));
} else {
if (multiSessionRequest.getMessages().isEmpty()) {
multiSessionRequest.getMessages().add(MessageUtil.createSystemMessage(PromptConst.RESPONSE_FORMAT));
}
devPilotChatCompletionRequest.setStream(multiSessionRequest.isStream());
devPilotChatCompletionRequest.setModel(multiSessionRequest.getModel());
multiSessionRequest.getMessages().add(devPilotMessage);
multiSessionRequest.getMessages().add(userMessage);
devPilotChatCompletionRequest.getMessages().addAll(multiSessionRequest.getMessages());
}
String chatCompletion = llmProvider.chatCompletion(devPilotChatCompletionRequest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.intellij.util.ui.JBUI;
import com.vladsch.flexmark.ast.FencedCodeBlock;
import com.vladsch.flexmark.parser.Parser;
import com.zhongan.devpilot.constant.DefaultConst;
import com.zhongan.devpilot.enums.EditorActionEnum;
import com.zhongan.devpilot.gui.toolwindows.components.code.CodeHeaderComponent;
import com.zhongan.devpilot.gui.toolwindows.components.code.GoToCode;
Expand Down Expand Up @@ -46,7 +47,7 @@ public Component createCodeComponent(Project project, String codeBlock, EditorAc
return null;
}
String code = codeNode.getContentChars().unescape().replaceAll("\\n$", "");
String language = StringUtils.isEmpty(codeNode.getInfo().toString()) ? "text" : codeNode.getInfo().toString();
String language = StringUtils.isEmpty(codeNode.getInfo().toString()) ? DefaultConst.DEFAULT_CODE_LANGUAGE : codeNode.getInfo().toString();

String fileExt = MarkdownUtil.getFileExtensionFromLanguage(language);
LightVirtualFile lightVirtualFile = new LightVirtualFile(System.currentTimeMillis() + fileExt, code);
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/zhongan/devpilot/util/MarkdownUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@

public class MarkdownUtil {

private static final String DEFAULT_CODE_FILE_EXTENSION = ".java";

public static String getFileExtensionFromLanguage(String language) {
return languageFileExtMap.getOrDefault(language.toLowerCase(), ".txt");
return languageFileExtMap.getOrDefault(language.toLowerCase(), DEFAULT_CODE_FILE_EXTENSION);
}

/**
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/com/zhongan/devpilot/util/MessageUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.zhongan.devpilot.util;

import com.zhongan.devpilot.integrations.llms.entity.DevPilotMessage;

public class MessageUtil {

public static DevPilotMessage createMessage(String role, String content) {
DevPilotMessage message = new DevPilotMessage();
message.setRole(role);
message.setContent(content);
return message;
}

public static DevPilotMessage createUserMessage(String content) {
return createMessage("user", content);
}

public static DevPilotMessage createSystemMessage(String content) {
return createMessage("system", content);
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ void doTestAction(EditorActionEnum action, String testCode, String exceptedChatR
// verify sent message
verify(mockedLlmProvider, times(1)).chatCompletion(argThat(argument -> {
assertNotEmpty(argument.getMessages());
String content = argument.getMessages().get(0).getContent();
String content = argument.getMessages().get(1).getContent();
return content.equals(exceptedPrompt);
}));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void testDevPilotLlmSettings() {
public void testEditorActionConfigurationState() {
var settings = EditorActionConfigurationState.getInstance();

assertEquals("{{selectedCode}}\nGiving the code above, please help to generate JUnit test cases for it, be aware that if the code is untestable, please state it and give suggestions instead.Put the code in code block.\n", settings.getDefaultActions().get("devpilot.action.generate.tests"));
assertEquals("{{selectedCode}}\nGiving the code above, please help to generate JUnit test cases for it, be aware that if the code is untestable, please state it and give suggestions instead.", settings.getDefaultActions().get("devpilot.action.generate.tests"));
assertEquals(6, settings.getDefaultActions().size());
}

Expand Down
3 changes: 2 additions & 1 deletion src/test/java/com/zhongan/devpilot/util/DevPilotTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.intellij.openapi.roots.ui.componentsList.components.ScrollablePanel;
import com.intellij.ui.components.JBScrollPane;
import com.zhongan.devpilot.constant.PromptConst;
import com.zhongan.devpilot.enums.EditorActionEnum;
import com.zhongan.devpilot.gui.toolwindows.chat.DevPilotChatToolWindow;
import com.zhongan.devpilot.gui.toolwindows.components.ChatDisplayPanel;
Expand Down Expand Up @@ -60,7 +61,7 @@ public static ContentComponent getLastContentComponent(@NotNull DevPilotChatTool
public static String emplacePrompt(@NotNull EditorActionEnum actionEnum, @NotNull String selectedText) {
String exceptedPrompt = actionEnum.getPrompt().replace("{{selectedCode}}", selectedText);
if (LanguageSettingsState.getInstance().getLanguageIndex() == 1) {
exceptedPrompt += "Please response in Chinese.";
exceptedPrompt += PromptConst.ANSWER_IN_CHINESE;
}
return exceptedPrompt;
}
Expand Down

0 comments on commit d539fd5

Please sign in to comment.