From 10830e0e72029bc40d0e9dc9f1854a561b54b9fe Mon Sep 17 00:00:00 2001 From: Tom Liu Date: Wed, 6 Aug 2025 00:40:04 +0000 Subject: [PATCH 1/2] To fix bug on the notebook: 1) replace sdxl with stable-diffusion-3-medium as sdxl nim was deprecated; 2) remove unnecessary import statements Signed-off-by: Tom Liu --- ...tool_call_HumanInTheLoop_MultiAgents.ipynb | 54 ++++++++++++------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/RAG/notebooks/langchain/NIM_tool_call_HumanInTheLoop_MultiAgents.ipynb b/RAG/notebooks/langchain/NIM_tool_call_HumanInTheLoop_MultiAgents.ipynb index 2f7465d94..b0d8aa0dc 100644 --- a/RAG/notebooks/langchain/NIM_tool_call_HumanInTheLoop_MultiAgents.ipynb +++ b/RAG/notebooks/langchain/NIM_tool_call_HumanInTheLoop_MultiAgents.ipynb @@ -65,7 +65,16 @@ "outputs": [], "source": [ "## install a few python packages we will need\n", - "#!pip install colorama markdown langgraph" + "# !pip install langchain==0.3.27\n", + "# !pip install langchain-core>=0.3.72\n", + "# !pip install langchain-community==0.3.27\n", + "# !pip install langchain-nvidia-ai-endpoints==0.3.13\n", + "# !pip install langchain-openai>=0.3.28\n", + "# !pip install langgraph==0.6.3\n", + "# !pip install colorama>=0.4.6\n", + "# !pip install markdown>=3.8.2\n", + "# !pip install pydantic==2.11.7\n", + "# !pip install pip" ] }, { @@ -75,9 +84,6 @@ "metadata": {}, "outputs": [], "source": [ - "import getpass\n", - "import os\n", - "\n", "import getpass\n", "import os\n", "\n", @@ -140,11 +146,9 @@ "\n", "# test run and see that you can genreate a respond successfully \n", "from langchain_nvidia_ai_endpoints import ChatNVIDIA\n", - "from langchain import prompts, chat_models, hub\n", - "from langchain_core.output_parsers import StrOutputParser\n", - "from langchain_core.prompts import ChatPromptTemplate, PromptTemplate\n", - "from langchain_core.pydantic_v1 import BaseModel, Field, validator\n", - "from typing import Optional, List\n", + "from langchain_core.prompts import PromptTemplate\n", + "from pydantic import BaseModel, Field\n", + "from typing import List\n", "\n", "## construct the system prompt \n", "prompt_template = \"\"\"\n", @@ -231,7 +235,7 @@ "\n", "We will equip the **digital_artist** with the following :\n", "\n", - "- a text-to-image model [stableXL-turbo from NVIDIA NIM ](https://build.nvidia.com/explore/visual-design?snippet_tab=Python#sdxl-turbo)\n", + "- a text-to-image model [stable-diffusion-3-medium from NVIDIA NIM ](https://build.nvidia.com/explore/visual-design?snippet_tab=stable-diffusion-3-medium)\n", "- wrap this tool into llm with llm.bind_tools\n", "- construct our **digital_artist** agent with LCEL expression" ] @@ -294,7 +298,11 @@ " ## re-writing the input promotion title in to appropriate image_gen prompt \n", " gen_prompt=llm_rewrite_to_image_prompts(prompt)\n", " print(\"start generating image with llm re-write prompt:\", gen_prompt)\n", - " invoke_url = \"https://ai.api.nvidia.com/v1/genai/stabilityai/sdxl-turbo\"\n", + " \n", + " invoke_url = \"https://ai.api.nvidia.com/v1/genai/stabilityai/stable-diffusion-3-medium\"\n", + "\n", + " # SD XL turbo was deprecated.\n", + " # invoke_url = \"https://ai.api.nvidia.com/v1/genai/stabilityai/sdxl-turbo\" \n", " \n", " headers = {\n", " \"Authorization\": f\"Bearer {nvapi_key}\",\n", @@ -302,10 +310,12 @@ " }\n", " \n", " payload = {\n", - " \"text_prompts\": [{\"text\": gen_prompt}],\n", + " \"prompt\": gen_prompt,\n", " \"seed\": 0,\n", - " \"sampler\": \"K_EULER_ANCESTRAL\",\n", - " \"steps\": 2\n", + " \"cfg_scale\": 5,\n", + " \"aspect_ratio\": \"16:9\",\n", + " \"steps\": 10,\n", + " \"negative_prompt\": \"\"\n", " }\n", " \n", " response = requests.post(invoke_url, headers=headers, json=payload)\n", @@ -313,8 +323,7 @@ " response.raise_for_status()\n", " response_body = response.json()\n", " ## load back to numpy array \n", - " print(response_body['artifacts'][0].keys())\n", - " imgdata = base64.b64decode(response_body[\"artifacts\"][0][\"base64\"])\n", + " imgdata = base64.b64decode(response_body[\"image\"])\n", " filename = 'output.jpg'\n", " with open(filename, 'wb') as f:\n", " f.write(imgdata) \n", @@ -544,7 +553,6 @@ "outputs": [], "source": [ "from langgraph.graph import END, StateGraph\n", - "from langgraph.prebuilt import ToolInvocation\n", "from colorama import Fore,Style\n", "# Define the functions needed \n", "def human_assign_to_agent(state):\n", @@ -746,11 +754,19 @@ " display(Markdown(markdown_str))\n", "printmd(markdown_str)" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b2252a44", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "generativeaiexamples", "language": "python", "name": "python3" }, @@ -764,7 +780,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.4" + "version": "3.12.11" } }, "nbformat": 4, From 64de0d53d173f76ff4f31f4d3c3d2d655b1b0e7f Mon Sep 17 00:00:00 2001 From: Tom Liu Date: Sun, 10 Aug 2025 01:32:16 +0000 Subject: [PATCH 2/2] To upgrade langchain, langgraph, faiss packagge; correct embedding QA model id and use correct class on langgraph. Signed-off-by: Tom Liu --- ...raph_HandlingAgent_IntermediateSteps.ipynb | 54 +++++++++++++------ 1 file changed, 37 insertions(+), 17 deletions(-) diff --git a/RAG/notebooks/langchain/LangGraph_HandlingAgent_IntermediateSteps.ipynb b/RAG/notebooks/langchain/LangGraph_HandlingAgent_IntermediateSteps.ipynb index e1f18b27b..9d0a8dd74 100644 --- a/RAG/notebooks/langchain/LangGraph_HandlingAgent_IntermediateSteps.ipynb +++ b/RAG/notebooks/langchain/LangGraph_HandlingAgent_IntermediateSteps.ipynb @@ -44,13 +44,19 @@ "metadata": {}, "outputs": [], "source": [ - "!pip install --upgrade pip\n", - "!pip install langchain==0.2.5\n", - "!pip install langchain-nvidia-ai-endpoints==0.1.2\n", - "!pip install langchain-community==0.2.2\n", - "!pip install langgraph==0.0.62\n", - "!pip install faiss-gpu==1.7.2\n", - "!pip install wikipedia==1.4.0" + "# install below package when 'pip' is referred to the dedicated python virtual environment. \n", + "# !pip install --upgrade pip\n", + "# !pip install langchain==0.3.27\n", + "# !pip install langchain-nvidia-ai-endpoints==0.3.13\n", + "# !pip install langchain-community==0.3.27\n", + "# !pip install langgraph==0.6.3\n", + "# !pip install wikipedia==1.4.0\n", + "\n", + "# faiss-gpu can only be used up to py310, when using later Python versions, please move to faiss-cpu\n", + "# with py311+\n", + "# !pip install faiss-cpu=1.11.0\n", + "# or with GPU & py310 and below\n", + "# !pip install faiss-gpu==1.7.2\n" ] }, { @@ -113,8 +119,12 @@ "from langchain_nvidia_ai_endpoints import ChatNVIDIA\n", "from langchain_nvidia_ai_endpoints import NVIDIAEmbeddings\n", "\n", - "llm = ChatNVIDIA(model=\"mistralai/mixtral-8x7b-instruct-v0.1\", nvidia_api_key=nvapi_key, max_tokens=2048)\n", - "embedder = NVIDIAEmbeddings(model=\"NV-Embed-QA\", truncate=\"END\")\n" + "llm = ChatNVIDIA(model=\"mistralai/mixtral-8x7b-instruct-v0.1\", \n", + " nvidia_api_key=nvapi_key, \n", + " max_completion_tokens=2048) # max_tokens is about to be depreciated.\n", + "\n", + "embedder = NVIDIAEmbeddings(model=\"nvidia/nv-embedqa-e5-v5\", # recent valid embedding QA model\n", + " truncate=\"END\")\n" ] }, { @@ -197,12 +207,13 @@ "outputs": [], "source": [ "from langchain.tools import BaseTool\n", + "from pydantic import Field\n", "\n", "class SwedenRetriever(BaseTool):\n", - " name = \"AboutSweden\"\n", - " description = \"Useful for when you need to answer questions about Sweden's population, history, and so on.\"\n", + " name: str = \"AboutSweden\"\n", + " description: str = \"Useful for when you need to answer questions about Sweden's population, history, and so on.\"\n", "\n", - " def _run(self, query):\n", + " def _run(self, query) -> str:\n", " out = retriever.invoke(query)\n", " o = out[0]\n", " item=o.page_content.split('|')\n", @@ -257,6 +268,7 @@ " func=wikipedia.run,\n", " name=\"Wiki\",\n", " description=\"useful for when you need to search certain topic on Wikipedia, aka wiki\")\n", + "\n", "retriever_tool=Tool.from_function(\n", " func=sv.invoke,\n", " name=\"AboutSweden\",\n", @@ -283,11 +295,11 @@ "outputs": [], "source": [ "from langchain_core.agents import AgentFinish\n", - "from langgraph.prebuilt.tool_executor import ToolExecutor\n", + "from langgraph.prebuilt import ToolNode\n", "\n", "# This a helper class we have that is useful for running tools\n", "# It takes in an agent action and calls that tool and returns the result\n", - "tool_executor = ToolExecutor(tools)\n" + "tool_node = ToolNode(tools)\n" ] }, { @@ -505,7 +517,7 @@ " agent_output = data[\"agent_outcome\"]\n", " if len(agent_output['intermediate_steps'])>=1 :\n", " agent_action = agent_output['intermediate_steps'][0][0]\n", - " output = tool_executor.invoke(agent_action)\n", + " output = tool_node.invoke(agent_action)\n", " return {\"intermediate_steps\": [(agent_action, str(output))]}\n", " else:\n", " return {\"intermediate_steps\":[]}\n", @@ -622,11 +634,19 @@ "inputs = {\"input\": \"Find me Taylor Swift information on wiki?\"}\n", "outputs=app.invoke(inputs)" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9801bb4c", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "generativeaiexamples", "language": "python", "name": "python3" }, @@ -640,7 +660,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.6" + "version": "3.12.11" } }, "nbformat": 4,