Skip to content

Support streaming from Anthropic requests

Tan Le requested to merge stream-anthropic into main

What does this merge request do and why?

Add streaming support for code completions and generations with Anthorpic models.

How to set up and validate locally

  1. Check out to this merge request's branch.
  2. Ensure a local Docker image built successfully.
    docker buildx build --platform linux/amd64 \
      -t code-suggestions-api:dev .
  3. Run a local service on Docker.
    docker run --platform linux/amd64 --rm \
      -p 5052:5052 \
      -e AUTH_BYPASS_EXTERNAL=true \
      -v $PWD:/app -it code-suggestions-api:dev

Send a cURL request with stream: true, confirm the response is in plaintext and streamed.

Code Completions
curl --request POST \
  --no-buffer
  --url http://codesuggestions.gdk.test:5999/v2/completions \
  --header 'Content-Type: application/json' \
  --header 'X-Gitlab-Authentication-Type: oidc' \
  --data '{
   "prompt_version": 2,
   "project_path": "awesome_project",
   "project_id": 23,
   "current_file": {
     "file_name": "main.py",
     "content_above_cursor": "class Calculator:\n    @staticmethod\n    def add(a, b):\n",
     "content_below_cursor": "\n@staticmethod       \ndef substract(a, b):\n    return a - b    \n                    \n@staticmethod       \ndef multiply(a, b): \n    return a * b\n"
   },
   "stream": true
}'
Code Generations
curl --request POST \
   --no-buffer
   --url http://codesuggestions.gdk.test:5999/v2/code/generations \
   --header 'Content-Type: application/json' \
   --header 'X-Gitlab-Authentication-Type: oidc' \
   --data '{
    "current_file": {
      "file_name": "world.py",
      "content_above_cursor": "",
      "content_below_cursor": ""
    },
    "prompt_version": 2,
    "prompt": "Human: Write a wonderful Python function to generate prime numbers. Take a deep breath and work on this problem step-by-step.\nAssistant:",
    "model_provider": "anthropic",
    "stream": true
  }' 

Send a cURL request without stream, confirm the response is in json and not streamed.

Code Completions
curl --request POST \
  --url http://codesuggestions.gdk.test:5999/v2/completions \
  --header 'Content-Type: application/json' \
  --header 'X-Gitlab-Authentication-Type: oidc' \
  --data '{
   "prompt_version": 2,
   "project_path": "awesome_project",
   "project_id": 23,
   "current_file": {
     "file_name": "main.py",
     "content_above_cursor": "class Calculator:\n    @staticmethod\n    def add(a, b):\n",
     "content_below_cursor": "\n@staticmethod       \ndef substract(a, b):\n    return a - b    \n                    \n@staticmethod       \ndef multiply(a, b): \n    return a * b\n"
   }
}'
Code Generations
curl --request POST \
   --url http://codesuggestions.gdk.test:5999/v2/code/generations \
   --header 'Content-Type: application/json' \
   --header 'X-Gitlab-Authentication-Type: oidc' \
   --data '{
    "current_file": {
      "file_name": "world.py",
      "content_above_cursor": "",
      "content_below_cursor": ""
    },
    "prompt_version": 2,
    "prompt": "Human: Write a wonderful Python function to generate prime numbers. Take a deep breath and work on this problem step-by-step.\nAssistant:",
    "model_provider": "anthropic"
  }' 

Merge request checklist

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

Relates to #341 (closed)

Edited by Tan Le

Merge request reports