IBM watsonx.ai Python SDK

1.5.6 · active · verified Sat Apr 11

The `ibm-watsonx-ai` library is the official Python SDK for IBM watsonx.ai, an enterprise-grade AI and data platform for building, training, tuning, deploying, and operating AI models at scale. It provides a unified interface to watsonx.ai capabilities, including Foundation Models (LLMs), AutoAI experiments, Retrieval-Augmented Generation (RAG), model tuning, deployment, and data integration. The library is actively maintained with regular updates and new feature releases.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to authenticate with the `ibm-watsonx-ai` SDK using environment variables and perform a basic text generation task with a foundation model. Ensure your `IBM_CLOUD_API_KEY`, `WATSONX_AI_URL`, and `WATSONX_PROJECT_ID` environment variables are set.

import os
from ibm_watsonx_ai import APIClient, Credentials
from ibm_watsonx_ai.foundation_models import Model
from ibm_watsonx_ai.foundation_models.utils.enums import ModelTypes, DecodingMethods

# --- Authentication ---
# Set these environment variables: IBM_CLOUD_API_KEY, WATSONX_AI_URL, WATSONX_PROJECT_ID
api_key = os.environ.get("IBM_CLOUD_API_KEY", "")
watsonx_url = os.environ.get("WATSONX_AI_URL", "https://us-south.ml.cloud.ibm.com")
project_id = os.environ.get("WATSONX_PROJECT_ID", "")

if not all([api_key, watsonx_url, project_id]):
    raise ValueError("Please set IBM_CLOUD_API_KEY, WATSONX_AI_URL, and WATSONX_PROJECT_ID environment variables.")

credentials = Credentials(
    api_key=api_key,
    url=watsonx_url
)

client = APIClient(credentials)
client.set.default_project(project_id)
print(f"Authenticated successfully for project_id: {project_id}")

# --- Foundation Model Inference (Text Generation) ---
model_id = ModelTypes.GRANITE_13B_INSTRUCT.value # Or other supported model like MISTRAL_7B_INSTRUCT

# Model parameters for text generation
parameters = {
    "decoding_method": DecodingMethods.GREEDY,
    "max_new_tokens": 50,
    "min_new_tokens": 1,
    "stop_sequences": []
}

# Initialize the model object
model = Model(
    model_id=model_id,
    credentials=credentials,
    project_id=project_id,
    parameters=parameters
)

# Define the prompt
prompt = "Write a short poem about artificial intelligence:"

# Generate text
try:
    print(f"\nPrompt: {prompt}")
    generated_text = model.generate_text(prompt=prompt)
    print(f"Generated Text:\n{generated_text}")
except Exception as e:
    print(f"An error occurred during text generation: {e}")

view raw JSON →