File size: 25,780 Bytes
fa76b17 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 | {
"cells": [
{
"cell_type": "markdown",
"id": "b947c78e",
"metadata": {},
"source": [
"<font color='green'>\n",
"Pip install is the command you use to install Python packages with the help of a tool called Pip package manager.\n",
"<br><br>Installing LangChain package\n",
"</font>"
]
},
{
"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": [
"<font color='green'>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",
"<br><br>\n",
"The environ attribute is a dictionary-like object that contains the environment variables of the current operating system session\n",
"<br><br>\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",
"</font>"
]
},
{
"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": [
"<font color='green'>\n",
"LangChain has built a Wrapper around HuggingFace APIs, using which we can get access to all the services HuggingFace provides.\n",
"<br>\n",
"The code snippet below imports a specific class called 'HuggingFaceEndpoint'(Wrapper around HuggingFace large language models) from 'langchain_huggingface' library.\n",
"\n",
"<font>"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "4c808209",
"metadata": {},
"outputs": [],
"source": [
"from langchain_huggingface import HuggingFaceEndpoint"
]
},
{
"cell_type": "markdown",
"id": "e8cdfdbb",
"metadata": {},
"source": [
"<font color='green'>Here we are instantiating a language model object called HuggingFaceEndpoint, for our natural language processing tasks.\n",
"<br><br>\n",
"The parameter repo_id is provided\n",
"<font>"
]
},
{
"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": [
"<font color='green'>\n",
"Installing Openai package, which includes the classes that we can use to communicate with Openai services\n",
"<font>"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "5cb5a1a7",
"metadata": {},
"outputs": [],
"source": [
"# !pip install langchain-openai"
]
},
{
"cell_type": "markdown",
"id": "f274e9c5",
"metadata": {},
"source": [
"<font color='green'>\n",
"Imports the Python built-in module called \"os.\"\n",
"<br>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",
"<br><br>\n",
"The environ attribute is a dictionary-like object that contains the environment variables of the current operating system session\n",
"<br><br>\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",
"<font>"
]
},
{
"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": [
"<font color='green'>\n",
"LangChain has built a Wrapper around OpenAI APIs, using which we can get access to all the services OpenAI provides.\n",
"<br>\n",
"The code snippet below imports a specific class called 'OpenAI'(Wrapper around OpenAI large language models) from 'langchain-openai' library.\n",
"\n",
"<font>"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "db64eaa7",
"metadata": {},
"outputs": [],
"source": [
"from langchain_openai import ChatOpenAI"
]
},
{
"cell_type": "markdown",
"id": "6a9d124f",
"metadata": {},
"source": [
"<font color='green'>Here we are instantiating a language model object called OpenAI, for our natural language processing tasks.\n",
"<br><br>\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",
"<font>"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "9a3be989",
"metadata": {},
"outputs": [],
"source": [
"llm = ChatOpenAI(model=\"gpt-4o\")"
]
},
{
"cell_type": "markdown",
"id": "396c7904",
"metadata": {},
"source": [
"<font color='green'>\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",
"<br><br>\n",
"The query, stored in the \"our_query\" variable is bieng passed to the model through llm object.\n",
"<font>"
]
},
{
"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<?, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"dataset.save_to_disk(\"my_dataset\")"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "46c552ea",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Status: 200\n",
"URL: https://api.nhtsa.gov/complaints/complaintsByVehicle?make=Tesla&model=Model+3&modelYear=2020\n",
"\n",
"Total complaints found: 435\n",
"Message: Results returned successfully\n",
"\n",
"--- First Complaint ---\n",
"ODI Number: 11751847\n",
"Component: UNKNOWN OR OTHER,FORWARD COLLISION AVOIDANCE\n",
"Date: 07/16/2026\n",
"Description: ...\n"
]
}
],
"source": [
"import requests\n",
"\n",
"url = \"https://api.nhtsa.gov/complaints/complaintsByVehicle\"\n",
"params = {\"make\": \"Tesla\", \"model\": \"Model 3\", \"modelYear\": \"2020\"}\n",
"\n",
"r = requests.get(url, params=params)\n",
"\n",
"print(\"Status:\", r.status_code)\n",
"print(\"URL:\", r.url)\n",
"\n",
"# Parse JSON properly\n",
"data = r.json()\n",
"\n",
"print(f\"\\nTotal complaints found: {data['count']}\")\n",
"print(f\"Message: {data['message']}\")\n",
"\n",
"# Show first complaint\n",
"if data['results']:\n",
" first = data['results'][0]\n",
" print(\"\\n--- First Complaint ---\")\n",
" print(f\"ODI Number: {first.get('odiNumber')}\")\n",
" print(f\"Component: {first.get('components')}\")\n",
" print(f\"Date: {first.get('dateOfIncident')}\")\n",
" print(f\"Description: {first.get('description', '')[:200]}...\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv (3.12.8.final.0)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
} |