From 7c3f6823b9664c9928d4a54e151acac28a793914 Mon Sep 17 00:00:00 2001 From: David Tayebwa Date: Mon, 27 Jul 2026 17:33:57 +0200 Subject: [PATCH 1/2] completed lab-chains-in-langchain --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2eea525 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.env \ No newline at end of file From 08d240c419de91601a7dc27e4566f339c9cc9335 Mon Sep 17 00:00:00 2001 From: David Tayebwa Date: Mon, 27 Jul 2026 17:40:30 +0200 Subject: [PATCH 2/2] Completed lab-chains-in-lanchain --- lab-chains-in-langchain.ipynb | 414 +++++++++++++++++++++++++++------- 1 file changed, 333 insertions(+), 81 deletions(-) diff --git a/lab-chains-in-langchain.ipynb b/lab-chains-in-langchain.ipynb index 290f3bb..6e70f55 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": [] @@ -41,13 +41,13 @@ "from dotenv import load_dotenv, find_dotenv\n", "_ = load_dotenv(find_dotenv())\n", "\n", - "OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')\n", + "OPENAI_API_KEY = os.getenv('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')\n", "HUGGINGFACEHUB_API_TOKEN = os.getenv('HUGGINGFACEHUB_API_TOKEN')" ] }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 3, "id": "b84e441b", "metadata": { "tags": [] @@ -59,7 +59,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 4, "id": "974acf8e-8f88-42de-88f8-40a82cb58e8b", "metadata": { "tags": [] @@ -67,12 +67,12 @@ "outputs": [], "source": [ "import pandas as pd\n", - "df = pd.read_csv('./lab/data/Data.csv')" + "df = pd.read_csv('./data/Data.csv')" ] }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 5, "id": "b7a09c35", "metadata": { "tags": [] @@ -126,7 +126,7 @@ " \n", " \n", " 4\n", - " Milk Frother Handheld\\n\n", + " Milk Frother Handheld\\r\\n\n", " I loved this product. But they only seem to l...\n", " \n", " \n", @@ -134,15 +134,22 @@ "" ], "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 \\\n", + "0 Queen Size Sheet Set \n", + "1 Waterproof Phone Pouch \n", + "2 Luxury Air Mattress \n", + "3 Pillows Insert \n", + "4 Milk Frother Handheld\\r\\n \n", + "\n", + " Review \n", + "0 I ordered a king size set. My only criticism w... \n", + "1 I loved the waterproof sac, although the openi... \n", + "2 This mattress had a small hole in the top of i... \n", + "3 This is the best throw pillow fillers on Amazo... \n", + "4  I loved this product. But they only seem to l... " ] }, - "execution_count": 15, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -161,17 +168,65 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "id": "427e1119", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Requirement already satisfied: langchain_community in C:\\Users\\Gebruiker\\Desktop\\Backup\\.venv\\langchain-v0.2.x\\Lib\\site-packages (0.2.19)\n", + "Requirement already satisfied: PyYAML>=5.3 in C:\\Users\\Gebruiker\\Desktop\\Backup\\.venv\\langchain-v0.2.x\\Lib\\site-packages (from langchain_community) (6.0.3)\n", + "Requirement already satisfied: SQLAlchemy<3,>=1.4 in C:\\Users\\Gebruiker\\Desktop\\Backup\\.venv\\langchain-v0.2.x\\Lib\\site-packages (from langchain_community) (2.0.51)\n", + "Requirement already satisfied: aiohttp<4.0.0,>=3.8.3 in C:\\Users\\Gebruiker\\Desktop\\Backup\\.venv\\langchain-v0.2.x\\Lib\\site-packages (from langchain_community) (3.14.3)\n", + "Requirement already satisfied: dataclasses-json<0.7,>=0.5.7 in C:\\Users\\Gebruiker\\Desktop\\Backup\\.venv\\langchain-v0.2.x\\Lib\\site-packages (from langchain_community) (0.6.7)\n", + "Requirement already satisfied: langchain<0.3.0,>=0.2.17 in C:\\Users\\Gebruiker\\Desktop\\Backup\\.venv\\langchain-v0.2.x\\Lib\\site-packages (from langchain_community) (0.2.17)\n", + "Requirement already satisfied: langchain-core<0.3.0,>=0.2.43 in C:\\Users\\Gebruiker\\Desktop\\Backup\\.venv\\langchain-v0.2.x\\Lib\\site-packages (from langchain_community) (0.2.43)\n", + "Requirement already satisfied: langsmith<0.2.0,>=0.1.112 in C:\\Users\\Gebruiker\\Desktop\\Backup\\.venv\\langchain-v0.2.x\\Lib\\site-packages (from langchain_community) (0.1.147)\n", + "Requirement already satisfied: numpy<2,>=1 in C:\\Users\\Gebruiker\\Desktop\\Backup\\.venv\\langchain-v0.2.x\\Lib\\site-packages (from langchain_community) (1.26.4)\n", + "Requirement already satisfied: requests<3,>=2 in C:\\Users\\Gebruiker\\Desktop\\Backup\\.venv\\langchain-v0.2.x\\Lib\\site-packages (from langchain_community) (2.34.2)\n", + "Requirement already satisfied: tenacity!=8.4.0,<9.0.0,>=8.1.0 in C:\\Users\\Gebruiker\\Desktop\\Backup\\.venv\\langchain-v0.2.x\\Lib\\site-packages (from langchain_community) (8.5.0)\n", + "Requirement already satisfied: aiohappyeyeballs>=2.5.0 in C:\\Users\\Gebruiker\\Desktop\\Backup\\.venv\\langchain-v0.2.x\\Lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain_community) (2.7.1)\n", + "Requirement already satisfied: aiosignal>=1.4.0 in C:\\Users\\Gebruiker\\Desktop\\Backup\\.venv\\langchain-v0.2.x\\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\\Gebruiker\\Desktop\\Backup\\.venv\\langchain-v0.2.x\\Lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain_community) (26.1.0)\n", + "Requirement already satisfied: frozenlist>=1.1.1 in C:\\Users\\Gebruiker\\Desktop\\Backup\\.venv\\langchain-v0.2.x\\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\\Gebruiker\\Desktop\\Backup\\.venv\\langchain-v0.2.x\\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\\Gebruiker\\Desktop\\Backup\\.venv\\langchain-v0.2.x\\Lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain_community) (0.5.2)\n", + "Requirement already satisfied: typing_extensions>=4.4 in C:\\Users\\Gebruiker\\Desktop\\Backup\\.venv\\langchain-v0.2.x\\Lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain_community) (4.16.0)\n", + "Requirement already satisfied: yarl<2.0,>=1.17.0 in C:\\Users\\Gebruiker\\Desktop\\Backup\\.venv\\langchain-v0.2.x\\Lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain_community) (1.24.5)\n", + "Requirement already satisfied: marshmallow<4.0.0,>=3.18.0 in C:\\Users\\Gebruiker\\Desktop\\Backup\\.venv\\langchain-v0.2.x\\Lib\\site-packages (from dataclasses-json<0.7,>=0.5.7->langchain_community) (3.26.2)\n", + "Requirement already satisfied: typing-inspect<1,>=0.4.0 in C:\\Users\\Gebruiker\\Desktop\\Backup\\.venv\\langchain-v0.2.x\\Lib\\site-packages (from dataclasses-json<0.7,>=0.5.7->langchain_community) (0.9.0)\n", + "Requirement already satisfied: langchain-text-splitters<0.3.0,>=0.2.0 in C:\\Users\\Gebruiker\\Desktop\\Backup\\.venv\\langchain-v0.2.x\\Lib\\site-packages (from langchain<0.3.0,>=0.2.17->langchain_community) (0.2.4)\n", + "Requirement already satisfied: pydantic<3,>=1 in C:\\Users\\Gebruiker\\Desktop\\Backup\\.venv\\langchain-v0.2.x\\Lib\\site-packages (from langchain<0.3.0,>=0.2.17->langchain_community) (2.13.4)\n", + "Requirement already satisfied: jsonpatch<2.0,>=1.33 in C:\\Users\\Gebruiker\\Desktop\\Backup\\.venv\\langchain-v0.2.x\\Lib\\site-packages (from langchain-core<0.3.0,>=0.2.43->langchain_community) (1.33)\n", + "Requirement already satisfied: packaging<25,>=23.2 in C:\\Users\\Gebruiker\\Desktop\\Backup\\.venv\\langchain-v0.2.x\\Lib\\site-packages (from langchain-core<0.3.0,>=0.2.43->langchain_community) (24.2)\n", + "Requirement already satisfied: jsonpointer>=1.9 in C:\\Users\\Gebruiker\\Desktop\\Backup\\.venv\\langchain-v0.2.x\\Lib\\site-packages (from jsonpatch<2.0,>=1.33->langchain-core<0.3.0,>=0.2.43->langchain_community) (3.1.1)\n", + "Requirement already satisfied: httpx<1,>=0.23.0 in C:\\Users\\Gebruiker\\Desktop\\Backup\\.venv\\langchain-v0.2.x\\Lib\\site-packages (from langsmith<0.2.0,>=0.1.112->langchain_community) (0.28.1)\n", + "Requirement already satisfied: orjson<4.0.0,>=3.9.14 in C:\\Users\\Gebruiker\\Desktop\\Backup\\.venv\\langchain-v0.2.x\\Lib\\site-packages (from langsmith<0.2.0,>=0.1.112->langchain_community) (3.11.9)\n", + "Requirement already satisfied: requests-toolbelt<2.0.0,>=1.0.0 in C:\\Users\\Gebruiker\\Desktop\\Backup\\.venv\\langchain-v0.2.x\\Lib\\site-packages (from langsmith<0.2.0,>=0.1.112->langchain_community) (1.0.0)\n", + "Requirement already satisfied: anyio in C:\\Users\\Gebruiker\\Desktop\\Backup\\.venv\\langchain-v0.2.x\\Lib\\site-packages (from httpx<1,>=0.23.0->langsmith<0.2.0,>=0.1.112->langchain_community) (4.14.2)\n", + "Requirement already satisfied: certifi in C:\\Users\\Gebruiker\\Desktop\\Backup\\.venv\\langchain-v0.2.x\\Lib\\site-packages (from httpx<1,>=0.23.0->langsmith<0.2.0,>=0.1.112->langchain_community) (2026.7.22)\n", + "Requirement already satisfied: httpcore==1.* in C:\\Users\\Gebruiker\\Desktop\\Backup\\.venv\\langchain-v0.2.x\\Lib\\site-packages (from httpx<1,>=0.23.0->langsmith<0.2.0,>=0.1.112->langchain_community) (1.0.9)\n", + "Requirement already satisfied: idna in C:\\Users\\Gebruiker\\Desktop\\Backup\\.venv\\langchain-v0.2.x\\Lib\\site-packages (from httpx<1,>=0.23.0->langsmith<0.2.0,>=0.1.112->langchain_community) (3.18)\n", + "Requirement already satisfied: h11>=0.16 in C:\\Users\\Gebruiker\\Desktop\\Backup\\.venv\\langchain-v0.2.x\\Lib\\site-packages (from httpcore==1.*->httpx<1,>=0.23.0->langsmith<0.2.0,>=0.1.112->langchain_community) (0.16.0)\n", + "Requirement already satisfied: annotated-types>=0.6.0 in C:\\Users\\Gebruiker\\Desktop\\Backup\\.venv\\langchain-v0.2.x\\Lib\\site-packages (from pydantic<3,>=1->langchain<0.3.0,>=0.2.17->langchain_community) (0.8.0)\n", + "Requirement already satisfied: pydantic-core==2.46.4 in C:\\Users\\Gebruiker\\Desktop\\Backup\\.venv\\langchain-v0.2.x\\Lib\\site-packages (from pydantic<3,>=1->langchain<0.3.0,>=0.2.17->langchain_community) (2.46.4)\n", + "Requirement already satisfied: typing-inspection>=0.4.2 in C:\\Users\\Gebruiker\\Desktop\\Backup\\.venv\\langchain-v0.2.x\\Lib\\site-packages (from pydantic<3,>=1->langchain<0.3.0,>=0.2.17->langchain_community) (0.4.2)\n", + "Requirement already satisfied: charset_normalizer<4,>=2 in C:\\Users\\Gebruiker\\Desktop\\Backup\\.venv\\langchain-v0.2.x\\Lib\\site-packages (from requests<3,>=2->langchain_community) (3.4.9)\n", + "Requirement already satisfied: urllib3<3,>=1.26 in C:\\Users\\Gebruiker\\Desktop\\Backup\\.venv\\langchain-v0.2.x\\Lib\\site-packages (from requests<3,>=2->langchain_community) (2.7.0)\n", + "Requirement already satisfied: greenlet>=1 in C:\\Users\\Gebruiker\\Desktop\\Backup\\.venv\\langchain-v0.2.x\\Lib\\site-packages (from SQLAlchemy<3,>=1.4->langchain_community) (3.5.4)\n", + "Requirement already satisfied: mypy-extensions>=0.3.0 in C:\\Users\\Gebruiker\\Desktop\\Backup\\.venv\\langchain-v0.2.x\\Lib\\site-packages (from typing-inspect<1,>=0.4.0->dataclasses-json<0.7,>=0.5.7->langchain_community) (1.1.0)\n" + ] + } + ], "source": [ "!pip install langchain_community" ] }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 7, "id": "e92dff22", "metadata": { "tags": [] @@ -185,33 +240,64 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, + "id": "999c5e25", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from dotenv import load_dotenv\n", + "load_dotenv()" + ] + }, + { + "cell_type": "code", + "execution_count": 9, "id": "943237a7", "metadata": {}, "outputs": [], "source": [ "#Replace None by your own value and justify\n", - "llm = ChatOpenAI(temperature=None)\n" + "llm = ChatOpenAI(temperature=0.7)\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "id": "cdcdb42d", "metadata": {}, "outputs": [], "source": [ - "prompt = ChatPromptTemplate.from_template( #Write a query that would take a variable to describe any product\n", + "prompt = ChatPromptTemplate.from_template(\"Write a short, catchy product description for a {product}.\"\n", " \n", ")" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "id": "d7abc20b", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\Gebruiker\\AppData\\Local\\Temp\\ipykernel_4200\\546483037.py:1: LangChainDeprecationWarning: The class `LLMChain` was deprecated in LangChain 0.1.17 and will be removed in 1.0. Use RunnableSequence, e.g., `prompt | llm` instead.\n", + " chain = LLMChain(llm=llm, prompt=prompt)\n" + ] + } + ], "source": [ "\n", "chain = LLMChain(llm=llm, prompt=prompt)" @@ -219,12 +305,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "id": "ad44d1fb", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\Gebruiker\\AppData\\Local\\Temp\\ipykernel_4200\\2533209733.py:2: LangChainDeprecationWarning: The method `Chain.run` was deprecated in langchain 0.1.0 and will be removed in 1.0. Use invoke instead.\n", + " chain.run(product)\n" + ] + }, + { + "data": { + "text/plain": [ + "'Introducing the sleek and stylish Peugeot 5008 2025 - where innovation meets elegance. With cutting-edge technology and luxurious design, this SUV is the ultimate combination of performance and sophistication. Upgrade your drive and experience the future of automotive excellence with the Peugeot 5008 2025.'" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "product = #Select a product type to be describe\n", + "product = \"A peugeot 5008 2025\"\n", "chain.run(product)" ] }, @@ -238,7 +343,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "id": "febee243", "metadata": {}, "outputs": [], @@ -248,7 +353,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "id": "2f31aa8a", "metadata": {}, "outputs": [], @@ -256,8 +361,8 @@ "llm = ChatOpenAI(temperature=0.9)\n", "\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", + "first_prompt = ChatPromptTemplate.from_template(\"Write a short, catchy product description for a {product}.\"\n", + " \n", ")\n", "\n", "# Chain 1\n", @@ -266,15 +371,15 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "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", + "second_prompt = ChatPromptTemplate.from_template(\"Translate the following product description into French:\\n\\n{text}\"\n", + " \n", ")\n", "# chain 2\n", "chain_two = LLMChain(llm=llm, prompt=second_prompt)" @@ -282,7 +387,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "id": "6c1eb2c4", "metadata": {}, "outputs": [], @@ -294,10 +399,38 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "id": "78458efe", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Error in StdOutCallbackHandler.on_chain_start callback: AttributeError(\"'NoneType' object has no attribute 'get'\")\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[36;1m\u001b[1;3mIntroducing the sleek and stylish Peugeot 5008 2025 - the ultimate blend of luxury and performance. Get ready to turn heads with its futuristic design and advanced technology features. Upgrade your drive and experience the next level of comfort and sophistication. Elevate your journey with the Peugeot 5008 2025.\u001b[0m\n", + "\u001b[33;1m\u001b[1;3mDécouvrez le Peugeot 5008 2025 élégant et stylé - le mariage ultime du luxe et des performances. Préparez-vous à attirer tous les regards avec son design futuriste et ses fonctionnalités technologiques avancées. Améliorez votre conduite et vivez le confort et la sophistication au niveau supérieur. Élevez votre voyage avec le Peugeot 5008 2025.\u001b[0m\n", + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n" + ] + }, + { + "data": { + "text/plain": [ + "'Découvrez le Peugeot 5008 2025 élégant et stylé - le mariage ultime du luxe et des performances. Préparez-vous à attirer tous les regards avec son design futuriste et ses fonctionnalités technologiques avancées. Améliorez votre conduite et vivez le confort et la sophistication au niveau supérieur. Élevez votre voyage avec le Peugeot 5008 2025.'" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "overall_simple_chain.run(product)" ] @@ -320,7 +453,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "id": "4c129ef6", "metadata": {}, "outputs": [], @@ -330,7 +463,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "id": "016187ac", "metadata": {}, "outputs": [], @@ -338,68 +471,69 @@ "llm = ChatOpenAI(temperature=0.9)\n", "\n", "\n", - "first_prompt = ChatPromptTemplate.from_template(\n", - " #This prompt should translate a review\n", + "first_prompt = ChatPromptTemplate.from_template(\"Translate the following review to English\"\n", + " \n", ")\n", "\n", "chain_one = LLMChain(llm=llm, prompt=first_prompt, \n", - " output_key=None #Give a name to your output\n", + " output_key=\"English_review\"\n", " )\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "id": "0fb0730e", "metadata": {}, "outputs": [], "source": [ - "second_prompt = ChatPromptTemplate.from_template(\n", - " #Write a promplt to summarize a review\n", + "second_prompt = ChatPromptTemplate.from_template(\"Summarize the following review in one sentence:\\n\\n{English_review}\"\n", + " \n", ")\n", "\n", "chain_two = LLMChain(llm=llm, prompt=second_prompt, \n", - " output_key=None #give a name to this output\n", + " output_key=\"summary\"\n", " )\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "id": "6accf92d", "metadata": {}, "outputs": [], "source": [ "# prompt template 3: translate to english or other language\n", - "third_prompt = ChatPromptTemplate.from_template(\n", - " None\n", + "third_prompt = ChatPromptTemplate.from_template(\"What language is the following review in?\\n\\n{review}\"\n", + " \n", ")\n", "# chain 3: input= Review and output= language\n", "chain_three = LLMChain(llm=llm, prompt=third_prompt,\n", - " output_key=None\n", + " output_key=\"language\"\n", " )\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "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", + "fourth_prompt = ChatPromptTemplate.from_template(\"Write a follow up response to this summary in the specified language.\\n\\n\"\n", + " \"Summary: {summary}\\n\\nLanguage:{language}\"\n", + " \n", ")\n", "chain_four = LLMChain(llm=llm, prompt=fourth_prompt,\n", - " output_key=None\n", + " output_key=\"followup_message\"\n", " )\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "id": "89603117", "metadata": {}, "outputs": [], @@ -408,18 +542,49 @@ "# 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", ")" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "id": "51b04f45", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\Gebruiker\\AppData\\Local\\Temp\\ipykernel_4200\\1992003631.py:2: LangChainDeprecationWarning: The method `Chain.__call__` was deprecated in langchain 0.1.0 and will be removed in 1.0. Use invoke instead.\n", + " overall_chain(review)\n", + "Error in StdOutCallbackHandler.on_chain_start callback: AttributeError(\"'NoneType' object has no attribute 'get'\")\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\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': '\"Este restaurante es simplemente increíble. La comida es deliciosa y el servicio es excelente. Recomiendo el ceviche y el lomo saltado. ¡Definitivamente volveré pronto!\"',\n", + " 'summary': 'El restaurante es increíble con deliciosa comida y excelente servicio, se recomienda el ceviche y el lomo saltado, y el revisor planea volver pronto.',\n", + " 'followup_message': \"Je suis ravi d'entendre que vous avez apprécié votre expérience au restaurant! Le ceviche et le lomo saltado sont en effet des plats délicieux. J'espère que vous reviendrez bientôt pour en profiter à nouveau. Bon appétit!\"}" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "review = df.Review[5]\n", "overall_chain(review)" @@ -443,7 +608,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "id": "ade83f4f", "metadata": {}, "outputs": [], @@ -509,7 +674,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "id": "5f590e9f", "metadata": {}, "outputs": [], @@ -545,7 +710,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "id": "31b06fc8", "metadata": {}, "outputs": [], @@ -557,7 +722,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 28, "id": "f3f50bcc", "metadata": {}, "outputs": [], @@ -567,7 +732,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "id": "8eefec24", "metadata": {}, "outputs": [], @@ -586,7 +751,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 30, "id": "9f98018a", "metadata": {}, "outputs": [], @@ -597,7 +762,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 31, "id": "11b2e2ba", "metadata": {}, "outputs": [], @@ -635,7 +800,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 32, "id": "1387109d", "metadata": {}, "outputs": [], @@ -654,10 +819,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 33, "id": "2fb7d560", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\Gebruiker\\AppData\\Local\\Temp\\ipykernel_4200\\3038952769.py:1: LangChainDeprecationWarning: Use RunnableLambda to select from multiple prompt templates. See example in API reference: https://api.python.langchain.com/en/latest/chains/langchain.chains.router.multi_prompt.MultiPromptChain.html\n", + " chain = MultiPromptChain(router_chain=router_chain,\n" + ] + } + ], "source": [ "chain = MultiPromptChain(router_chain=router_chain, \n", " destination_chains=destination_chains, \n", @@ -667,32 +841,110 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 34, "id": "d86b2131", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Error in StdOutCallbackHandler.on_chain_start callback: AttributeError(\"'NoneType' object has no attribute 'get'\")\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "History: {'input': 'Who was the first president of Rwanda?'}\n", + "\u001b[1m> Finished chain.\u001b[0m\n" + ] + }, + { + "data": { + "text/plain": [ + "'The first president of Rwanda was Grégoire Kayibanda, who served from 1962 to 1973.'" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "chain.run(\"What is black body radiation?\")" + "chain.run(\"Who was the first president of Rwanda?\")" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 35, "id": "3b717379", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Error in StdOutCallbackHandler.on_chain_start callback: AttributeError(\"'NoneType' object has no attribute 'get'\")\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "physics: {'input': 'How is calculus used in Physics'}\n", + "\u001b[1m> Finished chain.\u001b[0m\n" + ] + }, + { + "data": { + "text/plain": [ + "'Calculus is a fundamental tool in physics that is used to describe and analyze the relationships between quantities such as position, velocity, acceleration, force, and energy. It allows us to understand how these quantities change over time and space.\\n\\nIn physics, calculus is used to solve problems involving motion, forces, energy, and many other phenomena. For example, calculus is used to calculate the velocity and acceleration of an object in motion, to determine the work done by a force, and to analyze the behavior of systems in equilibrium or in motion.\\n\\nOverall, calculus is an essential mathematical tool in physics that helps us to model and understand the natural world around us.'" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "chain.run(\"what is 2 + 2\")" + "chain.run(\"How is calculus used in Physics\")" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 36, "id": "29e5be01", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Error in StdOutCallbackHandler.on_chain_start callback: AttributeError(\"'NoneType' object has no attribute 'get'\")\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "computer science: {'input': 'What is the difference between machine learning and deep learning?'}\n", + "\u001b[1m> Finished chain.\u001b[0m\n" + ] + }, + { + "data": { + "text/plain": [ + "'Machine learning and deep learning are both subsets of artificial intelligence, but they have some key differences.\\n\\nMachine learning is a broader concept that involves algorithms that can learn from and make predictions or decisions based on data. It typically involves training a model on a dataset and then using that model to make predictions on new data. Machine learning algorithms can be categorized into supervised, unsupervised, and reinforcement learning.\\n\\nDeep learning, on the other hand, is a specific type of machine learning that uses neural networks with multiple layers to learn complex patterns in large amounts of data. Deep learning algorithms are able to automatically learn features from the data without the need for manual feature extraction. Deep learning has been particularly successful in tasks such as image and speech recognition.\\n\\nIn summary, deep learning is a subset of machine learning that uses neural networks with multiple layers to learn complex patterns in data, while machine learning is a broader concept that encompasses a variety of algorithms for learning from data.'" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "chain.run(\"Why does every cell in our body contain DNA?\")" + "chain.run(\"What is the difference between machine learning and deep learning?\")" ] }, { @@ -706,9 +958,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python (LangChain 0.2.x)", "language": "python", - "name": "python3" + "name": "langchain-v0.2.x" }, "language_info": { "codemirror_mode": { @@ -720,7 +972,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.8" + "version": "3.11.9" } }, "nbformat": 4,