CodeLlama and Qwen2.5 are both powerful open-source LLMs fine-tuned for code generation, and Ollama makes running them locally a breeze.

Here’s a peek at CodeLlama 7B generating Python code:

from ollama import chat

response = chat(
    model='codellama',
    messages=[
        {
            'role': 'user',
            'content': 'Write a Python function to calculate the factorial of a number.',
        },
    ],
)

print(response['message']['content'])

Output:

def factorial(n):
    """
    Calculates the factorial of a non-negative integer.

    Args:
        n: The non-negative integer.

    Returns:
        The factorial of n.
    """
    if n == 0:
        return 1
    else:
        return n * factorial(n-1)

# Example usage:
num = 5
print(f"The factorial of {num} is {factorial(num)}")

Now, let’s see Qwen2.5 0.5B do the same:

from ollama import chat

response = chat(
    model='qwen:0.5b',
    messages=[
        {
            'role': 'user',
            'content': 'Write a Python function to calculate the factorial of a number.',
        },
    ],
)

print(response['message']['content'])

Output:

def calculate_factorial(n):
  """
  This function calculates the factorial of a non-negative integer.
  """
  if not isinstance(n, int) or n < 0:
    raise ValueError("Input must be a non-negative integer.")
  if n == 0:
    return 1
  else:
    result = 1
    for i in range(1, n + 1):
      result *= i
    return result

# Example usage
number = 5
print(f"The factorial of {number} is: {calculate_factorial(number)}")

Both models, when prompted with the same request, produce valid Python functions for calculating factorials. CodeLlama’s output is more concise, using recursion, while Qwen2.5’s implementation is iterative and includes more robust input validation. The key difference here isn’t just the code style, but the underlying architecture and training data. CodeLlama, as its name suggests, is specifically trained on a massive dataset of code, giving it a deep understanding of programming languages and patterns. Qwen2.5, while also code-aware, has a broader training base, allowing it to handle more diverse tasks, including code generation.

The primary problem these models solve is democratizing access to advanced code generation capabilities. Historically, such powerful AI models were either proprietary, expensive to run, or required significant infrastructure. Ollama, by packaging these models and providing a simple API, allows developers to integrate AI-powered code assistance directly into their local workflows without needing cloud GPUs or complex setup. This enables tasks like boilerplate code generation, debugging assistance, code explanation, and even unit test creation, all executed on your own machine.

Internally, these models are transformer-based neural networks. When you send a prompt, it’s tokenized, fed through layers of attention mechanisms, and then decoded back into a sequence of tokens, which are then converted into human-readable code. The "magic" lies in the weights and biases of these networks, learned during their extensive training. For CodeLlama, this training focused heavily on code repositories, enabling it to predict the next token in a code sequence with high accuracy. Qwen2.5, part of a larger family of models, also benefits from extensive pre-training, with specific fine-tuning for coding tasks.

When working with these models via Ollama, you’re essentially interacting with a local server that loads the model into your machine’s RAM and/or VRAM. The ollama run codellama command downloads and sets up the model, making it available to the ollama chat API. You control the output by crafting your prompts. The more specific and clear your prompt, the better the generated code will likely be. You can also experiment with parameters like temperature (controlling randomness) and top_p (nucleus sampling) within the ollama API to influence the creativity and determinism of the output. For instance, a lower temperature (e.g., 0.2) will yield more predictable and focused code, while a higher temperature (e.g., 0.8) might produce more novel but potentially less coherent suggestions.

A crucial aspect often overlooked is how these models handle context. While they can generate code based on a single prompt, their true power emerges when you maintain a conversation. By sending previous turns of the conversation (including generated code and your feedback) back to the model, you allow it to build upon its prior outputs, correcting mistakes or refining the code iteratively. This stateful interaction is key to using LLMs effectively for complex coding tasks, mimicking a human pair-programming session where context is implicitly maintained.

The next step in exploring these models is likely delving into prompt engineering techniques specifically for code generation, such as few-shot prompting or providing detailed specifications.

Want structured learning?

Take the full Ollama course →