{ "cells": [ { "cell_type": "markdown", "id": "b947c78e", "metadata": {}, "source": [ "\n", "Pip install is the command you use to install Python packages with the help of a tool called Pip package manager.\n", "

Installing LangChain package\n", "
" ] }, { "cell_type": "code", "execution_count": 2, "id": "3d4a481b-60ed-4177-93ea-a234186eb787", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "d:\\2026\\July\\Agentic-RAG-with-LangGraph-and-Ollama\\.venv\\Scripts\\python.exe\n" ] } ], "source": [ "import sys\n", "print(sys.executable)" ] }, { "cell_type": "code", "execution_count": 3, "id": "6347bffa-cd6e-4fab-b987-e91c21d353ac", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "✅ Installed into: d:\\2026\\July\\Agentic-RAG-with-LangGraph-and-Ollama\\.venv\\Scripts\\python.exe\n" ] } ], "source": [ "import sys\n", "!{sys.executable} -m pip install -q \"huggingface_hub<1.0,>=0.34.0\"\n", "\n", "print(f\"✅ Installed into: {sys.executable}\")" ] }, { "cell_type": "code", "execution_count": 4, "id": "8236fd59", "metadata": {}, "outputs": [], "source": [ "#!pip install langchain" ] }, { "cell_type": "markdown", "id": "2f9c241d", "metadata": {}, "source": [ "## Let's use open-source LLM hosted on Hugging Face" ] }, { "cell_type": "code", "execution_count": 5, "id": "78a5b3f2", "metadata": {}, "outputs": [], "source": [ "# huggingface_hub pinned <1.0 — 1.x drops the InferenceClient(api_key=...) arg\n", "# that langchain_huggingface uses, and breaks transformers.\n", "#!pip install langchain-huggingface langchain \"huggingface_hub<1.0,>=0.34.0\"" ] }, { "cell_type": "markdown", "id": "1a2152cf", "metadata": {}, "source": [ "Imports the Python built-in module called \"os.\"\n", "This module provides a way to interact with the operating system, such as accessing environment variables, working with files and directories, executing shell commands, etc\n", "

\n", "The environ attribute is a dictionary-like object that contains the environment variables of the current operating system session\n", "

\n", "By accessing os.environ, you can retrieve and manipulate environment variables within your Python program. For example, you can retrieve the value of a specific environment variable using the syntax os.environ['VARIABLE_NAME'], where \"VARIABLE_NAME\" is the name of the environment variable you want to access.\n", "
" ] }, { "cell_type": "code", "execution_count": 6, "id": "6ea1b4d2", "metadata": {}, "outputs": [], "source": [ "import os\n", "# HUGGINGFACEHUB_API_TOKEN is loaded from .env (see load_dotenv); never hardcode it here" ] }, { "cell_type": "markdown", "id": "2dfc23e4", "metadata": {}, "source": [ "\n", "LangChain has built a Wrapper around HuggingFace APIs, using which we can get access to all the services HuggingFace provides.\n", "
\n", "The code snippet below imports a specific class called 'HuggingFaceEndpoint'(Wrapper around HuggingFace large language models) from 'langchain_huggingface' library.\n", "\n", "" ] }, { "cell_type": "code", "execution_count": 7, "id": "4c808209", "metadata": {}, "outputs": [], "source": [ "from langchain_huggingface import HuggingFaceEndpoint" ] }, { "cell_type": "markdown", "id": "e8cdfdbb", "metadata": {}, "source": [ "Here we are instantiating a language model object called HuggingFaceEndpoint, for our natural language processing tasks.\n", "

\n", "The parameter repo_id is provided\n", "" ] }, { "cell_type": "code", "execution_count": 8, "id": "8a492cf3-15f3-4bd2-b602-29b702c6e1b0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "huggingface_hub: 0.36.2\n", "langchain_huggingface: 1.2.2\n", "langchain: 1.3.11\n" ] } ], "source": [ "from importlib.metadata import version\n", "\n", "print(f\"huggingface_hub: {version('huggingface_hub')}\")\n", "print(f\"langchain_huggingface: {version('langchain-huggingface')}\")\n", "print(f\"langchain: {version('langchain')}\")" ] }, { "cell_type": "code", "execution_count": 9, "id": "a12912cc-10a9-4e13-8aae-be96267261c3", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The currency of India is the Indian Rupee (INR).\n", "\n", "To convert 1 Malaysian Ringgit (MYR) to Indian Rupee (INR), you can use a currency converter or check the current exchange rate from a reliable financial source, as exchange rates fluctuate frequently.\n", "\n", "As of the latest available data, 1 MYR is approximately 18.50 INR. However, for the most accurate and up-to-date conversion, please refer to a current financial news source or a reliable currency converter online.\n" ] } ], "source": [ "from dotenv import load_dotenv\n", "import os\n", "from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace\n", "\n", "# Load keys from .env file\n", "load_dotenv()\n", "\n", "# Notes on why this shape:\n", "# - task=\"conversational\": instruct/chat models on HF Inference are served\n", "# as chat-completions, NOT \"text-generation\".\n", "# - provider=\"novita\": route to a provider that actually hosts the model\n", "# and is live (featherless-ai was returning 503).\n", "# - ChatHuggingFace wraps the endpoint so it calls the chat API correctly.\n", "llm = HuggingFaceEndpoint(\n", " repo_id=\"Qwen/Qwen2.5-72B-Instruct\",\n", " task=\"conversational\",\n", " provider=\"novita\",\n", " huggingfacehub_api_token=os.getenv(\"HUGGINGFACEHUB_API_TOKEN\"),\n", ")\n", "chat = ChatHuggingFace(llm=llm)\n", "\n", "completion = chat.invoke(\"What is the currency of India and convert one MYR to INR?\")\n", "print(completion.content)" ] }, { "cell_type": "code", "execution_count": 10, "id": "5432711b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The currency of India is the Indian Rupee (INR). As an AI language model, I don't have real-time data access, so I can't provide you with the current exchange rate between MYR and INR. However, you can easily find this information on various financial websites or mobile apps that offer currency conversion services.\n" ] } ], "source": [ "from dotenv import load_dotenv\n", "import os\n", "from huggingface_hub import InferenceClient\n", "\n", "load_dotenv()\n", "\n", "client = InferenceClient(\n", " api_key=os.getenv(\"HUGGINGFACEHUB_API_TOKEN\"),\n", ")\n", "\n", "response = client.chat.completions.create(\n", " model=\"Qwen/Qwen2.5-1.5B-Instruct\", # ← free, works on free tier\n", " messages=[\n", " {\"role\": \"user\", \"content\": \"What is the currency of India and convert one MYR to INR today's rate?\"}\n", " ],\n", " max_tokens=200,\n", ")\n", "\n", "print(response.choices[0].message.content)" ] }, { "cell_type": "markdown", "id": "32c225b4", "metadata": {}, "source": [ "## Let's use Proprietary LLM from - OpenAI" ] }, { "cell_type": "markdown", "id": "2a0e925c", "metadata": {}, "source": [ "\n", "Installing Openai package, which includes the classes that we can use to communicate with Openai services\n", "" ] }, { "cell_type": "code", "execution_count": 11, "id": "5cb5a1a7", "metadata": {}, "outputs": [], "source": [ "# !pip install langchain-openai" ] }, { "cell_type": "markdown", "id": "f274e9c5", "metadata": {}, "source": [ "\n", "Imports the Python built-in module called \"os.\"\n", "
This module provides a way to interact with the operating system, such as accessing environment variables, working with files and directories, executing shell commands, etc\n", "

\n", "The environ attribute is a dictionary-like object that contains the environment variables of the current operating system session\n", "

\n", "By accessing os.environ, you can retrieve and manipulate environment variables within your Python program. For example, you can retrieve the value of a specific environment variable using the syntax os.environ['VARIABLE_NAME'], where \"VARIABLE_NAME\" is the name of the environment variable you want to access.\n", "" ] }, { "cell_type": "code", "execution_count": 12, "id": "a4d278e8", "metadata": {}, "outputs": [], "source": [ "import os\n", "# OPENAI_API_KEY is loaded from .env (see load_dotenv); never hardcode it here" ] }, { "cell_type": "markdown", "id": "5aef06cb", "metadata": {}, "source": [ "\n", "LangChain has built a Wrapper around OpenAI APIs, using which we can get access to all the services OpenAI provides.\n", "
\n", "The code snippet below imports a specific class called 'OpenAI'(Wrapper around OpenAI large language models) from 'langchain-openai' library.\n", "\n", "" ] }, { "cell_type": "code", "execution_count": 13, "id": "db64eaa7", "metadata": {}, "outputs": [], "source": [ "from langchain_openai import ChatOpenAI" ] }, { "cell_type": "markdown", "id": "6a9d124f", "metadata": {}, "source": [ "Here we are instantiating a language model object called OpenAI, for our natural language processing tasks.\n", "

\n", "The parameter model_name is provided with the value \"gpt-3.5-turbo-instruct\" which is a specific version or variant of a language model (examples - gpt-3.5-turbo, text-ada-001 and more).\n", "" ] }, { "cell_type": "code", "execution_count": 14, "id": "9a3be989", "metadata": {}, "outputs": [], "source": [ "llm = ChatOpenAI(model=\"gpt-4o\")" ] }, { "cell_type": "markdown", "id": "396c7904", "metadata": {}, "source": [ "\n", "Here language model is represented by the object \"llm,\" which is being utilized to generate a completion or response based on a specific query. \n", "

\n", "The query, stored in the \"our_query\" variable is bieng passed to the model through llm object.\n", "" ] }, { "cell_type": "code", "execution_count": 15, "id": "a2e267f6", "metadata": {}, "outputs": [], "source": [ "our_query = \"What is the currency of India and convert one MYR to INR today's rate?\"\n", "\n", "completion = llm.invoke(our_query)" ] }, { "cell_type": "code", "execution_count": 16, "id": "39f1b87e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The currency of India is the Indian Rupee (INR). Exchange rates fluctuate frequently, so to get the most accurate and up-to-date rate for converting Malaysian Ringgit (MYR) to Indian Rupees (INR), you should check a reliable financial news source, a bank, or a currency conversion website. As of my last update, I don't have access to real-time data, so it's best to look up the current exchange rate online or through a financial service.\n" ] } ], "source": [ "print(completion.content)" ] }, { "cell_type": "code", "execution_count": 17, "id": "d0d8a94b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The currency of India is the Indian Rupee (INR). Exchange rates fluctuate frequently, so to get the most accurate and up-to-date rate for converting Malaysian Ringgit (MYR) to Indian Rupees (INR), you should check a reliable financial news source, a bank, or a currency conversion website. As of my last update, I don't have access to real-time data, so it's best to look up the current exchange rate online or through a financial service.\n" ] } ], "source": [ "print(completion.content)" ] }, { "cell_type": "code", "execution_count": null, "id": "65a783d6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Collecting datasets\n", " Using cached datasets-5.0.0-py3-none-any.whl.metadata (23 kB)\n", "Requirement already satisfied: filelock in d:\\2026\\July\\Agentic-RAG-with-LangGraph-and-Ollama\\.venv\\Lib\\site-packages (from datasets) (3.29.6)\n", "Requirement already satisfied: numpy>=1.17 in d:\\2026\\July\\Agentic-RAG-with-LangGraph-and-Ollama\\.venv\\Lib\\site-packages (from datasets) (2.5.1)\n", "Requirement already satisfied: pyarrow>=21.0.0 in d:\\2026\\July\\Agentic-RAG-with-LangGraph-and-Ollama\\.venv\\Lib\\site-packages (from datasets) (24.0.0)\n", "Requirement already satisfied: dill<0.4.2,>=0.3.0 in d:\\2026\\July\\Agentic-RAG-with-LangGraph-and-Ollama\\.venv\\Lib\\site-packages (from datasets) (0.4.1)\n", "Requirement already satisfied: pandas in d:\\2026\\July\\Agentic-RAG-with-LangGraph-and-Ollama\\.venv\\Lib\\site-packages (from datasets) (2.3.3)\n", "Requirement already satisfied: requests>=2.32.2 in d:\\2026\\July\\Agentic-RAG-with-LangGraph-and-Ollama\\.venv\\Lib\\site-packages (from datasets) (2.34.2)\n", "Requirement already satisfied: httpx<1.0.0 in d:\\2026\\July\\Agentic-RAG-with-LangGraph-and-Ollama\\.venv\\Lib\\site-packages (from datasets) (0.28.1)\n", "Requirement already satisfied: tqdm>=4.66.3 in d:\\2026\\July\\Agentic-RAG-with-LangGraph-and-Ollama\\.venv\\Lib\\site-packages (from datasets) (4.68.4)\n", "Requirement already satisfied: xxhash in d:\\2026\\July\\Agentic-RAG-with-LangGraph-and-Ollama\\.venv\\Lib\\site-packages (from datasets) (3.8.1)\n", "Requirement already satisfied: multiprocess<0.70.20 in d:\\2026\\July\\Agentic-RAG-with-LangGraph-and-Ollama\\.venv\\Lib\\site-packages (from datasets) (0.70.19)\n", "Collecting fsspec<=2026.4.0,>=2023.1.0 (from fsspec[http]<=2026.4.0,>=2023.1.0->datasets)\n", " Using cached fsspec-2026.4.0-py3-none-any.whl.metadata (10 kB)\n", "Requirement already satisfied: huggingface-hub<2.0,>=0.25.0 in d:\\2026\\July\\Agentic-RAG-with-LangGraph-and-Ollama\\.venv\\Lib\\site-packages (from datasets) (0.36.2)\n", "Requirement already satisfied: packaging in d:\\2026\\July\\Agentic-RAG-with-LangGraph-and-Ollama\\.venv\\Lib\\site-packages (from datasets) (26.2)\n", "Requirement already satisfied: pyyaml>=5.1 in d:\\2026\\July\\Agentic-RAG-with-LangGraph-and-Ollama\\.venv\\Lib\\site-packages (from datasets) (6.0.3)\n", "Requirement already satisfied: aiohttp!=4.0.0a0,!=4.0.0a1 in d:\\2026\\July\\Agentic-RAG-with-LangGraph-and-Ollama\\.venv\\Lib\\site-packages (from fsspec[http]<=2026.4.0,>=2023.1.0->datasets) (3.14.1)\n", "Requirement already satisfied: anyio in d:\\2026\\July\\Agentic-RAG-with-LangGraph-and-Ollama\\.venv\\Lib\\site-packages (from httpx<1.0.0->datasets) (4.14.1)\n", "Requirement already satisfied: certifi in d:\\2026\\July\\Agentic-RAG-with-LangGraph-and-Ollama\\.venv\\Lib\\site-packages (from httpx<1.0.0->datasets) (2026.6.17)\n", "Requirement already satisfied: httpcore==1.* in d:\\2026\\July\\Agentic-RAG-with-LangGraph-and-Ollama\\.venv\\Lib\\site-packages (from httpx<1.0.0->datasets) (1.0.9)\n", "Requirement already satisfied: idna in d:\\2026\\July\\Agentic-RAG-with-LangGraph-and-Ollama\\.venv\\Lib\\site-packages (from httpx<1.0.0->datasets) (3.18)\n", "Requirement already satisfied: h11>=0.16 in d:\\2026\\July\\Agentic-RAG-with-LangGraph-and-Ollama\\.venv\\Lib\\site-packages (from httpcore==1.*->httpx<1.0.0->datasets) (0.16.0)\n", "Requirement already satisfied: typing-extensions>=3.7.4.3 in d:\\2026\\July\\Agentic-RAG-with-LangGraph-and-Ollama\\.venv\\Lib\\site-packages (from huggingface-hub<2.0,>=0.25.0->datasets) (4.16.0)\n", "Requirement already satisfied: aiohappyeyeballs>=2.5.0 in d:\\2026\\July\\Agentic-RAG-with-LangGraph-and-Ollama\\.venv\\Lib\\site-packages (from aiohttp!=4.0.0a0,!=4.0.0a1->fsspec[http]<=2026.4.0,>=2023.1.0->datasets) (2.7.1)\n", "Requirement already satisfied: aiosignal>=1.4.0 in d:\\2026\\July\\Agentic-RAG-with-LangGraph-and-Ollama\\.venv\\Lib\\site-packages (from aiohttp!=4.0.0a0,!=4.0.0a1->fsspec[http]<=2026.4.0,>=2023.1.0->datasets) (1.4.0)\n", "Requirement already satisfied: attrs>=17.3.0 in d:\\2026\\July\\Agentic-RAG-with-LangGraph-and-Ollama\\.venv\\Lib\\site-packages (from aiohttp!=4.0.0a0,!=4.0.0a1->fsspec[http]<=2026.4.0,>=2023.1.0->datasets) (26.1.0)\n", "Requirement already satisfied: frozenlist>=1.1.1 in d:\\2026\\July\\Agentic-RAG-with-LangGraph-and-Ollama\\.venv\\Lib\\site-packages (from aiohttp!=4.0.0a0,!=4.0.0a1->fsspec[http]<=2026.4.0,>=2023.1.0->datasets) (1.8.0)\n", "Requirement already satisfied: multidict<7.0,>=4.5 in d:\\2026\\July\\Agentic-RAG-with-LangGraph-and-Ollama\\.venv\\Lib\\site-packages (from aiohttp!=4.0.0a0,!=4.0.0a1->fsspec[http]<=2026.4.0,>=2023.1.0->datasets) (6.7.1)\n", "Requirement already satisfied: propcache>=0.2.0 in d:\\2026\\July\\Agentic-RAG-with-LangGraph-and-Ollama\\.venv\\Lib\\site-packages (from aiohttp!=4.0.0a0,!=4.0.0a1->fsspec[http]<=2026.4.0,>=2023.1.0->datasets) (0.5.2)\n", "Requirement already satisfied: yarl<2.0,>=1.17.0 in d:\\2026\\July\\Agentic-RAG-with-LangGraph-and-Ollama\\.venv\\Lib\\site-packages (from aiohttp!=4.0.0a0,!=4.0.0a1->fsspec[http]<=2026.4.0,>=2023.1.0->datasets) (1.24.2)\n", "Requirement already satisfied: charset_normalizer<4,>=2 in d:\\2026\\July\\Agentic-RAG-with-LangGraph-and-Ollama\\.venv\\Lib\\site-packages (from requests>=2.32.2->datasets) (3.4.9)\n", "Requirement already satisfied: urllib3<3,>=1.26 in d:\\2026\\July\\Agentic-RAG-with-LangGraph-and-Ollama\\.venv\\Lib\\site-packages (from requests>=2.32.2->datasets) (2.7.0)\n", "Requirement already satisfied: colorama in d:\\2026\\July\\Agentic-RAG-with-LangGraph-and-Ollama\\.venv\\Lib\\site-packages (from tqdm>=4.66.3->datasets) (0.4.6)\n", "Requirement already satisfied: python-dateutil>=2.8.2 in d:\\2026\\July\\Agentic-RAG-with-LangGraph-and-Ollama\\.venv\\Lib\\site-packages (from pandas->datasets) (2.9.0.post0)\n", "Requirement already satisfied: pytz>=2020.1 in d:\\2026\\July\\Agentic-RAG-with-LangGraph-and-Ollama\\.venv\\Lib\\site-packages (from pandas->datasets) (2026.2)\n", "Requirement already satisfied: tzdata>=2022.7 in d:\\2026\\July\\Agentic-RAG-with-LangGraph-and-Ollama\\.venv\\Lib\\site-packages (from pandas->datasets) (2026.2)\n", "Requirement already satisfied: six>=1.5 in d:\\2026\\July\\Agentic-RAG-with-LangGraph-and-Ollama\\.venv\\Lib\\site-packages (from python-dateutil>=2.8.2->pandas->datasets) (1.17.0)\n", "Using cached datasets-5.0.0-py3-none-any.whl (555 kB)\n", "Using cached fsspec-2026.4.0-py3-none-any.whl (203 kB)\n", "Installing collected packages: fsspec, datasets\n", "\n", " Attempting uninstall: fsspec\n", "\n", " Found existing installation: fsspec 2026.6.0\n", "\n", " Uninstalling fsspec-2026.6.0:\n", "\n", " Successfully uninstalled fsspec-2026.6.0\n", "\n", " ---------------------------------------- 0/2 [fsspec]\n", " ---------------------------------------- 0/2 [fsspec]\n", " ---------------------------------------- 0/2 [fsspec]\n", " -------------------- ------------------- 1/2 [datasets]\n", " -------------------- ------------------- 1/2 [datasets]\n", " -------------------- ------------------- 1/2 [datasets]\n", " -------------------- ------------------- 1/2 [datasets]\n", " -------------------- ------------------- 1/2 [datasets]\n", " -------------------- ------------------- 1/2 [datasets]\n", " -------------------- ------------------- 1/2 [datasets]\n", " -------------------- ------------------- 1/2 [datasets]\n", " -------------------- ------------------- 1/2 [datasets]\n", " ---------------------------------------- 2/2 [datasets]\n", "\n", "Successfully installed datasets-5.0.0 fsspec-2026.4.0\n" ] } ], "source": [ "#!pip install datasets" ] }, { "cell_type": "code", "execution_count": null, "id": "97e2464f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Dataset({\n", " features: ['text', 'label'],\n", " num_rows: 2\n", "})\n", "Value('string')\n", "Column([1, 1])\n" ] } ], "source": [ "import pandas as pd\n", "from datasets import Dataset\n", "\n", "# Define your raw structured data\n", "raw_data = {\n", " \"text\": [\"I love machine learning\", \"Hugging Face makes AI easy\"],\n", " \"label\": [1, 1]\n", "}\n", "\n", "# Convert to a Hugging Face Dataset object\n", "dataset = Dataset.from_pandas(pd.DataFrame(raw_data))\n", "print(dataset)\n", "print(dataset[\"text\"])\n", "print(dataset[\"label\"])" ] }, { "cell_type": "code", "execution_count": 28, "id": "4ea59a37", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Dataset({\n", " features: ['text', 'label'],\n", " num_rows: 10\n", "})\n" ] } ], "source": [ "data = {\n", " \"text\": [\n", " \"I love machine learning\",\n", " \"Hugging Face makes AI easy\",\n", " \"Natural language processing is interesting\",\n", " \"Deep learning models are powerful\",\n", " \"AI is transforming industries\",\n", " \"Data science is exciting\",\n", " \"Python is widely used in AI\",\n", " \"Models require good datasets\",\n", " \"Learning AI step by step is helpful\",\n", " \"Custom datasets improve performance\"\n", " ],\n", " \"label\": [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]\n", "}\n", "df = pd.DataFrame(data)\n", "dataset = Dataset.from_pandas(df)\n", "print(dataset)" ] }, { "cell_type": "code", "execution_count": 29, "id": "7c13f568", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "09acd1375a0d4365a564944c8bd94a23", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Saving the dataset (0/1 shards): 0%| | 0/10 [00:00