Code Llama Prompting Guide

Meta's Code Llama is available in three primary variations—Instruct, Code completion, and Python—each requiring different prompting strategies to maximize effectiveness. Understanding these distinctions allows developers to use the model for tasks ranging from natural language interaction to precise code infilling.

Code Llama Instruct: Natural Language Interaction

The instruct model is designed to provide human-like answers to questions, combining natural language explanations with code blocks. It is the most accessible variation and functions similarly to a conversational AI.

Code Generation and Explanation

The Instruct model can generate functions based on natural language requests. For example, asking the model to write a Python function for the nth Fibonacci number results in both the implementation and a brief explanation of the recursive formula used.

Code Review and Bug Detection

Code Llama Instruct can act as a secondary reviewer for commits or pull requests to identify simple bugs. When provided with a buggy function, the model can pinpoint the specific logic error, such as failing to handle a specific input case (e.g., where n is equal to 1).

Unit Test Generation

The model reduces boilerplate overhead by generating unit tests for existing functions. By passing the content of a source file to the prompt, the model can produce a comprehensive unittest class covering multiple edge cases.

Code Completion and Infill

The standard code completion model focuses on generating subsequent tokens based on the provided prompt, making it ideal for writing functions after a signature or a comment.

Standard Completion

When provided with a comment describing a function's purpose (e.g., removing whitespace from a string), the completion model generates the corresponding Python code to fulfill that requirement.

Fill-in-the-Middle (FIM) / Infill

Infill is a specialized prompt format that allows the model to complete code between two existing blocks. This is critical for inserting logic into the middle of an existing function. To use this feature, the model requires a specific tag-based format:

<PRE> {prefix} <SUF>{suffix} <MID>

In this structure, the prefix is the code before the insertion point and the suffix is the code following it. The model may return an <EOT> (End of Token) tag to signal the completion of the response.

Code Llama Python

The Python variation is fine-tuned on an additional 100 billion Python tokens. This makes it specifically optimized for Python development, including machine learning tooling and web frameworks like Django.

Model Availability and Ecosystem

Code Llama is available in 7 billion, 13 billion, and 34 billion parameter versions. While the provided examples utilize 4-bit quantization of the 7B model via Ollama, the prompts are compatible across different model providers and runtimes.

Several tools have already integrated Code Llama, including:

  • Cody: Offers an experimental version with infill support.
  • Continue: Supports Code Llama as a replacement for GPT-4.
  • Open Interpreter: Uses the model to generate functions for local terminal execution.
  • Fine-tuned variants: Specialized versions have been released by teams such as Phind and WizardLM.

Sources

Related

  • Dispatch
  • Dispatch
  • Dispatch
  • Project
  • Dispatch