# Foundation models Python library #

You can prompt foundation models in IBM watsonx\.ai programmatically by using the Python library\.

The Watson Machine Learning Python library is a publicly available library that you can use to work with Watson Machine Learning services\. The Watson Machine Learning service hosts the watsonx\.ai foundation models\.

## Using the Python library ##

After you create a prompt in the Prompt Lab, you can save the prompt as a notebook, and then edit the notebook\. Using the generated notebook as a starting point is useful because it handles the initial setup steps, such as getting credentials and the project ID information for you\.

If you want to work with the models directly from a notebook, you can do so by using the Watson Machine Learning Python library\.

The `ibm-watson-machine-learning` Python library is publicly available on PyPI from the url: [https://pypi\.org/project/ibm\-watson\-machine\-learning/](https://pypi.org/project/ibm-watson-machine-learning/)\. However, you can install it in your development environment by using the following command:

    pip install ibm-watson-machine-learning

If you installed the library before, include the `-U` parameter to ensure that you have the latest version\.

    pip install -U ibm-watson-machine-learning

For more information about the available methods for working with foundation models, see [Foundation models Python library](https://ibm.github.io/watson-machine-learning-sdk/foundation_models.html)\.

You need to take some steps before you can use the Python library:

<!-- <ul> -->

 *  [Setting up credentials](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/fm-credentials.html)
 *  [Looking up your project ID](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/fm-python-lib.html?context=cdpaas&locale=en#project-id)

<!-- </ul> -->

### Looking up your project ID ###

To prompt foundation models in IBM watsonx\.ai programmatically, you need to pass the identifier (ID) of a project that has an instance of IBM Watson Machine Learning associated with it\.

To get the ID of a project, complete the following steps:

<!-- <ol> -->

1.  Navigate to the project in the watsonx web console, open the project, and then click the **Manage** tab\.
2.  Copy the project ID from the *Details* section of the *General* page\.

<!-- </ol> -->

## Examples ##

The following examples show you how to use the library to perform a few basic tasks in a notebook\.

### Example 1: List available foundation models ###

You can view [`ModelTypes`](https://ibm.github.io/watson-machine-learning-sdk/foundation_models.html#ibm_watson_machine_learning.foundation_models.utils.enums.ModelTypes) to see available foundation models\.

**Python code**  

    from ibm_watson_machine_learning.foundation_models.utils.enums import ModelTypes
    import json
    
    print( json.dumps( ModelTypes._member_names_, indent=2 ) )

**Sample output**  

    [
      "FLAN_T5_XXL",
      "FLAN_UL2",
      "MT0_XXL",
      ...
    ]

### Example: View details of a foundation model ###

You can view details, such as a short description and foundation model limits, by using [`get_details()`](https://ibm.github.io/watson-machine-learning-sdk/foundation_models.html#ibm_watson_machine_learning.foundation_models.Model.get_details)\.

**Python code**  

    from ibm_watson_machine_learning.foundation_models.utils.enums import ModelTypes
    from ibm_watson_machine_learning.foundation_models import Model
    import json
    
    my_credentials = { 
        "url"    : "https://us-south.ml.cloud.ibm.com", 
        "apikey" : {my-IBM-Cloud-API-key}
    }
    
    model_id    = ModelTypes.MPT_7B_INSTRUCT2
    gen_parms   = None
    project_id  = {my-project-ID}
    space_id    = None
    verify      = False
    
    model = Model( model_id, my_credentials, gen_parms, project_id, space_id, verify )
    
    model_details = model.get_details()
    
    print( json.dumps( model_details, indent=2 ) )

Note:Replace `{my-IBM-Cloud-API-key}` and `{my-project-ID}` with your API key and project ID\.

**Sample output**  

    {
      "model_id": "ibm/mpt-7b-instruct2",
      "label": "mpt-7b-instruct2",
      "provider": "IBM",
      "source": "Hugging Face",
      "short_description": "MPT-7B is a decoder-style transformer pretrained from 
    scratch on 1T tokens of English text and code. This model was trained by IBM.",
      ...
    }

### Example: Prompt a foundation model with default parameters ###

Prompt a foundation model to generate a response\.

**Python code**  

    from ibm_watson_machine_learning.foundation_models.utils.enums import ModelTypes
    from ibm_watson_machine_learning.foundation_models import Model
    import json
    
    my_credentials = { 
        "url"    : "https://us-south.ml.cloud.ibm.com", 
        "apikey" : {my-IBM-Cloud-API-key}
    }      
    
    model_id    = ModelTypes.FLAN_T5_XXL
    gen_parms   = None
    project_id  = {my-project-ID}
    space_id    = None
    verify      = False
    
    model = Model( model_id, my_credentials, gen_parms, project_id, space_id, verify )   
    
    prompt_txt = "In today's sales meeting, we "
    gen_parms_override = None
    
    generated_response = model.generate( prompt_txt, gen_parms_override )
    
    print( json.dumps( generated_response, indent=2 ) )

Note:Replace `{my-IBM-Cloud-API-key}` and `{my-project-ID}` with your API key and project ID\.

**Sample output**

    {
      "model_id": "google/flan-t5-xxl",
      "created_at": "2023-07-27T03:40:17.575Z",
      "results": [
        {
          "generated_text": "will discuss the new product line.",
          "generated_token_count": 8,
          "input_token_count": 10,
          "stop_reason": "EOS_TOKEN"
        }
      ],
      ...
    }

## Learn more ##

<!-- <ul> -->

 *  [Credentials for prompting foundation models](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/fm-credentials.html)

<!-- </ul> -->

**Parent topic:**[Foundation models](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/fm-overview.html)

<!-- </article "role="article" "> -->