Browse Source

ibm watsonxai code

Yuce Dincer 2 months ago
parent
commit
cc2edb6fe5
1 changed files with 87 additions and 34 deletions
  1. 87 34
      3p-integrations/ibm/Get Started with watsonx.ai & Llama.ipynb

+ 87 - 34
3p-integrations/ibm/Get Started with watsonx.ai & Llama.ipynb

@@ -2,14 +2,6 @@
  "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",
    "metadata": {},
    "source": [
@@ -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"
    ]
   },
   {