Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
},
{
Expand Down Expand Up @@ -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"
]
},
{
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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"
]
},
{
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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"
},
Expand All @@ -640,7 +660,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.6"
"version": "3.12.11"
}
},
"nbformat": 4,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
},
{
Expand All @@ -75,9 +84,6 @@
"metadata": {},
"outputs": [],
"source": [
"import getpass\n",
"import os\n",
"\n",
"import getpass\n",
"import os\n",
"\n",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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"
]
Expand Down Expand Up @@ -294,27 +298,32 @@
" ## 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",
" \"Accept\": \"application/json\",\n",
" }\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",
" \n",
" 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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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"
},
Expand All @@ -764,7 +780,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.4"
"version": "3.12.11"
}
},
"nbformat": 4,
Expand Down