llama-2-70b-chat
to answer question about an article¶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.
The goal of this notebook is to demonstrate how to use llama-2-70b-chat
model to answer question about provided article.
This notebook contains the following parts:
Before you use the sample code in this notebook, you must perform the following setup tasks:
!pip install "ibm-watson-machine-learning>=1.0.321" | tail -n 1
This cell defines the WML credentials required to work with watsonx Foundation Model inferencing.
Action: Provide the IBM Cloud user API key. For details, see documentation.
import getpass
credentials = {
"url": "https://us-south.ml.cloud.ibm.com",
"apikey": getpass.getpass("Please enter your WML api key (hit enter): ")
}
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): ")
watsonx.ai
¶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
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
class with previous set params.
from ibm_watson_machine_learning.foundation_models import Model
model = Model(
model_id=model_id,
params=parameters,
credentials=credentials,
project_id=project_id)
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'}
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?"
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.
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 the Online Documentation for more samples, tutorials, documentation, how-tos, and blog posts.
Daniel Ryszka, watsonx.ai & Watson Machine Learning.
Copyright © 2023 IBM. This notebook and its source code are released under the terms of the MIT License.