code-intelligence

Prompt Templates

There are basically two use cases for custom prompts:

Prompts are parsed using the jmustache library, so they use Mustache syntax.

Instruction Prompts (Completions)

Instruction prompts are used to generate code completions. The template is currently given the following parameters:

You can use these parameters to structure the prompt according to the target model’s requirements.

Example 1: Fill-in-the-Middle (FIM) Prompt

Many modern code generation models are trained with a specific Fill-in-the-Middle (FIM) format. This usually involves special tokens to denote the prefix, suffix, and the point where the model should insert the completion.
The exact tokens depend on the model being used. Common examples include:

A template for a model expecting <|fim_prefix|>, <|fim_suffix|>, <|fim_middle|> tokens might look like this:

<|fim_prefix|><|fim_suffix|><|fim_middle|>

Note: If there is a selection, the prefix ends at the cursor, and the suffix starts after the selection. The model is expected to generate code that effectively replaces the selection.

Example 2: Instruction-Based Prompt with Context

Alternatively, you can use a more traditional instruction-based prompt, providing the context and explicitly asking the model to complete the code. The contextWithTags variable is useful here.

Complete the code starting at the <<<cursor>>> position within the following context.
If text is selected (between <<<selection_start>>> and <<<selection_end>>>), replace the selection.
Only provide the code completion itself, without any explanation or surrounding text.

Context:
```

```

The user recently made these edits:




Completion:

Example 3: Combining FIM with Recent Edits

You might want to provide recent edits as additional context even when using FIM format, if the model supports it.

Here are recent changes made by the user:




Complete the code based on the following prefix and suffix:
<|fim_prefix|><|fim_suffix|><|fim_middle|>

Choose the prompt structure that works best for the specific AI model you are interacting with via the API. The FIM style (prefix/suffix) is often preferred for models specifically trained for code completion, while the instruction style (contextWithTags) might be more suitable for general-purpose instruction-following models.