Blog AI/ML 10 best practices for using AI-powered GitLab Duo Chat
Published on: April 2, 2024
17 min read

10 best practices for using AI-powered GitLab Duo Chat

Explore tips and tricks for integrating GitLab Duo Chat into your AI-powered DevSecOps workflows. Plus, expert advice on how to refine chat prompts for the best results.

duo chat cover image

Getting into a conversation with AI can be challenging. What question do you start with? How do you frame the question? How much context is needed? Will the conversation provide the best and most efficient results?

In this tutorial, we explore 10 tips and best practices to integrate GitLab Duo Chat into your AI-powered DevSecOps workflows and refine your prompts for the best results.

Get started: Keep GitLab Duo Chat open and in sight

10 best practices for using GitLab Duo Chat

  1. Have a conversation
  2. Refine the prompt for more efficiency
  3. Follow prompt patterns
  4. Use low-context communication
  5. Repeat yourself
  6. Be patient
  7. Reset and start anew
  8. Gain efficiency with slash commands in the IDE
  9. Refine the prompt for slash commands
  10. Get creative with slash commands

Bonus content:

Live demo! Discover the future of AI-driven software development with our GitLab 17 virtual launch event. Register today!

Get started: Keep GitLab Duo Chat open and in sight

GitLab Duo Chat is available in the GitLab UI, Web IDE, and supported programming IDEs, for example, VS Code.

In VS Code, you can open GitLab Duo Chat in the default left pane. You can also drag and drop the icon into the right pane. This allows you to keep Chat open while you write code and navigate the file tree, perform Git actions, etc. To reset the Chat location, open the command palette (by pressing the Command+Shift+P (on macOS) or Ctrl+Shift+P (on Windows/Linux) keyboard shortcut and then type View: Reset View Locations. The following short video shows you how to do it.

The Web IDE and VS Code share the same framework – the same method works in the Web IDE for more efficient workflows.

Chat in Web IDE

10 best practices for using GitLab Duo Chat

1. Have a conversation

Chats are conversations, not search forms.

For the first conversation icebreaker, you can start with the same search terms similar to a browser search and experiment with the response and output. In this example, let's start with a C# project and best practices.

c# start project best practices

Chat prompt for C# start project best practices and response

The response is helpful to understand a broad scope of C#, but does not kickstart immediate best practices. Let's follow up with a more focused question in the same context.

Please show the project structure for the C# project.

Chat prompt for project structure for the C# project and response

This answer is helpful. Next, let's follow up with a Git question, and use the same question structure: Direct request to show something.

Show an example for a .gitignore for C#

Chat prompt for a .gitignore for C# and response

Continue with CI/CD and ask how to build the C# project.

Show a GitLab CI/CD configuration for building the C# project

Chat prompt for GitLab CI/CD configuration for building C# project and response

In this example, Chat encouraged us to request specific changes. Let's ask to use the .NET SDK 8.0 instead of 6.0.

In the above example, please use the .NET SDK 8.0 image

Chat prompt to use .NET SDK 8.0 image and response

The CI/CD configuration uses the .NET command line interface (CLI). Maybe we can use that for more efficient commands to create the projects and tests structure, too?

Explain how to create projects and test structure on the CLI

Chat prompt to explain how to create projects and test structure on the CLI and response

Of course, we could execute these commands in the terminal, but what if we wanted to stay in VS Code? Let's ask Chat.

Explain how to open a new terminal in VS Code

Chat prompt to explain how to open a new terminal in VS Code and response

2. Refine the prompt for more efficiency

Think of GitLab Duo Chat as a human, and engage with full sentences that provide as much context into your thoughts and questions.

Experienced browser search users might know this approach to queries: Build up the question, add more terms to refine the scope, and restart the search after opening plenty of tabs.

In a browser search, this probably would result in four to five different search windows.

c# start project best practices
c# .gitignore
c# gitlab cicd 
c# gitlab security scanning 
c# solutions and projects, application and tests

You can follow this strategy in a chat conversation, too. It requires adding more context, making it a conversational approach. GitLab Duo Chat enables you to ask multiple questions in one conversation request. Example: You need to start with a new C# project, apply best practices, add a .gitignore file, and configure CI/CD and security scanning, just like in the above search. In Chat, you can combine the questions into one request.

How can I get started creating an empty C# console application in VS Code? Please show a .gitignore and .gitlab-ci.yml configuration with steps for C#, and add security scanning for GitLab. Explain how solutions and projects in C# work, and how to add a test project on the CLI.

Chat prompt adding more context and response

In this response, Chat suggests to ask for specific configuration examples in follow-up questions in the conversation. Async practice: Create follow-up questions. You can omit C# as context in the same chat session.

Please show an example for a .gitignore. Please show a CI/CD configuration. Include the SAST template.

3. Follow prompt patterns

Follow the pattern: Problem statement, ask for help, provide additional requests. Not everything comes to mind when asking the first question – don't feel blocked, and instead start with Problem statement, ask for help in the first iteration.

I need to fulfill compliance requirements. How can I get started with Codeowners and approval rules?

Chat prompt to get started with Codeowners and approval rules and response

The answer is helpful but obviously generic. Now, you may want to get specific help for your team setup.

Please show an example for Codeowners with different teams: backend, frontend, release managers.

Chat prompt to show an example for Codeowners with different teams: backend, frontend, release managers and reponse

An alternative is to describe the situation you are in and to ask for input. It can feel a bit like a conversation to follow the STAR model (Situation, Task, Action, Results).

I have a Kubernetes cluster integrated in GitLab. Please generate a Yaml configuration for a Kubernetes service deployment. Explain how GitOps works as a second step. How to verify the results?

Chat prompt with multiple questions and response

4. Use low-context communication

Provide as much context as needed to provide an answer. Sometimes, the previous history or opened source code does not provide that helpful context. To make questions more efficient, apply a pattern of low-context communication, which is used in all-remote communication at GitLab.

The following question did not provide enough context in a C++ project.

Should I use virtual override instead of just override?

Chat prompt asking if the users should use virtual override instead of just override and response

Instead, try to add more context:

When implementing a pure virtual function in an inherited class, should I use virtual function override, or just function override? Context is C++.

Chat prompt with more detail and response

The example is also shown in the GitLab Duo Coffee Chat: Refactor C++ functions into OOP classes for abstract database handling.

5. Repeat yourself

AI is not predictable. Sometimes, it may not answer with the expected results, or does not produce source code examples or configuration snippets because it lacked context. It is recommended to repeat the question and refine the requirements.

In the following example, we want to create a C# application. In the first attempt, we did not specify the application type – C# can be used to create console/terminal but also UI applications. The result also does not provide an empty example source code. The second, repeated prompt adds two more words - console and empty.

How can I get started creating an C# application in VSCode?

How can I get started creating an empty C# console application in VSCode?

The results in the prompt differ. The first response is helpful to get started by following the instructions in the VS Code window, but it does not tell us where the source code is located and how to modify it. The repeated prompt with refinements modifies the response and provides instructions how to override the default template with some “hello world” code.

Chat prompt with repeated prompt with modifications and response

You can also combine repeat and refine strategies, and ask Chat to show an example for application code and tests.

How can I get started creating an empty C# console application in VSCode? Please show an example for application and tests.

Chat prompt that asks for example for application and tests and response

Repeat yourself after generic questions

When asking generic technology questions, GitLab Duo Chat might not be able to help. In the following scenario, I wanted to get a suggestion for Java build tools and framework, and it did not work. There could be many answers: Maven, Gradle, etc., as build tools, and 100+ Java frameworks, depending on the technology stack and requirements.

Chat prompt for Java build tools and framework and response

Let's assume that we want to focus on a customer environment with Java Spring Boot.

I want to create a Java Spring Boot application. Please explain the project structure and show a hello world example.

Chat prompt that asks for more, including a hello world example and response

This provides great results already. As an async exercise, repeat the prompt, and ask how to deploy the application, adding more refinements in each step. Alternatively, you can make it a follow-up conversation.

I want to create a Java Spring Boot application. Please explain the project structure and show a hello world example. Show how to build and deploy the application in CI/CD.

I want to create a Java Spring Boot application. Please explain the project structure and show a hello world example. Show how to build and deploy the application in CI/CD, using container images.

I want to create a Java Spring Boot application. Please explain the project structure and show a hello world example. Show how to build and deploy the application in CI/CD, using container images. Use Kubernetes and GitOps in GitLab.

6. Be patient

Single words or short sentences might not generate the desired results, as shown in this video example. Sometimes, GitLab Duo Chat is able to guess from available data, but sometimes also might insist on providing more context.

Example: labels matches the GitLab documentation content.

Chat prompt about labels and response

Refine the question to problem statements and more refinements for issue board usage.

Explain labels in GitLab. Provide an example for efficient usage with issue boards.

Chat prompt that includes asking for an example and response

Or use a problem statement, followed by a question and the ask for additional examples.

I don't know how to use labels in GitLab. Please provide examples, and how to use them for filters in different views. Explain these views with examples.

Chat prompt with problem statement and response

Also, avoid yes/no questions and instead add specific context.

Can you help me fix performance regressions?

Chat promptt that asks for help with fixing performance regressions and response

Instead, provide the context of the performance regression, including the programming languages, frameworks, technology stack, and environments. The following example uses an environment from some years ago, which can still be accurate today.

My PHP application encounters performance regressions using PHP 5.6 and MySQL 5.5. Please explain potential root causes, and how to address them. The app is deployed on Linux VMs.

Chat prompt that includes more detail and response

7. Reset and start anew

Sometimes, the chat history shows a different learning curve and provides the wrong context for follow-up questions. Or, you asked specific questions where GitLab Duo Chat cannot provide answers. Since generative AI is not predictable, it might also lack the ability to provide certain examples, but think it gave them in a future response (observed in Chat Beta). The underlying large language models, or LLMs, sometimes might insist on giving a specific response, in an endless loop.

How can I get started creating an empty C# console application in VSCode? Please show a .gitignore and .gitlab-ci.yml configuration with steps for C#, and add security scanning for GitLab. Explain how solutions and projects in C# work, and how to add a test project on the CLI.

After asking the question above with an example configuration, I wanted to reduce the scope of the question to get a more tailored response. It did not work as expected, since Chat knows about the chat history in context, and refers to previous answers.

How can I get started creating an empty C# console application in VSCode? Please show a .gitignore and .gitlab-ci.yml configuration with steps for C#.

Chat prompt that asks for configuration examples and response

To force Chat into a new context, use /reset as slash command to reset the session, and repeat the question to get better results. You can also use /clean or /clear to delete all messages in the conversation.

8. Gain efficiency with slash commands in the IDE

Explain code

  • Q: Generated code? Existing code? Legacy code?
  • A: Use the /explain slash command in the IDE.
  • A2: Refine the prompt with more focused responses, for example: /explain focus on potential shortcomings or bugs.

Chat prompt with /explain slash command

Chat prompt with refined prompt

Refactor code

  • Q: Unreadable code? Long spaghetti code? Zero test coverage?
  • A: Use the /refactor slash command in the IDE.
  • A2: Refine the prompt for more targeted actions, for example object-oriented patterns: /refactor into object-oriented classes with methods and attributes.

Chat prompt with /refactor slash command

Chat prompt with refined prompt

Generate tests

  • Q: Testable code but writing tests takes too much time?
  • A: Use the /tests slash command in the IDE.
  • A2: Refine the prompt for specific test frameworks, or test targets. You can also instruct the prompt to focus on refactoring, and then generate tests: /tests focus on refactoring the code into functions, and generate tests.

Chat prompt with /tests slash command

Chat prompt with refined prompt

More practical examples in complete development workflows are available in the GitLab Duo examples documentation.

9. Refine the prompt for slash commands

You will see refined prompts tips in this blog post a lot. It is one of the ingredients for better AI-powered workflow efficiency. Slash commands are no different, and allow for better results in GitLab Duo Chat.

A customer recently asked: "Can code explanations using /explain create comments in code?" The answer is: no. But you can use the Chat prompt to ask follow-up questions, and ask for a summary in a code comment format. It requires the context of the language.

The following example with a C++ HTTP client code using the curl library needs more documentation. You can refine the /explain prompt by giving more refined instructions to explain the code by adding code comments, and then copy-paste that into the editor.

/explain add documentation, rewrite the code snippet

Chat prompt to add documentation and rewrite code snippet and response

Alternatively, you can ask Chat to /refactor the source code, and generate missing code comments through a refined prompt.

/refactor add code comments and documentation

Chat prompt to refactor source code and generate code comments

10. Get creative with slash commands

When the Chat prompt does not know an answer to a question about the source code or programming language, look into the slash commands /explain, /refactor, and /tests and how much they can help in the context.

In the following example, an SQL query string in C++ is created in a single line. To increase readability, and also add more database columns in the future, it can be helpful to change the formatting into a multi-line string.

std::string sql = "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, email TEXT NOT NULL)";

You can ask GitLab Duo Chat about it, for example, with the following question:

How to create a string in C++ using multiple lines?

Chat may answer with an explanation and optional, a source code example. In this context, it can interpret the question to create a C++ string value with multiple lines, for example, using the \n character, assigned to a variable.

The requirement instead is to only format the written code, and variable value assignment in multiple lines. The string value itself does not need to contain a multi-line string representation.

There is an alternative for additional context in VS Code and the Web IDE: Select the source code in question, right-click, and navigate into GitLab Duo Chat > Refactor. This opens the Chat prompt and fires the /refactor code task immediately.

Although, the code task might not bring the expected results. Refactoring a single-line SQL string can mean a lot of things: Use multiple lines for readability, create constants, etc.

Code tasks provide an option to refine the prompt. You can add more text after the /refactor command, and instruct GitLab Duo Chat to use a specific code type, algorithm, or design pattern.

Let's try it again: Select the source code, change focus into Chat, and type the following prompt, followed by Enter.

/refactor into a multi-line written string. Show different approaches for all C++ standards.

Chat prompt to refactor into a multi-line written string and response

Tip: You can use GitLab Duo Code Suggestions to refine the source code even more after refactoring, or use alternative /refactor prompt refinements.

/refactor into a multi-line written string, show different approaches

/refactor into multi-line string, not using raw string literals

/refactor into a multi-line written string. Make the table name parametrizable

An alternative approach with the stringstream type is shown in the GitLab Duo Coffee Chat: Refactor C++ functions into OOP classes for abstract database handling, MR diff.

Explain vulnerabilities

It might not always work, but the /explain slash command can be asked about security vulnerability explanations, too. In this example, the C code contains multiple vulnerabilities for strcpy() buffer overflows, world writable file permissions, race condition attacks, and more.

/explain why this code has multiple vulnerabilities

Chat prompt about the code's multiple vulnerabilities

Refactor C code into Rust

Rust provides memory safety. You can ask Duo Chat to refactor the vulnerable C code into Rust, using /refactor into Rust. Practice with more refined prompts to get better results.

/refactor into Rust and use high level libraries

Chat prompt

Shortcuts

Give these shortcuts a try in your environment, and practice async using GitLab Duo Chat.

  1. Inspect vulnerable code from CVEs, and ask what it does, and how to fix it, using /explain why is this code vulnerable. Tip: Import open-source projects in GitLab to take advantage of GitLab Duo Chat code explanations.
  2. Try to refactor code into new programming languages to help legacy code migration plans.
  3. You can also try to refactor Jenkins configuration into GitLab CI/CD, using /refactor into GitLab CI/CD configuration.

Fun exercises

Try to convince Chat to behave like Clippy. Chat prompt

Ask about GitLab's mission: "Everyone can contribute."

Chat prompt

Learn more

There are many different environments and challenges out there. We have updated the GitLab Duo Chat documentation with more practical examples, and added a new GitLab Duo examples section with deep dives into AI-powered DevSecOps workflows, including Chat.

Learning GitLab Duo works best through playful challenges, and real production code. The new learning series, GitLab Duo Coffee Chat, will continue in 2024. Until then, you can watch the recordings in this YouTube playlist. If you are a GitLab customer, and are interested in joining a GitLab Duo Coffee Chat to learn together, let me know in this planning epic.

Want to get going with GitLab Duo Chat? Start your free trial today.

We want to hear from you

Enjoyed reading this blog post or have questions or feedback? Share your thoughts by creating a new topic in the GitLab community forum. Share your feedback

Ready to get started?

See what your team could do with a unified DevSecOps Platform.

Get free trial

Find out which plan works best for your team

Learn about pricing

Learn about what GitLab can do for your team

Talk to an expert