Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ibm
GitHub Repository: ibm/watson-machine-learning-samples
Path: blob/master/cloud/notebooks/python_sdk/deployments/foundation_models/Use watsonx, and `mistral-small-3-1-24b-instruct-2503` to generate code based on instruction.ipynb
9466 views
Kernel: clear_env

image

Use watsonx, and mistral-small-3-1-24b-instruct-2503 to generate code based on instruction

Disclaimers

  • Use only Projects and Spaces that are available in watsonx context.

Notebook content

This notebook contains the steps and code to demonstrate support for code generating in watsonx. It introduces commands for defining prompt and model testing.

Some familiarity with Python is helpful. This notebook uses Python 3.11.

Learning goal

The goal of this notebook is to demonstrate how to generate code using IBM mistral-small-3-1-24b-instruct-2503 watsonx model based on instruction provided by the user.

Contents

This notebook contains the following parts:

Set up the environment

Before you use the sample code in this notebook, you must perform the following setup tasks:

Install and import dependencies

Note: ibm-watsonx-ai documentation can be found here.

%pip install -U ibm-watsonx-ai | tail -n 1
Successfully installed anyio-4.11.0 cachetools-6.2.1 certifi-2025.10.5 charset_normalizer-3.4.4 h11-0.16.0 httpcore-1.0.9 httpx-0.28.1 ibm-cos-sdk-2.14.3 ibm-cos-sdk-core-2.14.3 ibm-cos-sdk-s3transfer-2.14.3 ibm-watsonx-ai-1.4.4 idna-3.11 jmespath-1.0.1 lomond-0.3.3 numpy-2.3.4 pandas-2.2.3 pytz-2025.2 requests-2.32.5 sniffio-1.3.1 tabulate-0.9.0 tzdata-2025.2 urllib3-2.5.0

Defining the watsonx.ai credentials

This cell defines the watsonx.ai credentials required to work with watsonx Foundation Model inferencing.

Action: Provide the IBM Cloud user API key. For details, see documentation.

import getpass from ibm_watsonx_ai import Credentials credentials = Credentials( url="https://us-south.ml.cloud.ibm.com", api_key=getpass.getpass("Please enter your watsonx.ai api key (hit enter): "), )

Defining the project id

The Foundation Model requires project id that provides the context for the call. We will obtain the id from the project in which this notebook runs. Otherwise, please provide the project id.

import os try: project_id = os.environ["PROJECT_ID"] except KeyError: project_id = input("Please enter your project_id (hit enter): ")
from ibm_watsonx_ai import APIClient api_client = APIClient(credentials=credentials, project_id=project_id)

Foundation Models on watsonx.ai

List available models

All avaliable models are presented under TextModels class. For more information refer to documentation.

print([model.name for model in api_client.foundation_models.TextModels])
['DEFENCE_GRANITE', 'GRANITE_3_2_8B_INSTRUCT', 'GRANITE_3_2B_INSTRUCT', 'GRANITE_3_3_8B_INSTRUCT', 'GRANITE_3_8B_INSTRUCT', 'GRANITE_4_H_SMALL', 'GRANITE_8B_CODE_INSTRUCT', 'GRANITE_GUARDIAN_3_8B', 'GRANITE_VISION_3_2_2B', 'LLAMA_3_2_11B_VISION_INSTRUCT', 'LLAMA_3_2_90B_VISION_INSTRUCT', 'LLAMA_3_3_70B_INSTRUCT', 'LLAMA_3_405B_INSTRUCT', 'LLAMA_4_MAVERICK_17B_128E_INSTRUCT_FP8', 'LLAMA_GUARD_3_11B_VISION', 'MISTRAL_MEDIUM_2505', 'MISTRAL_SMALL_3_1_24B_INSTRUCT_2503', 'GPT_OSS_120B', 'ALLAM_1_13B_INSTRUCT']

You need to specify model_id that will be used for inferencing:

model_id = api_client.foundation_models.TextModels.MISTRAL_SMALL_3_1_24B_INSTRUCT_2503

Defining the model parameters

You might need to adjust model parameters for different models or tasks, to do so please refer to documentation.

from ibm_watsonx_ai.metanames import GenTextParamsMetaNames as GenParams parameters = { GenParams.DECODING_METHOD: "greedy", GenParams.MAX_NEW_TOKENS: 200, GenParams.STOP_SEQUENCES: ["<end·of·code>"], }

Initialize the model

Initialize the ModelInference class with previous set params.

from ibm_watsonx_ai.foundation_models import ModelInference model = ModelInference( model_id=model_id, params=parameters, credentials=credentials, project_id=project_id )

Model's details

model.get_details()
{'model_id': 'mistralai/mistral-small-3-1-24b-instruct-2503', 'label': 'mistral-small-3-1-24b-instruct-2503', 'provider': 'Mistral AI', 'source': 'Hugging Face', 'indemnity': 'NON_IBM', 'functions': [{'id': 'autoai_rag'}, {'id': 'image_chat'}, {'id': 'text_chat'}, {'id': 'text_generation'}], 'short_description': 'This model is an instruction-finetuned version of: Mistral-Small-3.1-24B-Base-2503.', 'long_description': 'Mistral Small 3 (2501), Mistral Small 3.1 (2503) adds state-of-the-art vision understanding and enhances long context capabilities up to 128k tokens without compromising text performance. With 24 billion parameters, this model achieves top-tier capabilities in both text and vision tasks.', 'terms_url': 'https://www.apache.org/licenses/LICENSE-2.0', 'input_tier': 'class_c1', 'output_tier': 'class_17', 'number_params': '24b', 'min_shot_size': 1, 'task_ids': ['question_answering', 'summarization', 'retrieval_augmented_generation', 'retrieval_augmented_generation', 'classification', 'generation', 'code', 'extraction', 'function_calling'], 'tasks': [{'id': 'question_answering'}, {'id': 'summarization'}, {'id': 'retrieval_augmented_generation'}, {'id': 'classification'}, {'id': 'generation'}, {'id': 'code'}, {'id': 'extraction'}, {'id': 'function_calling'}], 'model_limits': {'max_sequence_length': 128000, 'max_output_tokens': 16384}, 'limits': {'3f6acf43-ede8-413a-ac69-f8af3bb0cbfe': {'max_output_tokens': 16384}, 'a3d2f92f-06f9-48d0-b2e6-a7ba2b4e0577': {'max_output_tokens': 16384}, 'd18d88b9-be7a-46ec-be1e-aff14904f1e9': {'max_output_tokens': 16384}}, 'lifecycle': [{'id': 'available', 'start_date': '2025-04-22'}], 'versions': [{'version': '1.0.0', 'available_date': '2025-04-22'}]}

Generate code based on instruction

Define instructions for the model with at-least one example.

instruction = """Using the directions below, generate Python code for the given task. Input: # Write a Python function that prints 'Hello World!' string 'n' times. Output: def print_n_times(n): for i in range(n): print("Hello World!") <end of code> """

Prepare question for the model.

question = """Input: # Write a Python function, which generates sequence of prime numbers. # The function 'primes' will take the argument 'n', an int. It will return a list which contains all primes less than 'n'."""

Generat the code using IBM mistral-small-3-1-24b-instruct-2503 model.

Inter the model to generate the code, according to provided instruction.

result = model.generate_text(" ".join([instruction, question]))

Formatting the text to get the function itself

code_as_text = result.split("Output:")[1].split("<end of code>")[0]

Generated code testing

The resulting code looks as below.

print(code_as_text)
def primes(n): sieve = [True] * n for current in range(2, int(n**0.5) + 1): if sieve[current]: for multiple in range(current*2, n, current): sieve[multiple] = False return [num for num in range(2, n) if sieve[num]]

Use generated code to make it as function.

exec(code_as_text)

Define the number 'n' for which the primes() function should process prime numbers.

n = 25

Test and run the generated function.

primes(n)
[2, 3, 5, 7, 11, 13, 17, 19, 23]

Summary and next steps

You successfully completed this notebook!

You learned how to generate code based on instuction with IBM mistral-small-3-1-24b-instruct-2503 on watsonx.

Check out our Online Documentation for more samples, tutorials, documentation, how-tos, and blog posts.

Authors:

Mateusz Szewczyk, Software Engineer at watsonx.ai.

Copyright © 2024-2026 IBM. This notebook and its source code are released under the terms of the MIT License.