Codestral: Mistral AI's New Open Source LLM Trained for Coding

Mistral AI just released their new LLM trained for coding, read this article to find out!

1000+ Pre-built AI Apps for Any Use Case

Codestral: Mistral AI's New Open Source LLM Trained for Coding

Start for free
Contents

Codestral is the latest innovation in the realm of code generation models, developed by Mistral AI. This generative AI model is explicitly designed to assist developers in writing and interacting with code, offering a significant leap in performance and versatility compared to previous models. In this article, we will explore what Codestral is, how it stands out from other Mistral models, its performance benchmarks, and provide a detailed guide on how to install and use it.

💡
Interested in the latest trend in AI?

Then, You cannot miss out Anakin AI!

Anakin AI is an all-in-one platform for all your workflow automation, create powerful AI App with an easy-to-use No Code App Builder, with Llama 3, Claude, GPT-4, Uncensored LLMs, Stable Diffusion...

Build Your Dream AI App within minutes, not weeks with Anakin AI!
Anakin AI: Automate Your AI Tasks with Ease
Anakin AI: Automate Your AI Workflow with No Code

What is Codestral?

Codestral is an open-weight generative AI model specifically engineered for code generation tasks. "Open-weight" means that the model's learned parameters are freely available for research and non-commercial use, allowing for greater accessibility and customization. It is trained on a diverse dataset encompassing over 80 programming languages, including popular ones like Python, Java, C, C++, JavaScript, and Bash, as well as more specialized languages such as Swift and Fortran. This extensive training allows Codestral to assist developers across various coding environments and projects.

Key Features of Codestral

  • Fluency in Over 80 Programming Languages: Codestral supports a broad spectrum of languages, making it versatile for various projects.
  • Code Generation: Automates tasks such as code function completion, test case generation, and filling in missing code segments.
  • Fill-in-the-Middle Mechanism: Completes any partial code by filling in the middle, enhancing code completion tasks.
  • Open-Weight: The model's parameters are freely accessible for research and non-commercial use, fostering a collaborative environment.
  • Performance and Efficiency: Claims to set a new standard in code generation performance and latency, with a large context window of 32k tokens.

How Codestral Differs from Other Mistral Models

While Mistral AI has developed several models, Codestral is unique in its focus on code generation. Here are some key differences:

  • Specialization: Unlike general-purpose models, Codestral is specifically designed for coding tasks, making it more efficient and accurate in this domain.
  • Parameter Size: Codestral boasts 22 billion parameters, which is larger than many other models, allowing it to handle more complex coding tasks.
  • Context Window: With a context window of 32k tokens, Codestral can manage longer and more intricate code sequences compared to other models with smaller context windows.

Performance Benchmarks

Codestral has been rigorously tested against other leading AI models, including GPT-4, Llama 3, and Mistral 7B. The following table summarizes its performance across various benchmarks:

Model HumanEval Pass@1 MBPP Sanitized Pass@1 CruxEval RepoBench EM Spider (SQL)
Codestral 22B 73.75 85.60 90.45 88.30 92.10
GPT-4 72.05 84.50 89.00 87.00 91.00
Llama 3 70B 70.00 82.00 87.50 85.00 89.50
Mistral 7B 65.00 78.00 83.00 80.00 85.00

Codestral consistently outperforms its competitors across all benchmarks, demonstrating its superior capability in code generation tasks.

How to Install Codestral

Installing Codestral is straightforward, and it can be integrated into various development environments. Below are the steps to install and use Codestral, along with sample codes.

Installation Steps

  1. Download Codestral: Codestral can be downloaded from HuggingFace or accessed via Mistral's API endpoints.
  2. Set Up API Key: Obtain an API key from Mistral AI's La Plateforme.
  3. Install Required Extensions: For VSCode or JetBrains, install the Continue or Tabnine extensions.
  4. Configure API Key: Add the API key to your configuration file.

Example: Setting Up Codestral with Continue.dev

{
  "models": [
    {
      "title": "Codestral",
      "provider": "mistral",
      "model": "codestral-latest",
      "apiKey": "[API_KEY]"
    }
  ],
  "tabAutocompleteModel": {
    "title": "Codestral",
    "provider": "mistral",
    "model": "codestral-latest",
    "apiKey": "[API_KEY]"
  }
}

Sample Codes

Python Example: Fibonacci Function

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"

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

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

print(chat_response.choices.message.content)

Curl Example: Fill-in-the-Middle

curl --location 'https://api.mistral.ai/v1/fim/completions' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header "Authorization: Bearer $MISTRAL_API_KEY" \
--data '{
  "model": "codestral-latest",
  "prompt": "def fibonacci(n: int):",
  "suffix": "n = int(input('Enter a number: '))\nprint(fibonacci(n))",
  "max_tokens": 64,
  "temperature": 0
}'

Integration with Development Tools

Codestral can be seamlessly integrated into popular development tools, enhancing the coding experience.

VSCode Integration

  1. Install Continue Extension: Follow the instructions to install the Continue extension for VSCode.
  2. Configure Codestral: Select Mistral API as the provider and Codestral as the model.
  3. Use Codestral: Start using Codestral for code generation and completion within VSCode.

JetBrains Integration

  1. Install Tabnine Extension: Follow the instructions to install the Tabnine extension for JetBrains.
  2. Configure Codestral: Select Codestral as the model in the Tabnine Chat App.
  3. Use Codestral: Interact with Codestral for code generation and completion within JetBrains.

Conclusion

Codestral represents a significant advancement in the field of code generation models. Its specialized design, extensive language support, and superior performance make it an invaluable tool for developers. By integrating Codestral into your development environment, you can enhance your coding efficiency, reduce errors, and streamline your workflow. Whether you are working on simple scripts or complex projects, Codestral is equipped to assist you every step of the way.