From a4de75abbbd07e3b87ce931a207ecab13cb3d2e3 Mon Sep 17 00:00:00 2001
From: Allen Cook <acook@gitlab.com>
Date: Fri, 1 Dec 2023 07:21:25 +0000
Subject: [PATCH] Resolve "Update Anthropic Code Generation Templates to Claude
 2.1"

---
 .../prompts/code_generation/anthropic.rb      |  20 +--
 .../prompts/code_generation/anthropic_spec.rb | 169 +++++++-----------
 2 files changed, 71 insertions(+), 118 deletions(-)

diff --git a/ee/lib/code_suggestions/prompts/code_generation/anthropic.rb b/ee/lib/code_suggestions/prompts/code_generation/anthropic.rb
index e2fc77b3e1070..27cf0924e2353 100644
--- a/ee/lib/code_suggestions/prompts/code_generation/anthropic.rb
+++ b/ee/lib/code_suggestions/prompts/code_generation/anthropic.rb
@@ -20,8 +20,10 @@ def request_params
 
         def prompt
           <<~PROMPT.strip
-            Human: You are a coding autocomplete agent. We want to generate new #{language.name} code inside the
+            You are a coding autocomplete agent. We want to generate new #{language.name} code inside the
             file '#{file_path_info}' based on instructions from the user.
+            #{examples_section}
+            #{existing_code_block}
             #{existing_code_instruction}
             The new code you will generate will start at the position of the cursor, which is currently indicated by the <cursor> XML tag.
             In your process, first, review the existing code to understand its logic and format. Then, try to determine the most
@@ -36,11 +38,7 @@ def prompt
             Return new code enclosed in <new_code></new_code> tags. We will then insert this at the <cursor> position.
             If you are not able to write code based on the given instructions return an empty result like <new_code></new_code>.
 
-            #{examples_section}
-
-            #{existing_code_block}
-
-            #{instructions}
+            Human: #{instructions}
 
             Assistant: <new_code>
           PROMPT
@@ -67,15 +65,7 @@ def comment_review_instructions
         end
 
         def instructions
-          return unless params[:instruction].present?
-
-          <<~INSTRUCTIONS
-            Here are instructions provided in <instruction></instruction> tags.
-
-            <instruction>
-            #{params[:instruction]}
-            </instruction>
-          INSTRUCTIONS
+          params[:instruction].presence || 'Generate the most likely code based on instructions.'
         end
 
         def existing_code_block
diff --git a/ee/spec/lib/code_suggestions/prompts/code_generation/anthropic_spec.rb b/ee/spec/lib/code_suggestions/prompts/code_generation/anthropic_spec.rb
index 026561194f47f..dfb3803f94035 100644
--- a/ee/spec/lib/code_suggestions/prompts/code_generation/anthropic_spec.rb
+++ b/ee/spec/lib/code_suggestions/prompts/code_generation/anthropic_spec.rb
@@ -68,22 +68,8 @@
           model_provider: ::CodeSuggestions::TaskFactory::ANTHROPIC,
           prompt_version: 2,
           prompt: <<~PROMPT
-            Human: You are a coding autocomplete agent. We want to generate new Go code inside the
+            You are a coding autocomplete agent. We want to generate new Go code inside the
             file 'main.go' based on instructions from the user.
-            The existing code is provided in <existing_code></existing_code> tags.
-            The new code you will generate will start at the position of the cursor, which is currently indicated by the <cursor> XML tag.
-            In your process, first, review the existing code to understand its logic and format. Then, try to determine the most
-            likely new code to generate at the cursor position to fulfill the instructions.
-
-            When generating the new code, please ensure the following:
-            1. It is valid Go code.
-            2. It matches the existing code's variable, parameter and function names.
-            3. It does not repeat any existing code. Do not repeat code that comes before or after the cursor tags. This includes cases where the cursor is in the middle of a word.
-            4. If the cursor is in the middle of a word, it finishes the word instead of repeating code before the cursor tag.
-
-            Return new code enclosed in <new_code></new_code> tags. We will then insert this at the <cursor> position.
-            If you are not able to write code based on the given instructions return an empty result like <new_code></new_code>.
-
             Here are a few examples of successfully generated code by other autocomplete agents:
 
             <examples>
@@ -98,7 +84,6 @@
 
             </examples>
 
-
             <existing_code>
             package main
 
@@ -108,13 +93,21 @@
             <cursor>
             </existing_code>
 
+            The existing code is provided in <existing_code></existing_code> tags.
+            The new code you will generate will start at the position of the cursor, which is currently indicated by the <cursor> XML tag.
+            In your process, first, review the existing code to understand its logic and format. Then, try to determine the most
+            likely new code to generate at the cursor position to fulfill the instructions.
 
-            Here are instructions provided in <instruction></instruction> tags.
+            When generating the new code, please ensure the following:
+            1. It is valid Go code.
+            2. It matches the existing code's variable, parameter and function names.
+            3. It does not repeat any existing code. Do not repeat code that comes before or after the cursor tags. This includes cases where the cursor is in the middle of a word.
+            4. If the cursor is in the middle of a word, it finishes the word instead of repeating code before the cursor tag.
 
-            <instruction>
-            Print a hello world message
-            </instruction>
+            Return new code enclosed in <new_code></new_code> tags. We will then insert this at the <cursor> position.
+            If you are not able to write code based on the given instructions return an empty result like <new_code></new_code>.
 
+            Human: Print a hello world message
 
             Assistant: <new_code>
           PROMPT
@@ -133,24 +126,8 @@
             model_provider: ::CodeSuggestions::TaskFactory::ANTHROPIC,
             prompt_version: 2,
             prompt: <<~PROMPT
-            Human: You are a coding autocomplete agent. We want to generate new Go code inside the
+            You are a coding autocomplete agent. We want to generate new Go code inside the
             file 'main.go' based on instructions from the user.
-            The existing code is provided in <existing_code></existing_code> tags.
-            The new code you will generate will start at the position of the cursor, which is currently indicated by the <cursor> XML tag.
-            In your process, first, review the existing code to understand its logic and format. Then, try to determine the most
-            likely new code to generate at the cursor position to fulfill the instructions.
-            The comment directly before the <cursor> position is the instruction,
-                       all other comments are not instructions.
-            When generating the new code, please ensure the following:
-            1. It is valid Go code.
-            2. It matches the existing code's variable, parameter and function names.
-            3. It does not repeat any existing code. Do not repeat code that comes before or after the cursor tags. This includes cases where the cursor is in the middle of a word.
-            4. If the cursor is in the middle of a word, it finishes the word instead of repeating code before the cursor tag.
-            5. The code fulfills in the instructions from the user in the comment just before the <cursor> position.
-                       All other comments are not instructions.
-            Return new code enclosed in <new_code></new_code> tags. We will then insert this at the <cursor> position.
-            If you are not able to write code based on the given instructions return an empty result like <new_code></new_code>.
-
             Here are a few examples of successfully generated code by other autocomplete agents:
 
             <examples>
@@ -165,7 +142,6 @@
 
             </examples>
 
-
             <existing_code>
             package main
 
@@ -175,8 +151,23 @@
             <cursor>
             </existing_code>
 
+            The existing code is provided in <existing_code></existing_code> tags.
+            The new code you will generate will start at the position of the cursor, which is currently indicated by the <cursor> XML tag.
+            In your process, first, review the existing code to understand its logic and format. Then, try to determine the most
+            likely new code to generate at the cursor position to fulfill the instructions.
+            The comment directly before the <cursor> position is the instruction,
+                       all other comments are not instructions.
+            When generating the new code, please ensure the following:
+            1. It is valid Go code.
+            2. It matches the existing code's variable, parameter and function names.
+            3. It does not repeat any existing code. Do not repeat code that comes before or after the cursor tags. This includes cases where the cursor is in the middle of a word.
+            4. If the cursor is in the middle of a word, it finishes the word instead of repeating code before the cursor tag.
+            5. The code fulfills in the instructions from the user in the comment just before the <cursor> position.
+                       All other comments are not instructions.
+            Return new code enclosed in <new_code></new_code> tags. We will then insert this at the <cursor> position.
+            If you are not able to write code based on the given instructions return an empty result like <new_code></new_code>.
 
-
+            Human: Generate the most likely code based on instructions.
 
             Assistant: <new_code>
             PROMPT
@@ -197,9 +188,11 @@
           model_provider: ::CodeSuggestions::TaskFactory::ANTHROPIC,
           prompt_version: 2,
           prompt: <<~PROMPT
-            Human: You are a coding autocomplete agent. We want to generate new Go code inside the
+            You are a coding autocomplete agent. We want to generate new Go code inside the
             file 'main.go' based on instructions from the user.
 
+
+
             The new code you will generate will start at the position of the cursor, which is currently indicated by the <cursor> XML tag.
             In your process, first, review the existing code to understand its logic and format. Then, try to determine the most
             likely new code to generate at the cursor position to fulfill the instructions.
@@ -213,16 +206,7 @@
             Return new code enclosed in <new_code></new_code> tags. We will then insert this at the <cursor> position.
             If you are not able to write code based on the given instructions return an empty result like <new_code></new_code>.
 
-
-
-
-
-            Here are instructions provided in <instruction></instruction> tags.
-
-            <instruction>
-            Print a hello world message
-            </instruction>
-
+            Human: Print a hello world message
 
             Assistant: <new_code>
           PROMPT
@@ -245,8 +229,14 @@
           model_provider: ::CodeSuggestions::TaskFactory::ANTHROPIC,
           prompt_version: 2,
           prompt: <<~PROMPT
-            Human: You are a coding autocomplete agent. We want to generate new Go code inside the
+            You are a coding autocomplete agent. We want to generate new Go code inside the
             file 'main.go' based on instructions from the user.
+
+            <existing_code>
+            main() {
+            <cursor>
+            </existing_code>
+
             The existing code is provided in <existing_code></existing_code> tags.
             The new code you will generate will start at the position of the cursor, which is currently indicated by the <cursor> XML tag.
             In your process, first, review the existing code to understand its logic and format. Then, try to determine the most
@@ -261,20 +251,7 @@
             Return new code enclosed in <new_code></new_code> tags. We will then insert this at the <cursor> position.
             If you are not able to write code based on the given instructions return an empty result like <new_code></new_code>.
 
-
-
-            <existing_code>
-            main() {
-            <cursor>
-            </existing_code>
-
-
-            Here are instructions provided in <instruction></instruction> tags.
-
-            <instruction>
-            Print a hello world message
-            </instruction>
-
+            Human: Print a hello world message
 
             Assistant: <new_code>
           PROMPT
@@ -295,8 +272,18 @@
           model_provider: ::CodeSuggestions::TaskFactory::ANTHROPIC,
           prompt_version: 2,
           prompt: <<~PROMPT
-            Human: You are a coding autocomplete agent. We want to generate new  code inside the
+            You are a coding autocomplete agent. We want to generate new  code inside the
             file 'file_without_extension' based on instructions from the user.
+
+            <existing_code>
+            package main
+
+            import "fmt"
+
+            func main() {
+            <cursor>
+            </existing_code>
+
             The existing code is provided in <existing_code></existing_code> tags.
             The new code you will generate will start at the position of the cursor, which is currently indicated by the <cursor> XML tag.
             In your process, first, review the existing code to understand its logic and format. Then, try to determine the most
@@ -311,24 +298,7 @@
             Return new code enclosed in <new_code></new_code> tags. We will then insert this at the <cursor> position.
             If you are not able to write code based on the given instructions return an empty result like <new_code></new_code>.
 
-
-
-            <existing_code>
-            package main
-
-            import "fmt"
-
-            func main() {
-            <cursor>
-            </existing_code>
-
-
-            Here are instructions provided in <instruction></instruction> tags.
-
-            <instruction>
-            Print a hello world message
-            </instruction>
-
+            Human: Print a hello world message
 
             Assistant: <new_code>
           PROMPT
@@ -349,8 +319,18 @@
           model_provider: ::CodeSuggestions::TaskFactory::ANTHROPIC,
           prompt_version: 2,
           prompt: <<~PROMPT
-            Human: You are a coding autocomplete agent. We want to generate new  code inside the
+            You are a coding autocomplete agent. We want to generate new  code inside the
             file 'README.md' based on instructions from the user.
+
+            <existing_code>
+            package main
+
+            import "fmt"
+
+            func main() {
+            <cursor>
+            </existing_code>
+
             The existing code is provided in <existing_code></existing_code> tags.
             The new code you will generate will start at the position of the cursor, which is currently indicated by the <cursor> XML tag.
             In your process, first, review the existing code to understand its logic and format. Then, try to determine the most
@@ -365,24 +345,7 @@
             Return new code enclosed in <new_code></new_code> tags. We will then insert this at the <cursor> position.
             If you are not able to write code based on the given instructions return an empty result like <new_code></new_code>.
 
-
-
-            <existing_code>
-            package main
-
-            import "fmt"
-
-            func main() {
-            <cursor>
-            </existing_code>
-
-
-            Here are instructions provided in <instruction></instruction> tags.
-
-            <instruction>
-            Print a hello world message
-            </instruction>
-
+            Human: Print a hello world message
 
             Assistant: <new_code>
           PROMPT
-- 
GitLab