Code Generation with Mistral’s Codestral

Let’s show three simple ways of using code generation, based on Mistral’s code generation tutorial: fill in the middle, code completion, and use instruction.

Setup

import os
from mistralai.client import MistralClient
from mistralai.models.chat_completion import ChatMessage

api_key = os.environ["MISTRAL_API_KEY"]
client = MistralClient(api_key=api_key)
model = "codestral-latest"

Fill in the Middle

Define a prompt and suffix, where the prompt is the beginning of the code, and suffix can be a function call to the code. For example, we want codstral to come up with the code for Fibonacci, and suffix is the function call to Fibonacci number:

prompt = "def fibonacci(n: int):"

# For suffix:
# n = int(input('Enter a number: '))
# print(fibonacci(n))
suffix = "n = int(input('Enter a number: '))\nprint(fibonacci(n))"

response = client.completion(
    model=model,
    prompt=prompt,
    suffix=suffix,
)

Response:

    if n <= 0:
        return 'Invalid input'
    elif n == 1:
        return 0
    elif n == 2:
        return 1
    else:
        a, b = 0, 1
        for _ in range(2, n):
            a, b = b, a + b
        return b

Code Completion

For code completion, simply give the prompt:

# Prompt:
# def is_odd(n): 
#  return n % 2 == 1 
# def test_is_odd():

prompt = "def is_odd(n): \n return n % 2 == 1 \ndef test_is_odd():"

response = client.completion(
    model=model,
    prompt=prompt
)

print(response.choices[0].message.content)

Response:

 assert is_odd(1)
 assert not is_odd(2)
 assert is_odd(3)
 assert not is_odd(4)
 assert is_odd(5)
 assert not is_odd(6)
 assert is_odd(7)
 assert not is_odd(8)
 assert is_odd(9)
 assert not is_odd(10)
 print("All tests passed!")

Use Instructions

Instead of having code in the prompt, give instructions to generate the code. This is likely how most of us use LLM’s code generation if we want to see something from scratch. For example:

messages = [
    ChatMessage(role="user", content="Write a function for fibonacci")
]

chat_response = client.chat(
    model=model,
    messages=messages
)

print(chat_response.choices[0].message.content)

Response:

Sure, here is a simple Python function that calculates the Fibonacci sequence up to the nth term:

```python
def fibonacci(n):
    fib_sequence = [0, 1]

    while len(fib_sequence) < n:
        fib_sequence.append(fib_sequence[-1] + fib_sequence[-2])

    return fib_sequence
```

You can use this function like this:

```python
print(fibonacci(10))
```

This will output the first 10 numbers in the Fibonacci sequence:

```python
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
```

That’s it for now!