From 3a9951d60261dda21da83ec098f5e42700a2b70b Mon Sep 17 00:00:00 2001 From: Yuce Dincer Date: Fri, 11 Jul 2025 14:57:33 -0400 Subject: [PATCH 1/7] ibm watsonxai code --- .../Get Started with watsonx.ai & Llama.ipynb | 697 ++++++++++++++++++ 1 file changed, 697 insertions(+) create mode 100644 3p-integrations/ibm/Get Started with watsonx.ai & Llama.ipynb diff --git a/3p-integrations/ibm/Get Started with watsonx.ai & Llama.ipynb b/3p-integrations/ibm/Get Started with watsonx.ai & Llama.ipynb new file mode 100644 index 000000000..e0b6431ba --- /dev/null +++ b/3p-integrations/ibm/Get Started with watsonx.ai & Llama.ipynb @@ -0,0 +1,697 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "83a07847-b672-4a88-9a6d-fdae11bb1efa", + "metadata": {}, + "source": [ + "# Basic Inference with Llama Models on watsonx.ai\n", + "## Introduction\n", + "\n", + "Welcome to this getting started guide for using Llama models on IBM watsonx.ai! This notebook will walk you through the fundamentals of:\n", + "\n", + "- Setting up your environment\n", + "- Making your first API calls to Llama models\n", + "- Understanding key parameters\n", + "- Building practical examples\n", + "\n", + "By the end of this notebook, you'll be comfortable using Llama models for various text generation tasks on watsonx.ai.\n", + "\n", + "## Prerequisites\n", + "\n", + "- IBM Cloud account with watsonx.ai access\n", + "- Python 3.8 or higher\n", + "- Basic Python knowledge\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "id": "e2d981c7-3e92-4c99-be30-ee1123227779", + "metadata": {}, + "source": [ + "## 1. Environment Setup\n", + "First, let's install the required packages and set up our environment." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "8abda17e-2849-4ad2-9cef-e2a1dd0b5827", + "metadata": {}, + "outputs": [], + "source": [ + "# # # Install required packages\n", + "!pip install ibm-watsonx-ai\n", + "!pip install python-dotenv pandas" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "bbc27c29-3848-41bd-8d60-71c450408371", + "metadata": {}, + "outputs": [], + "source": [ + "# Import necessary libraries\n", + "import os\n", + "from dotenv import load_dotenv\n", + "from ibm_watsonx_ai import APIClient, Credentials\n", + "from ibm_watsonx_ai.foundation_models import Model\n", + "from ibm_watsonx_ai.metanames import GenTextParamsMetaNames as GenParams\n", + "from ibm_watsonx_ai.foundation_models.utils.enums import ModelTypes\n", + "import pandas as pd\n", + "import json" + ] + }, + { + "cell_type": "markdown", + "id": "8f8d6bb9-96cb-44ea-b39c-1c957c7b1963", + "metadata": {}, + "source": [ + "## 2. Authentication and Configuration \n", + "Create a .env file in your project directory with your credentials:\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e274dd4a-7b72-4d8d-a09b-f58be1f84a09", + "metadata": {}, + "outputs": [], + "source": [ + "# Add your credentials\n", + "env_content = \"\"\"\\\n", + "# IBM_CLOUD_API_KEY=\"\"\n", + "# PROJECT_ID=\"\"\n", + "# IBM_CLOUD_URL=\"\"\n", + "\"\"\"\n", + "\n", + "# Write the file\n", + "with open('.env', 'w') as f:\n", + " f.write(env_content)\n", + "\n", + "print(\".env file created!\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "90b935cc-c5a5-4f07-87dd-a99b94f03692", + "metadata": {}, + "outputs": [], + "source": [ + "# Load environment variables\n", + "load_dotenv()\n", + "\n", + "# Set up credentials\n", + "credentials = Credentials(\n", + " api_key=os.getenv(\"IBM_CLOUD_API_KEY\"),\n", + " url=os.getenv(\"IBM_CLOUD_URL\", \"https://us-south.ml.cloud.ibm.com\")\n", + ")\n", + "\n", + "# Set project ID\n", + "try:\n", + " project_id = os.getenv(\"PROJECT_ID\")\n", + "except KeyError:\n", + " project_id = input(\"Please enter your project_id (hit enter): \")\n", + " \n", + "print(\"Credentials configured successfully!\")" + ] + }, + { + "cell_type": "markdown", + "id": "8ecfe82b-f37e-4908-93c7-c9d4e966e629", + "metadata": {}, + "source": [ + "Create an instance of APIClient with authentication details." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "932faf12-78b5-4f48-a5af-3d2ddefc49ac", + "metadata": {}, + "outputs": [], + "source": [ + "client = APIClient(credentials=credentials)" + ] + }, + { + "cell_type": "markdown", + "id": "27de1424-c146-4b22-b82e-6b81c77baeec", + "metadata": {}, + "source": [ + "## 3. Foundation Models on watsonx.ai" + ] + }, + { + "cell_type": "markdown", + "id": "4d34a4bf-58f1-4d3a-b14c-0f45bab56aa0", + "metadata": {}, + "source": [ + "### List available models\n", + "All avaliable models are presented under ModelTypes class. For more information refer to the [documentation](https://ibm.github.io/watsonx-ai-python-sdk/fm_models.html)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "d18dfc68-c2ce-4dc3-a6cf-4c0cdcae6586", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'GRANITE_20B_CODE_INSTRUCT': 'ibm/granite-20b-code-instruct', 'GRANITE_3_2_8B_INSTRUCT': 'ibm/granite-3-2-8b-instruct', 'GRANITE_3_2B_INSTRUCT': 'ibm/granite-3-2b-instruct', 'GRANITE_3_3_8B_INSTRUCT': 'ibm/granite-3-3-8b-instruct', 'GRANITE_3_8B_INSTRUCT': 'ibm/granite-3-8b-instruct', 'GRANITE_34B_CODE_INSTRUCT': 'ibm/granite-34b-code-instruct', 'GRANITE_GUARDIAN_3_2B': 'ibm/granite-guardian-3-2b', 'GRANITE_GUARDIAN_3_8B': 'ibm/granite-guardian-3-8b', 'GRANITE_VISION_3_2_2B': 'ibm/granite-vision-3-2-2b', 'LLAMA_3_2_11B_VISION_INSTRUCT': 'meta-llama/llama-3-2-11b-vision-instruct', 'LLAMA_3_2_1B_INSTRUCT': 'meta-llama/llama-3-2-1b-instruct', 'LLAMA_3_2_3B_INSTRUCT': 'meta-llama/llama-3-2-3b-instruct', 'LLAMA_3_2_90B_VISION_INSTRUCT': 'meta-llama/llama-3-2-90b-vision-instruct', 'LLAMA_3_3_70B_INSTRUCT': 'meta-llama/llama-3-3-70b-instruct', 'LLAMA_3_405B_INSTRUCT': 'meta-llama/llama-3-405b-instruct', 'LLAMA_4_MAVERICK_17B_128E_INSTRUCT_FP8': 'meta-llama/llama-4-maverick-17b-128e-instruct-fp8', 'LLAMA_GUARD_3_11B_VISION': 'meta-llama/llama-guard-3-11b-vision', 'MISTRAL_LARGE': 'mistralai/mistral-large', 'MISTRAL_MEDIUM_2505': 'mistralai/mistral-medium-2505', 'MISTRAL_SMALL_3_1_24B_INSTRUCT_2503': 'mistralai/mistral-small-3-1-24b-instruct-2503', 'MIXTRAL_8X7B_INSTRUCT_V01': 'mistralai/mixtral-8x7b-instruct-v01', 'PIXTRAL_12B': 'mistralai/pixtral-12b'}\n" + ] + } + ], + "source": [ + "client.foundation_models.ChatModels.show()" + ] + }, + { + "cell_type": "markdown", + "id": "8ca61a1e-f003-49e4-ae57-6e723ace7fea", + "metadata": {}, + "source": [ + "### Pick a model" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "17b4086b-b1f8-4aaf-91d7-f8e6a9fbc92f", + "metadata": {}, + "outputs": [], + "source": [ + "model_id = 'meta-llama/llama-4-maverick-17b-128e-instruct-fp8'" + ] + }, + { + "cell_type": "markdown", + "id": "c4879897-8323-4b31-8f27-36e6db8bce12", + "metadata": {}, + "source": [ + "## 4. Defining the model parameters\n", + "You might need to adjust model parameters for different models or tasks, to do so please refer to [documentation](https://ibm.github.io/watsonx-ai-python-sdk/fm_schema.html)." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "ae959e2e-96d9-440a-bcb2-6a27308475dc", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "+-------------------+----------------------------------------+------------------------------+\n", + "| PARAMETER | TYPE | EXAMPLE VALUE |\n", + "+===================+========================================+==============================+\n", + "| frequency_penalty | float, NoneType | 0.5 |\n", + "+-------------------+----------------------------------------+------------------------------+\n", + "| logprobs | bool, NoneType | True |\n", + "+-------------------+----------------------------------------+------------------------------+\n", + "| top_logprobs | int, NoneType | 3 |\n", + "+-------------------+----------------------------------------+------------------------------+\n", + "| presence_penalty | float, NoneType | 0.3 |\n", + "+-------------------+----------------------------------------+------------------------------+\n", + "| response_format | dict, TextChatResponseFormat, NoneType | {'type': 'json_object'} |\n", + "+-------------------+----------------------------------------+------------------------------+\n", + "| temperature | float, NoneType | 0.7 |\n", + "+-------------------+----------------------------------------+------------------------------+\n", + "| max_tokens | int, NoneType | 100 |\n", + "+-------------------+----------------------------------------+------------------------------+\n", + "| time_limit | int, NoneType | 600000 |\n", + "+-------------------+----------------------------------------+------------------------------+\n", + "| top_p | float, NoneType | 0.9 |\n", + "+-------------------+----------------------------------------+------------------------------+\n", + "| n | int, NoneType | 1 |\n", + "+-------------------+----------------------------------------+------------------------------+\n", + "| logit_bias | dict, NoneType | {'1003': -100, '1004': -100} |\n", + "+-------------------+----------------------------------------+------------------------------+\n", + "| seed | int, NoneType | 41 |\n", + "+-------------------+----------------------------------------+------------------------------+\n", + "| stop | list, NoneType | ['this', 'the'] |\n", + "+-------------------+----------------------------------------+------------------------------+\n" + ] + } + ], + "source": [ + "from ibm_watsonx_ai.foundation_models.schema import TextChatParameters\n", + "\n", + "TextChatParameters.show()" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "4194d595-3e1f-42c0-8e3a-312c253bd028", + "metadata": {}, + "outputs": [], + "source": [ + "params = TextChatParameters(\n", + " temperature=0.5,\n", + " max_tokens=100\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "f04278e1-c942-49ae-a40c-4b053606730d", + "metadata": {}, + "source": [ + "## 5. Initialize the model\n", + "Initialize the ModelInference class with previous set params." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "199d4189-dc80-49db-a901-ebe7e82437de", + "metadata": {}, + "outputs": [], + "source": [ + "from ibm_watsonx_ai.foundation_models import ModelInference\n", + "\n", + "model = ModelInference(\n", + " model_id=model_id, \n", + " params=params, \n", + " credentials=credentials,\n", + " project_id=project_id)" + ] + }, + { + "cell_type": "markdown", + "id": "192cb545-e4fa-477d-bf7a-ec7fb0ec0100", + "metadata": {}, + "source": [ + "### Model's details\n" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "737d9e62-b3d0-46ab-9fe5-387f01f93ff0", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'model_id': 'meta-llama/llama-4-maverick-17b-128e-instruct-fp8',\n", + " 'label': 'llama-4-maverick-17b-128e-instruct-fp8',\n", + " 'provider': 'Meta',\n", + " 'source': 'Hugging Face',\n", + " 'functions': [{'id': 'autoai_rag'},\n", + " {'id': 'image_chat'},\n", + " {'id': 'multilingual'},\n", + " {'id': 'text_chat'},\n", + " {'id': 'text_generation'}],\n", + " 'short_description': 'Llama 4 Maverick, a 17 billion active parameter model with 128 experts.',\n", + " 'long_description': 'The Llama 4 collection of models are natively multimodal AI models that enable text and multimodal experiences. These models leverage a mixture-of-experts architecture to offer industry-leading performance in text and image understanding.',\n", + " 'terms_url': 'https://github.com/meta-llama/llama-models/blob/main/models/llama4/LICENSE',\n", + " 'input_tier': 'class_9',\n", + " 'output_tier': 'class_16',\n", + " 'number_params': '400b',\n", + " 'min_shot_size': 1,\n", + " 'task_ids': ['question_answering',\n", + " 'summarization',\n", + " 'retrieval_augmented_generation',\n", + " 'classification',\n", + " 'generation',\n", + " 'code',\n", + " 'extraction',\n", + " 'translation',\n", + " 'function_calling'],\n", + " 'tasks': [{'id': 'question_answering'},\n", + " {'id': 'summarization'},\n", + " {'id': 'retrieval_augmented_generation'},\n", + " {'id': 'classification'},\n", + " {'id': 'generation'},\n", + " {'id': 'code'},\n", + " {'id': 'extraction'},\n", + " {'id': 'translation'},\n", + " {'id': 'function_calling'}],\n", + " 'model_limits': {'max_sequence_length': 131072, 'max_output_tokens': 8192},\n", + " 'lifecycle': [{'id': 'available', 'start_date': '2025-04-06'}],\n", + " 'versions': [{'version': '4.0.0', 'available_date': '2025-04-06'}],\n", + " 'supported_languages': ['en', 'de', 'fr', 'it', 'pt', 'hi', 'es', 'th']}" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "model.get_details()" + ] + }, + { + "cell_type": "markdown", + "id": "0c7c942b-f7b0-48ac-80f2-68700612e01b", + "metadata": {}, + "source": [ + "## 6.Your First Llama Model Chat" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "3d908c04-cb5d-42de-953f-561b55b0ccb9", + "metadata": {}, + "outputs": [], + "source": [ + "messages = [\n", + " {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n", + " {\"role\": \"user\", \"content\": \"Who won the last Fifa World Cup?\"}\n", + "]" + ] + }, + { + "cell_type": "markdown", + "id": "84257ade-ef78-42ac-a23a-406b565aba3b", + "metadata": {}, + "source": [ + "### Chat without Streaming" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "2bbbc1a4-51f3-48b2-a824-b24fb68a1514", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The last FIFA World Cup was held in 2022 in Qatar. The winner of the tournament was Argentina, led by Lionel Messi. They defeated France 4-2 in a penalty shootout after the match ended 3-3 after extra time in the final on December 18, 2022.\n" + ] + } + ], + "source": [ + "generated_response = model.chat(messages=messages)\n", + "\n", + "# # Print full response\n", + "# print(generated_response)\n", + "\n", + "# Print only content\n", + "print(generated_response[\"choices\"][0][\"message\"][\"content\"])" + ] + }, + { + "cell_type": "markdown", + "id": "02eca2ab-abd0-4dac-856a-7e0ebe819f26", + "metadata": {}, + "source": [ + "### Chat with Streaming" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "f5506a46-5e11-4054-80da-3a92b2531d08", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The last FIFA World Cup was held in 2022 in Qatar. The winner of that tournament was Argentina, led by Lionel Messi. They defeated France 4-2 in a penalty shootout after the match ended 3-3 after extra time in the final on December 18, 2022." + ] + } + ], + "source": [ + "generated_response = model.chat(messages=messages)\n", + "response = generated_response[\"choices\"][0][\"message\"][\"content\"]\n", + "for chunk in response:\n", + " print(chunk, end='', flush=True)" + ] + }, + { + "cell_type": "markdown", + "id": "d49fa67e-3680-4f9e-8930-b99b69ef9e54", + "metadata": {}, + "source": [ + "## 8. Examples" + ] + }, + { + "cell_type": "markdown", + "id": "f7de8291-e942-4497-9131-6626eb199fc9", + "metadata": {}, + "source": [ + "### Email Assistant" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "1c6f1a12-6943-428d-833a-46aaac498cb3", + "metadata": {}, + "outputs": [], + "source": [ + "def email_assistant(context, tone=\"professional\"):\n", + " \"\"\"Generate email responses based on context and tone\"\"\"\n", + " \n", + " messages = [\n", + " {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n", + " {\"role\": \"user\", \"content\": f\"\"\"\n", + " Write a {tone} email response based on this context:\n", + " Context: {context}\n", + " Email Response:\"\"\"}\n", + " ]\n", + " \n", + " params = TextChatParameters(\n", + " temperature=0.5,\n", + " max_tokens=250\n", + " )\n", + "\n", + " model = ModelInference(\n", + " model_id=model_id,\n", + " params=params,\n", + " credentials=credentials,\n", + " project_id=project_id\n", + " )\n", + " \n", + " response = model.chat(messages=messages)\n", + " clean_response = response[\"choices\"][0][\"message\"][\"content\"]\n", + " \n", + " return clean_response\n" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "04c7a135-2719-434a-ad3a-bf381fd32c29", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Here's a friendly email response:\n", + "\n", + "Dear [Name],\n", + "\n", + "Thank you so much for inviting me to meet on [Date and Time]. I appreciate you thinking of me and I'm glad we're in touch.\n", + "\n", + "Unfortunately, I have a prior commitment at that time and won't be able to make it to our meeting as scheduled. I'd love to reschedule for another time that works better for you. Would you be available to meet at an alternative time? I'm flexible and can work around your schedule.\n", + "\n", + "Let's touch base soon to find a new time that suits you. I'm looking forward to catching up with you then.\n", + "\n", + "Best regards,\n", + "[Your Name]\n" + ] + } + ], + "source": [ + "# Example usage\n", + "context = \"Declining a meeting invitation due to a scheduling conflict, but expressing interest in rescheduling\"\n", + "email_response = email_assistant(context, tone=\"friendly\")\n", + "\n", + "print(email_response)" + ] + }, + { + "cell_type": "markdown", + "id": "da35edf2-f203-4587-8fb5-1121dff6b664", + "metadata": {}, + "source": [ + "### Code Documentation Generator" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "856c6538-28fb-41f8-81f9-df0b38e47ce7", + "metadata": {}, + "outputs": [], + "source": [ + "def generate_docstring(code):\n", + " \"\"\"Generate documentation for code snippets\"\"\"\n", + " \n", + " messages = [\n", + " {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n", + " {\"role\": \"user\", \"content\": f\"\"\"\n", + " Generate a comprehensive docstring for this Python function:\n", + " {code}\n", + " \n", + " Include:\n", + " - Description\n", + " - Parameters\n", + " - Returns\n", + " - Example usage\n", + " \n", + " Docstring:\"\"\"}\n", + " ]\n", + " \n", + " params = TextChatParameters(\n", + " temperature=0.5,\n", + " max_tokens=1000\n", + " )\n", + " \n", + " model = ModelInference(\n", + " model_id=model_id,\n", + " params=params,\n", + " credentials=credentials,\n", + " project_id=project_id\n", + " )\n", + "\n", + " response = model.chat(messages=messages)\n", + " clean_response = response[\"choices\"][0][\"message\"][\"content\"]\n", + " \n", + " return clean_response\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "595320f2-88c7-46a9-a525-74ade6aaf9e6", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Generated Documentation:\n", + "--------------------------------------------------\n", + "```python\n", + "def calculate_discount(price, discount_percent, max_discount=None):\n", + " \"\"\"\n", + " Calculates the price after applying a discount.\n", + "\n", + " This function takes into account a percentage discount and an optional maximum discount amount.\n", + "\n", + " Parameters\n", + " ----------\n", + " price : float\n", + " The original price of the item.\n", + " discount_percent : float\n", + " The percentage discount to be applied.\n", + " max_discount : float, optional\n", + " The maximum discount amount allowed (default is None).\n", + "\n", + " Returns\n", + " -------\n", + " float\n", + " The price after applying the discount.\n", + "\n", + " Example\n", + " -------\n", + " >>> calculate_discount(100, 20)\n", + " 80.0\n", + " >>> calculate_discount(100, 20, max_discount=15)\n", + " 85.0\n", + " \"\"\"\n", + " discount_amount = price * (discount_percent / 100)\n", + " if max_discount and discount_amount > max_discount:\n", + " discount_amount = max_discount\n", + " return price - discount_amount\n", + "```\n" + ] + } + ], + "source": [ + "# Example code\n", + "sample_code = \"\"\"\n", + "def calculate_discount(price, discount_percent, max_discount=None):\n", + " discount_amount = price * (discount_percent / 100)\n", + " if max_discount and discount_amount > max_discount:\n", + " discount_amount = max_discount\n", + " return price - discount_amount\n", + "\"\"\"\n", + "\n", + "docstring = generate_docstring(sample_code)\n", + "print(\"Generated Documentation:\")\n", + "print(\"-\" * 50)\n", + "print(docstring)" + ] + }, + { + "cell_type": "markdown", + "id": "cc47ea30-8118-429d-a58b-f1a727db425a", + "metadata": {}, + "source": [ + "## Next Steps\n", + "### Congratulations! You've learned the basics of using Llama models on watsonx.ai. Here are some next steps:" + ] + }, + { + "cell_type": "markdown", + "id": "c89a0ad5-e6aa-4123-8821-4f8168560475", + "metadata": {}, + "source": [ + "## Useful Resources\n", + "\n", + "* [IBM watsonx.ai Documentation](https://www.ibm.com/docs/en/watsonx)\n", + "* [Llama Model Documentation](https://www.llama.com/docs/overview/)\n", + "* [Prompt Engineering Guide](https://www.promptingguide.ai/)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "86b7c696-2873-4c0f-a4a6-115357003a94", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "21ed78b9-d418-485e-8e8f-684c6a87e0d5", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "promptTesting", + "language": "python", + "name": "prompttesting" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.13.2" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From 14e3f97b8b63638725f75807ad4f6048d3f35fba Mon Sep 17 00:00:00 2001 From: Yuce Dincer Date: Fri, 11 Jul 2025 14:58:49 -0400 Subject: [PATCH 2/7] ibm watsonxai code colab addon --- .../Get Started with watsonx.ai & Llama.ipynb | 50 +++++++++++-------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/3p-integrations/ibm/Get Started with watsonx.ai & Llama.ipynb b/3p-integrations/ibm/Get Started with watsonx.ai & Llama.ipynb index e0b6431ba..64c4ffab6 100644 --- a/3p-integrations/ibm/Get Started with watsonx.ai & Llama.ipynb +++ b/3p-integrations/ibm/Get Started with watsonx.ai & Llama.ipynb @@ -1,5 +1,13 @@ { "cells": [ + { + "cell_type": "markdown", + "id": "35a81947", + "metadata": {}, + "source": [ + "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://github.com/meta-llama/llama-cookbook/blob/ibm-wxai/3p-integrations/ibm/Get%20Started%20with%20watsonx.ai%20%26%20Llama.ipynb)\n" + ] + }, { "cell_type": "markdown", "id": "83a07847-b672-4a88-9a6d-fdae11bb1efa", @@ -36,19 +44,19 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "id": "8abda17e-2849-4ad2-9cef-e2a1dd0b5827", "metadata": {}, "outputs": [], "source": [ - "# # # Install required packages\n", + "# Install required packages\n", "!pip install ibm-watsonx-ai\n", "!pip install python-dotenv pandas" ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "id": "bbc27c29-3848-41bd-8d60-71c450408371", "metadata": {}, "outputs": [], @@ -58,7 +66,6 @@ "from dotenv import load_dotenv\n", "from ibm_watsonx_ai import APIClient, Credentials\n", "from ibm_watsonx_ai.foundation_models import Model\n", - "from ibm_watsonx_ai.metanames import GenTextParamsMetaNames as GenParams\n", "from ibm_watsonx_ai.foundation_models.utils.enums import ModelTypes\n", "import pandas as pd\n", "import json" @@ -116,7 +123,7 @@ " project_id = os.getenv(\"PROJECT_ID\")\n", "except KeyError:\n", " project_id = input(\"Please enter your project_id (hit enter): \")\n", - " \n", + "\n", "print(\"Credentials configured successfully!\")" ] }, @@ -272,7 +279,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": null, "id": "199d4189-dc80-49db-a901-ebe7e82437de", "metadata": {}, "outputs": [], @@ -280,8 +287,8 @@ "from ibm_watsonx_ai.foundation_models import ModelInference\n", "\n", "model = ModelInference(\n", - " model_id=model_id, \n", - " params=params, \n", + " model_id=model_id,\n", + " params=params,\n", " credentials=credentials,\n", " project_id=project_id)" ] @@ -452,14 +459,14 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": null, "id": "1c6f1a12-6943-428d-833a-46aaac498cb3", "metadata": {}, "outputs": [], "source": [ "def email_assistant(context, tone=\"professional\"):\n", " \"\"\"Generate email responses based on context and tone\"\"\"\n", - " \n", + "\n", " messages = [\n", " {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n", " {\"role\": \"user\", \"content\": f\"\"\"\n", @@ -467,7 +474,7 @@ " Context: {context}\n", " Email Response:\"\"\"}\n", " ]\n", - " \n", + "\n", " params = TextChatParameters(\n", " temperature=0.5,\n", " max_tokens=250\n", @@ -479,10 +486,10 @@ " credentials=credentials,\n", " project_id=project_id\n", " )\n", - " \n", + "\n", " response = model.chat(messages=messages)\n", " clean_response = response[\"choices\"][0][\"message\"][\"content\"]\n", - " \n", + "\n", " return clean_response\n" ] }, @@ -529,34 +536,34 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": null, "id": "856c6538-28fb-41f8-81f9-df0b38e47ce7", "metadata": {}, "outputs": [], "source": [ "def generate_docstring(code):\n", " \"\"\"Generate documentation for code snippets\"\"\"\n", - " \n", + "\n", " messages = [\n", " {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n", " {\"role\": \"user\", \"content\": f\"\"\"\n", " Generate a comprehensive docstring for this Python function:\n", " {code}\n", - " \n", + "\n", " Include:\n", " - Description\n", " - Parameters\n", " - Returns\n", " - Example usage\n", - " \n", + "\n", " Docstring:\"\"\"}\n", " ]\n", - " \n", + "\n", " params = TextChatParameters(\n", " temperature=0.5,\n", " max_tokens=1000\n", " )\n", - " \n", + "\n", " model = ModelInference(\n", " model_id=model_id,\n", " params=params,\n", @@ -566,9 +573,8 @@ "\n", " response = model.chat(messages=messages)\n", " clean_response = response[\"choices\"][0][\"message\"][\"content\"]\n", - " \n", - " return clean_response\n", - "\n" + "\n", + " return clean_response\n" ] }, { From 0615c73d4e6ecb533e6363fc032200ee46606fcf Mon Sep 17 00:00:00 2001 From: Yuce Dincer Date: Fri, 11 Jul 2025 15:02:17 -0400 Subject: [PATCH 3/7] ibm watsonxai code colab addon --- 3p-integrations/ibm/Get Started with watsonx.ai & Llama.ipynb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/3p-integrations/ibm/Get Started with watsonx.ai & Llama.ipynb b/3p-integrations/ibm/Get Started with watsonx.ai & Llama.ipynb index 64c4ffab6..674264c7c 100644 --- a/3p-integrations/ibm/Get Started with watsonx.ai & Llama.ipynb +++ b/3p-integrations/ibm/Get Started with watsonx.ai & Llama.ipynb @@ -5,7 +5,7 @@ "id": "35a81947", "metadata": {}, "source": [ - "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://github.com/meta-llama/llama-cookbook/blob/ibm-wxai/3p-integrations/ibm/Get%20Started%20with%20watsonx.ai%20%26%20Llama.ipynb)\n" + "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/meta-llama/llama-cookbook/blob/ibm-wxai/3p-integrations/ibm/Get%20Started%20with%20watsonx.ai%20%26%20Llama.ipynb)\n" ] }, { @@ -23,7 +23,7 @@ "- Understanding key parameters\n", "- Building practical examples\n", "\n", - "By the end of this notebook, you'll be comfortable using Llama models for various text generation tasks on watsonx.ai.\n", + "By the end of this notebook, you'll be comfortable using Llama models for various text generation tasks on watsonx.ai!\n", "\n", "## Prerequisites\n", "\n", From cc2edb6fe5e450ccd14101a1d4e1905ba9c5bf8b Mon Sep 17 00:00:00 2001 From: Yuce Dincer Date: Fri, 11 Jul 2025 15:10:04 -0400 Subject: [PATCH 4/7] ibm watsonxai code --- .../Get Started with watsonx.ai & Llama.ipynb | 121 +++++++++++++----- 1 file changed, 87 insertions(+), 34 deletions(-) diff --git a/3p-integrations/ibm/Get Started with watsonx.ai & Llama.ipynb b/3p-integrations/ibm/Get Started with watsonx.ai & Llama.ipynb index 674264c7c..6216db8fd 100644 --- a/3p-integrations/ibm/Get Started with watsonx.ai & Llama.ipynb +++ b/3p-integrations/ibm/Get Started with watsonx.ai & Llama.ipynb @@ -1,13 +1,5 @@ { "cells": [ - { - "cell_type": "markdown", - "id": "35a81947", - "metadata": {}, - "source": [ - "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/meta-llama/llama-cookbook/blob/ibm-wxai/3p-integrations/ibm/Get%20Started%20with%20watsonx.ai%20%26%20Llama.ipynb)\n" - ] - }, { "cell_type": "markdown", "id": "83a07847-b672-4a88-9a6d-fdae11bb1efa", @@ -23,7 +15,7 @@ "- Understanding key parameters\n", "- Building practical examples\n", "\n", - "By the end of this notebook, you'll be comfortable using Llama models for various text generation tasks on watsonx.ai!\n", + "By the end of this notebook, you'll be comfortable using Llama models for various text generation tasks on watsonx.ai.\n", "\n", "## Prerequisites\n", "\n", @@ -44,19 +36,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "8abda17e-2849-4ad2-9cef-e2a1dd0b5827", "metadata": {}, "outputs": [], "source": [ - "# Install required packages\n", + "# # # Install required packages\n", "!pip install ibm-watsonx-ai\n", "!pip install python-dotenv pandas" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "bbc27c29-3848-41bd-8d60-71c450408371", "metadata": {}, "outputs": [], @@ -66,19 +58,34 @@ "from dotenv import load_dotenv\n", "from ibm_watsonx_ai import APIClient, Credentials\n", "from ibm_watsonx_ai.foundation_models import Model\n", + "from ibm_watsonx_ai.metanames import GenTextParamsMetaNames as GenParams\n", "from ibm_watsonx_ai.foundation_models.utils.enums import ModelTypes\n", "import pandas as pd\n", "import json" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "d5d854f2-a6f9-4107-a687-492d6eba2003", + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "id": "8f8d6bb9-96cb-44ea-b39c-1c957c7b1963", "metadata": {}, "source": [ - "## 2. Authentication and Configuration \n", - "Create a .env file in your project directory with your credentials:\n", - "\n" + "## 2. Authentication and Configuration" + ] + }, + { + "cell_type": "markdown", + "id": "c28dd58f-d085-4531-b6e0-c577edd58df7", + "metadata": {}, + "source": [ + "### To set env values in local development" ] }, { @@ -88,7 +95,8 @@ "metadata": {}, "outputs": [], "source": [ - "# Add your credentials\n", + "# Add your credentials - Create a .env file in your project directory with your credentials:\n", + "\n", "env_content = \"\"\"\\\n", "# IBM_CLOUD_API_KEY=\"\"\n", "# PROJECT_ID=\"\"\n", @@ -123,7 +131,7 @@ " project_id = os.getenv(\"PROJECT_ID\")\n", "except KeyError:\n", " project_id = input(\"Please enter your project_id (hit enter): \")\n", - "\n", + " \n", "print(\"Credentials configured successfully!\")" ] }, @@ -132,7 +140,51 @@ "id": "8ecfe82b-f37e-4908-93c7-c9d4e966e629", "metadata": {}, "source": [ - "Create an instance of APIClient with authentication details." + "### To set env values in Google Colab\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "75d0d514-bef9-4744-8fd4-5ceeda45901f", + "metadata": {}, + "outputs": [], + "source": [ + "# from google.colab import userdata\n", + "\n", + "# # This must be done manually per session (or via an init cell)\n", + "# userdata.set_secret('IBM_CLOUD_API_KEY', 'your_api_key_here')\n", + "# userdata.set_secret('PROJECT_ID', 'your_project_id_here')\n", + "# userdata.set_secret('IBM_CLOUD_URL', 'https://us-south.ml.cloud.ibm.com')\n", + "\n", + "# # Import necessary libraries\n", + "# from google.colab import userdata\n", + "\n", + "# # Retrieve secrets securely from Colab & set up credentials\n", + "# credentials = {\n", + "# \"apikey\": userdata.get('IBM_CLOUD_API_KEY'),\n", + "# \"url\": userdata.get('IBM_CLOUD_URL') or \"https://us-south.ml.cloud.ibm.com\"\n", + "# }\n", + "\n", + "# project_id = userdata.get('PROJECT_ID')\n", + "\n", + "# client = APIClient(credentials)\n", + "\n", + "# # Set project ID\n", + "# if not project_id:\n", + "# project_id = input(\"Please enter your project_id: \")\n", + "\n", + "# client.set.default_project(project_id)\n", + "\n", + "# print(\"Credentials configured successfully!\")" + ] + }, + { + "cell_type": "markdown", + "id": "b0f62771-68ef-45f2-b5a5-b64b039b0dbc", + "metadata": {}, + "source": [ + "### Create an instance of APIClient with authentication details" ] }, { @@ -279,7 +331,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "id": "199d4189-dc80-49db-a901-ebe7e82437de", "metadata": {}, "outputs": [], @@ -287,8 +339,8 @@ "from ibm_watsonx_ai.foundation_models import ModelInference\n", "\n", "model = ModelInference(\n", - " model_id=model_id,\n", - " params=params,\n", + " model_id=model_id, \n", + " params=params, \n", " credentials=credentials,\n", " project_id=project_id)" ] @@ -459,14 +511,14 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "id": "1c6f1a12-6943-428d-833a-46aaac498cb3", "metadata": {}, "outputs": [], "source": [ "def email_assistant(context, tone=\"professional\"):\n", " \"\"\"Generate email responses based on context and tone\"\"\"\n", - "\n", + " \n", " messages = [\n", " {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n", " {\"role\": \"user\", \"content\": f\"\"\"\n", @@ -474,7 +526,7 @@ " Context: {context}\n", " Email Response:\"\"\"}\n", " ]\n", - "\n", + " \n", " params = TextChatParameters(\n", " temperature=0.5,\n", " max_tokens=250\n", @@ -486,10 +538,10 @@ " credentials=credentials,\n", " project_id=project_id\n", " )\n", - "\n", + " \n", " response = model.chat(messages=messages)\n", " clean_response = response[\"choices\"][0][\"message\"][\"content\"]\n", - "\n", + " \n", " return clean_response\n" ] }, @@ -536,34 +588,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "id": "856c6538-28fb-41f8-81f9-df0b38e47ce7", "metadata": {}, "outputs": [], "source": [ "def generate_docstring(code):\n", " \"\"\"Generate documentation for code snippets\"\"\"\n", - "\n", + " \n", " messages = [\n", " {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n", " {\"role\": \"user\", \"content\": f\"\"\"\n", " Generate a comprehensive docstring for this Python function:\n", " {code}\n", - "\n", + " \n", " Include:\n", " - Description\n", " - Parameters\n", " - Returns\n", " - Example usage\n", - "\n", + " \n", " Docstring:\"\"\"}\n", " ]\n", - "\n", + " \n", " params = TextChatParameters(\n", " temperature=0.5,\n", " max_tokens=1000\n", " )\n", - "\n", + " \n", " model = ModelInference(\n", " model_id=model_id,\n", " params=params,\n", @@ -573,8 +625,9 @@ "\n", " response = model.chat(messages=messages)\n", " clean_response = response[\"choices\"][0][\"message\"][\"content\"]\n", - "\n", - " return clean_response\n" + " \n", + " return clean_response\n", + "\n" ] }, { From 941905415fb44062005b9e824c5b0bf953027160 Mon Sep 17 00:00:00 2001 From: Yuce Dincer Date: Fri, 11 Jul 2025 15:11:03 -0400 Subject: [PATCH 5/7] ibm watsonxai code --- .../Get Started with watsonx.ai & Llama.ipynb | 43 ++++++++----------- 1 file changed, 17 insertions(+), 26 deletions(-) diff --git a/3p-integrations/ibm/Get Started with watsonx.ai & Llama.ipynb b/3p-integrations/ibm/Get Started with watsonx.ai & Llama.ipynb index 6216db8fd..b10010ecb 100644 --- a/3p-integrations/ibm/Get Started with watsonx.ai & Llama.ipynb +++ b/3p-integrations/ibm/Get Started with watsonx.ai & Llama.ipynb @@ -64,14 +64,6 @@ "import json" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "d5d854f2-a6f9-4107-a687-492d6eba2003", - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "markdown", "id": "8f8d6bb9-96cb-44ea-b39c-1c957c7b1963", @@ -131,7 +123,7 @@ " project_id = os.getenv(\"PROJECT_ID\")\n", "except KeyError:\n", " project_id = input(\"Please enter your project_id (hit enter): \")\n", - " \n", + "\n", "print(\"Credentials configured successfully!\")" ] }, @@ -331,7 +323,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": null, "id": "199d4189-dc80-49db-a901-ebe7e82437de", "metadata": {}, "outputs": [], @@ -339,8 +331,8 @@ "from ibm_watsonx_ai.foundation_models import ModelInference\n", "\n", "model = ModelInference(\n", - " model_id=model_id, \n", - " params=params, \n", + " model_id=model_id,\n", + " params=params,\n", " credentials=credentials,\n", " project_id=project_id)" ] @@ -511,14 +503,14 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": null, "id": "1c6f1a12-6943-428d-833a-46aaac498cb3", "metadata": {}, "outputs": [], "source": [ "def email_assistant(context, tone=\"professional\"):\n", " \"\"\"Generate email responses based on context and tone\"\"\"\n", - " \n", + "\n", " messages = [\n", " {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n", " {\"role\": \"user\", \"content\": f\"\"\"\n", @@ -526,7 +518,7 @@ " Context: {context}\n", " Email Response:\"\"\"}\n", " ]\n", - " \n", + "\n", " params = TextChatParameters(\n", " temperature=0.5,\n", " max_tokens=250\n", @@ -538,10 +530,10 @@ " credentials=credentials,\n", " project_id=project_id\n", " )\n", - " \n", + "\n", " response = model.chat(messages=messages)\n", " clean_response = response[\"choices\"][0][\"message\"][\"content\"]\n", - " \n", + "\n", " return clean_response\n" ] }, @@ -588,34 +580,34 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": null, "id": "856c6538-28fb-41f8-81f9-df0b38e47ce7", "metadata": {}, "outputs": [], "source": [ "def generate_docstring(code):\n", " \"\"\"Generate documentation for code snippets\"\"\"\n", - " \n", + "\n", " messages = [\n", " {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n", " {\"role\": \"user\", \"content\": f\"\"\"\n", " Generate a comprehensive docstring for this Python function:\n", " {code}\n", - " \n", + "\n", " Include:\n", " - Description\n", " - Parameters\n", " - Returns\n", " - Example usage\n", - " \n", + "\n", " Docstring:\"\"\"}\n", " ]\n", - " \n", + "\n", " params = TextChatParameters(\n", " temperature=0.5,\n", " max_tokens=1000\n", " )\n", - " \n", + "\n", " model = ModelInference(\n", " model_id=model_id,\n", " params=params,\n", @@ -625,9 +617,8 @@ "\n", " response = model.chat(messages=messages)\n", " clean_response = response[\"choices\"][0][\"message\"][\"content\"]\n", - " \n", - " return clean_response\n", - "\n" + "\n", + " return clean_response\n" ] }, { From 88c431ceeb5b8e93e59dabe48d8647d8699a2295 Mon Sep 17 00:00:00 2001 From: Yuce Dincer Date: Fri, 11 Jul 2025 15:15:35 -0400 Subject: [PATCH 6/7] updated 3p documentation --- 3p-integrations/README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/3p-integrations/README.md b/3p-integrations/README.md index 6b5a88aa5..3cc42bb78 100644 --- a/3p-integrations/README.md +++ b/3p-integrations/README.md @@ -40,6 +40,9 @@ AI-powered code and data analysis tool using Meta Llama and the E2B SDK, support ### [Groq](./groq) Examples and templates for using Llama models with Groq's high-performance inference API. +### [IBM](./ibm) +Recipe to use llama models within IBM's watsonx.ai platform. + ### [Lamini](./lamini) Integration examples with Lamini's platform, including text2sql with memory tuning. From 555bda1373d803cbb348e5aeeaeeb8457f37d2a3 Mon Sep 17 00:00:00 2001 From: Yuce Dincer Date: Fri, 11 Jul 2025 15:21:28 -0400 Subject: [PATCH 7/7] updated colab code --- .../Get Started with watsonx.ai & Llama.ipynb | 35 ++++++++----------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/3p-integrations/ibm/Get Started with watsonx.ai & Llama.ipynb b/3p-integrations/ibm/Get Started with watsonx.ai & Llama.ipynb index b10010ecb..949984cb3 100644 --- a/3p-integrations/ibm/Get Started with watsonx.ai & Llama.ipynb +++ b/3p-integrations/ibm/Get Started with watsonx.ai & Llama.ipynb @@ -1,5 +1,13 @@ { "cells": [ + { + "cell_type": "markdown", + "id": "efbe6b67", + "metadata": {}, + "source": [ + "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/meta-llama/llama-cookbook/blob/ibm-wxai/3p-integrations/ibm/Get%20Started%20with%20watsonx.ai%20%26%20Llama.ipynb)" + ] + }, { "cell_type": "markdown", "id": "83a07847-b672-4a88-9a6d-fdae11bb1efa", @@ -137,17 +145,18 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "id": "75d0d514-bef9-4744-8fd4-5ceeda45901f", "metadata": {}, "outputs": [], "source": [ "# from google.colab import userdata\n", "\n", - "# # This must be done manually per session (or via an init cell)\n", - "# userdata.set_secret('IBM_CLOUD_API_KEY', 'your_api_key_here')\n", - "# userdata.set_secret('PROJECT_ID', 'your_project_id_here')\n", - "# userdata.set_secret('IBM_CLOUD_URL', 'https://us-south.ml.cloud.ibm.com')\n", + "# Set your env values in Secrets\n", + "\n", + "# IBM_CLOUD_API_KEY=\"\"\n", + "# PROJECT_ID=\"\"\n", + "# IBM_CLOUD_URL=\"\"\n", "\n", "# # Import necessary libraries\n", "# from google.colab import userdata\n", @@ -705,22 +714,6 @@ "* [Llama Model Documentation](https://www.llama.com/docs/overview/)\n", "* [Prompt Engineering Guide](https://www.promptingguide.ai/)" ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "86b7c696-2873-4c0f-a4a6-115357003a94", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "21ed78b9-d418-485e-8e8f-684c6a87e0d5", - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": {