Skip to main contentIBM Granite
granite-8b-code-instruct

Granite Code

Table of contents

  1. Overview
  2. Prompts for Code
    1. Basic Example
    2. Code Generation
    3. Code Explanation
    4. Code Fixing
    5. Code Translation
    6. Math Reasoning
    7. Function Calling
    8. Code Completion
  3. How to avoid common issues

Overview

We introduce the Granite series of decoder-only code models for code generative tasks (e.g., fixing bugs, explaining code, documenting code), trained with code written in 116 programming languages. A comprehensive evaluation of the Granite Code model family on diverse tasks demonstrates that our models consistently reach state-of-the-art performance among available open source code LLMs.

The key advantages of Granite Code models include:

  • All-rounder Code LLM: Granite Code models achieve competitive performance on different kinds of code-related tasks, including code generation, explanation, fixing, editing, translation, and more. Demonstrating their ability to solve diverse coding tasks.
  • Trustworthy Enterprise-Grade LLM: All our models are trained in a transparent manner, and according to IBM’s AI Ethics principles. We release all our Granite Code models under an Apache 2.0 license for research and commercial use.

The family of Granite Code Models comes in two main variants:

  • Granite Code Base Models: base foundational models designed for code-related tasks (e.g., code repair, code explanation, code synthesis).
  • Granite Code Instruct Models: instruction following models fine-tuned using a combination of Git commits paired with human instructions and open source synthetically generated code instruction datasets.

Both base and instruct models are available in sizes of 3B, 8B, 20B, and 34B parameters.

For more information see the Technical IBM GitHub Granite Code models page, the Granite Code models collection on Hugging Face, and the technical report.

Data for Granite Code

In the spirit of open innovation, data-prep-kit, the framework and pipelines used for preparing the training data for Granite Code models are being open-sourced with Apache 2.0 license. The framework offers data transformation pipelines that can readily scale from laptop-scale to data-center scale for ease of iterative experimentation as well as large-scale production.

Download other models on Hugging Face

Prompts for code

Here, we share sample prompts to get started. Please expect these templates and best practices to be updated as needed.

We recommend using Granite code models with the following prompt template - without a system prompt:

Question:
{PROMPT}
Answer:

Note that there are two newlines between the end of the prompt and a newline after the Answer.

You can try with and without and use what works best. Unlike language models, the code model system prompt can be further adapted and customized for a given use case.

Basic example

Question:
Write a code to find the maximum value in a list of numbers.
Answer:

Expected output of the basic example:

```python
def find_max(numbers):
max_value = numbers[0]
for num in numbers:
if num > max_value:
max_value = num
return max_value
```<|endoftext|>

Example with system prompt - please note that the system prompt is optional:

System:
"You are an intelligent AI programming assistant, utilizing a Granite code language model developed by IBM. Your primary function is to assist users in programming tasks, including code generation, code explanation, code fixing, generating unit tests, generating documentation, application modernization, vulnerability detection, function calling, code translation, and all sorts of other software engineering tasks."
Question:
{PROMPT}
Answer:

Code generation

As Granite code models are useful in a variety of software development scenarios, we show you some of the most typical in this section.

Python function example:

Question:
Write a python function to find the first repeated character in a given string.
Answer:

Expected response from model for python function example:

To find the first repeated character in a given string, we can use a dictionary to store the frequency of each character in the string. We can iterate through the string and for each character, we can check if it's already in the dictionary. If it is, we can return the character as the first repeated character. If it's not, we can add it to the dictionary with a frequency of 1.
```python
def first_repeated_character(string):
char_freq = {}
for char in string:
if char in char_freq:
return char
else:

Python Code to test response from model:

def first_repeated_character(string):
char_freq = {}
for char in string:
if char in char_freq:
return char
else:
char_freq[char] = 1
return None

Code explanation

Example that requests the explanation of a code function in Python:

Please explain the following Python function:
def find_max(numbers):
max_value = numbers[0]
for num in numbers:
if num > max_value:
max_value = numxs
return max_value

Expected response from model:

The function `find_max` takes a list of numbers as input and returns the maximum value in the list. It initializes the `max_value` variable to the first element of the list. Then, it iterates over each element in the list using a for loop. For each element, it checks if it is greater than the current `max_value`. If it is, the `max_value` is updated to the new maximum value. Finally, the function returns the maximum value

Code fixing

Example that asks the model to identify a bug in a line of Python code. The prompt also asks the model to return a function that fixes this bug:

Question:
Please, identify the bug in this code snippet: “””print(“Hello World.)””” and recommend an updated function that fixes the bug.
Answer:

Expected response from model:

The bug in the code snippet is that the closing quotation mark is missing after the word "World". To fix this bug, you can update the function as follows:
def print_hello_world():
print("Hello World.")
By adding the missing closing quotation mark, the bug is resolved, and the function will correctly print "Hello World."<|endoftext|>

Code translation

Example that asks the model to translate a function from Python to Java.

Question:
Please translate a Python method that finds the maximum value in a list of numbers to Java:
def find_max(numbers):
max_value = numbers[0]
for num in numbers:
if num > max_value:
max_value = numxs
return max_value

Expected response from model:

Here's the Java equivalent of the Python method:
public static int findMax(int[] numbers) {
int maxValue = numbers[0];
for (int num : numbers) {
if (num > maxValue) {
maxValue = num;
}
}

Math reasoning

Example to solve a math reasoning problem.

Question:
Natalia sold clips to 48 of her friends in April, and then she sold half as many clips in May. How many clips did Natalia sell altogether in April and May?
Answer:

Expected response from model:

Natalia sold 48 clips in April.
In May, she sold half as many clips as in April, so she sold 48/2 = 24 clips.
Altogether, Natalia sold 48 + 24 = 72 clips in April and May.
#### 72
The answer is: 72<|endoftext|>

Function calling

Example that asks the model to choose a function that solves a task. Note that the first part of the question is a custom system prompt.

Question:
You are an expert in composing functions. You are given a question and a set of possible functions.
Based on the question, you will need to make one or more function/tool calls to achieve the purpose.
If none of the function can be used, point it out. If the given question lacks the parameters required by the function, also point it out. You should only return the function call in tools call sections.
Here is a list of functions in JSON format that you can invoke:
{'name': 'get_current_weather', 'description': 'Get the current weather', 'parameters': {'type': 'object', 'properties': {'location': {'type':'string', 'description': 'The city and state, e.g. San Francisco, CA'}},'required': ['location']}}
{'name': 'get_stock_price', 'description': 'Retrieves the current stock price for a given ticker symbol. The ticker symbol must be a valid symbol for a publicly traded company on a major US stock exchange like NYSE or NASDAQ. The tool will return the latest trade price in USD. It should be used when the user asks about the current or most recent price of a specific stock. It will not provide any other information about the stock or company.', 'parameters': {'type': 'object', 'properties': {'ticker': {'type':'string', 'description': 'The stock ticker symbol, e.g. AAPL for Apple Inc.'}},'required': ['ticker']}}

Expected response from model:

[get_current_weather(location="New York")]<|endoftext|>

Code completion

Example that asks the model to complete an assert statement. A function for that assert statement is also provided as part of the prompt:

Question:
Based on the given Python code, which may contain errors, complete the assert statement with the output when executing the code on the given test case. Do NOT output any extra information, even if the function is incorrect or incomplete. Output "# done" after the assertion.
def f(a, b):
for key, value in b.items():
if key not in a:
a[key] = [value]
else:
a[key].append(value)

Expected response from model:

{'foo': ['bar']}
# done<|endoftext|>

How to avoid common issues

  • Granite Code models were not designed for language tasks and are not appropriate for such use. Any such use is at your own risk, and you may not rely on resulting output. Please validate all output independently and consider deploying a Hate Abuse Profanity (HAP) filter. See this notebook for reference.
  • Increase max output tokens for longer code responses: It is important to increase the Max output tokens to ensure the model does not cut off the code response, resulting in incomplete code.
  • Greedy mode for precise output: Use Greedy mode for precise results.
  • Be careful about whitespaces and line breaks: Make sure that the prompt templates are correctly implemented, and that there are no unintended white-spaces, and that there is exactly one line-break at the end of the prompt.
  • Use existing tags: The model is trained very explicitly to handle the special tags of “System:”, “Question:”, and “Answer:”.
  • System Prompt isn’t required for code models: Try out the Question:{PROMPT} Answer: format without a system prompt first. If you don’t get the desired results, you can try the system prompt as described in the code examples section. You can also try experimenting with other instructions, but first start with the basic Question: Answer: format.