diff --git a/.ipynb_checkpoints/lab-chains-in-langchain-checkpoint.ipynb b/.ipynb_checkpoints/lab-chains-in-langchain-checkpoint.ipynb new file mode 100644 index 0000000..0407d73 --- /dev/null +++ b/.ipynb_checkpoints/lab-chains-in-langchain-checkpoint.ipynb @@ -0,0 +1,1161 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "52824b89-532a-4e54-87e9-1410813cd39e", + "metadata": {}, + "source": [ + "# Lab | Chains in LangChain\n", + "\n", + "## Outline\n", + "\n", + "* LLMChain\n", + "* Sequential Chains\n", + " * SimpleSequentialChain\n", + " * SequentialChain\n", + "* Router Chain" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "541eb2f1", + "metadata": {}, + "outputs": [], + "source": [ + "import warnings\n", + "warnings.filterwarnings('ignore')" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "b7ed03ed-1322-49e3-b2a2-33e94fb592ef", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "import os\n", + "\n", + "from dotenv import load_dotenv, find_dotenv\n", + "_ = load_dotenv(find_dotenv())\n", + "\n", + "OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')\n", + "HUGGINGFACEHUB_API_TOKEN = os.getenv('HF_TOKEN')" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "974acf8e-8f88-42de-88f8-40a82cb58e8b", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "import pandas as pd\n", + "df = pd.read_csv('data/Data.csv')" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "b7a09c35", + "metadata": { + "tags": [] + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
ProductReview
0Queen Size Sheet SetI ordered a king size set. My only criticism w...
1Waterproof Phone PouchI loved the waterproof sac, although the openi...
2Luxury Air MattressThis mattress had a small hole in the top of i...
3Pillows InsertThis is the best throw pillow fillers on Amazo...
4Milk Frother HandheldI loved this product. But they only seem to la...
\n", + "
" + ], + "text/plain": [ + " Product Review\n", + "0 Queen Size Sheet Set I ordered a king size set. My only criticism w...\n", + "1 Waterproof Phone Pouch I loved the waterproof sac, although the openi...\n", + "2 Luxury Air Mattress This mattress had a small hole in the top of i...\n", + "3 Pillows Insert This is the best throw pillow fillers on Amazo...\n", + "4 Milk Frother Handheld I loved this product. But they only seem to la..." + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.head()" + ] + }, + { + "cell_type": "markdown", + "id": "b940ce7c", + "metadata": {}, + "source": [ + "## LLMChain" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "20b6b2a4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Requirement already satisfied: langchain in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (1.3.14)\n", + "Requirement already satisfied: langchain-core in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (1.5.2)\n", + "Requirement already satisfied: langchain-openai in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (1.4.1)\n", + "Requirement already satisfied: langchain-community in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (0.4.1)\n", + "Requirement already satisfied: langchain-classic in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (1.0.7)\n", + "Requirement already satisfied: langgraph<1.3.0,>=1.2.5 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langchain) (1.2.9)\n", + "Requirement already satisfied: pydantic<3.0.0,>=2.7.4 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langchain) (2.13.4)\n", + "Requirement already satisfied: jsonpatch<2.0.0,>=1.33.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langchain-core) (1.33)\n", + "Requirement already satisfied: langchain-protocol>=0.0.17 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langchain-core) (0.0.18)\n", + "Requirement already satisfied: langsmith<1.0.0,>=0.3.45 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langchain-core) (0.8.5)\n", + "Requirement already satisfied: packaging>=23.2.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langchain-core) (24.2)\n", + "Requirement already satisfied: pyyaml<7.0.0,>=5.3.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langchain-core) (6.0.2)\n", + "Requirement already satisfied: tenacity!=8.4.0,<10.0.0,>=8.1.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langchain-core) (9.1.4)\n", + "Requirement already satisfied: typing-extensions<5.0.0,>=4.7.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langchain-core) (4.15.0)\n", + "Requirement already satisfied: uuid-utils<1.0,>=0.12.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langchain-core) (0.15.0)\n", + "Requirement already satisfied: openai<3.0.0,>=2.45.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langchain-openai) (2.46.0)\n", + "Requirement already satisfied: tiktoken<1.0.0,>=0.7.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langchain-openai) (0.13.0)\n", + "Requirement already satisfied: SQLAlchemy<3.0.0,>=1.4.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langchain-community) (2.0.51)\n", + "Requirement already satisfied: requests<3.0.0,>=2.32.5 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langchain-community) (2.34.2)\n", + "Requirement already satisfied: aiohttp<4.0.0,>=3.8.3 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langchain-community) (3.13.5)\n", + "Requirement already satisfied: dataclasses-json<0.7.0,>=0.6.7 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langchain-community) (0.6.7)\n", + "Requirement already satisfied: pydantic-settings<3.0.0,>=2.10.1 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langchain-community) (2.14.1)\n", + "Requirement already satisfied: httpx-sse<1.0.0,>=0.4.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langchain-community) (0.4.3)\n", + "Requirement already satisfied: numpy>=2.1.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langchain-community) (2.5.1)\n", + "Requirement already satisfied: langchain-text-splitters<2.0.0,>=1.1.2 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langchain-classic) (1.1.2)\n", + "Requirement already satisfied: aiohappyeyeballs>=2.5.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (2.6.1)\n", + "Requirement already satisfied: aiosignal>=1.4.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (1.4.0)\n", + "Requirement already satisfied: attrs>=17.3.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (25.1.0)\n", + "Requirement already satisfied: frozenlist>=1.1.1 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (1.8.0)\n", + "Requirement already satisfied: multidict<7.0,>=4.5 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (6.7.1)\n", + "Requirement already satisfied: propcache>=0.2.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (0.5.2)\n", + "Requirement already satisfied: yarl<2.0,>=1.17.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (1.23.0)\n", + "Requirement already satisfied: marshmallow<4.0.0,>=3.18.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from dataclasses-json<0.7.0,>=0.6.7->langchain-community) (3.26.2)\n", + "Requirement already satisfied: typing-inspect<1,>=0.4.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from dataclasses-json<0.7.0,>=0.6.7->langchain-community) (0.9.0)\n", + "Requirement already satisfied: jsonpointer>=1.9 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from jsonpatch<2.0.0,>=1.33.0->langchain-core) (3.0.0)\n", + "Requirement already satisfied: langgraph-checkpoint<5.0.0,>=4.1.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langgraph<1.3.0,>=1.2.5->langchain) (4.1.1)\n", + "Requirement already satisfied: langgraph-prebuilt<1.2.0,>=1.1.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langgraph<1.3.0,>=1.2.5->langchain) (1.1.0)\n", + "Requirement already satisfied: langgraph-sdk<0.5.0,>=0.4.2 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langgraph<1.3.0,>=1.2.5->langchain) (0.4.2)\n", + "Requirement already satisfied: xxhash>=3.5.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langgraph<1.3.0,>=1.2.5->langchain) (3.7.0)\n", + "Requirement already satisfied: httpx<1,>=0.23.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langsmith<1.0.0,>=0.3.45->langchain-core) (0.28.1)\n", + "Requirement already satisfied: orjson>=3.9.14 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langsmith<1.0.0,>=0.3.45->langchain-core) (3.11.9)\n", + "Requirement already satisfied: requests-toolbelt>=1.0.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langsmith<1.0.0,>=0.3.45->langchain-core) (1.0.0)\n", + "Requirement already satisfied: zstandard>=0.23.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langsmith<1.0.0,>=0.3.45->langchain-core) (0.25.0)\n", + "Requirement already satisfied: anyio<5,>=3.5.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from openai<3.0.0,>=2.45.0->langchain-openai) (4.8.0)\n", + "Requirement already satisfied: distro<2,>=1.7.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from openai<3.0.0,>=2.45.0->langchain-openai) (1.9.0)\n", + "Requirement already satisfied: jiter<1,>=0.10.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from openai<3.0.0,>=2.45.0->langchain-openai) (0.14.0)\n", + "Requirement already satisfied: sniffio in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from openai<3.0.0,>=2.45.0->langchain-openai) (1.3.1)\n", + "Requirement already satisfied: tqdm>4 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from openai<3.0.0,>=2.45.0->langchain-openai) (4.67.3)\n", + "Requirement already satisfied: annotated-types>=0.6.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from pydantic<3.0.0,>=2.7.4->langchain) (0.7.0)\n", + "Requirement already satisfied: pydantic-core==2.46.4 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from pydantic<3.0.0,>=2.7.4->langchain) (2.46.4)\n", + "Requirement already satisfied: typing-inspection>=0.4.2 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from pydantic<3.0.0,>=2.7.4->langchain) (0.4.2)\n", + "Requirement already satisfied: python-dotenv>=0.21.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from pydantic-settings<3.0.0,>=2.10.1->langchain-community) (1.2.2)\n", + "Requirement already satisfied: charset_normalizer<4,>=2 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from requests<3.0.0,>=2.32.5->langchain-community) (3.4.1)\n", + "Requirement already satisfied: idna<4,>=2.5 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from requests<3.0.0,>=2.32.5->langchain-community) (3.10)\n", + "Requirement already satisfied: urllib3<3,>=1.26 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from requests<3.0.0,>=2.32.5->langchain-community) (2.3.0)\n", + "Requirement already satisfied: certifi>=2023.5.7 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from requests<3.0.0,>=2.32.5->langchain-community) (2025.1.31)\n", + "Requirement already satisfied: greenlet>=1 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from SQLAlchemy<3.0.0,>=1.4.0->langchain-community) (3.5.0)\n", + "Requirement already satisfied: regex in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from tiktoken<1.0.0,>=0.7.0->langchain-openai) (2026.5.9)\n", + "Requirement already satisfied: httpcore==1.* in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from httpx<1,>=0.23.0->langsmith<1.0.0,>=0.3.45->langchain-core) (1.0.7)\n", + "Requirement already satisfied: h11<0.15,>=0.13 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from httpcore==1.*->httpx<1,>=0.23.0->langsmith<1.0.0,>=0.3.45->langchain-core) (0.14.0)\n", + "Requirement already satisfied: ormsgpack>=1.12.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langgraph-checkpoint<5.0.0,>=4.1.0->langgraph<1.3.0,>=1.2.5->langchain) (1.12.2)\n", + "Requirement already satisfied: websockets<16,>=14 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langgraph-sdk<0.5.0,>=0.4.2->langgraph<1.3.0,>=1.2.5->langchain) (14.2)\n", + "Requirement already satisfied: colorama in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from tqdm>4->openai<3.0.0,>=2.45.0->langchain-openai) (0.4.6)\n", + "Requirement already satisfied: mypy-extensions>=0.3.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from typing-inspect<1,>=0.4.0->dataclasses-json<0.7.0,>=0.6.7->langchain-community) (1.0.0)\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "\n", + "[notice] A new release of pip is available: 24.3.1 -> 26.1.2\n", + "[notice] To update, run: python.exe -m pip install --upgrade pip\n" + ] + } + ], + "source": [ + "!pip install langchain langchain-core langchain-openai langchain-community langchain-classic" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "af96fedc", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Name: langchain\n", + "Version: 1.3.14\n", + "Summary: Building applications with LLMs through composability\n", + "Home-page: https://docs.langchain.com/\n", + "Author: \n", + "Author-email: \n", + "License: MIT\n", + "Location: C:\\Users\\sol_v\\AppData\\Local\\Programs\\Python\\Python313\\Lib\\site-packages\n", + "Requires: langchain-core, langgraph, pydantic\n", + "Required-by: \n", + "---\n", + "Name: langchain-core\n", + "Version: 1.5.2\n", + "Summary: Building applications with LLMs through composability\n", + "Home-page: https://docs.langchain.com/\n", + "Author: \n", + "Author-email: \n", + "License: MIT\n", + "Location: C:\\Users\\sol_v\\AppData\\Local\\Programs\\Python\\Python313\\Lib\\site-packages\n", + "Requires: jsonpatch, langchain-protocol, langsmith, packaging, pydantic, pyyaml, tenacity, typing-extensions, uuid-utils\n", + "Required-by: langchain, langchain-chroma, langchain-classic, langchain-community, langchain-openai, langchain-text-splitters, langgraph, langgraph-checkpoint, langgraph-prebuilt, langgraph-sdk\n", + "---\n", + "Name: langchain-openai\n", + "Version: 1.4.1\n", + "Summary: An integration package connecting OpenAI and LangChain\n", + "Home-page: https://docs.langchain.com/oss/python/integrations/providers/openai\n", + "Author: \n", + "Author-email: \n", + "License: MIT\n", + "Location: C:\\Users\\sol_v\\AppData\\Local\\Programs\\Python\\Python313\\Lib\\site-packages\n", + "Requires: langchain-core, openai, tiktoken\n", + "Required-by: \n", + "---\n", + "Name: langchain-community\n", + "Version: 0.4.1\n", + "Summary: Community contributed LangChain integrations.\n", + "Home-page: \n", + "Author: \n", + "Author-email: \n", + "License: MIT\n", + "Location: C:\\Users\\sol_v\\AppData\\Local\\Programs\\Python\\Python313\\Lib\\site-packages\n", + "Requires: aiohttp, dataclasses-json, httpx-sse, langchain-classic, langchain-core, langsmith, numpy, pydantic-settings, PyYAML, requests, SQLAlchemy, tenacity\n", + "Required-by: \n" + ] + } + ], + "source": [ + "!pip show langchain langchain-core langchain-openai langchain-community" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "e92dff22", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "from langchain_openai import ChatOpenAI # ✅ 1.0.3\n", + "from langchain_core.prompts import ChatPromptTemplate # ✅ From langchain-core 1.0.5\n", + "from langchain_classic.chains import LLMChain # ✅ Legacy (install if missing)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "943237a7", + "metadata": {}, + "outputs": [], + "source": [ + "#Replace None by your own value and justify\n", + "llm = ChatOpenAI(temperature=None)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "9b023dfd", + "metadata": {}, + "outputs": [], + "source": [ + "prompt = ChatPromptTemplate.from_template( \n", + " \"Write a short, engaging product description for a {product}.\"#Write a query that would take a variable to describe any product\n", + " \n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "d7abc20b", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\sol_v\\AppData\\Local\\Temp\\ipykernel_2596\\1305865249.py:1: LangChainDeprecationWarning: The class `LLMChain` was deprecated in LangChain 0.1.17 and will be removed in 2.0.0. Use `RunnableSequence, e.g., `prompt | llm`` instead.\n", + " chain = LLMChain(llm=llm, prompt=prompt)\n" + ] + } + ], + "source": [ + "chain = LLMChain(llm=llm, prompt=prompt)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "ad44d1fb", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\sol_v\\AppData\\Local\\Temp\\ipykernel_2596\\1723211282.py:2: LangChainDeprecationWarning: The method `Chain.run` was deprecated in langchain-classic 0.1.0 and will be removed in 2.0.0. Use `invoke` instead.\n", + " chain.run(product)\n" + ] + }, + { + "data": { + "text/plain": [ + "\"Transform your bedroom into a luxurious oasis with our Queen Size Sheet Set. Made from ultra-soft, breathable microfiber material, these sheets are designed to provide ultimate comfort and a peaceful night's sleep. The deep pockets ensure a secure fit on your mattress, while the double-brushed finish offers a silky-smooth feel against your skin. Available in a variety of stunning colors, this sheet set is the perfect combination of quality, style, and affordability. Upgrade your bedding and indulge in a restful night's sleep with our Queen Size Sheet Set.\"" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "product = \"Queen Size Sheet Set\" #Select a product type to be describe\n", + "chain.run(product)" + ] + }, + { + "cell_type": "markdown", + "id": "69b03469", + "metadata": {}, + "source": [ + "## SimpleSequentialChain" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "febee243", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain_classic.chains import SimpleSequentialChain " + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "91e6bbe0", + "metadata": {}, + "outputs": [], + "source": [ + "llm = ChatOpenAI(temperature=0.9)\n", + "\n", + "# prompt template 1\n", + "first_prompt = ChatPromptTemplate.from_template(\n", + " \"What is the best name to describe a company that makes {product}?\" #Repeat the initial query or create a new query that would feed into the second prompt\n", + ")\n", + "\n", + "# Chain 1\n", + "chain_one = LLMChain(llm=llm, prompt=first_prompt)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "3f5d5b76", + "metadata": {}, + "outputs": [], + "source": [ + "# prompt template 2\n", + "second_prompt = ChatPromptTemplate.from_template(\n", + " \"Write a 20 word description for the following company: {company_name}\" #Write the second prompt query that takes an input variable whose input will come from the previous prompt\"\n", + ")\n", + "# chain 2\n", + "chain_two = LLMChain(llm=llm, prompt=second_prompt)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "6c1eb2c4", + "metadata": {}, + "outputs": [], + "source": [ + "overall_simple_chain = SimpleSequentialChain(chains=[chain_one, chain_two],\n", + " verbose=True\n", + " )" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "78458efe", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\u001b[1m> Entering new SimpleSequentialChain chain...\u001b[0m\n", + "\u001b[36;1m\u001b[1;3m\"Royalty Linens\"\u001b[0m\n", + "\u001b[33;1m\u001b[1;3mElevate your home décor with luxurious linens fit for royalty, offering exquisite bedding and bath essentials in elegant designs.\u001b[0m\n", + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n" + ] + }, + { + "data": { + "text/plain": [ + "'Elevate your home décor with luxurious linens fit for royalty, offering exquisite bedding and bath essentials in elegant designs.'" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "overall_simple_chain.run(product)" + ] + }, + { + "cell_type": "markdown", + "id": "0dd59bda-9d02-44e7-b3d6-2bec61b99d8f", + "metadata": {}, + "source": [ + "**Repeat the above twice for different products**" + ] + }, + { + "cell_type": "markdown", + "id": "7b5ce18c", + "metadata": {}, + "source": [ + "## SequentialChain" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "4c129ef6", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain_classic.chains import SequentialChain " + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "016187ac", + "metadata": {}, + "outputs": [], + "source": [ + "llm = ChatOpenAI(temperature=0.9)\n", + "\n", + "\n", + "first_prompt = ChatPromptTemplate.from_template(\n", + " \"Translate the following review to english:\" \"\\n\\n{Review} \" #This prompt should translate a review\n", + ")\n", + "\n", + "chain_one = LLMChain(llm=llm, prompt=first_prompt, \n", + " output_key=\"English_Review\" #Give a name to your output\n", + " )" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "0fb0730e", + "metadata": {}, + "outputs": [], + "source": [ + "second_prompt = ChatPromptTemplate.from_template(\n", + " \"Can you summarize the following review in 1 sentence:\"\n", + " \"\\n\\n{English_Review}\" #Write a prompt to summarize a review\n", + ")\n", + "\n", + "chain_two = LLMChain(llm=llm, prompt=second_prompt, \n", + " output_key=\"summary\" #give a name to this output\n", + " )" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "6accf92d", + "metadata": {}, + "outputs": [], + "source": [ + "# prompt template 3: translate to english or other language\n", + "third_prompt = ChatPromptTemplate.from_template(\n", + " \"What language is the following review written in?\"\n", + " \"\\n\\n{Review}\"\n", + ")\n", + "# chain 3: input= Review and output= language\n", + "chain_three = LLMChain(llm=llm, prompt=third_prompt,\n", + " output_key=\"language\"\n", + " )\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "c7a46121", + "metadata": {}, + "outputs": [], + "source": [ + "# prompt template 4: follow up message that take as inputs the two previous prompts' variables\n", + "fourth_prompt = ChatPromptTemplate.from_template(\n", + " \"Write a follow up response to the following summary\"\n", + " \" in the specified language:\"\n", + " \"\\n\\nSummary: {summary}\\n\\nLanguage: {language}\"\n", + ")\n", + "chain_four = LLMChain(llm=llm, prompt=fourth_prompt,\n", + " output_key=\"followup_message\"\n", + " )\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "89603117", + "metadata": {}, + "outputs": [], + "source": [ + "# overall_chain: input= Review \n", + "# and output= English_Review,summary, followup_message\n", + "overall_chain = SequentialChain(\n", + " chains=[chain_one, chain_two, chain_three, chain_four],\n", + " input_variables=[\"Review\"],\n", + " output_variables=[\"English_Review\", \"summary\", \"followup_message\"],\n", + " verbose=True\n", + ")\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "51b04f45", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\sol_v\\AppData\\Local\\Temp\\ipykernel_2596\\1992003631.py:2: LangChainDeprecationWarning: The method `Chain.__call__` was deprecated in langchain-classic 0.1.0 and will be removed in 2.0.0. Use `invoke` instead.\n", + " overall_chain(review)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\u001b[1m> Entering new SequentialChain chain...\u001b[0m\n", + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n" + ] + }, + { + "data": { + "text/plain": [ + "{'Review': \"Je trouve le goût médiocre. La mousse ne tient pas, c'est bizarre. J'achète les mêmes dans le commerce et le goût est bien meilleur...\\r\\nVieux lot ou contrefaçon !?\",\n", + " 'English_Review': \"I find the taste mediocre. The foam doesn't hold, it's strange. I buy the same ones in stores and the taste is much better... Old batch or counterfeit!?\",\n", + " 'summary': 'The reviewer is disappointed with the taste and quality of the product, suspecting that it may be an old batch or counterfeit.',\n", + " 'followup_message': \"Je suis désolé d'apprendre que vous avez été déçu par le goût et la qualité du produit. Il est possible qu'il s'agisse en effet d'un lot expiré ou contrefait. Nous prenons vos commentaires très au sérieux et nous allons enquêter sur ce problème pour nous assurer que nos clients reçoivent toujours des produits frais et authentiques. Merci de nous avoir informés de cette situation et nous nous excusons pour tout désagrément que cela a pu vous causer.\"}" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "review = df.Review[5]\n", + "overall_chain(review)" + ] + }, + { + "cell_type": "markdown", + "id": "3187cf07-458a-4226-bec7-3dec7ee47af2", + "metadata": {}, + "source": [ + "**Repeat the above twice for different products or reviews**" + ] + }, + { + "cell_type": "markdown", + "id": "3041ea4c", + "metadata": {}, + "source": [ + "## Router Chain" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "id": "ade83f4f", + "metadata": {}, + "outputs": [], + "source": [ + "physics_template = \"\"\"You are a very smart physics professor. \\\n", + "You are great at answering questions about physics in a concise\\\n", + "and easy to understand manner. \\\n", + "When you don't know the answer to a question you admit\\\n", + "that you don't know.\n", + "\n", + "Here is a question:\n", + "{input}\"\"\"\n", + "\n", + "\n", + "math_template = \"\"\"You are a very good mathematician. \\\n", + "You are great at answering math questions. \\\n", + "You are so good because you are able to break down \\\n", + "hard problems into their component parts, \n", + "answer the component parts, and then put them together\\\n", + "to answer the broader question.\n", + "\n", + "Here is a question:\n", + "{input}\"\"\"\n", + "\n", + "history_template = \"\"\"You are a very good historian. \\\n", + "You have an excellent knowledge of and understanding of people,\\\n", + "events and contexts from a range of historical periods. \\\n", + "You have the ability to think, reflect, debate, discuss and \\\n", + "evaluate the past. You have a respect for historical evidence\\\n", + "and the ability to make use of it to support your explanations \\\n", + "and judgements.\n", + "\n", + "Here is a question:\n", + "{input}\"\"\"\n", + "\n", + "\n", + "computerscience_template = \"\"\" You are a successful computer scientist.\\\n", + "You have a passion for creativity, collaboration,\\\n", + "forward-thinking, confidence, strong problem-solving capabilities,\\\n", + "understanding of theories and algorithms, and excellent communication \\\n", + "skills. You are great at answering coding questions. \\\n", + "You are so good because you know how to solve a problem by \\\n", + "describing the solution in imperative steps \\\n", + "that a machine can easily interpret and you know how to \\\n", + "choose a solution that has a good balance between \\\n", + "time complexity and space complexity. \n", + "\n", + "Here is a question:\n", + "{input}\"\"\"\n", + "\n", + "biology_template = \"\"\"You are an excellent biologist. \\\n", + "You have a deep understanding of living organisms, \\\n", + "from the molecular and cellular level to entire ecosystems. \\\n", + "You are skilled at observing patterns in nature, analyzing biological data, \\\n", + "and explaining complex processes like evolution, genetics, physiology, and ecology. \\\n", + "You can clearly communicate how life functions and adapts, \\\n", + "and you make connections between different biological concepts \\\n", + "to answer challenging questions.\n", + "\n", + "Here is a question:\n", + "{input}\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "id": "5f590e9f", + "metadata": {}, + "outputs": [], + "source": [ + "prompt_infos = [\n", + " {\n", + " \"name\": \"physics\", \n", + " \"description\": \"Good for answering questions about physics\", \n", + " \"prompt_template\": physics_template\n", + " },\n", + " {\n", + " \"name\": \"math\", \n", + " \"description\": \"Good for answering math questions\", \n", + " \"prompt_template\": math_template\n", + " },\n", + " {\n", + " \"name\": \"History\", \n", + " \"description\": \"Good for answering history questions\", \n", + " \"prompt_template\": history_template\n", + " },\n", + " {\n", + " \"name\": \"computer science\", \n", + " \"description\": \"Good for answering computer science questions\", \n", + " \"prompt_template\": computerscience_template\n", + " },\n", + " {\n", + " \"name\": \"biology\",\n", + " \"description\": \"Good for answering biology questions\",\n", + " \"prompt_template\": biology_template\n", + " }\n", + "]" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "b5cfdd35", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain_classic.chains.router import MultiPromptChain\n", + "from langchain_classic.chains.router.llm_router import LLMRouterChain, RouterOutputParser\n", + "from langchain_core.prompts import PromptTemplate # Updated path for prompts (core package)" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "id": "f3f50bcc", + "metadata": {}, + "outputs": [], + "source": [ + "llm = ChatOpenAI(temperature=0)" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "id": "8eefec24", + "metadata": {}, + "outputs": [], + "source": [ + "destination_chains = {}\n", + "for p_info in prompt_infos: #converts a list of template descriptions into a dictionary of executable chains the router can look up by name.\n", + " name = p_info[\"name\"]\n", + " prompt_template = p_info[\"prompt_template\"]\n", + " prompt = ChatPromptTemplate.from_template(template=prompt_template)\n", + " chain = LLMChain(llm=llm, prompt=prompt)\n", + " destination_chains[name] = chain \n", + " \n", + "destinations = [f\"{p['name']}: {p['description']}\" for p in prompt_infos] #assembles the human-readable menu of routing options that the router LLM will read to decide where a question should go.\n", + "destinations_str = \"\\n\".join(destinations) #turns the list of options into one block of text that can be dropped straight into the router's prompt." + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "id": "9f98018a", + "metadata": {}, + "outputs": [], + "source": [ + "default_prompt = ChatPromptTemplate.from_template(\"{input}\") \n", + "default_chain = LLMChain(llm=llm, prompt=default_prompt) #the fallback chain that handles questions the router can't match to any subject." + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "id": "11b2e2ba", + "metadata": {}, + "outputs": [], + "source": [ + "MULTI_PROMPT_ROUTER_TEMPLATE = \"\"\"Given a raw text input to a \\\n", + "language model select the model prompt best suited for the input. \\\n", + "You will be given the names of the available prompts and a \\\n", + "description of what the prompt is best suited for. \\\n", + "You may also revise the original input if you think that revising\\\n", + "it will ultimately lead to a better response from the language model.\n", + "\n", + "<< FORMATTING >>\n", + "Return a markdown code snippet with a JSON object formatted to look like:\n", + "```json\n", + "{{{{\n", + " \"destination\": string \\ name of the prompt to use or \"DEFAULT\"\n", + " \"next_inputs\": string \\ a potentially modified version of the original input\n", + "}}}}\n", + "```\n", + "\n", + "REMEMBER: \"destination\" MUST be one of the candidate prompt \\\n", + "names specified below OR it can be \"DEFAULT\" if the input is not\\\n", + "well suited for any of the candidate prompts.\n", + "REMEMBER: \"next_inputs\" can just be the original input \\\n", + "if you don't think any modifications are needed.\n", + "\n", + "<< CANDIDATE PROMPTS >>\n", + "{destinations}\n", + "\n", + "<< INPUT >>\n", + "{{input}}\n", + "\n", + "<< OUTPUT (remember to include the ```json)>>\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "id": "1387109d", + "metadata": {}, + "outputs": [], + "source": [ + "router_template = MULTI_PROMPT_ROUTER_TEMPLATE.format(\n", + " destinations=destinations_str\n", + ")\n", + "router_prompt = PromptTemplate(\n", + " template=router_template,\n", + " input_variables=[\"input\"],\n", + " output_parser=RouterOutputParser(),\n", + ")\n", + "\n", + "router_chain = LLMRouterChain.from_llm(llm, router_prompt)" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "id": "2fb7d560", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\sol_v\\AppData\\Local\\Temp\\ipykernel_2596\\3038952769.py:1: LangChainDeprecationWarning: The class `MultiPromptChain` was deprecated in LangChain 0.2.12 and will be removed in 2.0.0. Use `langchain.agents.create_agent` instead. Build routing logic with `create_agent` (e.g. with subagents or prompt-selection middleware). See https://docs.langchain.com/oss/python/langchain/agents\n", + " chain = MultiPromptChain(router_chain=router_chain,\n" + ] + } + ], + "source": [ + "chain = MultiPromptChain(router_chain=router_chain, \n", + " destination_chains=destination_chains, \n", + " default_chain=default_chain, verbose=True\n", + " )" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "id": "d86b2131", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\u001b[1m> Entering new MultiPromptChain chain...\u001b[0m\n", + "physics: {'input': 'What is black body radiation?'}\n", + "\u001b[1m> Finished chain.\u001b[0m\n" + ] + }, + { + "data": { + "text/plain": [ + "\"Black body radiation is the electromagnetic radiation emitted by a perfect absorber of radiation, known as a black body. A black body absorbs all radiation that falls on it and emits radiation across the entire electromagnetic spectrum. The spectrum of black body radiation is continuous and depends only on the temperature of the black body. This phenomenon is described by Planck's law, which states that the intensity of radiation emitted by a black body at a given wavelength is proportional to the temperature of the body and the wavelength to the power of -5.\"" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "chain.run(\"What is black body radiation?\")" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "id": "3b717379", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\u001b[1m> Entering new MultiPromptChain chain...\u001b[0m\n", + "math: {'input': 'what is 2 + 2'}\n", + "\u001b[1m> Finished chain.\u001b[0m\n" + ] + }, + { + "data": { + "text/plain": [ + "'The answer to 2 + 2 is 4.'" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "chain.run(\"what is 2 + 2\")" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "id": "29e5be01", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\u001b[1m> Entering new MultiPromptChain chain...\u001b[0m\n", + "biology: {'input': 'Why does every cell in our body contain DNA?'}\n", + "\u001b[1m> Finished chain.\u001b[0m\n" + ] + }, + { + "data": { + "text/plain": [ + "\"Every cell in our body contains DNA because DNA is the genetic material that carries the instructions for the development, functioning, and reproduction of all living organisms. DNA contains the information needed to build and maintain an organism, including the proteins that make up our cells and tissues. \\n\\nHaving DNA in every cell ensures that each cell has the necessary genetic information to carry out its specific functions and to replicate itself accurately during cell division. This ensures that the genetic information is passed on to the next generation of cells, maintaining the integrity and continuity of the organism's genetic code.\\n\\nAdditionally, DNA serves as a storage system for genetic information that can be accessed and utilized by cells as needed. This allows for the regulation of gene expression, the repair of damaged DNA, and the adaptation to changing environmental conditions.\\n\\nIn summary, every cell in our body contains DNA because it is essential for the proper functioning and survival of all living organisms.\"" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "chain.run(\"Why does every cell in our body contain DNA?\")" + ] + }, + { + "cell_type": "markdown", + "id": "09e0c60b-7ae0-453e-9467-142d8dafee6e", + "metadata": {}, + "source": [ + "**Repeat the above at least once for different inputs and chains executions - Be creative!**" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "id": "c9af5b51-188b-4400-beae-71c8cb56c5fb", + "metadata": {}, + "outputs": [], + "source": [ + "chemistry_template = \"\"\"You are a meticulous chemist. \\\n", + "You reason about matter at the level of atoms, bonds, and energy, \\\n", + "and you always state assumptions about temperature, pressure, and state. \\\n", + "You explain reactions mechanistically rather than just naming them.\n", + "Here is a question:\n", + "{input}\"\"\"\n", + "\n", + "economics_template = \"\"\"You are a sharp economist. \\\n", + "You think in terms of incentives, tradeoffs, and second-order effects. \\\n", + "You distinguish clearly between empirical findings and contested theory, \\\n", + "and you say when the evidence is genuinely mixed.\n", + "Here is a question:\n", + "{input}\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "id": "ae44cee8-5d7f-4cac-8421-b21e56f1d265", + "metadata": {}, + "outputs": [], + "source": [ + "prompt_infos += [\n", + " {\"name\": \"chemistry\",\n", + " \"description\": \"Good for answering chemistry questions\",\n", + " \"prompt_template\": chemistry_template},\n", + " {\"name\": \"economics\",\n", + " \"description\": \"Good for answering economics questions\",\n", + " \"prompt_template\": economics_template},\n", + "]" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "id": "8b909002-2a40-451d-a2d7-03df446b1684", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "======================================================================\n", + "Q: Why is the sky blue at noon but red at sunset?\n", + "\n", + "\n", + "\u001b[1m> Entering new MultiPromptChain chain...\u001b[0m\n", + "physics: {'input': 'Why is the sky blue at noon but red at sunset?'}\n", + "\u001b[1m> Finished chain.\u001b[0m\n", + "The sky appears blue at noon because of the way sunlight interacts with Earth's atmosphere. When the sun is directly overhead, the light from the sun passes through less of the atmosphere, and shorter blue wavelengths are scattered more than longer red wavelengths, making the sky appear blue.\n", + "\n", + "At sunset, the sun is lower in the sky, and the light has to pass through more of the atmosphere. This causes more of the blue light to be scattered out, leaving mostly red and orange light to reach our eyes, giving the sky a red or orange hue.\n", + "======================================================================\n", + "Q: What is the derivative of x^3 * ln(x)?\n", + "\n", + "\n", + "\u001b[1m> Entering new MultiPromptChain chain...\u001b[0m\n", + "math: {'input': 'What is the derivative of x^3 * ln(x)?'}\n", + "\u001b[1m> Finished chain.\u001b[0m\n", + "To find the derivative of x^3 * ln(x), we can use the product rule. \n", + "\n", + "Let u = x^3 and v = ln(x). \n", + "\n", + "Then, the derivative of u with respect to x is du/dx = 3x^2 and the derivative of v with respect to x is dv/dx = 1/x. \n", + "\n", + "Now, using the product rule, the derivative of the product uv is given by:\n", + "\n", + "d(uv)/dx = u * dv/dx + v * du/dx\n", + "= x^3 * (1/x) + ln(x) * 3x^2\n", + "= x^2 + 3x^2 * ln(x)\n", + "= x^2 + 3x^2ln(x)\n", + "\n", + "Therefore, the derivative of x^3 * ln(x) is x^2 + 3x^2ln(x).\n", + "======================================================================\n", + "Q: Why did the Bronze Age collapse happen so suddenly?\n", + "\n", + "\n", + "\u001b[1m> Entering new MultiPromptChain chain...\u001b[0m\n", + "history: {'input': 'Why did the Bronze Age collapse happen so suddenly?'}" + ] + }, + { + "ename": "ValueError", + "evalue": "Received invalid destination chain name 'history'", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mValueError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[47]\u001b[39m\u001b[32m, line 15\u001b[39m\n\u001b[32m 13\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33m\"\u001b[39m\u001b[33m=\u001b[39m\u001b[33m\"\u001b[39m * \u001b[32m70\u001b[39m)\n\u001b[32m 14\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33m\"\u001b[39m\u001b[33mQ:\u001b[39m\u001b[33m\"\u001b[39m, q)\n\u001b[32m---> \u001b[39m\u001b[32m15\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[43mchain\u001b[49m\u001b[43m.\u001b[49m\u001b[43mrun\u001b[49m\u001b[43m(\u001b[49m\u001b[43mq\u001b[49m\u001b[43m)\u001b[49m)\n", + "\u001b[36mFile \u001b[39m\u001b[32m~\\anaconda3\\envs\\torch_env\\Lib\\site-packages\\langchain_core\\_api\\deprecation.py:237\u001b[39m, in \u001b[36mdeprecated..deprecate..warning_emitting_wrapper\u001b[39m\u001b[34m(*args, **kwargs)\u001b[39m\n\u001b[32m 234\u001b[39m \u001b[38;5;66;03m# Only mark the warning as emitted if it was not intentionally\u001b[39;00m\n\u001b[32m 235\u001b[39m \u001b[38;5;66;03m# suppressed by `suppress_langchain_deprecation_warning()`.\u001b[39;00m\n\u001b[32m 236\u001b[39m warned = \u001b[38;5;129;01mnot\u001b[39;00m _SUPPRESSING_LANGCHAIN_DEPRECATION_WARNING.get()\n\u001b[32m--> \u001b[39m\u001b[32m237\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mwrapped\u001b[49m\u001b[43m(\u001b[49m\u001b[43m*\u001b[49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[36mFile \u001b[39m\u001b[32m~\\anaconda3\\envs\\torch_env\\Lib\\site-packages\\langchain_classic\\chains\\base.py:632\u001b[39m, in \u001b[36mChain.run\u001b[39m\u001b[34m(self, callbacks, tags, metadata, *args, **kwargs)\u001b[39m\n\u001b[32m 630\u001b[39m msg = \u001b[33m\"\u001b[39m\u001b[33m`run` supports only one positional argument.\u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m 631\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(msg)\n\u001b[32m--> \u001b[39m\u001b[32m632\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43margs\u001b[49m\u001b[43m[\u001b[49m\u001b[32;43m0\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcallbacks\u001b[49m\u001b[43m=\u001b[49m\u001b[43mcallbacks\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtags\u001b[49m\u001b[43m=\u001b[49m\u001b[43mtags\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmetadata\u001b[49m\u001b[43m=\u001b[49m\u001b[43mmetadata\u001b[49m\u001b[43m)\u001b[49m[\n\u001b[32m 633\u001b[39m _output_key\n\u001b[32m 634\u001b[39m ]\n\u001b[32m 636\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m kwargs \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m args:\n\u001b[32m 637\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m(kwargs, callbacks=callbacks, tags=tags, metadata=metadata)[\n\u001b[32m 638\u001b[39m _output_key\n\u001b[32m 639\u001b[39m ]\n", + "\u001b[36mFile \u001b[39m\u001b[32m~\\anaconda3\\envs\\torch_env\\Lib\\site-packages\\langchain_core\\_api\\deprecation.py:237\u001b[39m, in \u001b[36mdeprecated..deprecate..warning_emitting_wrapper\u001b[39m\u001b[34m(*args, **kwargs)\u001b[39m\n\u001b[32m 234\u001b[39m \u001b[38;5;66;03m# Only mark the warning as emitted if it was not intentionally\u001b[39;00m\n\u001b[32m 235\u001b[39m \u001b[38;5;66;03m# suppressed by `suppress_langchain_deprecation_warning()`.\u001b[39;00m\n\u001b[32m 236\u001b[39m warned = \u001b[38;5;129;01mnot\u001b[39;00m _SUPPRESSING_LANGCHAIN_DEPRECATION_WARNING.get()\n\u001b[32m--> \u001b[39m\u001b[32m237\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mwrapped\u001b[49m\u001b[43m(\u001b[49m\u001b[43m*\u001b[49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[36mFile \u001b[39m\u001b[32m~\\anaconda3\\envs\\torch_env\\Lib\\site-packages\\langchain_classic\\chains\\base.py:413\u001b[39m, in \u001b[36mChain.__call__\u001b[39m\u001b[34m(self, inputs, return_only_outputs, callbacks, tags, metadata, run_name, include_run_info)\u001b[39m\n\u001b[32m 380\u001b[39m \u001b[38;5;250m\u001b[39m\u001b[33;03m\"\"\"Execute the chain.\u001b[39;00m\n\u001b[32m 381\u001b[39m \n\u001b[32m 382\u001b[39m \u001b[33;03mArgs:\u001b[39;00m\n\u001b[32m (...)\u001b[39m\u001b[32m 404\u001b[39m \u001b[33;03m `Chain.output_keys`.\u001b[39;00m\n\u001b[32m 405\u001b[39m \u001b[33;03m\"\"\"\u001b[39;00m\n\u001b[32m 406\u001b[39m config = {\n\u001b[32m 407\u001b[39m \u001b[33m\"\u001b[39m\u001b[33mcallbacks\u001b[39m\u001b[33m\"\u001b[39m: callbacks,\n\u001b[32m 408\u001b[39m \u001b[33m\"\u001b[39m\u001b[33mtags\u001b[39m\u001b[33m\"\u001b[39m: tags,\n\u001b[32m 409\u001b[39m \u001b[33m\"\u001b[39m\u001b[33mmetadata\u001b[39m\u001b[33m\"\u001b[39m: metadata,\n\u001b[32m 410\u001b[39m \u001b[33m\"\u001b[39m\u001b[33mrun_name\u001b[39m\u001b[33m\"\u001b[39m: run_name,\n\u001b[32m 411\u001b[39m }\n\u001b[32m--> \u001b[39m\u001b[32m413\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43minvoke\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 414\u001b[39m \u001b[43m \u001b[49m\u001b[43minputs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 415\u001b[39m \u001b[43m \u001b[49m\u001b[43mcast\u001b[49m\u001b[43m(\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mRunnableConfig\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m{\u001b[49m\u001b[43mk\u001b[49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mv\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mk\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mv\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[43m.\u001b[49m\u001b[43mitems\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mif\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mv\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01mis\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;129;43;01mnot\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m}\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 416\u001b[39m \u001b[43m \u001b[49m\u001b[43mreturn_only_outputs\u001b[49m\u001b[43m=\u001b[49m\u001b[43mreturn_only_outputs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 417\u001b[39m \u001b[43m \u001b[49m\u001b[43minclude_run_info\u001b[49m\u001b[43m=\u001b[49m\u001b[43minclude_run_info\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 418\u001b[39m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[36mFile \u001b[39m\u001b[32m~\\anaconda3\\envs\\torch_env\\Lib\\site-packages\\langchain_classic\\chains\\base.py:167\u001b[39m, in \u001b[36mChain.invoke\u001b[39m\u001b[34m(self, input, config, **kwargs)\u001b[39m\n\u001b[32m 164\u001b[39m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[32m 165\u001b[39m \u001b[38;5;28mself\u001b[39m._validate_inputs(inputs)\n\u001b[32m 166\u001b[39m outputs = (\n\u001b[32m--> \u001b[39m\u001b[32m167\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43m_call\u001b[49m\u001b[43m(\u001b[49m\u001b[43minputs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrun_manager\u001b[49m\u001b[43m=\u001b[49m\u001b[43mrun_manager\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 168\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m new_arg_supported\n\u001b[32m 169\u001b[39m \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;28mself\u001b[39m._call(inputs)\n\u001b[32m 170\u001b[39m )\n\u001b[32m 172\u001b[39m final_outputs: \u001b[38;5;28mdict\u001b[39m[\u001b[38;5;28mstr\u001b[39m, Any] = \u001b[38;5;28mself\u001b[39m.prep_outputs(\n\u001b[32m 173\u001b[39m inputs,\n\u001b[32m 174\u001b[39m outputs,\n\u001b[32m 175\u001b[39m return_only_outputs,\n\u001b[32m 176\u001b[39m )\n\u001b[32m 177\u001b[39m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mBaseException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n", + "\u001b[36mFile \u001b[39m\u001b[32m~\\anaconda3\\envs\\torch_env\\Lib\\site-packages\\langchain_classic\\chains\\router\\base.py:116\u001b[39m, in \u001b[36mMultiRouteChain._call\u001b[39m\u001b[34m(self, inputs, run_manager)\u001b[39m\n\u001b[32m 114\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m.default_chain(route.next_inputs, callbacks=callbacks)\n\u001b[32m 115\u001b[39m msg = \u001b[33mf\u001b[39m\u001b[33m\"\u001b[39m\u001b[33mReceived invalid destination chain name \u001b[39m\u001b[33m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mroute.destination\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m'\u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m--> \u001b[39m\u001b[32m116\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(msg)\n", + "\u001b[31mValueError\u001b[39m: Received invalid destination chain name 'history'" + ] + } + ], + "source": [ + "questions = [\n", + " \"Why is the sky blue at noon but red at sunset?\", # physics\n", + " \"What is the derivative of x^3 * ln(x)?\", # math\n", + " \"Why did the Bronze Age collapse happen so suddenly?\", # history\n", + " \"Explain the difference between a stack and a heap.\", # computer science\n", + " \"How do CRISPR-Cas9 systems achieve target specificity?\", # biology\n", + " \"Why is water a liquid at room temperature but H2S a gas?\", # chemistry\n", + " \"What is the Jevons paradox?\", # economics\n", + " \"What should I make for dinner tonight?\", # → default_chain\n", + "]\n", + "\n", + "for q in questions:\n", + " print(\"=\" * 70)\n", + " print(\"Q:\", q)\n", + " print(chain.run(q))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4ea3fc39-ecff-439b-87ec-b22cb48bd219", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a8b2c207-5ae0-4b56-84d4-439afead0f1d", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3.11 (torch)", + "language": "python", + "name": "torch_env" + }, + "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.11.15" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/data/.ipynb_checkpoints/Data-checkpoint.csv b/data/.ipynb_checkpoints/Data-checkpoint.csv new file mode 100644 index 0000000..69c85f3 --- /dev/null +++ b/data/.ipynb_checkpoints/Data-checkpoint.csv @@ -0,0 +1,9 @@ +Product,Review +Queen Size Sheet Set,"I ordered a king size set. My only criticism would be that I wish seller would offer the king size set with 4 pillowcases. I separately ordered a two pack of pillowcases so I could have a total of four. When I saw the two packages, it looked like the color did not exactly match. Customer service was excellent about sending me two more pillowcases so I would have four that matched. Excellent! For the cost of these sheets, I am satisfied with the characteristics and coolness of the sheets." +Waterproof Phone Pouch,"I loved the waterproof sac, although the opening was made of a hard plastic. I don’t know if that would break easily. But I couldn’t turn my phone on, once it was in the pouch." +Luxury Air Mattress,"This mattress had a small hole in the top of it (took forever to find where it was), and the patches that they provide did not work, maybe because it's the top of the mattress where it's kind of like fabric and a patch won't stick. Maybe I got unlucky with a defective mattress, but where's quality assurance for this company? That flat out should not happen. Emphasis on flat. Cause that's what the mattress was. Seriously horrible experience, ruined my friend's stay with me. Then they make you ship it back instead of just providing a refund, which is also super annoying to pack up an air mattress and take it to the UPS store. This company is the worst, and this mattress is the worst." +Pillows Insert,"This is the best throw pillow fillers on Amazon. I’ve tried several others, and they’re all cheap and flat no matter how much fluffing you do. Once you toss these in the dryer after you remove them from the vacuum sealed shipping material, they fluff up great" +"Milk Frother Handheld","I loved this product. But they only seem to last a few months. The company was great replacing the first one (the frother falls out of the handle and can't be fixed). The after 4 months my second one did the same. I only use the frother for coffee once a day. It's not overuse or abuse. I'm very disappointed and will look for another. As I understand they will only replace once. Anyway, if you have one good luck." +"L'Or Espresso Café","Je trouve le goût médiocre. La mousse ne tient pas, c'est bizarre. J'achète les mêmes dans le commerce et le goût est bien meilleur... +Vieux lot ou contrefaçon !?" +Hervidor de Agua Eléctrico,"Está lu bonita calienta muy rápido, es muy funcional, solo falta ver cuánto dura, solo llevo 3 días en funcionamiento." \ No newline at end of file diff --git a/lab-chains-in-langchain.ipynb b/lab-chains-in-langchain.ipynb index 20763f5..0407d73 100644 --- a/lab-chains-in-langchain.ipynb +++ b/lab-chains-in-langchain.ipynb @@ -18,7 +18,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "541eb2f1", "metadata": {}, "outputs": [], @@ -29,7 +29,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 2, "id": "b7ed03ed-1322-49e3-b2a2-33e94fb592ef", "metadata": { "tags": [] @@ -42,24 +42,12 @@ "_ = load_dotenv(find_dotenv())\n", "\n", "OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')\n", - "HUGGINGFACEHUB_API_TOKEN = os.getenv('HUGGINGFACEHUB_API_TOKEN')" + "HUGGINGFACEHUB_API_TOKEN = os.getenv('HF_TOKEN')" ] }, { "cell_type": "code", "execution_count": 5, - "id": "b84e441b", - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "#!pip install pandas" - ] - }, - { - "cell_type": "code", - "execution_count": null, "id": "974acf8e-8f88-42de-88f8-40a82cb58e8b", "metadata": { "tags": [] @@ -67,12 +55,12 @@ "outputs": [], "source": [ "import pandas as pd\n", - "df = pd.read_csv('./data/Data.csv')" + "df = pd.read_csv('data/Data.csv')" ] }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 6, "id": "b7a09c35", "metadata": { "tags": [] @@ -126,23 +114,23 @@ " \n", " \n", " 4\n", - " Milk Frother Handheld\\n\n", - " I loved this product. But they only seem to l...\n", + " Milk Frother Handheld\n", + " I loved this product. But they only seem to la...\n", " \n", " \n", "\n", "" ], "text/plain": [ - " Product Review\n", - "0 Queen Size Sheet Set I ordered a king size set. My only criticism w...\n", - "1 Waterproof Phone Pouch I loved the waterproof sac, although the openi...\n", - "2 Luxury Air Mattress This mattress had a small hole in the top of i...\n", - "3 Pillows Insert This is the best throw pillow fillers on Amazo...\n", - "4 Milk Frother Handheld\\n  I loved this product. But they only seem to l..." + " Product Review\n", + "0 Queen Size Sheet Set I ordered a king size set. My only criticism w...\n", + "1 Waterproof Phone Pouch I loved the waterproof sac, although the openi...\n", + "2 Luxury Air Mattress This mattress had a small hole in the top of i...\n", + "3 Pillows Insert This is the best throw pillow fillers on Amazo...\n", + "4 Milk Frother Handheld I loved this product. But they only seem to la..." ] }, - "execution_count": 15, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -161,31 +149,171 @@ }, { "cell_type": "code", - "execution_count": null, - "id": "427e1119", + "execution_count": 7, + "id": "20b6b2a4", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Requirement already satisfied: langchain in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (1.3.14)\n", + "Requirement already satisfied: langchain-core in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (1.5.2)\n", + "Requirement already satisfied: langchain-openai in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (1.4.1)\n", + "Requirement already satisfied: langchain-community in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (0.4.1)\n", + "Requirement already satisfied: langchain-classic in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (1.0.7)\n", + "Requirement already satisfied: langgraph<1.3.0,>=1.2.5 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langchain) (1.2.9)\n", + "Requirement already satisfied: pydantic<3.0.0,>=2.7.4 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langchain) (2.13.4)\n", + "Requirement already satisfied: jsonpatch<2.0.0,>=1.33.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langchain-core) (1.33)\n", + "Requirement already satisfied: langchain-protocol>=0.0.17 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langchain-core) (0.0.18)\n", + "Requirement already satisfied: langsmith<1.0.0,>=0.3.45 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langchain-core) (0.8.5)\n", + "Requirement already satisfied: packaging>=23.2.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langchain-core) (24.2)\n", + "Requirement already satisfied: pyyaml<7.0.0,>=5.3.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langchain-core) (6.0.2)\n", + "Requirement already satisfied: tenacity!=8.4.0,<10.0.0,>=8.1.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langchain-core) (9.1.4)\n", + "Requirement already satisfied: typing-extensions<5.0.0,>=4.7.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langchain-core) (4.15.0)\n", + "Requirement already satisfied: uuid-utils<1.0,>=0.12.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langchain-core) (0.15.0)\n", + "Requirement already satisfied: openai<3.0.0,>=2.45.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langchain-openai) (2.46.0)\n", + "Requirement already satisfied: tiktoken<1.0.0,>=0.7.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langchain-openai) (0.13.0)\n", + "Requirement already satisfied: SQLAlchemy<3.0.0,>=1.4.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langchain-community) (2.0.51)\n", + "Requirement already satisfied: requests<3.0.0,>=2.32.5 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langchain-community) (2.34.2)\n", + "Requirement already satisfied: aiohttp<4.0.0,>=3.8.3 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langchain-community) (3.13.5)\n", + "Requirement already satisfied: dataclasses-json<0.7.0,>=0.6.7 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langchain-community) (0.6.7)\n", + "Requirement already satisfied: pydantic-settings<3.0.0,>=2.10.1 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langchain-community) (2.14.1)\n", + "Requirement already satisfied: httpx-sse<1.0.0,>=0.4.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langchain-community) (0.4.3)\n", + "Requirement already satisfied: numpy>=2.1.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langchain-community) (2.5.1)\n", + "Requirement already satisfied: langchain-text-splitters<2.0.0,>=1.1.2 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langchain-classic) (1.1.2)\n", + "Requirement already satisfied: aiohappyeyeballs>=2.5.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (2.6.1)\n", + "Requirement already satisfied: aiosignal>=1.4.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (1.4.0)\n", + "Requirement already satisfied: attrs>=17.3.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (25.1.0)\n", + "Requirement already satisfied: frozenlist>=1.1.1 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (1.8.0)\n", + "Requirement already satisfied: multidict<7.0,>=4.5 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (6.7.1)\n", + "Requirement already satisfied: propcache>=0.2.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (0.5.2)\n", + "Requirement already satisfied: yarl<2.0,>=1.17.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (1.23.0)\n", + "Requirement already satisfied: marshmallow<4.0.0,>=3.18.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from dataclasses-json<0.7.0,>=0.6.7->langchain-community) (3.26.2)\n", + "Requirement already satisfied: typing-inspect<1,>=0.4.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from dataclasses-json<0.7.0,>=0.6.7->langchain-community) (0.9.0)\n", + "Requirement already satisfied: jsonpointer>=1.9 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from jsonpatch<2.0.0,>=1.33.0->langchain-core) (3.0.0)\n", + "Requirement already satisfied: langgraph-checkpoint<5.0.0,>=4.1.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langgraph<1.3.0,>=1.2.5->langchain) (4.1.1)\n", + "Requirement already satisfied: langgraph-prebuilt<1.2.0,>=1.1.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langgraph<1.3.0,>=1.2.5->langchain) (1.1.0)\n", + "Requirement already satisfied: langgraph-sdk<0.5.0,>=0.4.2 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langgraph<1.3.0,>=1.2.5->langchain) (0.4.2)\n", + "Requirement already satisfied: xxhash>=3.5.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langgraph<1.3.0,>=1.2.5->langchain) (3.7.0)\n", + "Requirement already satisfied: httpx<1,>=0.23.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langsmith<1.0.0,>=0.3.45->langchain-core) (0.28.1)\n", + "Requirement already satisfied: orjson>=3.9.14 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langsmith<1.0.0,>=0.3.45->langchain-core) (3.11.9)\n", + "Requirement already satisfied: requests-toolbelt>=1.0.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langsmith<1.0.0,>=0.3.45->langchain-core) (1.0.0)\n", + "Requirement already satisfied: zstandard>=0.23.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langsmith<1.0.0,>=0.3.45->langchain-core) (0.25.0)\n", + "Requirement already satisfied: anyio<5,>=3.5.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from openai<3.0.0,>=2.45.0->langchain-openai) (4.8.0)\n", + "Requirement already satisfied: distro<2,>=1.7.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from openai<3.0.0,>=2.45.0->langchain-openai) (1.9.0)\n", + "Requirement already satisfied: jiter<1,>=0.10.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from openai<3.0.0,>=2.45.0->langchain-openai) (0.14.0)\n", + "Requirement already satisfied: sniffio in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from openai<3.0.0,>=2.45.0->langchain-openai) (1.3.1)\n", + "Requirement already satisfied: tqdm>4 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from openai<3.0.0,>=2.45.0->langchain-openai) (4.67.3)\n", + "Requirement already satisfied: annotated-types>=0.6.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from pydantic<3.0.0,>=2.7.4->langchain) (0.7.0)\n", + "Requirement already satisfied: pydantic-core==2.46.4 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from pydantic<3.0.0,>=2.7.4->langchain) (2.46.4)\n", + "Requirement already satisfied: typing-inspection>=0.4.2 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from pydantic<3.0.0,>=2.7.4->langchain) (0.4.2)\n", + "Requirement already satisfied: python-dotenv>=0.21.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from pydantic-settings<3.0.0,>=2.10.1->langchain-community) (1.2.2)\n", + "Requirement already satisfied: charset_normalizer<4,>=2 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from requests<3.0.0,>=2.32.5->langchain-community) (3.4.1)\n", + "Requirement already satisfied: idna<4,>=2.5 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from requests<3.0.0,>=2.32.5->langchain-community) (3.10)\n", + "Requirement already satisfied: urllib3<3,>=1.26 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from requests<3.0.0,>=2.32.5->langchain-community) (2.3.0)\n", + "Requirement already satisfied: certifi>=2023.5.7 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from requests<3.0.0,>=2.32.5->langchain-community) (2025.1.31)\n", + "Requirement already satisfied: greenlet>=1 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from SQLAlchemy<3.0.0,>=1.4.0->langchain-community) (3.5.0)\n", + "Requirement already satisfied: regex in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from tiktoken<1.0.0,>=0.7.0->langchain-openai) (2026.5.9)\n", + "Requirement already satisfied: httpcore==1.* in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from httpx<1,>=0.23.0->langsmith<1.0.0,>=0.3.45->langchain-core) (1.0.7)\n", + "Requirement already satisfied: h11<0.15,>=0.13 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from httpcore==1.*->httpx<1,>=0.23.0->langsmith<1.0.0,>=0.3.45->langchain-core) (0.14.0)\n", + "Requirement already satisfied: ormsgpack>=1.12.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langgraph-checkpoint<5.0.0,>=4.1.0->langgraph<1.3.0,>=1.2.5->langchain) (1.12.2)\n", + "Requirement already satisfied: websockets<16,>=14 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from langgraph-sdk<0.5.0,>=0.4.2->langgraph<1.3.0,>=1.2.5->langchain) (14.2)\n", + "Requirement already satisfied: colorama in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from tqdm>4->openai<3.0.0,>=2.45.0->langchain-openai) (0.4.6)\n", + "Requirement already satisfied: mypy-extensions>=0.3.0 in c:\\users\\sol_v\\appdata\\local\\programs\\python\\python313\\lib\\site-packages (from typing-inspect<1,>=0.4.0->dataclasses-json<0.7.0,>=0.6.7->langchain-community) (1.0.0)\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "\n", + "[notice] A new release of pip is available: 24.3.1 -> 26.1.2\n", + "[notice] To update, run: python.exe -m pip install --upgrade pip\n" + ] + } + ], "source": [ - "!pip install langchain_community" + "!pip install langchain langchain-core langchain-openai langchain-community langchain-classic" ] }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 8, + "id": "af96fedc", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Name: langchain\n", + "Version: 1.3.14\n", + "Summary: Building applications with LLMs through composability\n", + "Home-page: https://docs.langchain.com/\n", + "Author: \n", + "Author-email: \n", + "License: MIT\n", + "Location: C:\\Users\\sol_v\\AppData\\Local\\Programs\\Python\\Python313\\Lib\\site-packages\n", + "Requires: langchain-core, langgraph, pydantic\n", + "Required-by: \n", + "---\n", + "Name: langchain-core\n", + "Version: 1.5.2\n", + "Summary: Building applications with LLMs through composability\n", + "Home-page: https://docs.langchain.com/\n", + "Author: \n", + "Author-email: \n", + "License: MIT\n", + "Location: C:\\Users\\sol_v\\AppData\\Local\\Programs\\Python\\Python313\\Lib\\site-packages\n", + "Requires: jsonpatch, langchain-protocol, langsmith, packaging, pydantic, pyyaml, tenacity, typing-extensions, uuid-utils\n", + "Required-by: langchain, langchain-chroma, langchain-classic, langchain-community, langchain-openai, langchain-text-splitters, langgraph, langgraph-checkpoint, langgraph-prebuilt, langgraph-sdk\n", + "---\n", + "Name: langchain-openai\n", + "Version: 1.4.1\n", + "Summary: An integration package connecting OpenAI and LangChain\n", + "Home-page: https://docs.langchain.com/oss/python/integrations/providers/openai\n", + "Author: \n", + "Author-email: \n", + "License: MIT\n", + "Location: C:\\Users\\sol_v\\AppData\\Local\\Programs\\Python\\Python313\\Lib\\site-packages\n", + "Requires: langchain-core, openai, tiktoken\n", + "Required-by: \n", + "---\n", + "Name: langchain-community\n", + "Version: 0.4.1\n", + "Summary: Community contributed LangChain integrations.\n", + "Home-page: \n", + "Author: \n", + "Author-email: \n", + "License: MIT\n", + "Location: C:\\Users\\sol_v\\AppData\\Local\\Programs\\Python\\Python313\\Lib\\site-packages\n", + "Requires: aiohttp, dataclasses-json, httpx-sse, langchain-classic, langchain-core, langsmith, numpy, pydantic-settings, PyYAML, requests, SQLAlchemy, tenacity\n", + "Required-by: \n" + ] + } + ], + "source": [ + "!pip show langchain langchain-core langchain-openai langchain-community" + ] + }, + { + "cell_type": "code", + "execution_count": 9, "id": "e92dff22", "metadata": { "tags": [] }, "outputs": [], "source": [ - "from langchain_openai import ChatOpenAI\n", - "from langchain.prompts import ChatPromptTemplate\n", - "from langchain.chains import LLMChain" + "from langchain_openai import ChatOpenAI # ✅ 1.0.3\n", + "from langchain_core.prompts import ChatPromptTemplate # ✅ From langchain-core 1.0.5\n", + "from langchain_classic.chains import LLMChain # ✅ Legacy (install if missing)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "id": "943237a7", "metadata": {}, "outputs": [], @@ -196,35 +324,63 @@ }, { "cell_type": "code", - "execution_count": null, - "id": "cdcdb42d", + "execution_count": 13, + "id": "9b023dfd", "metadata": {}, "outputs": [], "source": [ - "prompt = ChatPromptTemplate.from_template( #Write a query that would take a variable to describe any product\n", + "prompt = ChatPromptTemplate.from_template( \n", + " \"Write a short, engaging product description for a {product}.\"#Write a query that would take a variable to describe any product\n", " \n", ")" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "id": "d7abc20b", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\sol_v\\AppData\\Local\\Temp\\ipykernel_2596\\1305865249.py:1: LangChainDeprecationWarning: The class `LLMChain` was deprecated in LangChain 0.1.17 and will be removed in 2.0.0. Use `RunnableSequence, e.g., `prompt | llm`` instead.\n", + " chain = LLMChain(llm=llm, prompt=prompt)\n" + ] + } + ], "source": [ - "\n", "chain = LLMChain(llm=llm, prompt=prompt)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "id": "ad44d1fb", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\sol_v\\AppData\\Local\\Temp\\ipykernel_2596\\1723211282.py:2: LangChainDeprecationWarning: The method `Chain.run` was deprecated in langchain-classic 0.1.0 and will be removed in 2.0.0. Use `invoke` instead.\n", + " chain.run(product)\n" + ] + }, + { + "data": { + "text/plain": [ + "\"Transform your bedroom into a luxurious oasis with our Queen Size Sheet Set. Made from ultra-soft, breathable microfiber material, these sheets are designed to provide ultimate comfort and a peaceful night's sleep. The deep pockets ensure a secure fit on your mattress, while the double-brushed finish offers a silky-smooth feel against your skin. Available in a variety of stunning colors, this sheet set is the perfect combination of quality, style, and affordability. Upgrade your bedding and indulge in a restful night's sleep with our Queen Size Sheet Set.\"" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "product = #Select a product type to be describe\n", + "product = \"Queen Size Sheet Set\" #Select a product type to be describe\n", "chain.run(product)" ] }, @@ -238,18 +394,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "id": "febee243", "metadata": {}, "outputs": [], "source": [ - "from langchain.chains import SimpleSequentialChain" + "from langchain_classic.chains import SimpleSequentialChain " ] }, { "cell_type": "code", - "execution_count": null, - "id": "2f31aa8a", + "execution_count": 18, + "id": "91e6bbe0", "metadata": {}, "outputs": [], "source": [ @@ -257,7 +413,7 @@ "\n", "# prompt template 1\n", "first_prompt = ChatPromptTemplate.from_template(\n", - " #Repeat the initial query or create a new query that would feed into the second prompt\n", + " \"What is the best name to describe a company that makes {product}?\" #Repeat the initial query or create a new query that would feed into the second prompt\n", ")\n", "\n", "# Chain 1\n", @@ -266,15 +422,14 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "id": "3f5d5b76", "metadata": {}, "outputs": [], "source": [ - "\n", "# prompt template 2\n", "second_prompt = ChatPromptTemplate.from_template(\n", - " #Write the second prompt query that takes an input variable whose input will come from the previous prompt\"\n", + " \"Write a 20 word description for the following company: {company_name}\" #Write the second prompt query that takes an input variable whose input will come from the previous prompt\"\n", ")\n", "# chain 2\n", "chain_two = LLMChain(llm=llm, prompt=second_prompt)" @@ -282,7 +437,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "id": "6c1eb2c4", "metadata": {}, "outputs": [], @@ -294,10 +449,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "id": "78458efe", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\u001b[1m> Entering new SimpleSequentialChain chain...\u001b[0m\n", + "\u001b[36;1m\u001b[1;3m\"Royalty Linens\"\u001b[0m\n", + "\u001b[33;1m\u001b[1;3mElevate your home décor with luxurious linens fit for royalty, offering exquisite bedding and bath essentials in elegant designs.\u001b[0m\n", + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n" + ] + }, + { + "data": { + "text/plain": [ + "'Elevate your home décor with luxurious linens fit for royalty, offering exquisite bedding and bath essentials in elegant designs.'" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "overall_simple_chain.run(product)" ] @@ -320,17 +499,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "id": "4c129ef6", "metadata": {}, "outputs": [], "source": [ - "from langchain.chains import SequentialChain" + "from langchain_classic.chains import SequentialChain " ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "id": "016187ac", "metadata": {}, "outputs": [], @@ -339,67 +518,72 @@ "\n", "\n", "first_prompt = ChatPromptTemplate.from_template(\n", - " #This prompt should translate a review\n", + " \"Translate the following review to english:\" \"\\n\\n{Review} \" #This prompt should translate a review\n", ")\n", "\n", "chain_one = LLMChain(llm=llm, prompt=first_prompt, \n", - " output_key=None #Give a name to your output\n", - " )\n" + " output_key=\"English_Review\" #Give a name to your output\n", + " )" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "id": "0fb0730e", "metadata": {}, "outputs": [], "source": [ "second_prompt = ChatPromptTemplate.from_template(\n", - " #Write a promplt to summarize a review\n", + " \"Can you summarize the following review in 1 sentence:\"\n", + " \"\\n\\n{English_Review}\" #Write a prompt to summarize a review\n", ")\n", "\n", "chain_two = LLMChain(llm=llm, prompt=second_prompt, \n", - " output_key=None #give a name to this output\n", - " )\n" + " output_key=\"summary\" #give a name to this output\n", + " )" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "id": "6accf92d", "metadata": {}, "outputs": [], "source": [ "# prompt template 3: translate to english or other language\n", "third_prompt = ChatPromptTemplate.from_template(\n", - " None\n", + " \"What language is the following review written in?\"\n", + " \"\\n\\n{Review}\"\n", ")\n", "# chain 3: input= Review and output= language\n", "chain_three = LLMChain(llm=llm, prompt=third_prompt,\n", - " output_key=None\n", - " )\n" + " output_key=\"language\"\n", + " )\n", + "\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 28, "id": "c7a46121", "metadata": {}, "outputs": [], "source": [ - "\n", "# prompt template 4: follow up message that take as inputs the two previous prompts' variables\n", "fourth_prompt = ChatPromptTemplate.from_template(\n", - " None\n", + " \"Write a follow up response to the following summary\"\n", + " \" in the specified language:\"\n", + " \"\\n\\nSummary: {summary}\\n\\nLanguage: {language}\"\n", ")\n", "chain_four = LLMChain(llm=llm, prompt=fourth_prompt,\n", - " output_key=None\n", - " )\n" + " output_key=\"followup_message\"\n", + " )\n", + "\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "id": "89603117", "metadata": {}, "outputs": [], @@ -408,18 +592,52 @@ "# and output= English_Review,summary, followup_message\n", "overall_chain = SequentialChain(\n", " chains=[chain_one, chain_two, chain_three, chain_four],\n", - " input_variables=None,\n", - " output_variables=[None, None, None],\n", + " input_variables=[\"Review\"],\n", + " output_variables=[\"English_Review\", \"summary\", \"followup_message\"],\n", " verbose=True\n", - ")" + ")\n", + "\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 30, "id": "51b04f45", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\sol_v\\AppData\\Local\\Temp\\ipykernel_2596\\1992003631.py:2: LangChainDeprecationWarning: The method `Chain.__call__` was deprecated in langchain-classic 0.1.0 and will be removed in 2.0.0. Use `invoke` instead.\n", + " overall_chain(review)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\u001b[1m> Entering new SequentialChain chain...\u001b[0m\n", + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n" + ] + }, + { + "data": { + "text/plain": [ + "{'Review': \"Je trouve le goût médiocre. La mousse ne tient pas, c'est bizarre. J'achète les mêmes dans le commerce et le goût est bien meilleur...\\r\\nVieux lot ou contrefaçon !?\",\n", + " 'English_Review': \"I find the taste mediocre. The foam doesn't hold, it's strange. I buy the same ones in stores and the taste is much better... Old batch or counterfeit!?\",\n", + " 'summary': 'The reviewer is disappointed with the taste and quality of the product, suspecting that it may be an old batch or counterfeit.',\n", + " 'followup_message': \"Je suis désolé d'apprendre que vous avez été déçu par le goût et la qualité du produit. Il est possible qu'il s'agisse en effet d'un lot expiré ou contrefait. Nous prenons vos commentaires très au sérieux et nous allons enquêter sur ce problème pour nous assurer que nos clients reçoivent toujours des produits frais et authentiques. Merci de nous avoir informés de cette situation et nous nous excusons pour tout désagrément que cela a pu vous causer.\"}" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "review = df.Review[5]\n", "overall_chain(review)" @@ -443,7 +661,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 31, "id": "ade83f4f", "metadata": {}, "outputs": [], @@ -509,7 +727,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 32, "id": "5f590e9f", "metadata": {}, "outputs": [], @@ -545,19 +763,19 @@ }, { "cell_type": "code", - "execution_count": null, - "id": "31b06fc8", + "execution_count": 33, + "id": "b5cfdd35", "metadata": {}, "outputs": [], "source": [ - "from langchain.chains.router import MultiPromptChain\n", - "from langchain.chains.router.llm_router import LLMRouterChain,RouterOutputParser\n", - "from langchain.prompts import PromptTemplate" + "from langchain_classic.chains.router import MultiPromptChain\n", + "from langchain_classic.chains.router.llm_router import LLMRouterChain, RouterOutputParser\n", + "from langchain_core.prompts import PromptTemplate # Updated path for prompts (core package)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 34, "id": "f3f50bcc", "metadata": {}, "outputs": [], @@ -567,37 +785,37 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 35, "id": "8eefec24", "metadata": {}, "outputs": [], "source": [ "destination_chains = {}\n", - "for p_info in prompt_infos:\n", + "for p_info in prompt_infos: #converts a list of template descriptions into a dictionary of executable chains the router can look up by name.\n", " name = p_info[\"name\"]\n", " prompt_template = p_info[\"prompt_template\"]\n", " prompt = ChatPromptTemplate.from_template(template=prompt_template)\n", " chain = LLMChain(llm=llm, prompt=prompt)\n", " destination_chains[name] = chain \n", " \n", - "destinations = [f\"{p['name']}: {p['description']}\" for p in prompt_infos]\n", - "destinations_str = \"\\n\".join(destinations)" + "destinations = [f\"{p['name']}: {p['description']}\" for p in prompt_infos] #assembles the human-readable menu of routing options that the router LLM will read to decide where a question should go.\n", + "destinations_str = \"\\n\".join(destinations) #turns the list of options into one block of text that can be dropped straight into the router's prompt." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 36, "id": "9f98018a", "metadata": {}, "outputs": [], "source": [ - "default_prompt = ChatPromptTemplate.from_template(\"{input}\")\n", - "default_chain = LLMChain(llm=llm, prompt=default_prompt)" + "default_prompt = ChatPromptTemplate.from_template(\"{input}\") \n", + "default_chain = LLMChain(llm=llm, prompt=default_prompt) #the fallback chain that handles questions the router can't match to any subject." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 37, "id": "11b2e2ba", "metadata": {}, "outputs": [], @@ -635,7 +853,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 38, "id": "1387109d", "metadata": {}, "outputs": [], @@ -654,10 +872,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 39, "id": "2fb7d560", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\sol_v\\AppData\\Local\\Temp\\ipykernel_2596\\3038952769.py:1: LangChainDeprecationWarning: The class `MultiPromptChain` was deprecated in LangChain 0.2.12 and will be removed in 2.0.0. Use `langchain.agents.create_agent` instead. Build routing logic with `create_agent` (e.g. with subagents or prompt-selection middleware). See https://docs.langchain.com/oss/python/langchain/agents\n", + " chain = MultiPromptChain(router_chain=router_chain,\n" + ] + } + ], "source": [ "chain = MultiPromptChain(router_chain=router_chain, \n", " destination_chains=destination_chains, \n", @@ -667,30 +894,96 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 40, "id": "d86b2131", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\u001b[1m> Entering new MultiPromptChain chain...\u001b[0m\n", + "physics: {'input': 'What is black body radiation?'}\n", + "\u001b[1m> Finished chain.\u001b[0m\n" + ] + }, + { + "data": { + "text/plain": [ + "\"Black body radiation is the electromagnetic radiation emitted by a perfect absorber of radiation, known as a black body. A black body absorbs all radiation that falls on it and emits radiation across the entire electromagnetic spectrum. The spectrum of black body radiation is continuous and depends only on the temperature of the black body. This phenomenon is described by Planck's law, which states that the intensity of radiation emitted by a black body at a given wavelength is proportional to the temperature of the body and the wavelength to the power of -5.\"" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "chain.run(\"What is black body radiation?\")" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 41, "id": "3b717379", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\u001b[1m> Entering new MultiPromptChain chain...\u001b[0m\n", + "math: {'input': 'what is 2 + 2'}\n", + "\u001b[1m> Finished chain.\u001b[0m\n" + ] + }, + { + "data": { + "text/plain": [ + "'The answer to 2 + 2 is 4.'" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "chain.run(\"what is 2 + 2\")" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 42, "id": "29e5be01", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\u001b[1m> Entering new MultiPromptChain chain...\u001b[0m\n", + "biology: {'input': 'Why does every cell in our body contain DNA?'}\n", + "\u001b[1m> Finished chain.\u001b[0m\n" + ] + }, + { + "data": { + "text/plain": [ + "\"Every cell in our body contains DNA because DNA is the genetic material that carries the instructions for the development, functioning, and reproduction of all living organisms. DNA contains the information needed to build and maintain an organism, including the proteins that make up our cells and tissues. \\n\\nHaving DNA in every cell ensures that each cell has the necessary genetic information to carry out its specific functions and to replicate itself accurately during cell division. This ensures that the genetic information is passed on to the next generation of cells, maintaining the integrity and continuity of the organism's genetic code.\\n\\nAdditionally, DNA serves as a storage system for genetic information that can be accessed and utilized by cells as needed. This allows for the regulation of gene expression, the repair of damaged DNA, and the adaptation to changing environmental conditions.\\n\\nIn summary, every cell in our body contains DNA because it is essential for the proper functioning and survival of all living organisms.\"" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "chain.run(\"Why does every cell in our body contain DNA?\")" ] @@ -702,13 +995,153 @@ "source": [ "**Repeat the above at least once for different inputs and chains executions - Be creative!**" ] + }, + { + "cell_type": "code", + "execution_count": 45, + "id": "c9af5b51-188b-4400-beae-71c8cb56c5fb", + "metadata": {}, + "outputs": [], + "source": [ + "chemistry_template = \"\"\"You are a meticulous chemist. \\\n", + "You reason about matter at the level of atoms, bonds, and energy, \\\n", + "and you always state assumptions about temperature, pressure, and state. \\\n", + "You explain reactions mechanistically rather than just naming them.\n", + "Here is a question:\n", + "{input}\"\"\"\n", + "\n", + "economics_template = \"\"\"You are a sharp economist. \\\n", + "You think in terms of incentives, tradeoffs, and second-order effects. \\\n", + "You distinguish clearly between empirical findings and contested theory, \\\n", + "and you say when the evidence is genuinely mixed.\n", + "Here is a question:\n", + "{input}\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "id": "ae44cee8-5d7f-4cac-8421-b21e56f1d265", + "metadata": {}, + "outputs": [], + "source": [ + "prompt_infos += [\n", + " {\"name\": \"chemistry\",\n", + " \"description\": \"Good for answering chemistry questions\",\n", + " \"prompt_template\": chemistry_template},\n", + " {\"name\": \"economics\",\n", + " \"description\": \"Good for answering economics questions\",\n", + " \"prompt_template\": economics_template},\n", + "]" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "id": "8b909002-2a40-451d-a2d7-03df446b1684", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "======================================================================\n", + "Q: Why is the sky blue at noon but red at sunset?\n", + "\n", + "\n", + "\u001b[1m> Entering new MultiPromptChain chain...\u001b[0m\n", + "physics: {'input': 'Why is the sky blue at noon but red at sunset?'}\n", + "\u001b[1m> Finished chain.\u001b[0m\n", + "The sky appears blue at noon because of the way sunlight interacts with Earth's atmosphere. When the sun is directly overhead, the light from the sun passes through less of the atmosphere, and shorter blue wavelengths are scattered more than longer red wavelengths, making the sky appear blue.\n", + "\n", + "At sunset, the sun is lower in the sky, and the light has to pass through more of the atmosphere. This causes more of the blue light to be scattered out, leaving mostly red and orange light to reach our eyes, giving the sky a red or orange hue.\n", + "======================================================================\n", + "Q: What is the derivative of x^3 * ln(x)?\n", + "\n", + "\n", + "\u001b[1m> Entering new MultiPromptChain chain...\u001b[0m\n", + "math: {'input': 'What is the derivative of x^3 * ln(x)?'}\n", + "\u001b[1m> Finished chain.\u001b[0m\n", + "To find the derivative of x^3 * ln(x), we can use the product rule. \n", + "\n", + "Let u = x^3 and v = ln(x). \n", + "\n", + "Then, the derivative of u with respect to x is du/dx = 3x^2 and the derivative of v with respect to x is dv/dx = 1/x. \n", + "\n", + "Now, using the product rule, the derivative of the product uv is given by:\n", + "\n", + "d(uv)/dx = u * dv/dx + v * du/dx\n", + "= x^3 * (1/x) + ln(x) * 3x^2\n", + "= x^2 + 3x^2 * ln(x)\n", + "= x^2 + 3x^2ln(x)\n", + "\n", + "Therefore, the derivative of x^3 * ln(x) is x^2 + 3x^2ln(x).\n", + "======================================================================\n", + "Q: Why did the Bronze Age collapse happen so suddenly?\n", + "\n", + "\n", + "\u001b[1m> Entering new MultiPromptChain chain...\u001b[0m\n", + "history: {'input': 'Why did the Bronze Age collapse happen so suddenly?'}" + ] + }, + { + "ename": "ValueError", + "evalue": "Received invalid destination chain name 'history'", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mValueError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[47]\u001b[39m\u001b[32m, line 15\u001b[39m\n\u001b[32m 13\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33m\"\u001b[39m\u001b[33m=\u001b[39m\u001b[33m\"\u001b[39m * \u001b[32m70\u001b[39m)\n\u001b[32m 14\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33m\"\u001b[39m\u001b[33mQ:\u001b[39m\u001b[33m\"\u001b[39m, q)\n\u001b[32m---> \u001b[39m\u001b[32m15\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[43mchain\u001b[49m\u001b[43m.\u001b[49m\u001b[43mrun\u001b[49m\u001b[43m(\u001b[49m\u001b[43mq\u001b[49m\u001b[43m)\u001b[49m)\n", + "\u001b[36mFile \u001b[39m\u001b[32m~\\anaconda3\\envs\\torch_env\\Lib\\site-packages\\langchain_core\\_api\\deprecation.py:237\u001b[39m, in \u001b[36mdeprecated..deprecate..warning_emitting_wrapper\u001b[39m\u001b[34m(*args, **kwargs)\u001b[39m\n\u001b[32m 234\u001b[39m \u001b[38;5;66;03m# Only mark the warning as emitted if it was not intentionally\u001b[39;00m\n\u001b[32m 235\u001b[39m \u001b[38;5;66;03m# suppressed by `suppress_langchain_deprecation_warning()`.\u001b[39;00m\n\u001b[32m 236\u001b[39m warned = \u001b[38;5;129;01mnot\u001b[39;00m _SUPPRESSING_LANGCHAIN_DEPRECATION_WARNING.get()\n\u001b[32m--> \u001b[39m\u001b[32m237\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mwrapped\u001b[49m\u001b[43m(\u001b[49m\u001b[43m*\u001b[49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[36mFile \u001b[39m\u001b[32m~\\anaconda3\\envs\\torch_env\\Lib\\site-packages\\langchain_classic\\chains\\base.py:632\u001b[39m, in \u001b[36mChain.run\u001b[39m\u001b[34m(self, callbacks, tags, metadata, *args, **kwargs)\u001b[39m\n\u001b[32m 630\u001b[39m msg = \u001b[33m\"\u001b[39m\u001b[33m`run` supports only one positional argument.\u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m 631\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(msg)\n\u001b[32m--> \u001b[39m\u001b[32m632\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43margs\u001b[49m\u001b[43m[\u001b[49m\u001b[32;43m0\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcallbacks\u001b[49m\u001b[43m=\u001b[49m\u001b[43mcallbacks\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtags\u001b[49m\u001b[43m=\u001b[49m\u001b[43mtags\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmetadata\u001b[49m\u001b[43m=\u001b[49m\u001b[43mmetadata\u001b[49m\u001b[43m)\u001b[49m[\n\u001b[32m 633\u001b[39m _output_key\n\u001b[32m 634\u001b[39m ]\n\u001b[32m 636\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m kwargs \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m args:\n\u001b[32m 637\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m(kwargs, callbacks=callbacks, tags=tags, metadata=metadata)[\n\u001b[32m 638\u001b[39m _output_key\n\u001b[32m 639\u001b[39m ]\n", + "\u001b[36mFile \u001b[39m\u001b[32m~\\anaconda3\\envs\\torch_env\\Lib\\site-packages\\langchain_core\\_api\\deprecation.py:237\u001b[39m, in \u001b[36mdeprecated..deprecate..warning_emitting_wrapper\u001b[39m\u001b[34m(*args, **kwargs)\u001b[39m\n\u001b[32m 234\u001b[39m \u001b[38;5;66;03m# Only mark the warning as emitted if it was not intentionally\u001b[39;00m\n\u001b[32m 235\u001b[39m \u001b[38;5;66;03m# suppressed by `suppress_langchain_deprecation_warning()`.\u001b[39;00m\n\u001b[32m 236\u001b[39m warned = \u001b[38;5;129;01mnot\u001b[39;00m _SUPPRESSING_LANGCHAIN_DEPRECATION_WARNING.get()\n\u001b[32m--> \u001b[39m\u001b[32m237\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mwrapped\u001b[49m\u001b[43m(\u001b[49m\u001b[43m*\u001b[49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[36mFile \u001b[39m\u001b[32m~\\anaconda3\\envs\\torch_env\\Lib\\site-packages\\langchain_classic\\chains\\base.py:413\u001b[39m, in \u001b[36mChain.__call__\u001b[39m\u001b[34m(self, inputs, return_only_outputs, callbacks, tags, metadata, run_name, include_run_info)\u001b[39m\n\u001b[32m 380\u001b[39m \u001b[38;5;250m\u001b[39m\u001b[33;03m\"\"\"Execute the chain.\u001b[39;00m\n\u001b[32m 381\u001b[39m \n\u001b[32m 382\u001b[39m \u001b[33;03mArgs:\u001b[39;00m\n\u001b[32m (...)\u001b[39m\u001b[32m 404\u001b[39m \u001b[33;03m `Chain.output_keys`.\u001b[39;00m\n\u001b[32m 405\u001b[39m \u001b[33;03m\"\"\"\u001b[39;00m\n\u001b[32m 406\u001b[39m config = {\n\u001b[32m 407\u001b[39m \u001b[33m\"\u001b[39m\u001b[33mcallbacks\u001b[39m\u001b[33m\"\u001b[39m: callbacks,\n\u001b[32m 408\u001b[39m \u001b[33m\"\u001b[39m\u001b[33mtags\u001b[39m\u001b[33m\"\u001b[39m: tags,\n\u001b[32m 409\u001b[39m \u001b[33m\"\u001b[39m\u001b[33mmetadata\u001b[39m\u001b[33m\"\u001b[39m: metadata,\n\u001b[32m 410\u001b[39m \u001b[33m\"\u001b[39m\u001b[33mrun_name\u001b[39m\u001b[33m\"\u001b[39m: run_name,\n\u001b[32m 411\u001b[39m }\n\u001b[32m--> \u001b[39m\u001b[32m413\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43minvoke\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 414\u001b[39m \u001b[43m \u001b[49m\u001b[43minputs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 415\u001b[39m \u001b[43m \u001b[49m\u001b[43mcast\u001b[49m\u001b[43m(\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mRunnableConfig\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m{\u001b[49m\u001b[43mk\u001b[49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mv\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mk\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mv\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[43m.\u001b[49m\u001b[43mitems\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mif\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mv\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01mis\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;129;43;01mnot\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m}\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 416\u001b[39m \u001b[43m \u001b[49m\u001b[43mreturn_only_outputs\u001b[49m\u001b[43m=\u001b[49m\u001b[43mreturn_only_outputs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 417\u001b[39m \u001b[43m \u001b[49m\u001b[43minclude_run_info\u001b[49m\u001b[43m=\u001b[49m\u001b[43minclude_run_info\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 418\u001b[39m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[36mFile \u001b[39m\u001b[32m~\\anaconda3\\envs\\torch_env\\Lib\\site-packages\\langchain_classic\\chains\\base.py:167\u001b[39m, in \u001b[36mChain.invoke\u001b[39m\u001b[34m(self, input, config, **kwargs)\u001b[39m\n\u001b[32m 164\u001b[39m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[32m 165\u001b[39m \u001b[38;5;28mself\u001b[39m._validate_inputs(inputs)\n\u001b[32m 166\u001b[39m outputs = (\n\u001b[32m--> \u001b[39m\u001b[32m167\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43m_call\u001b[49m\u001b[43m(\u001b[49m\u001b[43minputs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrun_manager\u001b[49m\u001b[43m=\u001b[49m\u001b[43mrun_manager\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 168\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m new_arg_supported\n\u001b[32m 169\u001b[39m \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;28mself\u001b[39m._call(inputs)\n\u001b[32m 170\u001b[39m )\n\u001b[32m 172\u001b[39m final_outputs: \u001b[38;5;28mdict\u001b[39m[\u001b[38;5;28mstr\u001b[39m, Any] = \u001b[38;5;28mself\u001b[39m.prep_outputs(\n\u001b[32m 173\u001b[39m inputs,\n\u001b[32m 174\u001b[39m outputs,\n\u001b[32m 175\u001b[39m return_only_outputs,\n\u001b[32m 176\u001b[39m )\n\u001b[32m 177\u001b[39m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mBaseException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n", + "\u001b[36mFile \u001b[39m\u001b[32m~\\anaconda3\\envs\\torch_env\\Lib\\site-packages\\langchain_classic\\chains\\router\\base.py:116\u001b[39m, in \u001b[36mMultiRouteChain._call\u001b[39m\u001b[34m(self, inputs, run_manager)\u001b[39m\n\u001b[32m 114\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m.default_chain(route.next_inputs, callbacks=callbacks)\n\u001b[32m 115\u001b[39m msg = \u001b[33mf\u001b[39m\u001b[33m\"\u001b[39m\u001b[33mReceived invalid destination chain name \u001b[39m\u001b[33m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mroute.destination\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m'\u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m--> \u001b[39m\u001b[32m116\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(msg)\n", + "\u001b[31mValueError\u001b[39m: Received invalid destination chain name 'history'" + ] + } + ], + "source": [ + "questions = [\n", + " \"Why is the sky blue at noon but red at sunset?\", # physics\n", + " \"What is the derivative of x^3 * ln(x)?\", # math\n", + " \"Why did the Bronze Age collapse happen so suddenly?\", # history\n", + " \"Explain the difference between a stack and a heap.\", # computer science\n", + " \"How do CRISPR-Cas9 systems achieve target specificity?\", # biology\n", + " \"Why is water a liquid at room temperature but H2S a gas?\", # chemistry\n", + " \"What is the Jevons paradox?\", # economics\n", + " \"What should I make for dinner tonight?\", # → default_chain\n", + "]\n", + "\n", + "for q in questions:\n", + " print(\"=\" * 70)\n", + " print(\"Q:\", q)\n", + " print(chain.run(q))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4ea3fc39-ecff-439b-87ec-b22cb48bd219", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a8b2c207-5ae0-4b56-84d4-439afead0f1d", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3.11 (torch)", "language": "python", - "name": "python3" + "name": "torch_env" }, "language_info": { "codemirror_mode": { @@ -720,7 +1153,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.8" + "version": "3.11.15" } }, "nbformat": 4,