Skip to content

feat: add multiple choices for v3 code completions

Jan Provaznik requested to merge jp-v3-choices into main

What does this merge request do and why?

Updates v3 completions endpoint:

  • accepts choices_count parameter (similar to v2 completions)
  • changes response structure - an array of choices is returned instead of just response (text attribute) - this is a breaking change, but this endpoint is not used yet

Related to #490 (closed)

How to set up and validate locally

Because v3 is not used yet, the simplest way is to enable direct access to this endpoint and then test it with direct connection request:

  • enable direct access:
diff --git a/ai_gateway/api/v3/code/completions.py b/ai_gateway/api/v3/code/completions.py
index 802614c8..a73a93a7 100644
--- a/ai_gateway/api/v3/code/completions.py
+++ b/ai_gateway/api/v3/code/completions.py
@@ -50,7 +50,7 @@ async def completions(
 ):
     if not current_user.can(
         GitLabUnitPrimitive.CODE_SUGGESTIONS,
-        disallowed_issuers=[SELF_SIGNED_TOKEN_ISSUER],
+        #disallowed_issuers=[SELF_SIGNED_TOKEN_ISSUER],
     ):
         raise HTTPException(
             status_code=status.HTTP_403_FORBIDDEN,
  • test with direct completion request:
USER_TOKEN=$(curl --request POST --url http://192.168.1.8:3000/api/v4/code_suggestions/direct_access --header "Authorization: Bearer $TOKEN" | jq ".token" | sed 's/"//g')

curl --header "PRIVATE-TOKEN: $USER_TOKEN" \
-H "Content-Type: application/json" \
-H "X-Gitlab-Authentication-Type: oidc" \
-H "Authorization: Bearer $USER_TOKEN" \
-H "X-Gitlab-Host-Name: 192.168.1.8" \
-H "X-Gitlab-Instance-Id: 292c3c7c-c5d5-48ec-b4bf-f00b724ce560" \
-H "X-Gitlab-Realm: self-managed" \
-H "X-Gitlab-Version: 17.2.0" \
-H "X-Gitlab-Global-User-Id: Df0Jhs9xlbetQR8YoZCKDZJflhxO0ZBI8uoRzmpnd1w=" \
--data-raw '{
  "prompt_components": [
    {
      "type": "code_editor_completion",
      "payload": {
        "choices_count": 3,
        "file_name": "test.rb",
        "content_above_cursor": "def user\n  User.find",
        "content_below_cursor": "",
        "stream": false
      },
      "metadata": {
        "source": "Gitlab EE",
        "version": "16.3"
      }
    }
  ]
}' \
--request POST http://192.168.1.8:5052/v3/code/completions
  • and with generation request (for which choices_count is ignored):
#USER_TOKEN=$(curl --request POST --url http://192.168.1.8:3000/api/v4/code_suggestions/direct_access --header "Authorization: Bearer $TOKEN" | jq ".token" | sed 's/"//g')

curl --header "PRIVATE-TOKEN: $USER_TOKEN" \
-H "Content-Type: application/json" \
-H "X-Gitlab-Authentication-Type: oidc" \
-H "Authorization: Bearer $USER_TOKEN" \
-H "X-Gitlab-Host-Name: 192.168.1.8" \
-H "X-Gitlab-Instance-Id: 292c3c7c-c5d5-48ec-b4bf-f00b724ce560" \
-H "X-Gitlab-Realm: self-managed" \
-H "X-Gitlab-Version: 17.2.0" \
-H "X-Gitlab-Global-User-Id: Df0Jhs9xlbetQR8YoZCKDZJflhxO0ZBI8uoRzmpnd1w=" \
--data-raw '{
  "choices_count": 3,
  "prompt_components": [
    {
      "type": "code_editor_generation",
      "payload": {
        "file_name": "test.rb",
        "content_above_cursor": "def user\n  User.find",
        "content_below_cursor": "",
        "stream": false
      },
      "metadata": {
        "source": "Gitlab EE",
        "version": "16.3"
      }
    }
  ]
}' \
--request POST http://192.168.1.8:5052/v3/code/completions

Merge request checklist

  • Tests added for new functionality. If not, please raise an issue to follow up.
  • Documentation added/updated, if needed.
Edited by Jan Provaznik

Merge request reports

Loading