Issue 8 - First rule of fight club, you do not talk about fight club: how to "customize" your AI Assistant

Issue 8 - First rule of fight club, you do not talk about fight club: how to "customize" your AI Assistant

The Movie "Fight Club" is a cult classic, it features Edvard Norton and Brad Pitt as the main characters.

You might be wondering why I'm quoting a movie like "Fight Club"? Well the saying goes "First rule of fight club, you do not about fight club". This is a good analogy for AI assistants as you want to be able to give them instructions for what they should and shouldn't do, just like the movie protagonists have rules for their fight club.

Why do we want to customize our AI Assistant?

So I just said we want to give instructions to our AI assistant, but why? Well, AI models comes with instructions already for how they should behave, but sometimes you want to give them more specific instructions to ensure the output is inclusive, follow company protocol and more.

How this is done when prompting an AI model is by using a technique called meta prompting, the idea is that for every prompt request you give, you prepend it with a meta prompt and the actual prompt. Thereby, you can guide the AI model to give the output you want.

An example of a meta prompt and a prompt could look like this:

  • Meta prompt: "Only mention places that are family friendly."
  • Prompt: "Show me tourist attractions in New York City."

In the preceding example, the meta prompt serves as guidance to filter out places that are not family friendly. Of course, you can take meta prompting as far as you want, here's another example:

  • Meta prompt: "Only mention insurance products from Contoso Insurance that meets {condition}"
  • Prompt: "Show me insurance products."

Here, we see how our meta prompt is guiding the AI model to produce insurance products from a specific company, which is advantageous if you're working for Contoso Insurance.

Great, so we see there are many different uses for meta prompting, for inclusiveness, for business rules and more. What about for software development?

Meta prompting in software development

How is meta prompting used in software development? The principle is the same but the use case is different. Let's look at some use cases:

  • Meta prompt: "Only show code that is compatible with Python 3.9."
  • Prompt: "Show me how to read a file in Python."
  • A:

with open('file.txt', 'r') as file: content = file.read()        

  • Meta prompt: "All variables should start with the prefix my_."
  • Prompt: "Show me how to declare a variable in Python."
  • A:

"my_variable = 10"        

We may think what we want about the exact guidance with the above examples, but the idea is that it ensures the generated code follows a specific guideline and thereby you're closer to being able to use the generated code in your project without having to refactor it first.

At this point, you might be thinking,

do I really need to prefix all my prompt communication with a bunch of meta prompts for each prompt, sounds exhausting?

Thankfully no if you use a tool like GitHub Copilot, so let's look at how to customize GitHub Copilot for your needs next.

Customizing GitHub Copilot

GitHub Copilot is a great tool for generating code and many other things, it's a great AI assistant that works for many of the big IDEs, for example Visual Studio Code. As we mentioned previously, to avoid having to rewrite code to adhere to company standards, let's try to customize GitHub Copilot. How can we get there? First it's known as "Custom Instructions", I', leaving some links at the bottom but there's two different ways to do this:

  • Include instructions via settings.
  • Provide instructions to a specific file that's part of your repo.

Let's explore both these approaches:

Include instructions via settings

There's a setting github.copilot.chat.codeGeneration.instructions that you can set in your settings file. Here's an example of how you can set it:

"github.copilot.chat.codeGeneration.instructions": [
    {
      "text": "Always add a comment: 'Generated by Copilot'."
    },
    {
      "text": "In TypeScript always use underscore for private field names."
    },
    {
      "file": "code-style.md" // import instructions from file `code-style.md`
    }
  ],
        

What the above instructions are saying is the following:

  • Always add a comment: 'Generated by Copilot'. This can be quite powerful as you can easily spot code that's been generated by Copilot to ensure there's a human verifying it.
  • In TypeScript always use underscore for private field names: Here's an instruction that's specific to TypeScript, and shows how you can control naming of variables, which may come in handy for for example a company or team standard for naming conventions.
  • Import instructions from file code-style.md: This is a good way to collect are your code styles, from naming, commenting and what have you into one place.

DEMO

Let's take it for a spin and see how it works.

"github.copilot.chat.codeGeneration.instructions": [
        {
            "text": "variables should be in caps",
            "file": "code-style.md"
        }
    ]
        

In above instructions, we do two things: we provide a text instruction and we import instructions from a file code-style.md. Let's see content of code-style.md so we know what to expect:

- prefix all variables with v_
- remove vowels from all variable names
        

Based on these instructions working in tandem, we can expect that all variables will be in caps, prefixed with v_ and have all vowels removed.

This instructions is definitely not what I recommend for your code, but it's a good example of how you can use the instructions to control the output of Copilot.

Let's test it:

Prompt: "generate code for a web server" A:
from flask import Flask

V_PP = Flask(__name__)

@V_PP.route('/')
def V_HM():
    return "Welcome to the Home Page!"

@V_PP.route('/BT')
def V_BT():
    return "This is the About Page."

if __name__ == '__main__':
    V_PP.run(debug=True)
        

Say what you will about the code, but it's clear that the instructions are working as intended.

Use a global file for instructions

Now that we've learned to use the settings file, let's create a file .github/copilot-instructions.md and add the following instructions:

Always add a text 'AI generated' to the code output        

Also, while at it, let's enable the setting in the settings file:

"github.copilot.chat.codeGeneration.useInstructionFiles": true        

Good now, we're ready for business.

DEMO

Let's test it by typing a prompt in GitHub Copilot chat window:

  • Prompt: "generate code for a web server":

A:

# AI generated
from flask import Flask

V_PP = Flask(__name__)

@V_PP.route('/')
def V_HM():
    return "Welcome to the Home Page!"

@V_PP.route('/BT')
def V_BT():
    return "This is the About Page."

if __name__ == '__main__':
    V_PP.run(debug=True)
        

Note the comment # AI generated in the code output, this is the instruction we added in the .github/copilot-instructions.md file. Now you should have an easier time spotting code generated by Copilot so you can verify it.

Summary

That was it folks, remember, meta prompts and customizing your AI assistant is important and there are many valid use cases for doing so. See the links at the bottom to learn more about how to customize GitHub Copilot for your needs.

Until next time, prompting!

Links

Laurence Duffy

Director @ LuminanceAI | Software Engineering, Full-Stack Development, AI App Development.

3 周

Very cool tips, and tricks, thanks for sharing ??

Very helpful! Thanks for sharing, these are some great use cases. I am going to look into custom instructions for my copilot settings.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了