Getting information about available foundation models programmatically
Find out what foundation models are available for use in IBM watsonx.ai.
The watsonx.ai Python library has a helper class for referencing the model IDs and names. For more information, see ChatModels.
The following code uses the ChatModels helper class to list the model IDs of the available models.
Python code
from ibm_watsonx_ai import APIClient
from ibm_watsonx_ai import Credentials
credentials = Credentials(
url = "https://<region>.ml.cloud.ibm.com",
api_key = <my-IBM-Cloud-API-key>,
)
api_client = APIClient(credentials)
api_client.foundation_models.ChatModels.show()
Sample output
{'GRANITE_3_3_8B_INSTRUCT': 'ibm/granite-3-3-8b-instruct',
'GRANITE_8B_CODE_INSTRUCT': 'ibm/granite-8b-code-instruct',
...
}
Example: View details of a foundation model
You can view details, such as a short description and foundation model limits, by using get_details().
Python code
from ibm_watsonx_ai.foundation_models import ModelInference
import json
model_id = api_client.foundation_models.ChatModels.GRANITE_3_3_8B_INSTRUCT
project_id = <my-project-ID>
model = ModelInference(model_id=model_id, project_id=project_id, api_client=api_client)
model_details = model.get_details()
print(json.dumps(model_details, indent=2))
Note:
Replace <region>, <my-IBM-Cloud-API-key>, and <my-project-ID> with valid values for your environment.
Sample output
{
"model_id": "ibm/granite-3-3-8b-instruct",
"label": "granite-3-3-8b-instruct",
"provider": "IBM",
"source": "Hugging Face",
"short_description": "granite-3-3-8b-instruct model is an 8 billion parameter model based on the Granite 3.3 Instruct foundation model family.",
...
}
The following code sample uses a foundation model's ID to view model details.
import json
model_id = api_client.foundation_models.ChatModels.GRANITE_3_3_8B_INSTRUCT
model_details = api_client.foundation_models.get_model_specs(model_id)
print(json.dumps(model_details, indent=2))
You can specify the model_id in inference requests as follows:
from ibm_watsonx_ai.foundation_models import ModelInference
model = ModelInference(
model_id="ibm/granite-3-3-8b-instruct",
...
)