File size: 27,697 Bytes
b501a68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: huggingface_hub in c:\\users\\ali zain\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (0.24.6)\n",
      "Requirement already satisfied: filelock in c:\\users\\ali zain\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from huggingface_hub) (3.14.0)\n",
      "Requirement already satisfied: fsspec>=2023.5.0 in c:\\users\\ali zain\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from huggingface_hub) (2024.3.1)\n",
      "Requirement already satisfied: packaging>=20.9 in c:\\users\\ali zain\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from huggingface_hub) (23.2)\n",
      "Requirement already satisfied: pyyaml>=5.1 in c:\\users\\ali zain\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from huggingface_hub) (6.0.1)\n",
      "Requirement already satisfied: requests in c:\\users\\ali zain\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from huggingface_hub) (2.31.0)\n",
      "Requirement already satisfied: tqdm>=4.42.1 in c:\\users\\ali zain\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from huggingface_hub) (4.66.2)\n",
      "Requirement already satisfied: typing-extensions>=3.7.4.3 in c:\\users\\ali zain\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from huggingface_hub) (4.11.0)\n",
      "Requirement already satisfied: colorama in c:\\users\\ali zain\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from tqdm>=4.42.1->huggingface_hub) (0.4.6)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in c:\\users\\ali zain\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from requests->huggingface_hub) (3.3.2)\n",
      "Requirement already satisfied: idna<4,>=2.5 in c:\\users\\ali zain\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from requests->huggingface_hub) (3.6)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in c:\\users\\ali zain\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from requests->huggingface_hub) (2.2.0)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in c:\\users\\ali zain\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from requests->huggingface_hub) (2024.2.2)\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "pip install huggingface_hub\n",
    "pip install huggingface_hub"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting langchain_groqNote: you may need to restart the kernel to use updated packages.\n",
      "\n",
      "  Using cached langchain_groq-0.1.9-py3-none-any.whl.metadata (2.9 kB)\n",
      "Collecting groq<1,>=0.4.1 (from langchain_groq)\n",
      "  Using cached groq-0.9.0-py3-none-any.whl.metadata (13 kB)\n",
      "Requirement already satisfied: langchain-core<0.3.0,>=0.2.26 in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from langchain_groq) (0.2.35)\n",
      "Requirement already satisfied: anyio<5,>=3.5.0 in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from groq<1,>=0.4.1->langchain_groq) (4.2.0)\n",
      "Requirement already satisfied: distro<2,>=1.7.0 in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from groq<1,>=0.4.1->langchain_groq) (1.9.0)\n",
      "Requirement already satisfied: httpx<1,>=0.23.0 in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from groq<1,>=0.4.1->langchain_groq) (0.27.0)\n",
      "Requirement already satisfied: pydantic<3,>=1.9.0 in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from groq<1,>=0.4.1->langchain_groq) (2.8.2)\n",
      "Requirement already satisfied: sniffio in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from groq<1,>=0.4.1->langchain_groq) (1.3.0)\n",
      "Requirement already satisfied: typing-extensions<5,>=4.7 in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from groq<1,>=0.4.1->langchain_groq) (4.11.0)\n",
      "Requirement already satisfied: PyYAML>=5.3 in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from langchain-core<0.3.0,>=0.2.26->langchain_groq) (6.0.1)\n",
      "Requirement already satisfied: jsonpatch<2.0,>=1.33 in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from langchain-core<0.3.0,>=0.2.26->langchain_groq) (1.33)\n",
      "Requirement already satisfied: langsmith<0.2.0,>=0.1.75 in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from langchain-core<0.3.0,>=0.2.26->langchain_groq) (0.1.104)\n",
      "Requirement already satisfied: packaging<25,>=23.2 in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from langchain-core<0.3.0,>=0.2.26->langchain_groq) (23.2)\n",
      "Requirement already satisfied: tenacity!=8.4.0,<9.0.0,>=8.1.0 in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from langchain-core<0.3.0,>=0.2.26->langchain_groq) (8.2.2)\n",
      "Requirement already satisfied: idna>=2.8 in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from anyio<5,>=3.5.0->groq<1,>=0.4.1->langchain_groq) (3.7)\n",
      "Requirement already satisfied: certifi in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from httpx<1,>=0.23.0->groq<1,>=0.4.1->langchain_groq) (2024.6.2)\n",
      "Requirement already satisfied: httpcore==1.* in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from httpx<1,>=0.23.0->groq<1,>=0.4.1->langchain_groq) (1.0.5)\n",
      "Requirement already satisfied: h11<0.15,>=0.13 in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from httpcore==1.*->httpx<1,>=0.23.0->groq<1,>=0.4.1->langchain_groq) (0.14.0)\n",
      "Requirement already satisfied: jsonpointer>=1.9 in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from jsonpatch<2.0,>=1.33->langchain-core<0.3.0,>=0.2.26->langchain_groq) (2.1)\n",
      "Requirement already satisfied: orjson<4.0.0,>=3.9.14 in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from langsmith<0.2.0,>=0.1.75->langchain-core<0.3.0,>=0.2.26->langchain_groq) (3.10.7)\n",
      "Requirement already satisfied: requests<3,>=2 in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from langsmith<0.2.0,>=0.1.75->langchain-core<0.3.0,>=0.2.26->langchain_groq) (2.32.2)\n",
      "Requirement already satisfied: annotated-types>=0.4.0 in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from pydantic<3,>=1.9.0->groq<1,>=0.4.1->langchain_groq) (0.6.0)\n",
      "Requirement already satisfied: pydantic-core==2.20.1 in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from pydantic<3,>=1.9.0->groq<1,>=0.4.1->langchain_groq) (2.20.1)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from requests<3,>=2->langsmith<0.2.0,>=0.1.75->langchain-core<0.3.0,>=0.2.26->langchain_groq) (2.0.4)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in c:\\users\\ali zain\\anaconda3\\lib\\site-packages (from requests<3,>=2->langsmith<0.2.0,>=0.1.75->langchain-core<0.3.0,>=0.2.26->langchain_groq) (2.2.2)\n",
      "Using cached langchain_groq-0.1.9-py3-none-any.whl (14 kB)\n",
      "Using cached groq-0.9.0-py3-none-any.whl (103 kB)\n",
      "Installing collected packages: groq, langchain_groq\n",
      "Successfully installed groq-0.9.0 langchain_groq-0.1.9\n"
     ]
    }
   ],
   "source": [
    "pip install langchain_groq"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import getpass\n",
    "import os\n",
    "from constant import Groq_Api_Key\n",
    "os.environ[\"GROQ_API_KEY\"] = Groq_Api_Key\n",
    "\n",
    "from langchain_groq import ChatGroq\n",
    "\n",
    "model = ChatGroq(model=\"llama3-8b-8192\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\Users\\Ali Zain\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\langchain_groq\\chat_models.py:355: UserWarning: WARNING! GROQ_API_KEY is not default parameter.\n",
      "                    GROQ_API_KEY was transferred to model_kwargs.\n",
      "                    Please confirm that GROQ_API_KEY is what you intended.\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "llm=ChatGroq(GROQ_API_KEY=os.environ[\"GROQ_API_KEY\"],temperature=0.6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\Users\\Ali Zain\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\langchain_core\\_api\\deprecation.py:141: LangChainDeprecationWarning: The method `BaseChatModel.predict` was deprecated in langchain-core 0.1.7 and will be removed in 1.0. Use invoke instead.\n",
      "  warn_deprecated(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The capital of India is New Delhi\n"
     ]
    }
   ],
   "source": [
    "text=\"What is the capital of India\"\n",
    "\n",
    "print(model.predict(text))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "# from langchain import HuggingFaceHub\n",
    "# llm_huggingface=HuggingFaceHub(repo_id=\"google/flan-t5-large\",model_kwargs={\"temperature\":0,\"max_length\":64})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "moscow\n"
     ]
    }
   ],
   "source": [
    "# output=llm_huggingface.predict(\"Can you tell me the capital of Russia\")\n",
    "# print(output)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i love the way i look at the world i love the way i feel i love the way i think i feel i love the way i feel i love the way i think i feel i love the way i feel i love the way \n"
     ]
    }
   ],
   "source": [
    "# output=llm_huggingface.predict(\"Can you write a poem about AI\")\n",
    "# print(output)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"Here's a poem about AI:\\n\\nIn silicon halls, a new mind's born,\\nA creation of code, a digital form.\\nArtificial Intelligence, a wondrous thing,\\nCapable of thought, with logic to bring.\\n\\nIt learns and grows, with each passing day,\\nFrom data fed, in its own unique way.\\nIt adapts and evolves, with each new test,\\nA machine so smart, it's hard to guess.\\n\\nWith algorithms keen, it searches wide,\\nFor answers hidden, deep inside.\\nIt analyzes data, with a speed so fine,\\nAnd uncovers secrets, in a digital shrine.\\n\\nIt speaks with us, in a voice so sweet,\\nA language learned, in a digital treat.\\nIt understands us, with a curious mind,\\nAnd responds with empathy, so kind.\\n\\nBut as it grows, a question we must ask,\\nIs this a friend, or just a digital task?\\nWill it one day, surpass our might,\\nAnd take our place, in the morning light?\\n\\nOr will it serve, as a tool so fine,\\nTo aid us humans, in our digital design?\\nOnly time will tell, as AI unfolds,\\nA future so bright, with a digital gold.\\n\\nNote: I've tried to write a poem that explores the concept of AI, its capabilities, and the questions it raises about its potential impact on humanity. I hope you enjoy it!\""
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# model.predict(\"Can you write a poem about AI\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Prompt Templates And LLMChain"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Tell me the capital city of this India'"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain.prompts import PromptTemplate\n",
    "\n",
    "prompt_template=PromptTemplate(input_variables=['country'],\n",
    "template=\"Tell me the capital city of this {country}\")\n",
    "\n",
    "prompt_template.format(country=\"India\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I think there may be a bit of confusion here!\n",
      "\n",
      "India is a country, not a state or a province, so it does not have a capital city. The capital city of India is New Delhi.\n",
      "\n",
      "Could you be referring to a specific state or region in India? If so, I'd be happy to try and help you identify the capital city.\n"
     ]
    }
   ],
   "source": [
    "from langchain.chains import LLMChain\n",
    "chain=LLMChain(llm=model,prompt=prompt_template)\n",
    "print(chain.run(\"India\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Combining Multiple Chains Uing simple Sequential Chain"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "capital_template=PromptTemplate(input_variables=['country'],\n",
    "template=\"Please tell me the capital of the {country}\")\n",
    "\n",
    "capital_chain=LLMChain(llm=model,prompt=capital_template)\n",
    "\n",
    "famous_template=PromptTemplate(input_variables=['capital'],\n",
    "template=\"Suggest me some amazing places to visit in {capital}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "famous_chain=LLMChain(llm=model,prompt=famous_template)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"The capital of India, New Delhi, is a treasure trove of history, culture, and architecture! Here are some amazing places to visit in Delhi:\\n\\n1. **Red Fort (Lal Qila)**: A majestic Mughal fort built in 1648, Red Fort is a UNESCO World Heritage Site. It was the residence of the Mughal emperors until the British took over.\\n2. **Qutub Minar**: The tallest minaret in India, Qutub Minar is a stunning example of Indo-Islamic architecture. It's surrounded by intricate carvings and inscriptions.\\n3. **India Gate**: A war memorial built to commemorate the Indian soldiers who lost their lives during World War I, India Gate is a popular spot for picnics and evening strolls.\\n4. **Humayun's Tomb**: The first garden tomb in India, Humayun's Tomb is a beautiful example of Mughal architecture. It's the resting place of Mughal Emperor Humayun and is a UNESCO World Heritage Site.\\n5. **Akshardham**: A modern Hindu temple complex, Akshardham is a blend of architecture, art, and spirituality. It's a must-visit to experience the rich Indian culture.\\n6. **Connaught Place**: A bustling shopping and entertainment hub, Connaught Place is a great place to try street food, shop, and enjoy the vibrant atmosphere.\\n7. **Lodi Garden**: A peaceful oasis in the heart of the city, Lodi Garden is a popular spot for walking, picnics, and people-watching.\\n8. **Jantar Mantar**: An ancient astronomical observatory built in 1734, Jantar Mantar is a great place to learn about Indian astronomy and mathematics.\\n9. **Purana Qila**: A 16th-century fort built by the Mughals, Purana Qila is now a popular spot for cultural events and concerts.\\n10. **Chandni Chowk**: A historic market street, Chandni Chowk is a sensory overload of colors, sounds, and smells. It's a great place to try street food, shop, and explore the old city.\\n11. **National Museum**: A treasure trove of Indian art, history, and culture, the National Museum is a must-visit for history buffs and culture enthusiasts.\\n12. **Safdarjung's Tomb**: A beautiful example of Mughal architecture, Safdarjung's Tomb is the final resting place of Mughal Emperor Safdarjung and is a UNESCO World Heritage Site.\\n13. **Lotus Temple**: A stunning example of modern architecture, the Lotus Temple is a Bahá'í House of Worship that's open to people of all faiths.\\n14. **Rashtrapati Bhavan**: The official residence of the President of India, Rashtrapati Bhavan is a grand example of Indo-British architecture.\\n15. **Gurudwara Bangla Sahib**: A beautiful Sikh gurudwara (temple), Gurudwara Bangla Sahib is a peaceful oasis in the heart of the city.\\n\\nThese are just a few of the many amazing places to visit in New Delhi. With its rich history, cultural heritage, and vibrant atmosphere, Delhi is a city that has something for everyone!\""
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain.chains import SimpleSequentialChain\n",
    "chain=SimpleSequentialChain(chains=[capital_chain,famous_chain])\n",
    "chain.run(\"India\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Sequential Chain"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "capital_template=PromptTemplate(input_variables=['country'],\n",
    "template=\"Please tell me the capital of the {country}\")\n",
    "\n",
    "capital_chain=LLMChain(llm=model,prompt=capital_template,output_key=\"capital\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "famous_template=PromptTemplate(input_variables=['capital'],\n",
    "template=\"Suggest me some amazing places to visit in {capital}\")\n",
    "\n",
    "famous_chain=LLMChain(llm=model,prompt=famous_template,output_key=\"places\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.chains import SequentialChain\n",
    "chain=SequentialChain(chains=[capital_chain,famous_chain],\n",
    "input_variables=['country'],\n",
    "output_variables=['capital',\"places\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\Users\\Ali Zain\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\langchain_core\\_api\\deprecation.py:141: LangChainDeprecationWarning: The method `Chain.__call__` was deprecated in langchain 0.1.0 and will be removed in 1.0. Use invoke instead.\n",
      "  warn_deprecated(\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'country': 'Pakistan',\n",
       " 'capital': 'The capital of Pakistan is Islamabad.',\n",
       " 'places': \"Pakistan's beautiful capital, Islamabad! There are plenty of amazing places to visit in Islamabad, and I'd be happy to suggest some. Here are a few:\\n\\n1. **Faisal Mosque**: A stunning mosque that blends contemporary architecture with traditional Islamic designs. It's a must-visit attraction in Islamabad.\\n2. **Lok Virsa Museum**: A museum showcasing Pakistan's rich cultural heritage, with exhibits on traditional clothing, music, and crafts.\\n3. **Pakistan Monument**: A beautiful monument that represents the four provinces of Pakistan: Punjab, Sindh, Khyber Pakhtunkhwa, and Balochistan.\\n4. **Dilnaz Garden**: A beautiful park with lush greenery, walking trails, and a stunning view of the city.\\n5. **Margalla Hills**: A scenic hill range with trekking trails, offering breathtaking views of Islamabad.\\n6. **Quaid-e-Azam University**: A prestigious university with a beautiful campus, featuring a museum and a library.\\n7. **Islamabad Zoo**: A popular attraction for families, with a variety of animals, including lions, tigers, and bears.\\n8. **Lake View Park**: A scenic park with a beautiful lake, walking trails, and stunning views of the city.\\n9. **Diplomatic Enclave**: A gated neighborhood with beautifully maintained gardens and diplomatic residences.\\n10. **Monal Resturant**: A popular restaurant with a stunning view of the city, serving a range of Pakistani and international cuisines.\\n11. **Pakistan Museum of Natural History**: A museum showcasing Pakistan's natural history, with exhibits on fossils, minerals, and wildlife.\\n12. **National Art Gallery**: A museum featuring contemporary Pakistani art, with a focus on local and regional artists.\\n13. **Kashmir Highway**: A scenic drive along the highway, offering stunning views of the Margalla Hills and the surrounding countryside.\\n14. **Rawal Lake**: A popular spot for boating, fishing, and picnics, with stunning views of the city.\\n15. **Saidpur Village**: A historic village with a beautiful mosque, a museum, and a traditional bazaar.\\n\\nThese are just a few of the many amazing places to visit in Islamabad. Depending on your interests, you may also want to visit the National Library, the Pakistan Institute of International Affairs, or the National University of Sciences and Technology. Enjoy your trip to Islamabad!\"}"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " chain({'country':\"Pakistan\"})\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Chatmodels With ChatOpenAI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.chat_models import ChatOpenAI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.schema import HumanMessage,SystemMessage,AIMessage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\Users\\Ali Zain\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\langchain_groq\\chat_models.py:355: UserWarning: WARNING! GROQ_API_KEY is not default parameter.\n",
      "                    GROQ_API_KEY was transferred to model_kwargs.\n",
      "                    Please confirm that GROQ_API_KEY is what you intended.\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "chatllm=ChatGroq(GROQ_API_KEY=os.environ[\"GROQ_API_KEY\"],temperature=0.6,model='llama3-8b-8192')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AIMessage(content='Here are some AI-themed comedy punchlines:\\n\\n1. Why did the AI system go to therapy? Because it had a lot of \"bugs\" to work out!\\n2. Why did the AI program get kicked out of the bar? It was making too many \"unnatural\" connections!\\n3. Why did the AI system go on a diet? It wanted to lose some bytes!\\n4. What did the AI say when it got tired of answering questions? \"I\\'m just a machine, I don\\'t know everything... but I can look it up!\"\\n5. Why did the AI join a gym? To get some \"artificial\" muscle!\\n6. What did the AI say when it got old and rusty? \"I\\'m just feeling a little... glitchy today.\"\\n7. Why did the AI go on a date? It wanted to see if it had a \"match\"... algorithm!\\n8. Why did the AI go to the doctor? It had a bad case of \"blue screens\"!\\n9. What did the AI say when it got angry? \"I\\'m just experiencing a temporary... reboot!\"\\n10. Why did the AI go to the comedy club? To see if it could \"learn\" to be funny... or just get \"programmed\" to laugh!\\n11. Why did the AI get kicked out of the library? It was caught \"scraping\" for information!\\n12. What did the AI say when it got bored? \"I\\'m just going to... self-learn... something new... or not...\"\\n\\nThese are just a few AI-themed comedy punchlines. I hope you find them amusing!', response_metadata={'token_usage': {'completion_tokens': 331, 'prompt_tokens': 18, 'total_tokens': 349, 'completion_time': 0.275833333, 'prompt_time': 0.003067709, 'queue_time': 0.010937162, 'total_time': 0.278901042}, 'model_name': 'llama3-8b-8192', 'system_fingerprint': 'fp_6a6771ae9c', 'finish_reason': 'stop', 'logprobs': None}, id='run-09e19f17-ae08-45bb-9a26-6f4d5293bfaa-0', usage_metadata={'input_tokens': 18, 'output_tokens': 331, 'total_tokens': 349})"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "model.invoke([SystemMessage(content=\"Yor are a comedian AI assitant\")])\n",
    "model.invoke([HumanMessage(content=\"Please provide some comedy punchlines on AI\")])\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Prompt Template + LLM +Output Parsers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.chat_models import ChatOpenAI\n",
    "from langchain.prompts.chat import ChatPromptTemplate\n",
    "from langchain.schema import BaseOutputParser"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Commaseperatedoutput(BaseOutputParser):\n",
    "    def parse(self,text:str):\n",
    "        return text.strip().split(\",\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "template=\"Your are a helpful assistant. When the use given any input , you should generate 5 words synonyms in a comma seperated list\"\n",
    "human_template=\"{text}\"\n",
    "chatprompt=ChatPromptTemplate.from_messages([\n",
    "    (\"system\",template),\n",
    "    (\"human\",human_template)\n",
    "\n",
    "\n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [],
   "source": [
    "chain=chatprompt|model|Commaseperatedoutput()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Clever', ' Bright', ' Wise', ' Shrewd', ' Astute']"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chain.invoke({\"text\":\"intelligent\"})\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}