- Use Code Suggestions
- View multiple code suggestions
- Code completion and generation
- Advanced Context
- Truncation of file content
- Output length
- Accuracy of results
- Response time
- Feedback
Code Suggestions
Qwen2.5 7B
and Vertex AI Codey code-gecko
. For code generation, Anthropic Claude 3.5 Sonnet.- Introduced support for Google Vertex AI Codey APIs in GitLab 16.1.
- Removed support for GitLab native model in GitLab 16.2.
- Introduced support for Code Generation in GitLab 16.3.
- Generally available in GitLab 16.7.
- Subscription changed to require GitLab Duo Pro on February 15, 2024.
- Changed to require GitLab Duo add-on in GitLab 17.6 and later.
-
Introduced support for Fireworks AI-hosted Qwen2.5 code completion model in GitLab 17.6, with a flag named
fireworks_qwen_code_completion
.
Use GitLab Duo Code Suggestions to write code more efficiently by using generative AI to suggest code while you’re developing.
Before you start using Code Suggestions, decide if you want to use the default GitLab-hosted LLM to manage Code Suggestions requests, or deploy a self-hosted model. Self-hosted models maximize security and privacy by making sure nothing is sent to an external model.
Use Code Suggestions
Prerequisites:
- You must have set up Code Suggestions.
To use Code Suggestions:
- Open your Git project in a supported IDE.
- Add the project as a remote of your local repository using
git remote add
. - Add your project directory, including the hidden
.git/
folder, to your IDE workspace or project. -
Author your code. As you type, suggestions are displayed. Code Suggestions provides code snippets or completes the current line, depending on the cursor position.
-
Describe the requirements in natural language. Code Suggestions generates functions and code snippets based on the context provided.
- When you receive a suggestion, you can do any of the following:
- To accept a suggestion, press Tab.
- To accept a partial suggestion, press either Control+Right arrow or Command+Right arrow.
- To reject a suggestion, press Esc.
- To ignore a suggestion, keep typing as you usually would.
View multiple code suggestions
- Introduced in GitLab 17.1.
For a code completion suggestion in VS Code, multiple suggestion options might be available. To view all available suggestions:
- Hover over the code completion suggestion.
- Scroll through the alternatives. Either:
- Use keyboard shortcuts:
- On a Mac, press Option + ] to view the next suggestion, and Option + [ to view the previous suggestions.
- On Windows, press Alt + ] to view the next suggestion, and Alt + [ to view the previous suggestions.
- On the dialog that’s displayed, select the right or left arrow to see next or previous options.
- Use keyboard shortcuts:
- Press Tab to apply the suggestion you prefer.
Code completion and generation
Code Suggestions uses code completion and code generation:
Code completion | Code generation | |
---|---|---|
Purpose | Provides suggestions for completing the current line of code. | Generates new code based on a natural language comment. |
Trigger | Triggers when typing, usually with a short delay. | Triggers when pressing Enter after writing a comment that includes specific keywords. |
Scope | Limited to the current line or small block of code. | Can generate entire methods, functions, or even classes based on the context. |
Accuracy | More accurate for small tasks and short blocks of code. | Is more accurate for complex tasks and large blocks of code because a bigger large language model (LLM) is used, additional context is sent in the request (for example, the libraries used by the project), and your instructions are passed to the LLM. |
How to use | Code completion automatically suggests completions to the line you are typing. | You write a comment and press Enter, or you enter an empty function or method. |
When to use | Use code completion to quickly complete one or a few lines of code. | Use code generation for more complex tasks, larger codebases, when you want to write new code from scratch based on a natural language description, or when the file you’re editing has fewer than five lines of code. |
Code Suggestions always uses both of these features. You cannot use only code generation or only code completion.
View a code completion vs. code generation comparison demo.
Best practices for code generation
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.
- In GitLab 17.2 and later, when the
advanced_context_resolver
andcode_suggestions_context
feature flags are enabled, open related files in other tabs to expand the inference window context.
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 sign in, run a security scan, and review the scan results.
# Each action (sign 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.
For use cases and best practices, follow the GitLab Duo examples documentation.
Advanced Context
When using Code Suggestions, the Advanced Context feature searches in the background to find and include relevant context from across a user’s repository.
For more information, see the Advanced Context Resolver project epic.
Advanced Context supported languages
The Advanced Context feature supports these languages:
- Code completion: all configured languages.
- Code generation:
- Go
- Java
- JavaScript
- Kotlin
- Python
- Ruby
- Rust
- TypeScript (
.ts
and.tsx
files) - Vue
- YAML
Open tabs as context
-
Introduced in GitLab 17.1 with a flag named
advanced_context_resolver
. Disabled by default. -
Introduced in GitLab 17.1 with a flag named
code_suggestions_context
. Disabled by default. - Introduced in GitLab Workflow for VS Code 4.20.0.
- Introduced in GitLab Duo for JetBrains 2.7.0.
- Added to the GitLab Neovim plugin on July 16, 2024.
- Feature flags
advanced_context_resolver
andcode_suggestions_context
enabled on GitLab.com in GitLab 17.2. - Feature flags
advanced_context_resolver
andcode_suggestions_context
enabled on self-managed in GitLab 17.4.
To get more accurate and relevant results from Code Suggestions and code generation, you can use the contents of the files open in tabs in your IDE. Similar to prompt engineering, these files give GitLab Duo more information about the standards and practices in your code project.
Open tabs as context is part of the Advanced Context feature.
Enable open tabs as context
By default, Code Suggestions uses the open files in your IDE for context when making suggestions.
Prerequisites:
- You must have GitLab 17.2 or later. Earlier GitLab versions that support Code Suggestions cannot weigh the content of open tabs more heavily than other files in your project.
- GitLab Duo Code Suggestions must be enabled for your project.
- Use a supported code language:
- Code completion: All configured languages.
- Code generation: Go, Java, JavaScript, Kotlin, Python, Ruby, Rust, TypeScript (
.ts
and.tsx
files), Vue, and YAML.
- For Visual Studio Code, you must have GitLab Workflow extension version 4.14.2 or later.
To confirm that open tabs are used as context:
- On the top bar, go to Code > Settings > Extensions.
- Search for GitLab Workflow in the list, and select the gear icon.
- Select Extension Settings.
- In your User settings, under GitLab › Duo Code Suggestions: Open Tabs Context, select Use the contents of open tabs as context.
- Go to your IDE’s top menu bar and select Settings.
- On the left sidebar, expand Tools, then select GitLab Duo.
- Expand GitLab Language Server.
- Under Code Completion, select Send open tabs as context.
- Select OK or Save.
Use open tabs as context
Open the files you want to provide for context:
- Open tabs uses the most recently opened or changed files.
- If you do not want a file used as additional context, close that file.
When you start working in a file, GitLab Duo uses your open files as extra context, within truncation limits.
You can adjust your code generation results by adding code comments to your file
that explain what you want to build. Code generation treats your code comments
like chat. Your code comments update the user_instruction
, and then improve
the next results you receive.
To learn about the code that builds the prompt, see these files:
-
Code generation:
ee/lib/api/code_suggestions.rb
in thegitlab
repository. -
Code completion:
ai_gateway/code_suggestions/processing/completions.py
in themodelops
repository.
Inference window context
- Generally available in GitLab 16.8.
-
Introduced open tabs context in GitLab 17.2 with flags named
advanced_context_resolver
andcode_suggestions_context
. Disabled by default.
Code Suggestions inferences against:
- The currently opened file.
- The content before and after the cursor.
- The filename and extension.
- In GitLab 17.2 and later when the
advanced_context_resolver
andcode_suggestions_context
feature flags are enabled.- Files opened in other tabs.
- User instructions.
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:
- In GitLab 17.5 and earlier, to 2,048 tokens (roughly 8,192 characters).
- In GitLab 17.6 and later, to 32,000 tokens (roughly 128,000 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.
Response time
Code Suggestions is powered by a generative AI model.
- For code completion, suggestions are usually low latency and take less than one second.
- For code generation, algorithms or large code blocks might take more than five seconds to generate.
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.
Streaming
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.
Direct and indirect connections
-
Introduced in GitLab 17.2 with a flag named
code_suggestions_direct_access
. Disabled by default.
By default, code completion requests are sent from the IDE directly to the AI gateway to minimize the latency.
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, which in turn sends the requests
to the AI gateway. This might result in your requests having higher latency.
Configure direct or indirect connections
Prerequisites:
- You must be an administrator for the GitLab self-managed instance.
- On the left sidebar, at the bottom, select Admin.
- Select Settings > General.
- Expand GitLab Duo features.
- Under Connection method, choose an option:
- To minimize latency for code completion requests, select Direct connections.
- To disable direct connections for all users, select Indirect connections through the GitLab self-managed instance.
- Select Save changes.
- On the left sidebar, at the bottom, select Admin.
- Select Settings > General.
- Expand AI-powered features.
- Choose an option:
- To enable direct connections and minimize latency for code completion requests, clear the Disable direct connections for code suggestions checkbox.
- To disable direct connections, select the Disable direct connections for code suggestions checkbox.
Feedback
Provide feedback about your Code Suggestions experience in issue 435783.