Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ibm
GitHub Repository: ibm/watson-machine-learning-samples
Path: blob/master/cpd4.8/notebooks/python_sdk/deployments/foundation_models/Use watsonx, and Meta `llama-2-70b-chat` to answer question about an article.ipynb
6408 views
Kernel: Python 3 (ipykernel)

image

Use watsonx, and Meta llama-2-70b-chat to answer question about an article

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 question answering in watsonx. It introduces commands for defining prompt and model testing.

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

Learning goal

The goal of this notebook is to demonstrate how to use llama-2-70b-chat model to answer question about provided article.

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:

  • Contact with your Cloud Pack for Data administrator and ask him for your account credentials

Install dependecies

!pip install "ibm-watson-machine-learning>=1.0.321" | tail -n 1

Connection to WML

Authenticate the Watson Machine Learning service on IBM Cloud Pack for Data. You need to provide platform url, your username and api_key.

username = 'PASTE YOUR USERNAME HERE' api_key = 'PASTE YOUR API_KEY HERE' url = 'PASTE THE PLATFORM URL HERE'
wml_credentials = { "username": username, "apikey": api_key, "url": url, "instance_id": 'openshift', "version": '4.8' }

Alternatively you can use username and password to authenticate WML services.

wml_credentials = { "username": ***, "password": ***, "url": ***, "instance_id": 'openshift', "version": '4.8' }

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_watson_machine_learning.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']

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

model_id = ModelTypes.LLAMA_2_70B_CHAT

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_watson_machine_learning.metanames import GenTextParamsMetaNames as GenParams parameters = { GenParams.DECODING_METHOD: "greedy", GenParams.MAX_NEW_TOKENS: 100, GenParams.STOP_SEQUENCES: ["\n\n"] }

Initialize the model

Initialize the Model class with previous set params.

from ibm_watson_machine_learning.foundation_models import Model model = Model( model_id=model_id, params=parameters, credentials=wml_credentials, project_id=project_id)

Model's details

model.get_details()
{'model_id': 'meta-llama/llama-2-70b-chat', 'label': 'llama-2-70b-chat', 'provider': 'Meta', 'source': 'Hugging Face', 'short_description': 'Llama-2-70b-chat is an auto-regressive language model that uses an optimized transformer architecture.', 'long_description': 'Llama-2-70b-chat is a pretrained and fine-tuned generative text model with 70 billion parameters, optimized for dialogue use cases.', 'task_ids': ['question_answering'], 'tasks': [], 'model_limits': {'max_sequence_length': 4096}, 'limits': {'lite': {'max_output_tokens': 900}, 'v2-professional': {'max_output_tokens': 900}, 'v2-standard': {'max_output_tokens': 900}}, 'min_shot_size': 0, 'tier': 'class_3', 'number_params': '70b'}

Answer the question about provided article

Define instructions for the model with few-shot example.

instruction = """ Answer the following question using only information from the article. If there is no good answer in the article, say "I don't know". Article: ### Tomatoes are one of the most popular plants for vegetable gardens. Tip for success: If you select varieties that are resistant to disease and pests, growing tomatoes can be quite easy. For experienced gardeners looking for a challenge, there are endless heirloom and specialty varieties to cultivate. Tomato plants come in a range of sizes. There are varieties that stay very small, less than 12 inches, and grow well in a pot or hanging basket on a balcony or patio. Some grow into bushes that are a few feet high and wide, and can be grown is larger containers. Other varieties grow into huge bushes that are several feet wide and high in a planter or garden bed. Still other varieties grow as long vines, six feet or more, and love to climb trellises. Tomato plants do best in full sun. You need to water tomatoes deeply and often. Using mulch prevents soil-borne disease from splashing up onto the fruit when you water. Pruning suckers and even pinching the tips will encourage the plant to put all its energy into producing fruit. ### Question: Is growing tomatoes easy? Answer: Yes, if you select varieties that are resistant to disease and pests. Question: What varieties of tomatoes are there? Answer: There are endless heirloom and specialty varieties. """

Prepare question for the model.

question = "Question: Why should you use mulch when growing tomatoes?"

Answer the question using Meta llama-2-70b-chat model.

Inter the model to answer the question, according to provided instruction.

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

Explore model output.

print(result)
Answer: Using mulch prevents soil-borne disease from splashing up onto the fruit when you water.

Summary and next steps

You successfully completed this notebook!.

You learned how to answer questions about body of text with Meta's llama-2-70b-chat on watsonx.

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

Authors

Daniel Ryszka, watsonx.ai & Watson Machine Learning.

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