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 `codellama-34b-instruct-hf` to generate code based on instruction.ipynb
6405 views
Kernel: Python 3 (ipykernel)

image

Use watsonx, and codellama-34b-instruct-hf 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 codellama-34b-instruct-hf 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 dependecies

!pip install -U ibm-watsonx-ai | tail -n 1

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): ")

Foundation Models on watsonx.ai

List available models

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

from ibm_watsonx_ai.foundation_models.utils.enums import ModelTypes print([model.name for model in ModelTypes])
['FLAN_T5_XXL', 'FLAN_UL2', 'MT0_XXL', 'GPT_NEOX', 'MPT_7B_INSTRUCT2', 'STARCODER', 'LLAMA_2_70B_CHAT', 'LLAMA_2_13B_CHAT', 'GRANITE_13B_INSTRUCT', 'GRANITE_13B_CHAT', 'FLAN_T5_XL', 'GRANITE_13B_CHAT_V2', 'GRANITE_13B_INSTRUCT_V2', 'ELYZA_JAPANESE_LLAMA_2_7B_INSTRUCT', 'MIXTRAL_8X7B_INSTRUCT_V01_Q']

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

model_id = "codellama/codellama-34b-instruct-hf"

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: 100, 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': 'codellama/codellama-34b-instruct-hf', 'label': 'codellama-34b-instruct-hf', 'provider': 'Code Llama', 'source': 'Hugging Face', 'short_description': 'Code Llama is an AI model built on top of Llama 2, fine-tuned for generating and discussing code.', 'long_description': 'Code Llama is a pretrained and fine-tuned generative text models with 34 billion parameters. This model is designed for general code synthesis and understanding.', 'tier': 'class_2', 'number_params': '34b', 'min_shot_size': 0, 'task_ids': ['code'], 'tasks': [{'id': 'code'}], 'model_limits': {'max_sequence_length': 4096}, 'limits': {'lite': {'call_time': '5m0s', 'max_output_tokens': 4095}, 'v2-professional': {'call_time': '5m0s', 'max_output_tokens': 4095}, 'v2-standard': {'call_time': '5m0s', 'max_output_tokens': 4095}}, 'lifecycle': [{'id': 'available', 'start_date': '2024-03-14'}]}

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 codellama-34b-instruct-hf 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): prime_list = [] for i in range(2, n): for j in range(2, i): if i % j == 0: break else: prime_list.append(i) return prime_list

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 codellama-34b-instruct-hf 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.