Skip to content

Introduce suggestions /v2 route

Tan Le requested to merge suggestions-v2 into main

What does this MR do and why?

This MR adds a new route to support new prompt input as per the related issue.

POST /v2/completions
Attribute Type Required Description Example
prompt_version int no The version of the prompt 1
repository_name string yes The name of the repository (max_len: 255) gitlab-shell
current_file.file_name string yes The name of the current file (max_len: 255) README.md
current_file.content_above_cursor string yes The content above cursor (max_len: 100,000) import numpy as np
current_file.content_below_cursor string yes The content below cursor (max_len: 100,000) def __main__:\n

Example request:

curl --silent \
     --request POST \
     --url http://codesuggestions.gitlab.com/v2/completions \
     --header 'authorization: Bearer glpat-.....' \
     --header 'Content-Type: application/json' \
     --data '{
       "prompt_version": 1,
       "repository_name": "awesome_project",
       "current_file": {
         "file_name": "main.py",
         "content_above_cursor": "\"\"\"\nImplement fastapi middleware to log all incoming requests\"\"\"\n",
         "content_below_cursor": "robin"
       }
     }'

Example response:

{
  "id": "id",
  "model": "codegen",
  "object": "text_completion",
  "created": 1678282416,
  "choices": [
    {
      "text": "\nimport logging\n\nfrom fastapi import FastAPI\nfrom fastapi.",
      "index": 0,
      "finish_reason": "length"
    }
  ]
}
  • Additionally, this endpoint will:
    • strip whitespace on some fields.
    • validate maximum lengths.
    • return validation errors if validation fails.

How to set up and validate locally

  1. Check out this branch
  2. Build a local Docker image
    docker buildx build -t code-suggestions-api:dev .
  3. Run the recently build docker image, map to port 5999 on localhost
    docker run --rm -p 5999:5000 -e BYPASSS_EXTERNAL_AUTH=true -it code-suggestions-api:dev
  4. Use your favourite REST client or curl to hit the endpoint (hint: use jq to parse JSON response)
    curl --silent \
      --request POST \
      --url http://localhost:5999/v2/completions \
      --header 'Content-Type: application/json' \
      --data '{
        "prompt_version": 1,
        "repository_name": "awesome_project",
        "current_file": {
          "file_name": "main.py",
          "content_above_cursor": "\"\"\"\nImplement fastapi middleware to log all incoming requests\"\"\"\n",
          "content_below_cursor": "robin"
        }
      }' | jq .
    
    {
      "id": "id",
      "model": "codegen",
      "object": "text_completion",
      "created": 1678282416,
      "choices": [
        {
          "text": "\nimport logging\n\nfrom fastapi import FastAPI\nfrom fastapi.",
          "index": 0,
          "finish_reason": "length"
        }
      ]
    }
Edited by Tan Le

Merge request reports