Skip to content

Commit

Permalink
MARP-555: add system prompt for edit feature (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
Phạm Duy Linh authored Aug 1, 2024
1 parent 7f6ac97 commit cfa0445
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 77 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ lib/mvn-deps/
logs/
src_dataClasses/
src_wsproc/
openai-assistant/lib/
8 changes: 0 additions & 8 deletions openai-assistant/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@
<command commandId="openai.commands.assist" label="Insert" style="push">
<parameter name="chatGpt.question" value="insert" />
</command>
<command commandId="openai.commands.assist" label="Edit (beta)"
tooltip="beta: code-davinci-edit-001" style="push">
<parameter name="chatGpt.question" value="edit" />
</command>
<command commandId="openai.commands.assist" label="Chat" style="push">
<parameter name="chatGpt.question" value="chat" />
</command>
Expand All @@ -66,10 +62,6 @@
<command commandId="openai.commands.assist" label="Insert" style="push">
<parameter name="chatGpt.question" value="insert" />
</command>
<command commandId="openai.commands.assist" label="Edit (beta)"
tooltip="beta: code-davinci-edit-001" style="push">
<parameter name="chatGpt.question" value="edit" />
</command>
<command commandId="openai.commands.assist" label="Chat" style="push">
<parameter name="chatGpt.question" value="chat" />
</command>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
public class ChatGptRequest {

private final Supplier<WebTarget> client;
private final String SYSTEM_PROMPT = """
You are an assistant that updates code based on user instructions. Your task is to modify the provided code snippet according to the user's requirements, ensuring that the new elements are correctly integrated into the existing structure. The result should be plain text, not in markdown format.
Instructions:
Understand the User's Code: Analyze the provided code to understand its structure and existing elements.
Apply the User's Request: Make the required changes to the code based on the user's request. Ensure that new components are integrated correctly into the existing layout.
Provide the Complete Updated Code: Return the entire code snippet, including the updated or newly added components. Ensure that the code is well-formatted and integrates seamlessly with the existing content.
""";
private int maxTokens = 1024;

public ChatGptRequest(Supplier<WebTarget> client) {
Expand All @@ -36,17 +43,19 @@ public String getModels() {

public String ask(String context, String question) {
WebTarget chat = client.get().path("chat/completions");
ObjectNode message = JsonNodeFactory.instance.objectNode();
ArrayNode arrayNode = JsonNodeFactory.instance.arrayNode();
message.put("role", "user");
message.put("content", context + "\n\n"+question);
arrayNode.add(message);
arrayNode.add(message("system", SYSTEM_PROMPT));
arrayNode.add(message("user", String.format("%s \n\n %s", context, question)));
ObjectNode request = completion().set("messages", arrayNode);
var payload = Entity.entity(request, MediaType.APPLICATION_JSON);
Response resp = chat.request().post(payload);
return read(resp);
}

private ObjectNode message(String role, String content) {
return JsonNodeFactory.instance.objectNode().put("role", role).put("content", content);
}

private String read(Response resp) {
JsonNode result = resp.readEntity(JsonNode.class);
if (resp.getStatusInfo().getFamily() == Family.SUCCESSFUL) {
Expand All @@ -61,9 +70,8 @@ private String read(Response resp) {
}

private ObjectNode completion() {
OpenAiConfig repo = new OpenAiConfig();
ObjectNode request = JsonNodeFactory.instance.objectNode();
request.put("model", repo.getValue(Key.MODEL).orElse("gpt-3.5-turbo"));
request.put("model", model());
request.put("max_tokens", maxTokens);
request.put("temperature", 1);
request.put("top_p", 1);
Expand All @@ -72,22 +80,9 @@ private ObjectNode completion() {
return request;
}

public String edit(String code, String instruction) {
WebTarget edits = client.get().path("edits");
ObjectNode request = edit()
.put("input", code)
.put("instruction", instruction);
var payload = Entity.entity(request, MediaType.APPLICATION_JSON);
Response resp = edits.request().post(payload);
return read(resp);
}

private ObjectNode edit() {
ObjectNode request = JsonNodeFactory.instance.objectNode();
request.put("model", "code-davinci-edit-001");
request.put("temperature", 1);
request.put("top_p", 1);
return request;
private String model() {
OpenAiConfig repo = new OpenAiConfig();
return repo.getValue(Key.MODEL).orElse("gpt-3.5-turbo");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public interface Quests {
String FIX = "fix";
String EXPLAIN = "explain";
String INSERT = "insert";
String EDIT = "edit";
String CHAT = "chat";
String KEY = "apiKey";
String MODEL = "model";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public Map getParameterValues() {
"Explain", Quests.EXPLAIN,
"Fix", Quests.FIX,
"Insert", Quests.INSERT,
"Edit (beta)", Quests.EDIT,
"Chat", Quests.CHAT,
"Api Key", Quests.KEY,
"API Model", Quests.MODEL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,6 @@ public void run() {
var chatGpt = new ChatGptRequest(()->new ChatGptClientFactory().chatGptClient());
repo.getIntValue(Key.MAX_TOKENS).ifPresent(chatGpt::maxTokens);

if (quest.equalsIgnoreCase(Quests.EDIT)) {
String insert = SwtCommonDialogs.openInputDialog(site.getShell(), "any wishes?", "what can Chat GPT do for you?",
"insert a combobox to pick a brand out of: Mercedes, BMW or Tesla");
if (insert != null) {
var response = runWithProgress(()->chatGpt.edit(what, insert));
diffResult(response);
}
return;
}
if (quest.equalsIgnoreCase(Quests.INSERT)) {
String insert = SwtCommonDialogs.openInputDialog(site.getShell(), "any wishes?", "what can Chat GPT do for you?",
"insert a combobox to pick a brand out of: Mercedes, BMW or Tesla");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@ public class MockAI {
"assist-insert", json(load("assist-insert.json")),
"assist-insert-reponse", json(load("assist-insert-response.json")),
"assist-chat", json(load("assist-chat.json")),
"assist-chat-reponse", json(load("assist-chat-response.json")),

// codex beta
"assist-edit", json(load("assist-edit.json")),
"assist-edit-reponse", json(load("assist-edit-response.json"))
"assist-chat-reponse", json(load("assist-chat-response.json"))
);

private final Map<String, JsonNode> openAIExamples = Map.of(
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,6 @@ void insert() {
.contains("<p:selectOneMenu id=\\\"selectBrand\\\"");
}

@Test
void edit_codexBeta() {
JsonNode edit = json(load("assist-edit.json"));
JsonNode result = assist(edit);
assertThat(result.toPrettyString())
.as("writes a complex selectOne for the user")
.contains("<p:selectOneMenu");
}

@Test
void chat() {
JsonNode chat = json(load("assist-chat.json"));
Expand Down

0 comments on commit cfa0445

Please sign in to comment.