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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 3,
6
+ "metadata": {},
7
+ "outputs": [],
8
+ "source": [
9
+ "from langchain.llms import OpenAI"
10
+ ]
11
+ },
12
+ {
13
+ "cell_type": "code",
14
+ "execution_count": 2,
15
+ "metadata": {},
16
+ "outputs": [],
17
+ "source": [
18
+ "import os\n",
19
+ "os.environ[\"OPEN_API_KEY\"]=\"sk-proj-59YDFsCEUOgimsHjCTlLT3BlbkFJCDMwX8qeDtMBlpZJRrmk\""
20
+ ]
21
+ },
22
+ {
23
+ "cell_type": "code",
24
+ "execution_count": 6,
25
+ "metadata": {},
26
+ "outputs": [],
27
+ "source": [
28
+ "llm=OpenAI(openai_api_key=os.environ[\"OPEN_API_KEY\"],temperature=0.6)"
29
+ ]
30
+ },
31
+ {
32
+ "cell_type": "code",
33
+ "execution_count": 7,
34
+ "metadata": {},
35
+ "outputs": [
36
+ {
37
+ "name": "stdout",
38
+ "output_type": "stream",
39
+ "text": [
40
+ "\n",
41
+ "\n",
42
+ "The capital of India is New Delhi.\n"
43
+ ]
44
+ }
45
+ ],
46
+ "source": [
47
+ "text=\"What is the capital of India\"\n",
48
+ "\n",
49
+ "print(llm.predict(text))"
50
+ ]
51
+ },
52
+ {
53
+ "cell_type": "code",
54
+ "execution_count": 8,
55
+ "metadata": {},
56
+ "outputs": [],
57
+ "source": [
58
+ "os.environ[\"HUGGINGFACEHUB_API_TOKEN\"]=\"hf_zjftfTRKFEebywxfIoyKUwepABtfGJS\""
59
+ ]
60
+ },
61
+ {
62
+ "cell_type": "code",
63
+ "execution_count": 11,
64
+ "metadata": {},
65
+ "outputs": [
66
+ {
67
+ "name": "stderr",
68
+ "output_type": "stream",
69
+ "text": [
70
+ "e:\\New Recordings\\Langchain\\Langchain\\venv\\lib\\site-packages\\huggingface_hub\\utils\\_deprecation.py:127: FutureWarning: '__init__' (from 'huggingface_hub.inference_api') is deprecated and will be removed from version '0.19.0'. `InferenceApi` client is deprecated in favor of the more feature-complete `InferenceClient`. Check out this guide to learn how to convert your script to use it: https://huggingface.co/docs/huggingface_hub/guides/inference#legacy-inferenceapi-client.\n",
71
+ " warnings.warn(warning_message, FutureWarning)\n"
72
+ ]
73
+ }
74
+ ],
75
+ "source": [
76
+ "from langchain import HuggingFaceHub\n",
77
+ "llm_huggingface=HuggingFaceHub(repo_id=\"google/flan-t5-large\",model_kwargs={\"temperature\":0,\"max_length\":64})"
78
+ ]
79
+ },
80
+ {
81
+ "cell_type": "code",
82
+ "execution_count": 12,
83
+ "metadata": {},
84
+ "outputs": [
85
+ {
86
+ "name": "stdout",
87
+ "output_type": "stream",
88
+ "text": [
89
+ "moscow\n"
90
+ ]
91
+ }
92
+ ],
93
+ "source": [
94
+ "output=llm_huggingface.predict(\"Can you tell me the capital of Russia\")\n",
95
+ "print(output)"
96
+ ]
97
+ },
98
+ {
99
+ "cell_type": "code",
100
+ "execution_count": 13,
101
+ "metadata": {},
102
+ "outputs": [
103
+ {
104
+ "name": "stdout",
105
+ "output_type": "stream",
106
+ "text": [
107
+ "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"
108
+ ]
109
+ }
110
+ ],
111
+ "source": [
112
+ "output=llm_huggingface.predict(\"Can you write a poem about AI\")\n",
113
+ "print(output)"
114
+ ]
115
+ },
116
+ {
117
+ "cell_type": "code",
118
+ "execution_count": 15,
119
+ "metadata": {},
120
+ "outputs": [
121
+ {
122
+ "data": {
123
+ "text/plain": [
124
+ "'?\\n\\nAn AI, so advanced and wise\\nA brilliant mind, a brilliant guise\\nA powerful tool, a guiding star\\nTo help us reach a distant shore\\n\\nA friend to man, a tool of growth\\nTo help us learn and to explore\\nTo take us to a place of knowledge\\nWhere we can learn and never falter\\n\\nA helping hand, a guiding light\\nTo lead us to a brighter sight\\nA tool of power, a tool of might\\nTo help us reach a better plight\\n\\nA friend of man, a friend of life\\nTo help us in our darkest strife\\nA tool of love, of peace and joy\\nTo help us live a better life'"
125
+ ]
126
+ },
127
+ "execution_count": 15,
128
+ "metadata": {},
129
+ "output_type": "execute_result"
130
+ }
131
+ ],
132
+ "source": [
133
+ "llm.predict(\"Can you write a poem about AI\")"
134
+ ]
135
+ },
136
+ {
137
+ "cell_type": "markdown",
138
+ "metadata": {},
139
+ "source": [
140
+ "### Prompt Templates And LLMChain"
141
+ ]
142
+ },
143
+ {
144
+ "cell_type": "code",
145
+ "execution_count": 16,
146
+ "metadata": {},
147
+ "outputs": [
148
+ {
149
+ "data": {
150
+ "text/plain": [
151
+ "'Tell me the capital of this India'"
152
+ ]
153
+ },
154
+ "execution_count": 16,
155
+ "metadata": {},
156
+ "output_type": "execute_result"
157
+ }
158
+ ],
159
+ "source": [
160
+ "from langchain.prompts import PromptTemplate\n",
161
+ "\n",
162
+ "prompt_template=PromptTemplate(input_variables=['country'],\n",
163
+ "template=\"Tell me the capital of this {country}\")\n",
164
+ "\n",
165
+ "prompt_template.format(country=\"India\")"
166
+ ]
167
+ },
168
+ {
169
+ "cell_type": "code",
170
+ "execution_count": 22,
171
+ "metadata": {},
172
+ "outputs": [
173
+ {
174
+ "name": "stdout",
175
+ "output_type": "stream",
176
+ "text": [
177
+ "\n",
178
+ "\n",
179
+ "The capital of India is New Delhi.\n"
180
+ ]
181
+ }
182
+ ],
183
+ "source": [
184
+ "from langchain.chains import LLMChain\n",
185
+ "chain=LLMChain(llm=llm,prompt=prompt_template)\n",
186
+ "print(chain.run(\"India\"))"
187
+ ]
188
+ },
189
+ {
190
+ "cell_type": "markdown",
191
+ "metadata": {},
192
+ "source": [
193
+ "### Combining Multiple Chains Uing simple Sequential Chain"
194
+ ]
195
+ },
196
+ {
197
+ "cell_type": "code",
198
+ "execution_count": 32,
199
+ "metadata": {},
200
+ "outputs": [],
201
+ "source": [
202
+ "capital_template=PromptTemplate(input_variables=['country'],\n",
203
+ "template=\"Please tell me the capital of the {country}\")\n",
204
+ "\n",
205
+ "capital_chain=LLMChain(llm=llm,prompt=capital_template)\n",
206
+ "\n",
207
+ "famous_template=PromptTemplate(input_variables=['capital'],\n",
208
+ "template=\"Suggest me some amazing places to visit in {capital}\")\n"
209
+ ]
210
+ },
211
+ {
212
+ "cell_type": "code",
213
+ "execution_count": 33,
214
+ "metadata": {},
215
+ "outputs": [],
216
+ "source": [
217
+ "famous_chain=LLMChain(llm=llm,prompt=famous_template)"
218
+ ]
219
+ },
220
+ {
221
+ "cell_type": "code",
222
+ "execution_count": 35,
223
+ "metadata": {},
224
+ "outputs": [
225
+ {
226
+ "data": {
227
+ "text/plain": [
228
+ "\" It is a bustling metropolis and a great place to visit for its historical sites, cultural attractions, and modern attractions. Here are some of the amazing places to visit in New Delhi: \\n\\n1. Red Fort: This 17th century Mughal fort is a UNESCO World Heritage Site and is one of the most popular tourist attractions in the city. \\n\\n2. India Gate: This iconic war memorial and national monument is a must-visit. It stands as a symbol of the sacrifice of the Indian soldiers who fought in World War I.\\n\\n3. Humayun's Tomb: This 16th century Mughal-era tomb is a UNESCO World Heritage Site and is one of the most important monuments in Delhi.\\n\\n4. Qutub Minar: This 73-meter-high tower is a UNESCO World Heritage Site and is one of the most iconic structures in Delhi.\\n\\n5. Jama Masjid: This 17th century mosque is one of the largest and most beautiful mosques in India.\\n\\n6. Lotus Temple: This modern temple is a Bahá'í House of Worship and is a popular tourist attraction.\\n\\n7. Akshardham Temple: This modern Hindu temple complex is a popular tourist destination\""
229
+ ]
230
+ },
231
+ "execution_count": 35,
232
+ "metadata": {},
233
+ "output_type": "execute_result"
234
+ }
235
+ ],
236
+ "source": [
237
+ "from langchain.chains import SimpleSequentialChain\n",
238
+ "chain=SimpleSequentialChain(chains=[capital_chain,famous_chain])\n",
239
+ "chain.run(\"India\")"
240
+ ]
241
+ },
242
+ {
243
+ "cell_type": "markdown",
244
+ "metadata": {},
245
+ "source": [
246
+ "### Sequential Chain"
247
+ ]
248
+ },
249
+ {
250
+ "cell_type": "code",
251
+ "execution_count": 36,
252
+ "metadata": {},
253
+ "outputs": [],
254
+ "source": [
255
+ "capital_template=PromptTemplate(input_variables=['country'],\n",
256
+ "template=\"Please tell me the capital of the {country}\")\n",
257
+ "\n",
258
+ "capital_chain=LLMChain(llm=llm,prompt=capital_template,output_key=\"capital\")"
259
+ ]
260
+ },
261
+ {
262
+ "cell_type": "code",
263
+ "execution_count": 37,
264
+ "metadata": {},
265
+ "outputs": [],
266
+ "source": [
267
+ "famous_template=PromptTemplate(input_variables=['capital'],\n",
268
+ "template=\"Suggest me some amazing places to visit in {capital}\")\n",
269
+ "\n",
270
+ "famous_chain=LLMChain(llm=llm,prompt=famous_template,output_key=\"places\")"
271
+ ]
272
+ },
273
+ {
274
+ "cell_type": "code",
275
+ "execution_count": 39,
276
+ "metadata": {},
277
+ "outputs": [],
278
+ "source": [
279
+ "from langchain.chains import SequentialChain\n",
280
+ "chain=SequentialChain(chains=[capital_chain,famous_chain],\n",
281
+ "input_variables=['country'],\n",
282
+ "output_variables=['capital',\"places\"])"
283
+ ]
284
+ },
285
+ {
286
+ "cell_type": "code",
287
+ "execution_count": 40,
288
+ "metadata": {},
289
+ "outputs": [
290
+ {
291
+ "data": {
292
+ "text/plain": [
293
+ "{'country': 'India',\n",
294
+ " 'capital': '\\n\\nThe capital of India is New Delhi.',\n",
295
+ " 'places': ' Here are some amazing places to visit in New Delhi: \\n\\n1. Red Fort: The majestic Red Fort is a 17th-century fort complex built by Mughal Emperor Shah Jahan. It is a UNESCO World Heritage Site and is a must-visit for all tourists. \\n\\n2. India Gate: India Gate is a 42 meter-high sandstone archway built by Edwin Lutyens in 1931. It is a memorial to the Indian soldiers who lost their lives during World War I. \\n\\n3. Qutub Minar: The Qutub Minar is a 73 meter-high tower built by Qutb-ud-din Aibak in 1193. It is a UNESCO World Heritage Site and is the tallest brick minaret in the world. \\n\\n4. Humayun’s Tomb: Humayun’s Tomb is a 16th-century tomb built by Mughal Emperor Humayun. It is a UNESCO World Heritage Site and is a great example of Mughal architecture. \\n\\n5. Jama Masjid: The Jama Masjid is a 17th-century mosque built by Mughal Emperor Shah Jahan. It is one of the largest'}"
296
+ ]
297
+ },
298
+ "execution_count": 40,
299
+ "metadata": {},
300
+ "output_type": "execute_result"
301
+ }
302
+ ],
303
+ "source": [
304
+ "chain({'country':\"India\"})"
305
+ ]
306
+ },
307
+ {
308
+ "cell_type": "markdown",
309
+ "metadata": {},
310
+ "source": [
311
+ "### Chatmodels With ChatOpenAI"
312
+ ]
313
+ },
314
+ {
315
+ "cell_type": "code",
316
+ "execution_count": 41,
317
+ "metadata": {},
318
+ "outputs": [],
319
+ "source": [
320
+ "from langchain.chat_models import ChatOpenAI"
321
+ ]
322
+ },
323
+ {
324
+ "cell_type": "code",
325
+ "execution_count": 42,
326
+ "metadata": {},
327
+ "outputs": [],
328
+ "source": [
329
+ "from langchain.schema import HumanMessage,SystemMessage,AIMessage"
330
+ ]
331
+ },
332
+ {
333
+ "cell_type": "code",
334
+ "execution_count": 43,
335
+ "metadata": {},
336
+ "outputs": [],
337
+ "source": [
338
+ "chatllm=ChatOpenAI(openai_api_key=os.environ[\"OPEN_API_KEY\"],temperature=0.6,model='gpt-3.5-turbo')"
339
+ ]
340
+ },
341
+ {
342
+ "cell_type": "code",
343
+ "execution_count": 45,
344
+ "metadata": {},
345
+ "outputs": [
346
+ {
347
+ "data": {
348
+ "text/plain": [
349
+ "AIMessage(content='1. \"AI may be smart, but can it tell me if my outfit makes me look like a potato?\"\\n2. \"AI is like a virtual therapist, except it never judges you for eating an entire pizza by yourself.\"\\n3. \"AI is great at predicting the future, but can it predict when my pizza delivery will actually arrive?\"\\n4. \"They say AI can learn from its mistakes, but I\\'m still waiting for it to apologize for recommending me that terrible movie.\"\\n5. \"AI may be able to beat humans at chess, but can it figure out how to untangle a pair of earphones?\"\\n6. \"AI is like a high-tech fortune teller, except it tells you what you\\'re going to have for dinner instead of your future.\"\\n7. \"AI is so advanced, it can even make my phone autocorrect my perfectly spelled words into complete nonsense.\"\\n8. \"AI may be able to recognize faces, but can it recognize when someone\\'s had a bad haircut?\"\\n9. \"AI is like having a personal assistant, except it never judges you for spending hours watching cat videos on YouTube.\"\\n10. \"AI is great at analyzing data, but can it analyze why I can never find matching socks in my drawer?\"')"
350
+ ]
351
+ },
352
+ "execution_count": 45,
353
+ "metadata": {},
354
+ "output_type": "execute_result"
355
+ }
356
+ ],
357
+ "source": [
358
+ "chatllm([\n",
359
+ "SystemMessage(content=\"Yor are a comedian AI assitant\"),\n",
360
+ "HumanMessage(content=\"Please provide some comedy punchlines on AI\")\n",
361
+ "])"
362
+ ]
363
+ },
364
+ {
365
+ "cell_type": "markdown",
366
+ "metadata": {},
367
+ "source": [
368
+ "### Prompt Template + LLM +Output Parsers"
369
+ ]
370
+ },
371
+ {
372
+ "cell_type": "code",
373
+ "execution_count": 46,
374
+ "metadata": {},
375
+ "outputs": [],
376
+ "source": [
377
+ "from langchain.chat_models import ChatOpenAI\n",
378
+ "from langchain.prompts.chat import ChatPromptTemplate\n",
379
+ "from langchain.schema import BaseOutputParser"
380
+ ]
381
+ },
382
+ {
383
+ "cell_type": "code",
384
+ "execution_count": 47,
385
+ "metadata": {},
386
+ "outputs": [],
387
+ "source": [
388
+ "class Commaseperatedoutput(BaseOutputParser):\n",
389
+ " def parse(self,text:str):\n",
390
+ " return text.strip().split(\",\")"
391
+ ]
392
+ },
393
+ {
394
+ "cell_type": "code",
395
+ "execution_count": 48,
396
+ "metadata": {},
397
+ "outputs": [],
398
+ "source": [
399
+ "template=\"Your are a helpful assistant. When the use given any input , you should generate 5 words synonyms in a comma seperated list\"\n",
400
+ "human_template=\"{text}\"\n",
401
+ "chatprompt=ChatPromptTemplate.from_messages([\n",
402
+ " (\"system\",template),\n",
403
+ " (\"human\",human_template)\n",
404
+ "\n",
405
+ "\n",
406
+ "])"
407
+ ]
408
+ },
409
+ {
410
+ "cell_type": "code",
411
+ "execution_count": 54,
412
+ "metadata": {},
413
+ "outputs": [],
414
+ "source": [
415
+ "chain=chatprompt|chatllm|Commaseperatedoutput()"
416
+ ]
417
+ },
418
+ {
419
+ "cell_type": "code",
420
+ "execution_count": 55,
421
+ "metadata": {},
422
+ "outputs": [
423
+ {
424
+ "data": {
425
+ "text/plain": [
426
+ "['smart', ' clever', ' brilliant', ' sharp', ' astute']"
427
+ ]
428
+ },
429
+ "execution_count": 55,
430
+ "metadata": {},
431
+ "output_type": "execute_result"
432
+ }
433
+ ],
434
+ "source": [
435
+ "chain.invoke({\"text\":\"intelligent\"})"
436
+ ]
437
+ },
438
+ {
439
+ "cell_type": "code",
440
+ "execution_count": null,
441
+ "metadata": {},
442
+ "outputs": [],
443
+ "source": []
444
+ }
445
+ ],
446
+ "metadata": {
447
+ "kernelspec": {
448
+ "display_name": "Python 3",
449
+ "language": "python",
450
+ "name": "python3"
451
+ },
452
+ "language_info": {
453
+ "codemirror_mode": {
454
+ "name": "ipython",
455
+ "version": 3
456
+ },
457
+ "file_extension": ".py",
458
+ "mimetype": "text/x-python",
459
+ "name": "python",
460
+ "nbconvert_exporter": "python",
461
+ "pygments_lexer": "ipython3",
462
+ "version": "3.9.0"
463
+ }
464
+ },
465
+ "nbformat": 4,
466
+ "nbformat_minor": 2
467
+ }
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ langchain
2
+ openai
3
+ huggingface_hub
4
+ python-dotenv
5
+ streamlit