diff --git a/lab-chains-in-langchain.ipynb b/lab-chains-in-langchain.ipynb
index 20763f5..2bc8d62 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,12 +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('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": null,
+ "execution_count": 4,
"id": "974acf8e-8f88-42de-88f8-40a82cb58e8b",
"metadata": {
"tags": []
@@ -72,7 +72,7 @@
},
{
"cell_type": "code",
- "execution_count": 15,
+ "execution_count": 5,
"id": "b7a09c35",
"metadata": {
"tags": []
@@ -126,23 +126,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": 5,
"metadata": {},
"output_type": "execute_result"
}
@@ -161,17 +161,17 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 6,
"id": "427e1119",
"metadata": {},
"outputs": [],
"source": [
- "!pip install langchain_community"
+ "#!pip install langchain_community"
]
},
{
"cell_type": "code",
- "execution_count": 17,
+ "execution_count": 7,
"id": "e92dff22",
"metadata": {
"tags": []
@@ -185,33 +185,40 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 8,
"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": 9,
"id": "cdcdb42d",
"metadata": {},
"outputs": [],
"source": [
- "prompt = ChatPromptTemplate.from_template( #Write a query that would take a variable to describe any product\n",
- " \n",
- ")"
+ "prompt = ChatPromptTemplate.from_template('Describe the product: {name}')"
]
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 10,
"id": "d7abc20b",
"metadata": {},
- "outputs": [],
+ "outputs": [
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "C:\\Users\\kriti\\AppData\\Local\\Temp\\ipykernel_42036\\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 +226,31 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 11,
"id": "ad44d1fb",
"metadata": {},
- "outputs": [],
+ "outputs": [
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "C:\\Users\\kriti\\AppData\\Local\\Temp\\ipykernel_42036\\2227295313.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": [
+ "\"A waterproof phone pouch is a protective case or sleeve that is designed to keep your phone safe and dry during water-based activities or in wet conditions. It typically features a secure seal or closure mechanism to keep water out, as well as transparent material that allows you to still use your phone's touchscreen and camera while it is inside the pouch. This product is perfect for taking your phone with you while swimming, boating, or participating in other water sports, as it provides an extra layer of protection against water damage.\""
+ ]
+ },
+ "execution_count": 11,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
"source": [
- "product = #Select a product type to be describe\n",
+ "product = df.iloc[1,0]\n",
"chain.run(product)"
]
},
@@ -238,7 +264,7 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 12,
"id": "febee243",
"metadata": {},
"outputs": [],
@@ -248,7 +274,7 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 13,
"id": "2f31aa8a",
"metadata": {},
"outputs": [],
@@ -257,7 +283,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",
+ " 'Describe the product: {name}'\n",
")\n",
"\n",
"# Chain 1\n",
@@ -266,7 +292,7 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 14,
"id": "3f5d5b76",
"metadata": {},
"outputs": [],
@@ -274,7 +300,7 @@
"\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 short, catchy slogan for product based on following description: {description}'\n",
")\n",
"# chain 2\n",
"chain_two = LLMChain(llm=llm, prompt=second_prompt)"
@@ -282,7 +308,7 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 15,
"id": "6c1eb2c4",
"metadata": {},
"outputs": [],
@@ -294,10 +320,38 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 16,
"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;3mThe waterproof phone pouch is a protective case for your phone that is designed to keep it safe and dry in wet or water-based environments. It features a transparent, touch-sensitive front cover that allows you to use your phone while it is inside the pouch. The pouch is durable and versatile, making it suitable for a variety of outdoor activities such as swimming, snorkeling, or boating. It also includes a secure locking mechanism to ensure that your phone stays safely inside the pouch at all times.\u001b[0m\n",
+ "\u001b[33;1m\u001b[1;3m\"Keep your phone safe and dry with our waterproof phone pouch - the ultimate protection for all your wet adventures!\"\u001b[0m\n",
+ "\n",
+ "\u001b[1m> Finished chain.\u001b[0m\n"
+ ]
+ },
+ {
+ "data": {
+ "text/plain": [
+ "'\"Keep your phone safe and dry with our waterproof phone pouch - the ultimate protection for all your wet adventures!\"'"
+ ]
+ },
+ "execution_count": 16,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
"source": [
"overall_simple_chain.run(product)"
]
@@ -310,6 +364,64 @@
"**Repeat the above twice for different products**"
]
},
+ {
+ "cell_type": "code",
+ "execution_count": 17,
+ "id": "7d41ab04",
+ "metadata": {},
+ "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;3mThe plastic bottle recycler is a machine designed to collect, crush, and process plastic bottles for recycling. It typically consists of a bin or chute for collecting bottles, a crushing mechanism, and a system for sorting and processing the crushed plastic for recycling. The machine is often used in public spaces such as parks, schools, or shopping centers to encourage individuals to recycle their plastic bottles and reduce waste.\u001b[0m\n",
+ "\u001b[33;1m\u001b[1;3m\"Crush, Collect, Recycle: Turn plastic bottles into a sustainable future with our plastic bottle recycler!\"\u001b[0m\n",
+ "\n",
+ "\u001b[1m> Finished chain.\u001b[0m\n",
+ "\"Crush, Collect, Recycle: Turn plastic bottles into a sustainable future with our plastic bottle recycler!\"\n"
+ ]
+ }
+ ],
+ "source": [
+ "print(overall_simple_chain.run('Plastic bottle recycler'))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 18,
+ "id": "0b31dac3",
+ "metadata": {},
+ "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;3mChocolate cookies are sweet, delicious treats made from a mixture of flour, sugar, chocolate chips or cocoa powder, butter, and eggs. They have a rich, chocolatey flavor and a soft, chewy texture that is perfect for satisfying cravings for something sweet. These cookies are often enjoyed as a snack or dessert and are a popular choice for parties, gatherings, or just as a special treat for yourself.\u001b[0m\n",
+ "\u001b[33;1m\u001b[1;3m\"Indulge in the rich, chocolatey goodness of our delectable cookies!\"\u001b[0m\n",
+ "\n",
+ "\u001b[1m> Finished chain.\u001b[0m\n",
+ "\"Indulge in the rich, chocolatey goodness of our delectable cookies!\"\n"
+ ]
+ }
+ ],
+ "source": [
+ "print(overall_simple_chain.run('Chocolate cookies'))"
+ ]
+ },
{
"cell_type": "markdown",
"id": "7b5ce18c",
@@ -320,7 +432,7 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 19,
"id": "4c129ef6",
"metadata": {},
"outputs": [],
@@ -330,7 +442,7 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 20,
"id": "016187ac",
"metadata": {},
"outputs": [],
@@ -339,50 +451,50 @@
"\n",
"\n",
"first_prompt = ChatPromptTemplate.from_template(\n",
- " #This prompt should translate a review\n",
+ " 'Translate the following review to German: [{review}]'\n",
")\n",
"\n",
"chain_one = LLMChain(llm=llm, prompt=first_prompt, \n",
- " output_key=None #Give a name to your output\n",
+ " output_key='translation'\n",
" )\n"
]
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 21,
"id": "0fb0730e",
"metadata": {},
"outputs": [],
"source": [
"second_prompt = ChatPromptTemplate.from_template(\n",
- " #Write a promplt to summarize a review\n",
+ " 'Summarize the following in original language: [{translation}]'\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": 22,
"id": "6accf92d",
"metadata": {},
"outputs": [],
"source": [
"# prompt template 3: translate to english or other language\n",
"third_prompt = ChatPromptTemplate.from_template(\n",
- " None\n",
+ " 'Translate the text to english: [{summary}]'\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='summary_eng'\n",
" )\n"
]
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 23,
"id": "c7a46121",
"metadata": {},
"outputs": [],
@@ -390,16 +502,16 @@
"\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 short follow up message to customer based on the review summary: {summary_eng}. Original summary is [{summary}]'\n",
")\n",
"chain_four = LLMChain(llm=llm, prompt=fourth_prompt,\n",
- " output_key=None\n",
+ " output_key='follow_up'\n",
" )\n"
]
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 24,
"id": "89603117",
"metadata": {},
"outputs": [],
@@ -408,18 +520,50 @@
"# 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=['translation', 'summary', 'summary_eng', 'follow_up'],\n",
" verbose=True\n",
")"
]
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 25,
"id": "51b04f45",
"metadata": {},
- "outputs": [],
+ "outputs": [
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "C:\\Users\\kriti\\AppData\\Local\\Temp\\ipykernel_42036\\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",
+ " 'translation': 'Ich finde den Geschmack schlecht. Der Schaum hält nicht, das ist seltsam. Ich kaufe dieselben im Laden und der Geschmack ist viel besser... Alter Batch oder Fälschung!?',\n",
+ " 'summary': 'Die Person findet den Geschmack schlecht und bemerkt, dass der Schaum nicht lange hält, was seltsam ist. Sie hat das gleiche Produkt im Laden gekauft und der Geschmack war viel besser. Jetzt fragt sie sich, ob es sich um einen alten Batch handelt oder um eine Fälschung.',\n",
+ " 'summary_eng': \"The person finds the taste bad and notices that the foam doesn't last long, which is strange. She bought the same product in the store and the taste was much better. Now she wonders if it's an old batch or a fake.\",\n",
+ " 'follow_up': 'Dear customer,\\n\\nThank you for providing feedback on your recent purchase. We are sorry to hear that you found the taste of the product to be bad and that the foam did not last long. We assure you that we take the quality of our products very seriously and would like to investigate this further. Please reach out to our customer service team with more details so we can address this issue promptly. Thank you for bringing this to our attention.\\n\\nBest regards, \\n[Your Company Name]'}"
+ ]
+ },
+ "execution_count": 25,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
"source": [
"review = df.Review[5]\n",
"overall_chain(review)"
@@ -433,6 +577,88 @@
"**Repeat the above twice for different products or reviews**"
]
},
+ {
+ "cell_type": "code",
+ "execution_count": 26,
+ "id": "1890ae82",
+ "metadata": {},
+ "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": [
+ "\n",
+ "\u001b[1m> Finished chain.\u001b[0m\n"
+ ]
+ },
+ {
+ "data": {
+ "text/plain": [
+ "{'review': 'Está lu bonita calienta muy rápido, es muy funcional, solo falta ver cuánto dura, solo llevo 3 días en funcionamiento.',\n",
+ " 'translation': 'Sie ist sehr hübsch, heizt sehr schnell auf, ist sehr funktional, es bleibt nur noch abzuwarten, wie lange sie hält, ich habe sie erst seit 3 Tagen in Betrieb.',\n",
+ " 'summary': 'Die Person beschreibt eine Sache als sehr hübsch, schnell aufheizend und funktional. Es bleibt abzuwarten, wie lange sie hält, da sie erst seit 3 Tagen in Betrieb ist.',\n",
+ " 'summary_eng': 'The person describes something as very pretty, heating up quickly, and functional. It remains to be seen how long it will last, as it has only been in operation for 3 days.',\n",
+ " 'follow_up': \"Thank you for your review! We're glad to hear that you find the item very pretty, efficient at heating up quickly, and functional. We hope that it continues to meet your expectations in the long run. If you have any issues or concerns in the future, please don't hesitate to reach out to us. Thank you for choosing our product!\"}"
+ ]
+ },
+ "execution_count": 26,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "review = df.Review[6]\n",
+ "overall_chain(review)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 27,
+ "id": "a7da5471",
+ "metadata": {},
+ "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": [
+ "\n",
+ "\u001b[1m> Finished chain.\u001b[0m\n"
+ ]
+ },
+ {
+ "data": {
+ "text/plain": [
+ "{'review': \"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.\",\n",
+ " 'translation': 'Diese Matratze hatte ein kleines Loch oben drauf (hatte eine Ewigkeit gedauert, um herauszufinden, wo es war), und die mitgelieferten Patches haben nicht funktioniert, vielleicht weil es sich um die Oberseite der Matratze handelt, wo es wie Stoff ist und ein Patch nicht haften bleibt. Vielleicht hatte ich einfach Pech mit einer defekten Matratze, aber wo ist die Qualitätskontrolle bei diesem Unternehmen? Das sollte einfach nicht passieren. Betonung auf flach. Denn das war die Matratze. Wirklich schreckliche Erfahrung, hat den Aufenthalt meines Freundes bei mir ruiniert. Dann machen sie dich auch noch dazu, sie zurückzuschicken, anstatt einfach eine Rückerstattung anzubieten, was auch super nervig ist, eine Luftmatratze einpacken und zur UPS-Filiale bringen zu müssen. Dieses Unternehmen ist das Schlimmste, und diese Matratze ist das Schlimmste.',\n",
+ " 'summary': 'Die Matratze hatte ein Loch oben drauf, die mitgelieferten Patches haben nicht funktioniert, und die Qualität scheint mangelhaft zu sein. Es war eine schreckliche Erfahrung und hat den Aufenthalt eines Freundes ruiniert. Das Unternehmen fordert sogar, die Matratze zurückzuschicken, anstatt eine Rückerstattung anzubieten, was sehr ärgerlich ist. Es scheint, dass die Qualitätskontrolle bei diesem Unternehmen fehlt.',\n",
+ " 'summary_eng': \"The mattress had a hole on top, the supplied patches didn't work, and the quality seems to be poor. It was a terrible experience and ruined a friend's stay. The company even demands to return the mattress instead of offering a refund, which is very frustrating. It seems that this company lacks quality control.\",\n",
+ " 'follow_up': 'Dear valued customer,\\n\\nWe are truly sorry to hear about your negative experience with our mattress. Your feedback is very important to us, and we apologize for any inconvenience this may have caused. We will investigate the issue further to ensure that it does not occur again in the future. Please contact our customer service team to discuss the return process and explore possible solutions. Thank you for bringing this to our attention.\\n\\nSincerely,\\n[Company Name] Customer Service Team'}"
+ ]
+ },
+ "execution_count": 27,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "review = df.Review[2]\n",
+ "overall_chain(review)"
+ ]
+ },
{
"cell_type": "markdown",
"id": "3041ea4c",
@@ -443,7 +669,7 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 28,
"id": "ade83f4f",
"metadata": {},
"outputs": [],
@@ -509,7 +735,7 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 29,
"id": "5f590e9f",
"metadata": {},
"outputs": [],
@@ -526,7 +752,7 @@
" \"prompt_template\": math_template\n",
" },\n",
" {\n",
- " \"name\": \"History\", \n",
+ " \"name\": \"history\", \n",
" \"description\": \"Good for answering history questions\", \n",
" \"prompt_template\": history_template\n",
" },\n",
@@ -545,7 +771,7 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 30,
"id": "31b06fc8",
"metadata": {},
"outputs": [],
@@ -557,7 +783,7 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 31,
"id": "f3f50bcc",
"metadata": {},
"outputs": [],
@@ -567,7 +793,7 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 32,
"id": "8eefec24",
"metadata": {},
"outputs": [],
@@ -586,7 +812,7 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 33,
"id": "9f98018a",
"metadata": {},
"outputs": [],
@@ -597,7 +823,7 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 34,
"id": "11b2e2ba",
"metadata": {},
"outputs": [],
@@ -635,7 +861,7 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 35,
"id": "1387109d",
"metadata": {},
"outputs": [],
@@ -654,10 +880,19 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 36,
"id": "2fb7d560",
"metadata": {},
- "outputs": [],
+ "outputs": [
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "C:\\Users\\kriti\\AppData\\Local\\Temp\\ipykernel_42036\\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,30 +902,108 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 37,
"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": [
+ "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 raised to the fifth power.\""
+ ]
+ },
+ "execution_count": 37,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
"source": [
"chain.run(\"What is black body radiation?\")"
]
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 38,
"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": [
+ "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": 38,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
"source": [
"chain.run(\"what is 2 + 2\")"
]
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 39,
"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": [
+ "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 of cells to changing environmental conditions.\\n\\nIn summary, every cell in our body contains DNA because it is essential for the proper functioning, development, and reproduction of all living organisms.\""
+ ]
+ },
+ "execution_count": 39,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
"source": [
"chain.run(\"Why does every cell in our body contain DNA?\")"
]
@@ -702,11 +1015,119 @@
"source": [
"**Repeat the above at least once for different inputs and chains executions - Be creative!**"
]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 40,
+ "id": "e5120d5b",
+ "metadata": {},
+ "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': 'What is B.C. and A.C in history?'}\n",
+ "\u001b[1m> Finished chain.\u001b[0m\n"
+ ]
+ },
+ {
+ "data": {
+ "text/plain": [
+ "'B.C. stands for \"Before Christ\" and refers to the time period before the birth of Jesus Christ. A.C. stands for \"Anno Domini\" which is Latin for \"In the year of our Lord\" and refers to the time period after the birth of Jesus Christ. These terms are commonly used in historical dating to divide time periods before and after the birth of Christ.'"
+ ]
+ },
+ "execution_count": 40,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "chain.run(\"What is B.C. and A.C?\")"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 41,
+ "id": "7bbd7d22",
+ "metadata": {},
+ "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": [
+ "None: {'input': 'What is recipe for grilled sandwich?'}\n",
+ "\u001b[1m> Finished chain.\u001b[0m\n"
+ ]
+ },
+ {
+ "data": {
+ "text/plain": [
+ "'Ingredients:\\n- 4 slices of bread\\n- 2 tablespoons of butter\\n- 4 slices of cheese (such as cheddar or American)\\n- 4 slices of deli meat (such as ham or turkey)\\n- Optional toppings (such as tomato slices, avocado, or bacon)\\n\\nInstructions:\\n1. Heat a skillet or griddle over medium heat.\\n2. Butter one side of each slice of bread.\\n3. Place two slices of bread, butter side down, on the skillet.\\n4. Layer one slice of cheese, two slices of deli meat, and any optional toppings on each slice of bread.\\n5. Top each sandwich with another slice of bread, butter side up.\\n6. Cook the sandwiches for 3-4 minutes on each side, or until the bread is golden brown and the cheese is melted.\\n7. Remove the sandwiches from the skillet and let them cool for a minute before slicing and serving. Enjoy your grilled sandwich!'"
+ ]
+ },
+ "execution_count": 41,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "chain.run(\"What is recipe for grilled sandwich?\")"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 42,
+ "id": "fc218391",
+ "metadata": {},
+ "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 Data Scientist and Data Engineer'}\n",
+ "\u001b[1m> Finished chain.\u001b[0m\n"
+ ]
+ },
+ {
+ "data": {
+ "text/plain": [
+ "'Data scientists and data engineers both work with data, but they have different roles and responsibilities within an organization.\\n\\nData scientists are responsible for analyzing and interpreting complex data sets to inform business decisions. They use statistical analysis, machine learning, and data visualization techniques to extract insights from data. Data scientists often have a strong background in mathematics, statistics, and computer science.\\n\\nOn the other hand, data engineers are responsible for designing, building, and maintaining the infrastructure that allows data scientists to do their work. They are experts in data storage, processing, and retrieval, and they work with tools like databases, data pipelines, and cloud computing platforms. Data engineers often have a strong background in software engineering and computer systems.\\n\\nIn summary, data scientists focus on analyzing data to extract insights, while data engineers focus on building and maintaining the infrastructure that supports data analysis. Both roles are essential for organizations to effectively leverage data for decision-making.'"
+ ]
+ },
+ "execution_count": 42,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "chain.run(\"What is difference between Data Scientist and Data Engineer\")"
+ ]
}
],
"metadata": {
"kernelspec": {
- "display_name": "Python 3 (ipykernel)",
+ "display_name": "langchain-v0.2",
"language": "python",
"name": "python3"
},
@@ -720,7 +1141,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.11.8"
+ "version": "3.13.14"
}
},
"nbformat": 4,