From 720c5741344361c7385b3100927a346bef55e085 Mon Sep 17 00:00:00 2001 From: raspawar Date: Thu, 17 Oct 2024 17:39:18 +0530 Subject: [PATCH] docs: test metadata changes notebook --- .../docs/chat/nvidia_ai_endpoints.ipynb | 282 ++++++++++++++++-- 1 file changed, 256 insertions(+), 26 deletions(-) diff --git a/libs/ai-endpoints/docs/chat/nvidia_ai_endpoints.ipynb b/libs/ai-endpoints/docs/chat/nvidia_ai_endpoints.ipynb index eb4cfdbc..33032175 100644 --- a/libs/ai-endpoints/docs/chat/nvidia_ai_endpoints.ipynb +++ b/libs/ai-endpoints/docs/chat/nvidia_ai_endpoints.ipynb @@ -41,7 +41,9 @@ "id": "e13eb331", "metadata": {}, "outputs": [], - "source": ["%pip install --upgrade --quiet langchain-nvidia-ai-endpoints"] + "source": [ + "%pip install --upgrade --quiet langchain-nvidia-ai-endpoints" + ] }, { "cell_type": "markdown", @@ -69,7 +71,18 @@ "id": "686c4d2f", "metadata": {}, "outputs": [], - "source": ["import getpass\nimport os\n\n# del os.environ['NVIDIA_API_KEY'] ## delete key and reset\nif os.environ.get(\"NVIDIA_API_KEY\", \"\").startswith(\"nvapi-\"):\n print(\"Valid NVIDIA_API_KEY already in environment. Delete to reset\")\nelse:\n nvapi_key = getpass.getpass(\"NVAPI Key (starts with nvapi-): \")\n assert nvapi_key.startswith(\"nvapi-\"), f\"{nvapi_key[:5]}... is not a valid key\"\n os.environ[\"NVIDIA_API_KEY\"] = nvapi_key"] + "source": [ + "import getpass\n", + "import os\n", + "\n", + "# del os.environ['NVIDIA_API_KEY'] ## delete key and reset\n", + "if os.environ.get(\"NVIDIA_API_KEY\", \"\").startswith(\"nvapi-\"):\n", + " print(\"Valid NVIDIA_API_KEY already in environment. Delete to reset\")\n", + "else:\n", + " nvapi_key = getpass.getpass(\"NVAPI Key (starts with nvapi-): \")\n", + " assert nvapi_key.startswith(\"nvapi-\"), f\"{nvapi_key[:5]}... is not a valid key\"\n", + " os.environ[\"NVIDIA_API_KEY\"] = nvapi_key" + ] }, { "cell_type": "markdown", @@ -91,7 +104,14 @@ "outputId": "e9c4cc72-8db6-414b-d8e9-95de93fc5db4" }, "outputs": [], - "source": ["## Core LC Chat Interface\nfrom langchain_nvidia_ai_endpoints import ChatNVIDIA\n\nllm = ChatNVIDIA(model=\"mistralai/mixtral-8x7b-instruct-v0.1\")\nresult = llm.invoke(\"Write a ballad about LangChain.\")\nprint(result.content)"] + "source": [ + "## Core LC Chat Interface\n", + "from langchain_nvidia_ai_endpoints import ChatNVIDIA\n", + "\n", + "llm = ChatNVIDIA(model=\"mistralai/mixtral-8x7b-instruct-v0.1\")\n", + "result = llm.invoke(\"Write a ballad about LangChain.\")\n", + "print(result.content)" + ] }, { "cell_type": "markdown", @@ -110,7 +130,12 @@ "id": "49838930", "metadata": {}, "outputs": [], - "source": ["from langchain_nvidia_ai_endpoints import ChatNVIDIA\n\n# connect to an embedding NIM running at localhost:8000, specifying a specific model\nllm = ChatNVIDIA(base_url=\"http://localhost:8000/v1\", model=\"meta/llama3-8b-instruct\")"] + "source": [ + "from langchain_nvidia_ai_endpoints import ChatNVIDIA\n", + "\n", + "# connect to an embedding NIM running at localhost:8000, specifying a specific model\n", + "llm = ChatNVIDIA(base_url=\"http://localhost:8000/v1\", model=\"meta/llama3-8b-instruct\")" + ] }, { "cell_type": "markdown", @@ -128,7 +153,11 @@ "id": "01fa5095-be72-47b0-8247-e9fac799435d", "metadata": {}, "outputs": [], - "source": ["print(llm.batch([\"What's 2*3?\", \"What's 2*6?\"]))\n# Or via the async API\n# await llm.abatch([\"What's 2*3?\", \"What's 2*6?\"])"] + "source": [ + "print(llm.batch([\"What's 2*3?\", \"What's 2*6?\"]))\n", + "# Or via the async API\n", + "# await llm.abatch([\"What's 2*3?\", \"What's 2*6?\"])" + ] }, { "cell_type": "code", @@ -136,7 +165,11 @@ "id": "75189ac6-e13f-414f-9064-075c77d6e754", "metadata": {}, "outputs": [], - "source": ["for chunk in llm.stream(\"How far can a seagull fly in one day?\"):\n # Show the token separations\n print(chunk.content, end=\"|\")"] + "source": [ + "for chunk in llm.stream(\"How far can a seagull fly in one day?\"):\n", + " # Show the token separations\n", + " print(chunk.content, end=\"|\")" + ] }, { "cell_type": "code", @@ -144,7 +177,12 @@ "id": "8a9a4122-7a10-40c0-a979-82a769ce7f6a", "metadata": {}, "outputs": [], - "source": ["async for chunk in llm.astream(\n \"How long does it take for monarch butterflies to migrate?\"\n):\n print(chunk.content, end=\"|\")"] + "source": [ + "async for chunk in llm.astream(\n", + " \"How long does it take for monarch butterflies to migrate?\"\n", + "):\n", + " print(chunk.content, end=\"|\")" + ] }, { "cell_type": "markdown", @@ -166,7 +204,10 @@ "id": "5b8a312d-38e9-4528-843e-59451bdadbac", "metadata": {}, "outputs": [], - "source": ["ChatNVIDIA.get_available_models()\n# llm.get_available_models()"] + "source": [ + "ChatNVIDIA.get_available_models()\n", + "# llm.get_available_models()" + ] }, { "cell_type": "markdown", @@ -206,7 +247,19 @@ "id": "f5f7aee8-e90c-4d5a-ac97-0dd3d45c3f4c", "metadata": {}, "outputs": [], - "source": ["from langchain_core.output_parsers import StrOutputParser\nfrom langchain_core.prompts import ChatPromptTemplate\nfrom langchain_nvidia_ai_endpoints import ChatNVIDIA\n\nprompt = ChatPromptTemplate.from_messages(\n [(\"system\", \"You are a helpful AI assistant named Fred.\"), (\"user\", \"{input}\")]\n)\nchain = prompt | ChatNVIDIA(model=\"meta/llama3-8b-instruct\") | StrOutputParser()\n\nfor txt in chain.stream({\"input\": \"What's your name?\"}):\n print(txt, end=\"\")"] + "source": [ + "from langchain_core.output_parsers import StrOutputParser\n", + "from langchain_core.prompts import ChatPromptTemplate\n", + "from langchain_nvidia_ai_endpoints import ChatNVIDIA\n", + "\n", + "prompt = ChatPromptTemplate.from_messages(\n", + " [(\"system\", \"You are a helpful AI assistant named Fred.\"), (\"user\", \"{input}\")]\n", + ")\n", + "chain = prompt | ChatNVIDIA(model=\"meta/llama3-8b-instruct\") | StrOutputParser()\n", + "\n", + "for txt in chain.stream({\"input\": \"What's your name?\"}):\n", + " print(txt, end=\"\")" + ] }, { "cell_type": "markdown", @@ -224,7 +277,21 @@ "id": "49aa569b-5f33-47b3-9edc-df58313eb038", "metadata": {}, "outputs": [], - "source": ["prompt = ChatPromptTemplate.from_messages(\n [\n (\n \"system\",\n \"You are an expert coding AI. Respond only in valid python; no narration whatsoever.\",\n ),\n (\"user\", \"{input}\"),\n ]\n)\nchain = prompt | ChatNVIDIA(model=\"meta/codellama-70b\") | StrOutputParser()\n\nfor txt in chain.stream({\"input\": \"How do I solve this fizz buzz problem?\"}):\n print(txt, end=\"\")"] + "source": [ + "prompt = ChatPromptTemplate.from_messages(\n", + " [\n", + " (\n", + " \"system\",\n", + " \"You are an expert coding AI. Respond only in valid python; no narration whatsoever.\",\n", + " ),\n", + " (\"user\", \"{input}\"),\n", + " ]\n", + ")\n", + "chain = prompt | ChatNVIDIA(model=\"meta/codellama-70b\") | StrOutputParser()\n", + "\n", + "for txt in chain.stream({\"input\": \"How do I solve this fizz buzz problem?\"}):\n", + " print(txt, end=\"\")" + ] }, { "cell_type": "markdown", @@ -244,7 +311,15 @@ "id": "26625437-1695-440f-b792-b85e6add9a90", "metadata": {}, "outputs": [], - "source": ["import IPython\nimport requests\n\nimage_url = \"https://www.nvidia.com/content/dam/en-zz/Solutions/research/ai-playground/nvidia-picasso-3c33-p@2x.jpg\" ## Large Image\nimage_content = requests.get(image_url).content\n\nIPython.display.Image(image_content)"] + "source": [ + "import IPython\n", + "import requests\n", + "\n", + "image_url = \"https://www.nvidia.com/content/dam/en-zz/Solutions/research/ai-playground/nvidia-picasso-3c33-p@2x.jpg\" ## Large Image\n", + "image_content = requests.get(image_url).content\n", + "\n", + "IPython.display.Image(image_content)" + ] }, { "cell_type": "code", @@ -252,7 +327,11 @@ "id": "dfbbe57c-27a5-4cbb-b967-19c4e7d29fd0", "metadata": {}, "outputs": [], - "source": ["from langchain_nvidia_ai_endpoints import ChatNVIDIA\n\nllm = ChatNVIDIA(model=\"nvidia/neva-22b\")"] + "source": [ + "from langchain_nvidia_ai_endpoints import ChatNVIDIA\n", + "\n", + "llm = ChatNVIDIA(model=\"nvidia/neva-22b\")" + ] }, { "cell_type": "markdown", @@ -268,7 +347,20 @@ "id": "432ea2a2-4d39-43f8-a236-041294171f14", "metadata": {}, "outputs": [], - "source": ["from langchain_core.messages import HumanMessage\n\nllm.invoke(\n [\n HumanMessage(\n content=[\n {\"type\": \"text\", \"text\": \"Describe this image:\"},\n {\"type\": \"image_url\", \"image_url\": {\"url\": image_url}},\n ]\n )\n ]\n)"] + "source": [ + "from langchain_core.messages import HumanMessage\n", + "\n", + "llm.invoke(\n", + " [\n", + " HumanMessage(\n", + " content=[\n", + " {\"type\": \"text\", \"text\": \"Describe this image:\"},\n", + " {\"type\": \"image_url\", \"image_url\": {\"url\": image_url}},\n", + " ]\n", + " )\n", + " ]\n", + ")" + ] }, { "cell_type": "markdown", @@ -366,7 +458,15 @@ "id": "c58f1dd0", "metadata": {}, "outputs": [], - "source": ["import IPython\nimport requests\n\nimage_url = \"https://picsum.photos/seed/kitten/300/200\"\nimage_content = requests.get(image_url).content\n\nIPython.display.Image(image_content)"] + "source": [ + "import IPython\n", + "import requests\n", + "\n", + "image_url = \"https://picsum.photos/seed/kitten/300/200\"\n", + "image_content = requests.get(image_url).content\n", + "\n", + "IPython.display.Image(image_content)" + ] }, { "cell_type": "code", @@ -374,7 +474,28 @@ "id": "8c721629-42eb-4006-bf68-0296f7925ebc", "metadata": {}, "outputs": [], - "source": ["import base64\n\nfrom langchain_core.messages import HumanMessage\n\n## Works for simpler images. For larger images, see actual implementation\nb64_string = base64.b64encode(image_content).decode(\"utf-8\")\n\nllm.invoke(\n [\n HumanMessage(\n content=[\n {\"type\": \"text\", \"text\": \"Describe this image:\"},\n {\n \"type\": \"image_url\",\n \"image_url\": {\"url\": f\"data:image/png;base64,{b64_string}\"},\n },\n ]\n )\n ]\n)"] + "source": [ + "import base64\n", + "\n", + "from langchain_core.messages import HumanMessage\n", + "\n", + "## Works for simpler images. For larger images, see actual implementation\n", + "b64_string = base64.b64encode(image_content).decode(\"utf-8\")\n", + "\n", + "llm.invoke(\n", + " [\n", + " HumanMessage(\n", + " content=[\n", + " {\"type\": \"text\", \"text\": \"Describe this image:\"},\n", + " {\n", + " \"type\": \"image_url\",\n", + " \"image_url\": {\"url\": f\"data:image/png;base64,{b64_string}\"},\n", + " },\n", + " ]\n", + " )\n", + " ]\n", + ")" + ] }, { "cell_type": "markdown", @@ -392,7 +513,10 @@ "id": "00c06a9a-497b-4192-a842-b075e27401aa", "metadata": {}, "outputs": [], - "source": ["base64_with_mime_type = f\"data:image/png;base64,{b64_string}\"\nllm.invoke(f'What\\'s in this image?\\n')"] + "source": [ + "base64_with_mime_type = f\"data:image/png;base64,{b64_string}\"\n", + "llm.invoke(f'What\\'s in this image?\\n')" + ] }, { "cell_type": "markdown", @@ -420,7 +544,9 @@ "id": "082ccb21-91e1-4e71-a9ba-4bff1e89f105", "metadata": {}, "outputs": [], - "source": ["%pip install --upgrade --quiet langchain"] + "source": [ + "%pip install --upgrade --quiet langchain" + ] }, { "cell_type": "code", @@ -430,7 +556,41 @@ "id": "fd2c6bc1" }, "outputs": [], - "source": ["from langchain_core.chat_history import InMemoryChatMessageHistory\nfrom langchain_core.runnables.history import RunnableWithMessageHistory\n\n# store is a dictionary that maps session IDs to their corresponding chat histories.\nstore = {} # memory is maintained outside the chain\n\n\n# A function that returns the chat history for a given session ID.\ndef get_session_history(session_id: str) -> InMemoryChatMessageHistory:\n if session_id not in store:\n store[session_id] = InMemoryChatMessageHistory()\n return store[session_id]\n\n\nchat = ChatNVIDIA(\n model=\"mistralai/mixtral-8x22b-instruct-v0.1\",\n temperature=0.1,\n max_tokens=100,\n top_p=1.0,\n)\n\n# Define a RunnableConfig object, with a `configurable` key. session_id determines thread\nconfig = {\"configurable\": {\"session_id\": \"1\"}}\n\nconversation = RunnableWithMessageHistory(\n chat,\n get_session_history,\n)\n\nconversation.invoke(\n \"Hi I'm Srijan Dubey.\", # input or query\n config=config,\n)"] + "source": [ + "from langchain_core.chat_history import InMemoryChatMessageHistory\n", + "from langchain_core.runnables.history import RunnableWithMessageHistory\n", + "\n", + "# store is a dictionary that maps session IDs to their corresponding chat histories.\n", + "store = {} # memory is maintained outside the chain\n", + "\n", + "\n", + "# A function that returns the chat history for a given session ID.\n", + "def get_session_history(session_id: str) -> InMemoryChatMessageHistory:\n", + " if session_id not in store:\n", + " store[session_id] = InMemoryChatMessageHistory()\n", + " return store[session_id]\n", + "\n", + "\n", + "chat = ChatNVIDIA(\n", + " model=\"mistralai/mixtral-8x22b-instruct-v0.1\",\n", + " temperature=0.1,\n", + " max_tokens=100,\n", + " top_p=1.0,\n", + ")\n", + "\n", + "# Define a RunnableConfig object, with a `configurable` key. session_id determines thread\n", + "config = {\"configurable\": {\"session_id\": \"1\"}}\n", + "\n", + "conversation = RunnableWithMessageHistory(\n", + " chat,\n", + " get_session_history,\n", + ")\n", + "\n", + "conversation.invoke(\n", + " \"Hi I'm Srijan Dubey.\", # input or query\n", + " config=config,\n", + ")" + ] }, { "cell_type": "code", @@ -445,7 +605,12 @@ "outputId": "79acc89d-a820-4f2c-bac2-afe99da95580" }, "outputs": [], - "source": ["conversation.invoke(\n \"I'm doing well! Just having a conversation with an AI.\",\n config=config,\n)"] + "source": [ + "conversation.invoke(\n", + " \"I'm doing well! Just having a conversation with an AI.\",\n", + " config=config,\n", + ")" + ] }, { "cell_type": "code", @@ -460,7 +625,12 @@ "outputId": "a1714513-a8fd-4d14-f974-233e39d5c4f5" }, "outputs": [], - "source": ["conversation.invoke(\n \"Tell me about yourself.\",\n config=config,\n)"] + "source": [ + "conversation.invoke(\n", + " \"Tell me about yourself.\",\n", + " config=config,\n", + ")" + ] }, { "cell_type": "markdown", @@ -488,7 +658,10 @@ "id": "e36c8911", "metadata": {}, "outputs": [], - "source": ["tool_models = [model for model in ChatNVIDIA.get_available_models() if model.supports_tools]\ntool_models"] + "source": [ + "tool_models = [model for model in ChatNVIDIA.get_available_models() if model.supports_tools]\n", + "tool_models" + ] }, { "cell_type": "markdown", @@ -504,7 +677,21 @@ "id": "bd54f174", "metadata": {}, "outputs": [], - "source": ["from pydantic import Field\nfrom langchain_core.tools import tool\n\n@tool\ndef get_current_weather(\n location: str = Field(..., description=\"The location to get the weather for.\")\n):\n \"\"\"Get the current weather for a location.\"\"\"\n ...\n\nllm = ChatNVIDIA(model=tool_models[0].id).bind_tools(tools=[get_current_weather])\nresponse = llm.invoke(\"What is the weather in Boston?\")\nresponse.tool_calls"] + "source": [ + "from pydantic import Field\n", + "from langchain_core.tools import tool\n", + "\n", + "@tool\n", + "def get_current_weather(\n", + " location: str = Field(..., description=\"The location to get the weather for.\")\n", + "):\n", + " \"\"\"Get the current weather for a location.\"\"\"\n", + " ...\n", + "\n", + "llm = ChatNVIDIA(model=tool_models[0].id).bind_tools(tools=[get_current_weather])\n", + "response = llm.invoke(\"What is the weather in Boston?\")\n", + "response.tool_calls" + ] }, { "cell_type": "markdown", @@ -542,7 +729,11 @@ "id": "0515f558", "metadata": {}, "outputs": [], - "source": ["from langchain_nvidia_ai_endpoints import ChatNVIDIA\nstructured_models = [model for model in ChatNVIDIA.get_available_models() if model.supports_structured_output]\nstructured_models"] + "source": [ + "from langchain_nvidia_ai_endpoints import ChatNVIDIA\n", + "structured_models = [model for model in ChatNVIDIA.get_available_models() if model.supports_structured_output]\n", + "structured_models" + ] }, { "cell_type": "markdown", @@ -558,7 +749,17 @@ "id": "482c37e8", "metadata": {}, "outputs": [], - "source": ["from pydantic import BaseModel, Field\n\nclass Person(BaseModel):\n first_name: str = Field(..., description=\"The person's first name.\")\n last_name: str = Field(..., description=\"The person's last name.\")\n\nllm = ChatNVIDIA(model=structured_models[0].id).with_structured_output(Person)\nresponse = llm.invoke(\"Who is Michael Jeffrey Jordon?\")\nresponse"] + "source": [ + "from pydantic import BaseModel, Field\n", + "\n", + "class Person(BaseModel):\n", + " first_name: str = Field(..., description=\"The person's first name.\")\n", + " last_name: str = Field(..., description=\"The person's last name.\")\n", + "\n", + "llm = ChatNVIDIA(model=structured_models[0].id).with_structured_output(Person)\n", + "response = llm.invoke(\"Who is Michael Jeffrey Jordon?\")\n", + "response" + ] }, { "cell_type": "markdown", @@ -574,7 +775,24 @@ "id": "7f802912", "metadata": {}, "outputs": [], - "source": ["from enum import Enum\n\nclass Choices(Enum):\n A = \"A\"\n B = \"B\"\n C = \"C\"\n\nllm = ChatNVIDIA(model=structured_models[2].id).with_structured_output(Choices)\nresponse = llm.invoke(\"\"\"\n What does 1+1 equal?\n A. -100\n B. 2\n C. doorstop\n \"\"\"\n)\nresponse"] + "source": [ + "from enum import Enum\n", + "\n", + "class Choices(Enum):\n", + " A = \"A\"\n", + " B = \"B\"\n", + " C = \"C\"\n", + "\n", + "llm = ChatNVIDIA(model=structured_models[2].id).with_structured_output(Choices)\n", + "response = llm.invoke(\"\"\"\n", + " What does 1+1 equal?\n", + " A. -100\n", + " B. 2\n", + " C. doorstop\n", + " \"\"\"\n", + ")\n", + "response" + ] }, { "cell_type": "code", @@ -582,7 +800,19 @@ "id": "02b7ef29", "metadata": {}, "outputs": [], - "source": ["model = structured_models[3].id\nllm = ChatNVIDIA(model=model).with_structured_output(Choices)\nprint(model)\nresponse = llm.invoke(\"\"\"\n What does 1+1 equal?\n A. -100\n B. 2\n C. doorstop\n \"\"\"\n)\nresponse"] + "source": [ + "model = structured_models[3].id\n", + "llm = ChatNVIDIA(model=model).with_structured_output(Choices)\n", + "print(model)\n", + "response = llm.invoke(\"\"\"\n", + " What does 1+1 equal?\n", + " A. -100\n", + " B. 2\n", + " C. doorstop\n", + " \"\"\"\n", + ")\n", + "response" + ] } ], "metadata": {