### Example: Create Credentials using API Key Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cloud/notebooks/python_sdk/experiments/autoai_rag/html/Use AutoAI RAG with predefined Milvus index to create a pattern about IBM.html Demonstrates how to create credentials using an API key for connecting to IBM watsonx.ai services. This is a common setup step for authentication. ```python from ibm_watsonx_ai import Credentials credentials = Credentials( url="https://us-south.ml.cloud.ibm.com", api_key=IAM_API_KEY ) ``` -------------------------------- ### Example: Create Credentials from Dictionary Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cloud/notebooks/python_sdk/experiments/autoai_rag/html/Use AutoAI RAG with predefined Milvus index to create a pattern about IBM.html Shows how to create credentials from a dictionary, providing an alternative method for authentication setup. Ensure the dictionary contains the correct keys like 'url' and 'apikey'. ```python from ibm_watsonx_ai import Credentials credentials = Credentials.from_dict({ 'url': "", 'apikey': IAM_API_KEY }) ``` -------------------------------- ### Initialize ChromaDB client and collection Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cloud/notebooks/python_sdk/deployments/foundation_models/RAG/html/Use watsonx, and Chroma to answer questions (RAG).html Sets up a ChromaDB client and creates or gets a collection to store the document embeddings. This example uses an in-memory database. ```python import chromadb # Initialize ChromaDB client (in-memory) client = chromadb.Client() # Get or create a collection collection_name = "my_documents" collection = client.get_or_create_collection(collection_name) print(f"ChromaDB client and collection '{collection_name}' initialized.") ``` -------------------------------- ### Process and Format Examples Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cloud/notebooks/rest_api/deployments/foundation_models/Use watsonx, and Google `flan-ul2` to extract the named entities of climate fever document.ipynb Iterates through a list of modified examples, creating formatted strings that include the document text and extracted entities. Each document and its entities are appended to the 'examples' list. ```python for i in range(len(modified_examples_list)): examples.append("document: \n" + modified_examples_list[i]["document"] + "\n") di = copy.deepcopy(modified_examples_list[i]) del di["document"] examples.append("\n") examples.append(str(di)) examples.append("\n\n\n") ``` -------------------------------- ### Install and Import Required Modules Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cpd5.3/notebooks/python_sdk/experiments/autoai_rag/Use AutoAI RAG with watsonx Text Extraction service.ipynb Installs the necessary IBM watsonx.ai SDK with RAG support and the wget utility. Restart the kernel if prompted after installation. ```python %pip install -U 'ibm-watsonx-ai[rag]>=1.4.10' | tail -n 1 %pip install 'wget' ``` -------------------------------- ### Install necessary libraries Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cloud/notebooks/python_sdk/experiments/autoai/html/Use AutoAI and Lale to predict credit risk.html Installs the required libraries for the notebook, including pandas, scikit-learn, and the watsonx.ai SDK. ```python import pandas as pd import numpy as np from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score, roc_auc_score, confusion_matrix from ibm_watson_machine_learning.foundation_models import AutoAI from ibm_watson_machine_learning.foundation_models.utils.enums import ModelTypes, DeploymentTypes, Space, Project from ibm_watson_machine_learning.preprocessing import: TabularPreprocessing from lale.lib.lale import Project as LaleProject from lale.lib.lale import make_pipeline from lale.lib.sklearn import LogisticRegression, RandomForestClassifier from lale.lib.lale import AutoMLClassifier from sklearn.model_selection import StratifiedKFold from sklearn.pipeline import Pipeline from sklearn.preprocessing import StandardScaler, OneHotEncoder from sklearn.compose import ColumnTransformer from sklearn.impute import SimpleImputer from sklearn.linear_model import LogisticRegression from sklearn.ensemble import RandomForestClassifier from sklearn.metrics import classification_report import warnings warnings.filterwarnings('ignore') from IPython.display import display, Markdown import logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) import os from dotenv import load_dotenv load_dotenv() # Set up Watsonx.ai credentials api_key = os.getenv("api_key") project_id = os.getenv("project_id") # Initialize Watsonx.ai client from ibm_watson_machine_learning import APIClient client = APIClient( {"url": "https://us-south.ml.cloud.ibm.com", "apikey": api_key} ) client.set.project_id(project_id) # Set the project and space project_link = Space.private space_link = Project.default ``` -------------------------------- ### Initialize Lale Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cloud/notebooks/python_sdk/experiments/autoai/html/Use AutoAI and Lale to predict credit risk.html Initialize Lale for use in the notebook. This is a standard setup step for Lale. ```python import lale.helpers as lh lale.helpers.init(verbose=True) ``` -------------------------------- ### Install necessary libraries Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cloud/notebooks/python_sdk/experiments/autoai/html/Use AutoAI and Lale to predict credit risk.html Installs the required libraries for the notebook, including pandas, scikit-learn, and lale. ```python import pandas as pd import numpy as np from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score, roc_auc_score, confusion_matrix, classification_report import lale.helpers import lale.lib.lale_datasets import lale.lib.sklearn from lale.lib.lale_datasets import load_breast_cancer from lale.lib.sklearn import LogisticRegression, XGBClassifier, LGBMClassifier, RandomForestClassifier, GradientBoostingClassifier, SVC from sklearn.model_selection import StratifiedKFold from sklearn.preprocessing import StandardScaler, QuantileTransformer from sklearn.pipeline import Pipeline from sklearn.compose import ColumnTransformer from sklearn.impute import SimpleImputer from sklearn.preprocessing import OneHotEncoder from autoai_ts_api.autoai_ts_api import AutoAI from autoai_ts_api.utils.data_utils import DataUtils from sklearn.model_selection import train_test_split import warnings warnings.filterwarnings('ignore') # Set pandas display options pd.set_option('display.max_rows', None) pd.set_option('display.max_columns', None) pd.set_option('display.width', None) pd.set_option('display.max_colwidth', None) # Set random seed for reproducibility np.random.seed(42) # Load the dataset df = pd.read_csv('credit_risk.csv') # Display the first 5 rows of the dataframe df.head() ``` -------------------------------- ### Install and import necessary libraries Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cloud/notebooks/python_sdk/experiments/autoai/html/Use AutoAI and Lale to predict credit risk.html Installs and imports the required libraries for the AutoAI and Lale functionalities. Ensure these are installed in your environment. ```python import sys from packaging.version import Version if Version(sys.version.split(" ")[0]) < Version("3.9.0"): %pip install "lale>=0.7.0" --upgrade else: %pip install "lale>=0.8.0" --upgrade import pandas as pd import numpy as np from sklearn.model_selection import traintest_split from lale.lib.lale_datasets import load_credit_risk from lale.lib.rasa import LogisticRegressionGD, LRLearner from lale.lib.lale import Hyperopt from sklearn.metrics import roc_auc_score from IPython.display import display import warnings warnings.filterwarnings("ignore") ``` -------------------------------- ### Install necessary libraries Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cloud/notebooks/python_sdk/experiments/autoai/html/Use AutoAI and Lale to predict credit risk.html Installs the required libraries for the notebook, including AutoAI and Lale. ```python import sys from packaging import version if version.parse(sys.version) < version.parse("3.9"): # Check if Python version is less than 3.9 print("Python 3.9 or higher is required to run this notebook.") else: print("Python version is 3.9 or higher.") # Install required libraries !pip install -U pip==21.3.1 !pip install -U scikit-learn==1.0.2 !pip install -U pandas==1.4.2 !pip install -U numpy==1.22.3 !pip install -U seaborn==0.11.2 !pip install -U matplotlib==3.5.1 !pip install -U autoai-libs==1.14.1 !pip install -U lale==1.7.0 !pip install -U folium==0.12.1.post1 !pip install -U ``` -------------------------------- ### Install ibm-watsonx-ai and scikit-learn Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cloud/notebooks/python_sdk/deployments/scikit-learn/Use scikit-learn to recognize hand-written digits.ipynb Installs the necessary libraries for interacting with watsonx.ai and for scikit-learn model development. Use `tail -n 1` to display only the last line of the pip installation output. ```python %pip install -U ibm-watsonx-ai | tail -n 1 %pip install "scikit-learn==1.6.1" | tail -n 1 ``` -------------------------------- ### Install ibm_watsonx_ai SDK Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cpd5.3/notebooks/python_sdk/deployments/ai_services/Use watsonx to run AI service and switch between LLMs by updating the deployment.ipynb Installs or upgrades the ibm_watsonx_ai Python SDK. Use `tail -n 1` to display only the last line of the output, which typically indicates the installed version. ```python %pip install -U "ibm_watsonx_ai>=1.3.33" | tail -n 1 ``` -------------------------------- ### Install necessary libraries Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cloud/notebooks/python_sdk/deployments/foundation_models/RAG/html/Use watsonx, and Elasticsearch Python SDK to answer questions (RAG).html Install the required Python libraries for watsonx.ai and Elasticsearch. ```python pip install --upgrade watson-machine-learning-sdk pip install --upgrade elasticsearch ``` -------------------------------- ### Install necessary libraries Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cloud/notebooks/python_sdk/experiments/autoai/html/Use AutoAI and Lale to predict credit risk.html Installs the required Python libraries for the project. Ensure you have a compatible Python version. ```python import sys if sys.version_info.major != 3 or sys.version_info.minor < 7: raise Exception('This notebook requires Python 3.7 or greater.') # Install the necessary libraries print("Installing necessary libraries...") !pip install -q -U pip !pip install -q -U scikit-learn !pip install -q -U pandas !pip install -q -U numpy !pip install -q -U matplotlib !pip install -q -U seaborn !pip install -q -U autoai-libs !pip install -q -U lale !pip install -q -U 'ibm-watson-machine-learning>=0.70.0' !pip install -q -U 'ibm-cloud-sdk-core>=3.10.0' !pip install -q -U 'ibm-cos-sdk>=2.7.0' !pip install -q -U 'jupyterlab>=3' !pip install -q -U nbformat print("Libraries installed.") ``` -------------------------------- ### Install and Initialize Watsonx.ai SDK Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cpd5.3/notebooks/python_sdk/deployments/custom_image/Use Custom Image, Software Specification and Runtime Definition to deploy a python function rt25.1.ipynb Install the `ibm-watsonx-ai` Python SDK and initialize the API client with your WML credentials. The SDK documentation can be found at the provided link. ```bash !pip install -U ibm-watsonx-ai | tail -n 1 ``` ```python from ibm_watsonx_ai import APIClient client = APIClient(wml_credentials) ``` -------------------------------- ### Get Start Angle Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cloud/notebooks/python_sdk/experiments/autoai/html/Use AutoAI and BatchedTreeEnsemble to solve large tabular problems.html A getter function for the start angle of an arc. ```javascript function pu(X){return X.startAngle} ``` -------------------------------- ### Initialize Examples List Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cloud/notebooks/rest_api/deployments/foundation_models/Use watsonx to extract the named entities of climate fever document.ipynb Initializes an empty list named 'examples'. This is likely a placeholder for further processing or data aggregation. ```python examples = [] ``` -------------------------------- ### Get example values for Text Extraction steps Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cloud/notebooks/python_sdk/deployments/foundation_models/Use watsonx Text Extraction service to extract text from file.ipynb Retrieve example parameter values for text extraction steps like OCR and table processing. This is useful for understanding the expected input format. ```python TextExtractionsMetaNames().get_example_values() ``` -------------------------------- ### Define Few-Shot Instructions Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cloud/notebooks/python_sdk/deployments/foundation_models/Use watsonx, and Meta `llama-3-3-70b-instruct` to answer question about an article.ipynb Prepare instructions for the model, including an article and example question-answer pairs, to guide its responses. ```python 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. """ ``` -------------------------------- ### Install Python Libraries Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cloud/notebooks/python_sdk/deployments/spark/html/Use Spark to predict product line.html Installs the `wget`, `pyspark`, and `ibm-watsonx-ai` libraries. Use this to set up your environment. ```python %pip install wget %pip install pyspark\==3.4.3 | tail \-n 1 %pip install \-U ibm\-watsonx\-ai | tail \-n 1 ``` -------------------------------- ### Get Line Clipping Information Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cloud/notebooks/python_sdk/experiments/autoai/html/Use AutoAI and BatchedTreeEnsemble to solve large tabular problems.html Extracts clipping start and end properties from a feature. These properties define the visible portion of a line. ```javascript sh.prototype.lineFeatureClips = function(y) { if (y.properties && y.properties.hasOwnProperty("mapbox_clip_start") && y.properties.hasOwnProperty("mapbox_clip_end")) { var I = +y.properties.mapbox_clip_start, U = +y.properties.mapbox_clip_end; return { start: I, end: U } } } ``` -------------------------------- ### Print First Few-Shot Example Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cpd5.3/notebooks/python_sdk/deployments/foundation_models/Use watsonx, and `granite-guardian-3-8b` to analyze eXtensive Business Reporting Language (XBRL) tags of financial reports.ipynb Display the first formatted example from the `few_shot_input` list to verify the data preparation. ```python print(few_shot_input[0]) ``` -------------------------------- ### Lale Pipeline Example Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cloud/notebooks/python_sdk/experiments/autoai/html/Use AutoAI and Lale to predict credit risk.html Demonstrates a simple Lale pipeline for data preprocessing and model training. ```python from lale.lib.lale import CombineFeatures, NoOp from lale.lib.sklearn import LogisticRegression # Define a Lale pipeline lale_pipeline = CombineFeatures(operator=NoOp(), input_features=['feature1', 'feature2'], output_features=['combined_feature']) lale_pipeline = lale_pipeline >> LogisticRegression() # Train the pipeline lale_pipeline.fit(train_data, label='Risk') # Make predictions lale_predictions = lale_pipeline.predict(test_data) ``` -------------------------------- ### Get selected pattern Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cloud/notebooks/python_sdk/experiments/autoai_rag/html/Use AutoAI RAG with predefined Milvus index to create a pattern about IBM.html This section details how to retrieve a selected pattern. It includes the return type and an example of how to use the `Credentials.from_dict` method. ```APIDOC ## Get selected pattern ### Description Retrieves a selected pattern. ### Return Type dict ### Example ```python from ibm_watsonx_ai import Credentials credentials = Credentials.from_dict({ 'url': '', 'apikey': IAM_API_KEY }) ``` ``` -------------------------------- ### Define a simple tool for the model Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cpd5.3/notebooks/python_sdk/deployments/foundation_models/chat/html/Use watsonx, and `granite-3-3-8b-instruct` to make simple chat conversation and tool calls.html Defines a Python function that the LLM can call. This example creates a tool to get the current date and time. ```python import datetime def get_current_datetime(): """Returns the current date and time.""" return datetime.datetime.now().isoformat() ``` -------------------------------- ### APIClient Initialization and Basic Usage Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cloud/notebooks/python_sdk/experiments/autoai_rag/Use AutoAI RAG and Milvus to create a pattern about IBM.ipynb Demonstrates how to initialize the APIClient with credentials and perform basic operations like listing models and getting deployment details. ```APIDOC ## APIClient Initialization and Usage ### Description Initializes the APIClient with credentials and demonstrates basic operations. ### Method `APIClient(credentials, space_id=None)` ### Parameters - **credentials** (Credentials) - Required - Authentication credentials. - **space_id** (str, optional) - The ID of the space to use. ### Request Example ```python from ibm_watsonx_ai import APIClient, Credentials credentials = Credentials( url = "", api_key = IAM_API_KEY ) client = APIClient(credentials, space_id="") # Example operations client.models.list() client.deployments.get_details() client.set.default_project("") ``` ``` -------------------------------- ### Score Deployment with Payload Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cpd5.3/notebooks/python_sdk/deployments/custom_image/Use Custom Image, Software Specification and Runtime Definition to deploy a python function rt25.1.ipynb Send a scoring payload to the deployed Python function to get predictions. This example demonstrates how to format the input data. ```python scoring_payload = { "input_data": [{"fields": ["dummy", "dummy_c2"], "values": [[1, 1]]}] } predictions = client.deployments.score(deployment_id, scoring_payload) predictions ``` -------------------------------- ### Initialize Lale Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cloud/notebooks/python_sdk/experiments/autoai/html/Use AutoAI and Lale to predict credit risk.html Initializes Lale for use in the notebook. This is a common setup step for Lale-based experiments. ```python import lale.helpers as h h.init(context='local') ``` -------------------------------- ### Create Few-Shot Examples Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cloud/notebooks/rest_api/deployments/foundation_models/Use watsonx to summarize Cybersecurity documents.ipynb Create few-shot examples by sampling two training instances and formatting them as 'sentence: [text]\nSimplification: [summary]' pairs. ```python train_samples = data_train_and_labels.sample(2) few_shot_example = [] examples = [] for s in range(len(train_samples)): examples.append( f"\tsentence:\t{train_samples['Paragraph'].iloc[s]}\n\tSimplification: {train_samples['Simplification'].iloc[s]}\n" ) few_shot_examples = ["".join(examples)] ``` -------------------------------- ### Define a simple tool for LlamaIndex Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cloud/notebooks/python_sdk/deployments/foundation_models/chat/html/Use watsonx, `llama-3-3-70b-instruct` and LlamaIndex to make simple chat conversation and tool calls.html Create a basic tool that the LlamaIndex agent can use. This example defines a tool to get the current date. ```python from llama_index.core.tools import FunctionTool from datetime import datetime def get_current_date(): "Returns the current date" return datetime.now().strftime("%Y-%m-%d") # Wrap the function in a FunctionTool date_tool = FunctionTool.from_defaults(fn=get_current_date) # Define a tool for a simple calculation def simple_calculator(expression: str): "Evaluates a mathematical expression" try: return eval(expression) except Exception as e: return f"Error evaluating expression: {e}" calculator_tool = FunctionTool.from_defaults(fn=simple_calculator, name="calculator", description="Evaluates a mathematical expression") ``` -------------------------------- ### Prepare Few-Shot Examples Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cloud/notebooks/python_sdk/deployments/foundation_models/Use watsonx, and `mistral-small-3-1-24b-instruct-2503` to summarize legal Contracts documents.ipynb Construct few-shot examples for the model by alternating between document and summary pairs from the data sample. ```python few_shot_input = [] few_shot_target = [] singleoutput = [] for i, tl in enumerate(data_sample.values): if (i + 1) % 2 == 0: singleoutput.append(f" document: {tl[0]} summary:") few_shot_input.append("".join(singleoutput)) few_shot_target.append(tl[1]) singleoutput = [] else: singleoutput.append(f" document: {tl[0]} summary: {tl[1]}") ``` -------------------------------- ### Import RAG Libraries Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cpd5.3/notebooks/python_sdk/deployments/foundation_models/RAG/Use watsonx, and Chroma to answer questions (RAG).ipynb Imports SentenceTransformer from sentence_transformers and ChromaDB components from chromadb. Includes error handling to guide users if packages are not installed. ```python try: from sentence_transformers import SentenceTransformer except ImportError: raise ImportError( "Could not import sentence_transformers: Please install sentence-transformers package." ) try: import chromadb from chromadb.api.types import EmbeddingFunction except ImportError: raise ImportError("Could not import chromadb: Please install chromadb package.") ``` -------------------------------- ### Initialize watsonx.ai and Chroma clients Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cpd5.3/notebooks/python_sdk/deployments/foundation_models/RAG/html/Use watsonx, and Chroma to answer questions (RAG).html Sets up the connection to watsonx.ai and initializes the Chroma in-memory vector store. ```python from ibm_watson_machine_learning.foundation_models.utils.enums import ModelTypes from ibm_watson_machine_learning.foundation_models import Model from langchain_community.document_loaders import PyPDFLoader from langchain_community.embeddings import HuggingFaceEmbeddings from langchain_community.vectorstores import Chroma from langchain.text_splitter import RecursiveCharacterTextSplitter # Initialize watsonx.ai model # Replace with your actual API key and URL # model = Model(model_id='meta-llama/llama-2-70b-chat', credentials={'apikey': 'YOUR_API_KEY'}, url='YOUR_WATSONX_URL') # Initialize Chroma in-memory vector store # persist_directory = "chroma_db" # vectorstore = Chroma(persist_directory=persist_directory, embedding_function=HuggingFaceEmbeddings()) # For demonstration purposes, we'll use a dummy model and skip Chroma persistence class DummyModel: def __init__(self, model_id): self.model_id = model_id def generate(self, prompt): # Simulate a response return {"results": [{"generated_text": f"This is a simulated answer to: {prompt}"}]} model = DummyModel(model_id='meta-llama/llama-2-70b-chat') # Initialize Chroma in-memory vector store vectorstore = Chroma(embedding_function=HuggingFaceEmbeddings()) ``` -------------------------------- ### Install ibm-watsonx-ai SDK Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cloud/notebooks/python_sdk/deployments/foundation_models/chat/Use watsonx, and `granite-3-3-8b-instruct` to perform chat conversation with control roles.ipynb Installs the `ibm-watsonx-ai` Python SDK. Use this to interact with watsonx.ai services. The `tail -n 1` command filters the output to show only the last line, indicating successful installation. ```python %pip install -U "ibm-watsonx-ai" | tail -n 1 ``` -------------------------------- ### Get Best RAG Pattern Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cloud/notebooks/python_sdk/experiments/autoai_rag/Use AutoAI RAG with SQL knowledge base reference.ipynb Retrieves the best RAG pattern from the RAG Optimizer experiment. This is useful for understanding and utilizing the most effective pattern for your RAG setup. ```python best_pattern_name = summary.index.values[0] print("Best pattern is:", best_pattern_name) best_pattern = rag_optimizer.get_pattern() ``` -------------------------------- ### Get Available Projects Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cloud/notebooks/python_sdk/lifecycle-management/Use ibm-watsonx-ai to extract data about historical AutoAI experiments and deployments stored in spaces.ipynb Retrieves a list of project GUIDs accessible via API. This method uses direct requests as SDK support may be pending. ```python projects_url = f"{api_client.PLATFORM_URL}/v2/projects" params = api_client._params(skip_for_create=True) params["limit"] = 100 response = requests.get( url=projects_url, params=params, headers=api_client._get_headers() ) available_projects = [ resource.get("metadata", {}).get("guid") for resource in response.json().get("resources", []) ] ``` ```python print(f"Available projects: [{', '.join(available_projects)}]") ``` -------------------------------- ### Initialize Lale Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cloud/notebooks/python_sdk/experiments/autoai/html/Use AutoAI and Lale to predict credit risk.html Initializes Lale for use in the notebook. This is a common setup step for Lale-based operations. ```python import lale.helpers lale.helpers.init_notebook_mode() ``` -------------------------------- ### WebGPU Buffer and Shader Setup Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cloud/notebooks/python_sdk/experiments/autoai/html/Use AutoAI and Lale to predict credit risk.html Initializes buffers and shaders for WebGPU rendering. This includes setting up vertex buffers, VAOs, and shaders. ```javascript function p(S,L,x,C,M,g,P,T){this.gl=S,this.vertBuffer=L,this.vao=x,this.shader=C,this.tickCount=M,this.tickOffset=g,this.gridCount=P,this.gridOffset=T} ``` -------------------------------- ### Define Instruction for Code Generation Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cpd5.3/notebooks/python_sdk/deployments/foundation_models/Use watsonx and IBM `granite-3b-code-instruct` to generate code based on instruction.ipynb Prepare a detailed instruction for the model, including an example of the desired input and output format. This guides the model in generating accurate code. ```python instruction = """Using the directions below, generate Python code for the given task. Input: # Write a Python function that prints 'Hello World!' string 'n' times. Output: def print_n_times(n): for i in range(n): print("Hello World!") """ ``` -------------------------------- ### Initialize ChromaDB Client and Collection Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cloud/notebooks/python_sdk/deployments/foundation_models/RAG/html/Use watsonx, and Chroma to answer questions (RAG).html Sets up the ChromaDB client and creates or gets a collection for storing embeddings. Ensure the 'sentence-transformers/all-MiniLM-L6-v2' model is available. ```python import chromadb from sentence_transformers import SentenceTransformer # Initialize ChromaDB client client = chromadb.Client() # Get or create a collection collection_name = "my_documents" collection = client.get_or_create_collection(collection_name) # Load the sentence transformer model model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2') ``` -------------------------------- ### Define Python Function for Custom Image Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cpd5.3/notebooks/python_sdk/deployments/custom_image/Use Custom Image, Software Specification and Runtime Definition to deploy a python function rt25.1.ipynb Define a Python function that utilizes packages installed in a custom Docker image. This example uses the 'pendulum' package to return its version. ```python def score_generator(): # Define scoring function def callModel(payload_scoring): import pendulum v4_scoring_response = {"predictions": [{"values": [pendulum.__version__]}]} return v4_scoring_response def score(input): # Score using the pre-defined model prediction = callModel(input) return prediction return score ``` -------------------------------- ### Create a watsonx.ai provider and deploy a model Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cloud/notebooks/python_sdk/deployments/ai_services/Use watsonx, and Model Gateway to run as AI service with load balancing.ipynb Create a watsonx.ai provider and then deploy the `ibm/granite-3-8b-instruct` model with the specified alias. This is the first step in setting up load balancing for this model. ```python granite_3_model = "ibm/granite-3-8b-instruct" watsonx_ai_provider_1_details = gateway.providers.create( provider="watsonxai", name="watsonx-ai-provider-1", secret_crn_id=secret_crn_id ) watsonx_ai_provider_1_id = gateway.providers.get_id(watsonx_ai_provider_1_details) granite_3_model_details = gateway.models.create( provider_id=watsonx_ai_provider_1_id, model=granite_3_model, alias=model_alias ) granite_3_model_id = gateway.models.get_id(granite_3_model_details) ``` -------------------------------- ### Initialize Watson Machine Learning Clients Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cloud/notebooks/python_sdk/migration/html/v3_to_v4ga_cloud_assets_migration_sample.html Demonstrates how to initialize both v3 and v4ga Watson Machine Learning clients using API keys and URLs. ```APIDOC ## Initialize Watson Machine Learning Clients ### Description Initialize the Watson Machine Learning clients for both v3 and v4ga. The v3 client requires an `instance_id`, while the v4ga client does not. ### Code Example ```python # v3 client initialization (deprecated) v3_wml_credentials = { "apikey": "", "url": "https://wml-fvt.ml.test.cloud.ibm.com", "instance_id": "a5a0e69c-cb3d-4b81-b357-3d3f5d8eb600" # v1 instance plan/id where assets are } v3_client = WatsonMachineLearningAPIClient(v3_wml_credentials) print('Using cluster', v3_wml_credentials['url']) # v4ga client initialization from ibm_watson_machine_learning import APIClient v4_wml_credentials = { "apikey": "", "url": "https://wml-fvt.ml.test.cloud.ibm.com" } v4ga_client = APIClient(v4_wml_credentials) print('Using cluster', v4_wml_credentials['url']) ``` ``` -------------------------------- ### Define English to Spanish Translation Query Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cloud/notebooks/python_sdk/deployments/foundation_models/Use watsonx, and `granite-4-h-small` to support multiple languages translation.ipynb Construct a query for translation, including at least one example to guide the model. Ensure a newline character at the end of the prompt for optimal performance. ```python english_to_spanish_query = """Translate the following text from English to Spanish: Input: So far, I have not been terribly encouraged by the stance adopted by the Commission. Output: Hasta ahora no me ha animado mucho la postura adoptada por la Comisión. Input: I am very pleased to see that the joint resolution adopts the suggestion we made. """ ``` -------------------------------- ### Initialize APIClient with Credentials Class Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cloud/notebooks/python_sdk/experiments/autoai_rag/Use AutoAI RAG and Milvus to create a pattern about IBM.ipynb Demonstrates the recommended way to initialize the `APIClient` using the `Credentials` class for secure and up-to-date credential management. This approach is preferred over using a dictionary. ```python from ibm_watsonx_ai import APIClient, Credentials # Create a Credentials instance credentials = Credentials( url="https://us-south.ml.cloud.ibm.com", token="***********" ) # Initialize the APIClient with the Credentials instance client = APIClient(credentials) # Now you can use the client to interact with the API url = client.credentials.url # OR url = credentials.url ``` -------------------------------- ### Configure Arc Properties Source: https://github.com/ibm/watsonx-ai-samples/blob/master/cloud/notebooks/python_sdk/experiments/autoai/html/Use AutoAI and BatchedTreeEnsemble to solve large tabular problems.html Sets or gets properties for an arc generator, including inner radius, outer radius, corner radius, padding radius, start angle, end angle, and padding angle. ```javascript nt.innerRadius=function(gr){return arguments.length?(X=vi(gr),nt):X},nt.outerRadius=function(gr){return arguments.length?(se=vi(gr),nt):se},nt.cornerRadius=function(gr){return arguments.length?(Te=vi(gr),nt):Te},nt.padRadius=function(gr){return arguments.length?(Ne=gr==Cu?Cu:vi(gr),nt):Ne},nt.startAngle=function(gr){return arguments.length?(He=vi(gr),nt):He},nt.endAngle=function(gr){return arguments.length?(Ye=vi(gr),nt):Ye},nt.padAngle=function(gr){return arguments.length?(kt=vi(gr),nt):kt} ```