Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code generation improvement with strict check #8

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 36 additions & 7 deletions src/main/java/io/github/jeddict/ai/JeddictChatModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,19 @@ public List<Snippet> suggestNextLineCode(String classDatas, String classContent,
+ "'snippet' should contain the suggested code as a text block, which may include multiple lines formatted as a single string using \\n for line breaks. \n\n"
+ "Make sure to escape any double quotes within the snippet using a backslash (\\) so that the JSON remains valid. \n\n"
+ "Java Class Content:\n" + classContent;
} else if (path.getLeaf().getKind() == Tree.Kind.CLASS
} else if (path.getLeaf().getKind() == Tree.Kind.MODIFIERS
&& path.getParentPath() != null
&& path.getParentPath().getLeaf().getKind() == Tree.Kind.METHOD) {
prompt = "You are an API server that suggests Java code modifications for a method. "
+ "At the placeholder location ${SUGGEST_CODE_LIST}, suggest method-level modifiers such as 'public', 'protected', 'private', 'abstract', 'static', 'final', 'synchronized', or relevant method-level annotations. "
+ "Additionally, you may suggest method-specific annotations like '@Override', '@Deprecated', '@Transactional', etc. "
+ "Ensure that the suggestions are appropriate for the method context provided. "
+ "Return a JSON array with a few best suggestions without any additional text or explanation. Each element should be an object containing two fields: 'imports' and 'snippet'. "
+ "'imports' should be an array of required Java import statements (if no imports are required, return an empty array). "
+ "'snippet' should contain the suggested code as a text block, which may include multiple lines formatted as a single string using \\n for line breaks. \n\n"
+ "Make sure to escape any double quotes within the snippet using a backslash (\\) so that the JSON remains valid. \n\n"
+ "Java Method Content:\n" + classContent;
} else if (path.getLeaf().getKind() == Tree.Kind.CLASS
&& path.getParentPath() != null
&& path.getParentPath().getLeaf().getKind() == Tree.Kind.CLASS) {
prompt = "You are an API server that suggests Java code for an inner class at the placeholder location ${SUGGEST_CODE_LIST}. "
Expand All @@ -428,6 +440,17 @@ public List<Snippet> suggestNextLineCode(String classDatas, String classContent,
+ "'snippet' should contain the suggested code as a text block, which may include multiple lines formatted as a single string using \\n for line breaks. \n\n"
+ "Make sure to escape any double quotes within the snippet using a backslash (\\) so that the JSON remains valid. \n\n"
+ "Java Class Content:\n" + classContent;
} else if (path.getLeaf().getKind() == Tree.Kind.CLASS
&& path.getParentPath() != null
&& path.getParentPath().getLeaf().getKind() == Tree.Kind.COMPILATION_UNIT) {
prompt = "You are an API server that suggests Java code for an class at the placeholder location ${SUGGEST_CODE_LIST}. "
+ "Based on the provided Java class content, suggest either relevant class level members, attributes, constants, methods or blocks. "
+ "Ensure that the suggestions are contextually appropriate for an class. "
+ "Return a JSON array with a few best suggestions without any additional text or explanation. Each element should be an object containing two fields: 'imports' and 'snippet'. "
+ "'imports' should be an array of required Java import statements (if no imports are required, return an empty array). "
+ "'snippet' should contain the suggested code as a text block, which may include multiple lines formatted as a single string using \\n for line breaks. \n\n"
+ "Make sure to escape any double quotes within the snippet using a backslash (\\) so that the JSON remains valid. \n\n"
+ "Java Class Content:\n" + classContent;
} else {
prompt = "You are an API server that suggests Java code for a specific context in a given Java class at the placeholder location ${SUGGEST_CODE_LIST}. "
+ "Based on the provided Java class content and the line of code: \"" + lineText + "\", suggest a relevant single line of code or a multi-line code block as appropriate for the context represented by the placeholder ${SUGGEST_CODE_LIST} in the Java class. "
Expand Down Expand Up @@ -478,18 +501,21 @@ public List<String> suggestJavadocOrComment(String classDatas, String classConte
return comments;
}

public List<String> suggestAnnotations(String classDatas, String classContent, String lineText) {
String prompt = "You are an API server that suggests Java annotations for a specific context in a given Java class at the placeholder location ${SUGGEST_ANNOTATION_LIST}. "
+ "Based on the provided Java class content and the line of code: \"" + lineText + "\", suggest relevant annotations that can be applied at the placeholder location represented by ${SUGGEST_ANNOTATION_LIST} in the Java Class. "
+ "Return a JSON array where each element is a single annotation formatted as a string. "
+ "Ensure that the suggestions are appropriate for the given Java Class Content:\n\n" + classContent;
public List<Snippet> suggestAnnotations(String classDatas, String classContent, String lineText) {
String prompt = "You are an API server that suggests Java annotations for a specific context in a given Java class at the placeholder location ${SUGGEST_ANNOTATION_LIST}. "
+ "Based on the provided Java class content and the line of code: \"" + lineText + "\", suggest relevant annotations that can be applied at the placeholder location represented by ${SUGGEST_ANNOTATION_LIST} in the Java Class. "
+ "Return a JSON array with a few best suggestions without any additional text or explanation. Each element should be an object containing two fields: 'imports' and 'snippet'. "
+ "'imports' should be an array of required Java import statements (if no imports are required, return an empty array). "
+ "'snippet' should contain the suggested code as a text block, which may include multiple lines formatted as a single string using \\n for line breaks. "
+ "Make sure to escape any double quotes within the snippet using a backslash (\\) so that the JSON remains valid. \n\n"
+ "Ensure that the suggestions are appropriate for the given Java Class Content:\n\n" + classContent;

// Generate the list of suggested annotations
String jsonResponse = generate(prompt);
System.out.println("jsonResponse " + jsonResponse);

// Parse the JSON response into a List
List<String> annotations = parseJsonToList(jsonResponse);
List<Snippet> annotations = parseJsonToSnippets(jsonResponse);
return annotations;
}

Expand Down Expand Up @@ -554,6 +580,9 @@ private List<String> parseJsonToList(String json) {

private List<String> parseJsonToListWithSplit(String json) {
List<String> variableNames = new ArrayList<>();
if(json == null || json.isEmpty()){
return variableNames;
}

json = removeCodeBlockMarkers(json).trim();
String newjson = json;
Expand Down
Loading
Loading