Code Suggestions

Tier: Premium or Ultimate with GitLab Duo Pro Offering: GitLab.com, Self-managed, GitLab Dedicated
History
note
GitLab Duo Code Suggestions requires GitLab 16.8 and later. Earlier GitLab versions are not supported.

Write code more efficiently by using generative AI to suggest code while you’re developing.

With GitLab Duo Code Suggestions, you get:

  • Code completion, which suggests completions to the current line you are typing.
  • Code generation, which generates code based on a natural language code comment block. Write a comment like # Type more here, then press Enter to generate code based on the context of your comment and the rest of your code.

View a click-through demo.

Use Code Suggestions

Prerequisites:

To use Code Suggestions:

  1. Author your code. As you type, suggestions are displayed. Code Suggestions provide code snippets or complete the current line, depending on the cursor position.

  2. Describe the requirements in natural language. Code Suggestions generates functions and code snippets based on the context provided.

  3. To accept a suggestion, press Tab. To reject a suggestion, press Esc.
  4. To ignore a suggestion, keep typing as you usually would.

AI is non-deterministic, so you may not get the same suggestion every time with the same input. To generate quality code, write clear, descriptive, specific tasks.

Best practices

To get the best results from code generation:

  • Be as specific as possible while remaining concise.
  • State the outcome you want to generate (for example, a function) and provide details on what you want to achieve.
  • Add additional information, like the framework or library you want to use.
  • Add a space or new line after each comment. This space tells the code generator that you have completed your instructions.

For example, to create a Python web service with some specific requirements, you might write something like:

# Create a web service using Tornado that allows a user to log in, run a security scan, and review the scan results.
# Each action (log in, run a scan, and review results) should be its own resource in the web service
...

AI is non-deterministic, so you may not get the same suggestion every time with the same input. To generate quality code, write clear, descriptive, specific tasks.

Best practice examples

For use cases and best practices, follow the GitLab Duo examples documentation.

Response time

Code Suggestions is powered by a generative AI model.

Your personal access token enables a secure API connection to GitLab.com or to your GitLab instance. This API connection securely transmits a context window from your IDE/editor to the GitLab AI Gateway, a GitLab hosted service. The gateway calls the large language model APIs, and then the generated suggestion is transmitted back to your IDE/editor.

  • Code completion suggestions are usually low latency.
  • For code generation:
    • Algorithms or large code blocks might take more than 10 seconds to generate.
    • Streaming of code generation responses is supported in VS Code, leading to faster average response times. Other supported IDEs offer slower response times and will return the generated code in a single block.

Disable direct connections to the AI Gateway

History

Prerequisites:

  • You must be an administrator for the GitLab self-managed instance.

To minimize latency for code completion requests, these requests are sent from the IDE directly to the AI Gateway. For this direct connection to work, the IDE must be able to connect to https://cloud.gitlab.com:443. If this is not possible (for example, because of network restrictions), you can disable direct connections for all users. If you do this, code completion requests are sent indirectly through the GitLab self-managed instance, and might result in your requests having higher latency.

To disable direct connections to the gateway:

  1. On the left sidebar, at the bottom, select Admin Area.
  2. Select Settings > General.
  3. Expand AI-powered features.
  4. Select the Disable direct connections for code suggestions checkbox.

Inference window context

Code Suggestions inferences against the currently opened file, the content before and after the cursor, the filename, and the extension type. For more information on possible future context expansion to improve the quality of suggestions, see epic 11669.

Truncation of file content

Because of LLM limits and performance reasons, the content of the currently opened file is truncated:

  • For code completion: to 2048 tokens (roughly 8192 characters).
  • For code generation: to 142,856 tokens (roughly 500,000 characters).

Content above the cursor is prioritized over content below the cursor. The content above the cursor is truncated from the left side, and content below the cursor is truncated from the right side. These numbers represent the maximum input context size for Code Suggestions.

Output length

Because of LLM limits and for performance reasons, the output of Code Suggestions is limited:

  • For code completion: to 64 tokens (roughly 256 characters).
  • For code generation: to 2048 tokens (roughly 7168 characters).

Accuracy of results

We are continuing to work on the accuracy of overall generated content. However, Code Suggestions might generate suggestions that are:

  • Irrelevant.
  • Incomplete.
  • Results in failed pipelines.
  • Potentially insecure.
  • Offensive or insensitive.

When using Code Suggestions, code review best practice still applies.

Disable Code Suggestions

To disable Code Suggestions, disable the feature in your IDE editor extension. For details, see the documentation for your extension.

If you’d prefer, you can turn off GitLab Duo for a group, project, or instance.

Feedback

Provide feedback about your Code Suggestions experience in issue 435783.