From f6e3aa9770e32216954c3e0f2fa6825e5d89bd75 Mon Sep 17 00:00:00 2001 From: Mateusz Szewczyk <139469471+MateuszOssGit@users.noreply.github.com> Date: Thu, 22 Feb 2024 19:22:18 +0100 Subject: [PATCH] docs: update IBM watsonx.ai docs (#17932) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - **Description:** Update IBM watsonx.ai docs and add IBM as a provider docs - **Dependencies:** [ibm-watsonx-ai](https://pypi.org/project/ibm-watsonx-ai/), - **Tag maintainer:** : Please make sure your PR is passing linting and testing before submitting. Run `make format`, `make lint` and `make test` to check this locally. ✅ --- docs/docs/integrations/llms/ibm_watsonx.ipynb | 82 ++++++++++++------- docs/docs/integrations/providers/ibm.mdx | 39 +++++++++ 2 files changed, 92 insertions(+), 29 deletions(-) create mode 100644 docs/docs/integrations/providers/ibm.mdx diff --git a/docs/docs/integrations/llms/ibm_watsonx.ipynb b/docs/docs/integrations/llms/ibm_watsonx.ipynb index eafeb095d83ec..47c0fb620dd40 100644 --- a/docs/docs/integrations/llms/ibm_watsonx.ipynb +++ b/docs/docs/integrations/llms/ibm_watsonx.ipynb @@ -19,17 +19,17 @@ "source": [ "## Setting up\n", "\n", - "Install the package [`ibm-watsonx-ai`](https://ibm.github.io/watsonx-ai-python-sdk/install.html)." + "Install the package `langchain-ibm`." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "id": "2f1fff4e", "metadata": {}, "outputs": [], "source": [ - "%pip install --upgrade --quiet ibm-watsonx-ai" + "!pip install -qU langchain-ibm" ] }, { @@ -57,6 +57,30 @@ "os.environ[\"WATSONX_APIKEY\"] = watsonx_api_key" ] }, + { + "cell_type": "markdown", + "id": "c59782a7", + "metadata": {}, + "source": [ + "Additionaly you are able to pass additional secrets as an environment variable. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f98c573c", + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "\n", + "os.environ[\"WATSONX_URL\"] = \"your service instance url\"\n", + "os.environ[\"WATSONX_TOKEN\"] = \"your token for accessing the CPD cluster\"\n", + "os.environ[\"WATSONX_PASSWORD\"] = \"your password for accessing the CPD cluster\"\n", + "os.environ[\"WATSONX_USERNAME\"] = \"your username for accessing the CPD cluster\"\n", + "os.environ[\"WATSONX_INSTANCE_ID\"] = \"your instance_id for accessing the CPD cluster\"" + ] + }, { "cell_type": "markdown", "id": "e36acbef", @@ -74,15 +98,13 @@ "metadata": {}, "outputs": [], "source": [ - "from ibm_watsonx_ai.metanames import GenTextParamsMetaNames as GenParams\n", - "\n", "parameters = {\n", - " GenParams.DECODING_METHOD: \"sample\",\n", - " GenParams.MAX_NEW_TOKENS: 100,\n", - " GenParams.MIN_NEW_TOKENS: 1,\n", - " GenParams.TEMPERATURE: 0.5,\n", - " GenParams.TOP_K: 50,\n", - " GenParams.TOP_P: 1,\n", + " \"decoding_method\": \"sample\",\n", + " \"max_new_tokens\": 100,\n", + " \"min_new_tokens\": 1,\n", + " \"temperature\": 0.5,\n", + " \"top_k\": 50,\n", + " \"top_p\": 1,\n", "}" ] }, @@ -99,12 +121,15 @@ "- To provide context for the API call, you must add `project_id` or `space_id`. For more information see [documentation](https://www.ibm.com/docs/en/watsonx-as-a-service?topic=projects).\n", "- Depending on the region of your provisioned service instance, use one of the urls described [here](https://ibm.github.io/watsonx-ai-python-sdk/setup_cloud.html#authentication).\n", "\n", - "In this example, we’ll use the `project_id` and Dallas url." + "In this example, we’ll use the `project_id` and Dallas url.\n", + "\n", + "\n", + "You need to specify `model_id` that will be used for inferencing. All avaliable models you can find in [documentation](https://ibm.github.io/watsonx-ai-python-sdk/fm_model.html#ibm_watsonx_ai.foundation_models.utils.enums.ModelTypes)." ] }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 4, "id": "359898de", "metadata": {}, "outputs": [], @@ -112,7 +137,7 @@ "from langchain_ibm import WatsonxLLM\n", "\n", "watsonx_llm = WatsonxLLM(\n", - " model_id=\"google/flan-ul2\",\n", + " model_id=\"ibm/granite-13b-instruct-v2\",\n", " url=\"https://us-south.ml.cloud.ibm.com\",\n", " project_id=\"PASTE YOUR PROJECT_ID HERE\",\n", " params=parameters,\n", @@ -135,7 +160,7 @@ "outputs": [], "source": [ "watsonx_llm = WatsonxLLM(\n", - " model_id=\"google/flan-ul2\",\n", + " model_id=\"ibm/granite-13b-instruct-v2\",\n", " url=\"PASTE YOUR URL HERE\",\n", " username=\"PASTE YOUR USERNAME HERE\",\n", " password=\"PASTE YOUR PASSWORD HERE\",\n", @@ -180,7 +205,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 5, "id": "c7d80c05", "metadata": {}, "outputs": [], @@ -201,18 +226,17 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 10, "id": "dc076c56", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "{'topic': 'dog',\n", - " 'text': 'What is the name of the dog that is the most popular in the world?'}" + "{'topic': 'dog', 'text': 'Why do dogs howl?'}" ] }, - "execution_count": 4, + "execution_count": 10, "metadata": {}, "output_type": "execute_result" } @@ -235,17 +259,17 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 7, "id": "beea2b5b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'dog'" + "\"Man's best friend is his dog. \"" ] }, - "execution_count": 6, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -258,17 +282,17 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 11, "id": "8ab1a25a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "LLMResult(generations=[[Generation(text='greyhounds', generation_info={'generated_token_count': 4, 'input_token_count': 8, 'finish_reason': 'eos_token'})], [Generation(text='The Basenji is a dog breed from South Africa.', generation_info={'generated_token_count': 13, 'input_token_count': 7, 'finish_reason': 'eos_token'})]], llm_output={'model_id': 'google/flan-ul2'}, run=[RunInfo(run_id=UUID('03c73a42-db68-428e-ab8d-8ae10abc84fc')), RunInfo(run_id=UUID('c289f67a-87d6-4c8b-a8b7-0b5012c94ca8'))])" + "LLMResult(generations=[[Generation(text='The fastest dog in the world is the greyhound, which can run up to 45 miles per hour. This is about the same speed as a human running down a track. Greyhounds are very fast because they have long legs, a streamlined body, and a strong tail. They can run this fast for short distances, but they can also run for long distances, like a marathon. ', generation_info={'finish_reason': 'eos_token'})], [Generation(text='The Beagle is a scent hound, meaning it is bred to hunt by following a trail of scents.', generation_info={'finish_reason': 'eos_token'})]], llm_output={'token_usage': {'generated_token_count': 106, 'input_token_count': 13}, 'model_id': 'ibm/granite-13b-instruct-v2', 'deployment_id': ''}, run=[RunInfo(run_id=UUID('52cb421d-b63f-4c5f-9b04-d4770c664725')), RunInfo(run_id=UUID('df2ea606-1622-4ed7-8d5d-8f6e068b71c4'))])" ] }, - "execution_count": 9, + "execution_count": 11, "metadata": {}, "output_type": "execute_result" } @@ -296,7 +320,7 @@ }, { "cell_type": "code", - "execution_count": 45, + "execution_count": 12, "id": "3f63166a", "metadata": {}, "outputs": [ @@ -304,7 +328,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "The golden retriever is my favorite dog because it is very friendly and good with children." + "My favorite breed of dog is a Labrador Retriever. Labradors are my favorite because they are extremely smart, very friendly, and love to be with people. They are also very playful and love to run around and have a lot of energy. " ] } ], @@ -332,7 +356,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.12" + "version": "3.10.13" } }, "nbformat": 4, diff --git a/docs/docs/integrations/providers/ibm.mdx b/docs/docs/integrations/providers/ibm.mdx new file mode 100644 index 0000000000000..d820b8d56452e --- /dev/null +++ b/docs/docs/integrations/providers/ibm.mdx @@ -0,0 +1,39 @@ +# IBM + +The `LangChain` integrations related to [IBM watsonx.ai](https://www.ibm.com/products/watsonx-ai) platform. + +IBM® watsonx.ai™ AI studio is part of the IBM [watsonx](https://www.ibm.com/watsonx)™ AI and data platform, bringing together new generative +AI capabilities powered by [foundation models](https://www.ibm.com/products/watsonx-ai/foundation-models) and traditional machine learning (ML) +into a powerful studio spanning the AI lifecycle. Tune and guide models with your enterprise data to meet your needs with easy-to-use tools for +building and refining performant prompts. With watsonx.ai, you can build AI applications in a fraction of the time and with a fraction of the data. +Watsonx.ai offers: + +- **Multi-model variety and flexibility:** Choose from IBM-developed, open-source and third-party models, or build your own model. +- **Differentiated client protection:** IBM stands behind IBM-developed models and indemnifies the client against third-party IP claims. +- **End-to-end AI governance:** Enterprises can scale and accelerate the impact of AI with trusted data across the business, using data wherever it resides. +- **Hybrid, multi-cloud deployments:** IBM provides the flexibility to integrate and deploy your AI workloads into your hybrid-cloud stack of choice. + + +## Installation and Setup + +Install the integration package with +```bash +pip install -qU langchain-ibm +``` + +Get an IBM watsonx.ai api key and set it as an environment variable (`WATSONX_APIKEY`) +```python +import os + +os.environ["WATSONX_APIKEY"] = "your IBM watsonx.ai api key" +``` + +## LLMs + +### WatsonxLLM + +See a [usage example](/docs/integrations/llms/ibm_watsonx). + +```python +from langchain_ibm import WatsonxLLM +```