|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# NVIDIA NIMs\n", |
| 8 | + "\n", |
| 9 | + "The `langchain-nvidia-ai-endpoints` package contains LangChain integrations building applications with models on \n", |
| 10 | + "NVIDIA NIM inference microservice. NIM supports models across domains like chat, embedding, and re-ranking models \n", |
| 11 | + "from the community as well as NVIDIA. These models are optimized by NVIDIA to deliver the best performance on NVIDIA \n", |
| 12 | + "accelerated infrastructure and deployed as a NIM, an easy-to-use, prebuilt containers that deploy anywhere using a single \n", |
| 13 | + "command on NVIDIA accelerated infrastructure.\n", |
| 14 | + "\n", |
| 15 | + "NVIDIA hosted deployments of NIMs are available to test on the [NVIDIA API catalog](https://build.nvidia.com/). After testing, \n", |
| 16 | + "NIMs can be exported from NVIDIA’s API catalog using the NVIDIA AI Enterprise license and run on-premises or in the cloud, \n", |
| 17 | + "giving enterprises ownership and full control of their IP and AI application.\n", |
| 18 | + "\n", |
| 19 | + "This example goes over how to use LangChain to interact with NVIDIA supported via the `ChatNVIDIA` class to implement Marketing Post CrewAI Agent.\n", |
| 20 | + "\n", |
| 21 | + "For more information on accessing the chat models through this api, check out the [ChatNVIDIA](https://python.langchain.com/docs/integrations/chat/nvidia_ai_endpoints/) documentation." |
| 22 | + ] |
| 23 | + }, |
| 24 | + { |
| 25 | + "cell_type": "code", |
| 26 | + "execution_count": null, |
| 27 | + "metadata": {}, |
| 28 | + "outputs": [], |
| 29 | + "source": [ |
| 30 | + "%pip install --upgrade --quiet marketing_posts" |
| 31 | + ] |
| 32 | + }, |
| 33 | + { |
| 34 | + "cell_type": "markdown", |
| 35 | + "metadata": {}, |
| 36 | + "source": [ |
| 37 | + "## Setup\n", |
| 38 | + "\n", |
| 39 | + "Import our dependencies and set up our NVIDIA API key from the API catalog, https://build.nvidia.com for the two models we'll use hosted on the catalog (embedding and re-ranking models).\n", |
| 40 | + "\n", |
| 41 | + "**To get started:**\n", |
| 42 | + "\n", |
| 43 | + "1. Create a free account with [NVIDIA](https://build.nvidia.com/), which hosts NVIDIA AI Foundation models.\n", |
| 44 | + "\n", |
| 45 | + "2. Click on your model of choice.\n", |
| 46 | + "\n", |
| 47 | + "3. Under Input select the Python tab, and click `Get API Key`. Then click `Generate Key`.\n", |
| 48 | + "\n", |
| 49 | + "4. Copy and save the generated key as NVIDIA_API_KEY. From there, you should have access to the endpoints." |
| 50 | + ] |
| 51 | + }, |
| 52 | + { |
| 53 | + "cell_type": "code", |
| 54 | + "execution_count": 2, |
| 55 | + "metadata": {}, |
| 56 | + "outputs": [], |
| 57 | + "source": [ |
| 58 | + "import getpass\n", |
| 59 | + "import os\n", |
| 60 | + "\n", |
| 61 | + "# del os.environ['NVIDIA_API_KEY'] ## delete key and reset\n", |
| 62 | + "if os.environ.get(\"NVIDIA_API_KEY\", \"\").startswith(\"nvapi-\"):\n", |
| 63 | + " print(\"Valid NVIDIA_API_KEY already in environment. Delete to reset\")\n", |
| 64 | + "else:\n", |
| 65 | + " nvapi_key = getpass.getpass(\"NVAPI Key (starts with nvapi-): \")\n", |
| 66 | + " assert nvapi_key.startswith(\n", |
| 67 | + " \"nvapi-\"\n", |
| 68 | + " ), f\"{nvapi_key[:5]}... is not a valid key\"\n", |
| 69 | + " os.environ[\"NVIDIA_API_KEY\"] = nvapi_key" |
| 70 | + ] |
| 71 | + }, |
| 72 | + { |
| 73 | + "cell_type": "code", |
| 74 | + "execution_count": null, |
| 75 | + "metadata": {}, |
| 76 | + "outputs": [], |
| 77 | + "source": [ |
| 78 | + "# set API Endoipoint\n", |
| 79 | + "# to call local model set NVIDIA_API_URL to local NIM endpoint\n", |
| 80 | + "os.environ[\"NVIDIA_API_URL\"] = \"http://localhost:8000/v1\" # for local NIM container\n", |
| 81 | + "# os.environ[\"NVIDIA_API_URL\"] = \"https://integrate.api.nvidia.com/v1\"" |
| 82 | + ] |
| 83 | + }, |
| 84 | + { |
| 85 | + "cell_type": "markdown", |
| 86 | + "metadata": {}, |
| 87 | + "source": [ |
| 88 | + "Setup model using environment variable MODEL as below" |
| 89 | + ] |
| 90 | + }, |
| 91 | + { |
| 92 | + "cell_type": "code", |
| 93 | + "execution_count": 4, |
| 94 | + "metadata": {}, |
| 95 | + "outputs": [], |
| 96 | + "source": [ |
| 97 | + "#set model\n", |
| 98 | + "os.environ[\"MODEL\"] = \"meta/llama-2-7b-chat\"" |
| 99 | + ] |
| 100 | + }, |
| 101 | + { |
| 102 | + "cell_type": "markdown", |
| 103 | + "metadata": {}, |
| 104 | + "source": [ |
| 105 | + "Import the run function and kickoff the marketting creawai agent" |
| 106 | + ] |
| 107 | + }, |
| 108 | + { |
| 109 | + "cell_type": "code", |
| 110 | + "execution_count": 5, |
| 111 | + "metadata": {}, |
| 112 | + "outputs": [], |
| 113 | + "source": [ |
| 114 | + "from marketing_posts.main import run" |
| 115 | + ] |
| 116 | + }, |
| 117 | + { |
| 118 | + "cell_type": "code", |
| 119 | + "execution_count": null, |
| 120 | + "metadata": {}, |
| 121 | + "outputs": [], |
| 122 | + "source": [ |
| 123 | + "run()" |
| 124 | + ] |
| 125 | + }, |
| 126 | + { |
| 127 | + "cell_type": "code", |
| 128 | + "execution_count": null, |
| 129 | + "metadata": {}, |
| 130 | + "outputs": [], |
| 131 | + "source": [] |
| 132 | + } |
| 133 | + ], |
| 134 | + "metadata": { |
| 135 | + "kernelspec": { |
| 136 | + "display_name": "Python 3 (ipykernel)", |
| 137 | + "language": "python", |
| 138 | + "name": "python3" |
| 139 | + }, |
| 140 | + "language_info": { |
| 141 | + "codemirror_mode": { |
| 142 | + "name": "ipython", |
| 143 | + "version": 3 |
| 144 | + }, |
| 145 | + "file_extension": ".py", |
| 146 | + "mimetype": "text/x-python", |
| 147 | + "name": "python", |
| 148 | + "nbconvert_exporter": "python", |
| 149 | + "pygments_lexer": "ipython3", |
| 150 | + "version": "3.10.12" |
| 151 | + } |
| 152 | + }, |
| 153 | + "nbformat": 4, |
| 154 | + "nbformat_minor": 2 |
| 155 | +} |
0 commit comments