Path: blob/master/cloud/notebooks/python_sdk/deployments/foundation_models/Use watsonx, and `mixtral-8x7b-instruct-v01` to generate code based on instruction.ipynb
6405 views
Use watsonx, and mixtral_8x7b_instruct_v01
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 mixtral_8x7b_instruct_v01
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:
Create a watsonx.ai Runtime Service instance (a free plan is offered and information about how to create the instance can be found here).
Install dependecies
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.
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.
List available models
All avaliable models are presented under ModelTypes class. For more information refer to documentation.
You need to specify model_id
that will be used for inferencing:
Defining the model parameters
You might need to adjust model parameters
for different models or tasks, to do so please refer to documentation.
Initialize the model
Initialize the ModelInference
class with previous set params.
Model's details
Define instructions for the model with at-least one example.
Prepare question for the model.
Generat the code using IBM mixtral_8x7b_instruct_v01
model.
Inter the model to generate the code, according to provided instruction.
Formatting the text to get the function itself
The resulting code looks as below.
Use generated code to make it as function.
Define the number 'n' for which the primes() function should process prime numbers.
Test and run the generated function.
Summary and next steps
You successfully completed this notebook!
You learned how to generate code based on instuction with IBM mixtral_8x7b_instruct_v01
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-2025 IBM. This notebook and its source code are released under the terms of the MIT License.