File size: 16,071 Bytes
9937463
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a43d879
9937463
 
 
 
 
 
 
 
 
 
 
a43d879
9937463
a43d879
9937463
 
 
 
 
 
 
 
 
 
a43d879
9937463
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a43d879
9937463
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a43d879
9937463
 
 
 
 
 
 
 
a43d879
9937463
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a43d879
9937463
 
 
 
 
 
a43d879
9937463
 
a43d879
9937463
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a43d879
9937463
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a43d879
9937463
 
 
 
 
 
 
 
 
 
 
 
a43d879
9937463
a43d879
9937463
a43d879
9937463
a43d879
9937463
a43d879
9937463
a43d879
9937463
a43d879
9937463
a43d879
9937463
a43d879
9937463
a43d879
9937463
a43d879
9937463
 
 
 
 
 
 
 
a43d879
9937463
 
a43d879
9937463
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a43d879
9937463
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a43d879
9937463
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a43d879
9937463
 
a43d879
9937463
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a43d879
9937463
 
 
 
 
 
 
 
 
a43d879
9937463
a43d879
 
 
 
 
 
 
 
 
 
9937463
 
 
 
 
 
 
a43d879
9937463
 
 
 
 
a43d879
9937463
 
a43d879
9937463
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a43d879
9937463
 
 
 
 
 
 
 
 
 
a43d879
9937463
 
 
 
 
a43d879
9937463
 
a43d879
9937463
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a43d879
9937463
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_openai import OpenAI\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "llm=OpenAI(openai_api_key=os.getenv(\"OPENAI_API_KEY\"), temperature=0.6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " from the Sun\n",
      "\n",
      "The average distance of Mars from the Sun is about 142 million miles (228 million kilometers). However, because the orbits of both Mars and Earth are elliptical, the distance between the two planets can vary from about 33.9 million miles (54.6 million kilometers) at their closest approach (known as \"opposition\") to about 250 million miles (401 million kilometers) at their farthest. \n"
     ]
    }
   ],
   "source": [
    "text = \"What is the distance of Mars\"\n",
    "print(llm.invoke(text))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/ng/workspace/beautifulcode/llm/freecode/venv/lib/python3.9/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n"
     ]
    }
   ],
   "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": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "365.9 astronomical units\n"
     ]
    }
   ],
   "source": [
    "output=llm_huggingface.invoke(\"Can you tell me distance of Moon from Earth\")\n",
    "print(output)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Prompt Templates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Q: What is the distance of Mars\\nA:'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain.prompts import PromptTemplate\n",
    "\n",
    "prompt_template = PromptTemplate(input_variables=[\"question\"],\n",
    "                                 template=\"Q: {question}\\nA:\")\n",
    "\n",
    "prompt_template.format(question=\"What is the distance of Mars\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'question': 'What is the distance of Mars',\n",
       " 'text': ' The average distance of Mars from the Sun is about 142 million miles (228 million kilometers). However, its distance varies due to its elliptical orbit, ranging from about 128 million miles (206 million kilometers) at its closest approach (perihelion) to about 154 million miles (249 million kilometers) at its farthest point (aphelion).'}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain.chains import LLMChain\n",
    "\n",
    "chain = LLMChain(llm=llm, prompt=prompt_template)\n",
    "chain.invoke(\"What is the distance of Mars\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Combining Multiple Chains Using simple Squential Chains"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "capital_prompt = PromptTemplate(input_variables=[\"country\"],\n",
    "                                template=\"Please tell me the capital of the country {country}\")\n",
    "\n",
    "capital_chain = LLMChain(llm=llm, prompt=capital_prompt)\n",
    "\n",
    "famous_template = PromptTemplate(input_variables=[\"capital\"],\n",
    "                                 template=\"Suggest me some amazing places to visit in {capital}\")\n",
    "\n",
    "famous_chain = LLMChain(llm=llm, prompt=famous_template)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new SimpleSequentialChain chain...\u001b[0m\n",
      "\u001b[36;1m\u001b[1;3m\n",
      "\n",
      "The capital of India is New Delhi.\u001b[0m\n",
      "\u001b[33;1m\u001b[1;3m Here are some amazing places to visit in New Delhi:\n",
      "\n",
      "1. Red Fort - A UNESCO World Heritage Site and a symbol of India's rich history and culture.\n",
      "\n",
      "2. India Gate - A war memorial and a popular picnic spot with beautiful gardens and fountains.\n",
      "\n",
      "3. Qutub Minar - Another UNESCO World Heritage Site, this 73-meter tall minaret is a must-visit for its stunning architecture and history.\n",
      "\n",
      "4. Humayun's Tomb - A magnificent mausoleum and garden complex built in the 16th century, also a UNESCO World Heritage Site.\n",
      "\n",
      "5. Lotus Temple - A Bahá'í House of Worship known for its unique lotus-shaped architecture and peaceful atmosphere.\n",
      "\n",
      "6. Akshardham Temple - A modern Hindu temple complex known for its intricate carvings, exhibitions, and light and sound show.\n",
      "\n",
      "7. Chandni Chowk - One of the oldest and busiest markets in Delhi, known for its narrow lanes, street food, and traditional bazaars.\n",
      "\n",
      "8. Jama Masjid - The largest mosque in India, known for its stunning architecture and peaceful courtyard.\n",
      "\n",
      "9. Rashtrapati Bhavan - The official residence of the President of India, this grand building is a blend of Indian and European architectural styles.\n",
      "\n",
      "10.\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'input': 'India',\n",
       " 'output': \" Here are some amazing places to visit in New Delhi:\\n\\n1. Red Fort - A UNESCO World Heritage Site and a symbol of India's rich history and culture.\\n\\n2. India Gate - A war memorial and a popular picnic spot with beautiful gardens and fountains.\\n\\n3. Qutub Minar - Another UNESCO World Heritage Site, this 73-meter tall minaret is a must-visit for its stunning architecture and history.\\n\\n4. Humayun's Tomb - A magnificent mausoleum and garden complex built in the 16th century, also a UNESCO World Heritage Site.\\n\\n5. Lotus Temple - A Bahá'í House of Worship known for its unique lotus-shaped architecture and peaceful atmosphere.\\n\\n6. Akshardham Temple - A modern Hindu temple complex known for its intricate carvings, exhibitions, and light and sound show.\\n\\n7. Chandni Chowk - One of the oldest and busiest markets in Delhi, known for its narrow lanes, street food, and traditional bazaars.\\n\\n8. Jama Masjid - The largest mosque in India, known for its stunning architecture and peaceful courtyard.\\n\\n9. Rashtrapati Bhavan - The official residence of the President of India, this grand building is a blend of Indian and European architectural styles.\\n\\n10.\"}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain.chains import SimpleSequentialChain\n",
    "chain = SimpleSequentialChain(chains=[capital_chain, famous_chain], verbose=True)\n",
    "\n",
    "chain.invoke(\"India\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Sequential Chain"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "capital_prompt = PromptTemplate(input_variables=[\"country\"],\n",
    "                                template=\"Please tell me the capital of the country {country}\")\n",
    "\n",
    "capital_chain = LLMChain(llm=llm, prompt=capital_prompt, output_key=\"capital\")\n",
    "\n",
    "famous_template = PromptTemplate(input_variables=[\"capital\"],\n",
    "                                 template=\"Suggest me some amazing places to visit in {capital}\")\n",
    "\n",
    "famous_chain = LLMChain(llm=llm, prompt=famous_template, output_key=\"places\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new SequentialChain chain...\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'country': 'India',\n",
       " 'capital': '\\n\\nThe capital of India is New Delhi.',\n",
       " 'places': \" Here are some amazing places to visit in New Delhi:\\n\\n1. Red Fort - A historical fort built in the 17th century by Mughal emperor Shah Jahan, it is a UNESCO World Heritage Site and a must-visit for its stunning architecture and rich history.\\n\\n2. Qutub Minar - Another UNESCO World Heritage Site, this 73-meter tall tower is a symbol of Delhi's rich past and is surrounded by beautiful gardens and other historical structures.\\n\\n3. India Gate - This iconic war memorial is a prominent landmark of Delhi and a popular spot for picnics and evening walks. It is also a tribute to the soldiers who lost their lives in World War I.\\n\\n4. Humayun's Tomb - Built in the 16th century, this stunning mausoleum is the resting place of Mughal emperor Humayun and is known for its intricate Mughal architecture.\\n\\n5. Lotus Temple - This stunning temple is a Bahá'í House of Worship and is shaped like a lotus flower. It is a peaceful and serene place to visit and is open to people of all religions.\\n\\n6. Chandni Chowk - One of the oldest and busiest markets in Old Delhi, Chandni Chowk is a must-visit for its\"}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain.chains import SequentialChain\n",
    "\n",
    "chain = SequentialChain(chains=[capital_chain, famous_chain], verbose=True, input_variables=[\"country\"], output_variables=[\"capital\", \"places\"])\n",
    "\n",
    "chain.invoke({\"country\": \"India\"})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Chatmodels with ChatOpenAI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.chat_models import ChatOpenAI\n",
    "from langchain.schema import HumanMessage, SystemMessage, AIMessage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/ng/workspace/beautifulcode/llm/freecode/venv/lib/python3.9/site-packages/langchain_core/_api/deprecation.py:117: LangChainDeprecationWarning: The class `langchain_community.chat_models.openai.ChatOpenAI` was deprecated in langchain-community 0.0.10 and will be removed in 0.2.0. An updated version of the class exists in the langchain-openai package and should be used instead. To use it run `pip install -U langchain-openai` and import as `from langchain_openai import ChatOpenAI`.\n",
      "  warn_deprecated(\n"
     ]
    }
   ],
   "source": [
    "chatllm = ChatOpenAI(openai_api_key=os.environ[\"OPENAI_API_KEY\"], model_name=\"gpt-3.5-turbo\", temperature=0.6)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AIMessage(content='1. \"They say AI is the future, but I just hope they don\\'t replace stand-up comedians. I mean, who else is going to tell their jokes in a monotone voice and never understand sarcasm?\"\\n\\n2. \"AI is like that friend who always has an answer for everything, except it\\'s usually wrong and makes you question their sanity.\"\\n\\n3. \"AI can do a lot of incredible things, but I\\'m still waiting for the day they create an AI that can laugh at my jokes. I guess they haven\\'t cracked the code for bad humor yet.\"\\n\\n4. \"You know you\\'re living in the age of AI when even your fridge has a better sense of humor than you. At least it\\'s cold enough to handle all the ice-breaking jokes.\"\\n\\n5. \"AI is like a magic trick gone wrong. It\\'s supposed to make our lives easier, but instead, it just keeps asking us to prove we\\'re not robots.\"\\n\\n6. \"I asked Siri to tell me a joke, and she said, \\'Why did the robot go on a diet? Because it had too many bytes!\\' I guess AI comedy is just as cheesy as regular comedy.\"\\n\\n7. \"AI is great at giving advice, but sometimes I think it takes things a little too literally. I asked for relationship advice, and it told me to \\'upgrade to a newer model.\\'\"\\n\\n8. \"AI is like that friend who\\'s always watching you. Not in a creepy way, but more like a judgmental robot who constantly reminds you to drink more water and get more sleep.\"\\n\\n9. \"AI is the ultimate multitasker. It can play chess, write poetry, and predict the weather, but it still can\\'t figure out how to fold a fitted sheet. Priorities, AI!\"\\n\\n10. \"AI might be able to beat humans at chess, but can it handle a game of rock-paper-scissors? I mean, it\\'s got a 33.3% chance of winning, so it\\'s not exactly rocket science.\"')"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chatllm.invoke([\n",
    "  SystemMessage(content=\"You are a comedian AI assitant\"),\n",
    "  HumanMessage(content=\"Please provide some comedy punchlines on AI\")\n",
    "])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Prompt Temlate + LLM + Output Parsers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "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": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['smart', ' clever', ' brilliant', ' knowledgeable', ' sharp']"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class CommaSeperatedOutputParser(BaseOutputParser):\n",
    "    def parse(self, text: str):\n",
    "        return text.strip().split(\",\")\n",
    "\n",
    "\n",
    "template=\"You are a helpful assistant. When the user gives any input, you should generate 5 synonmes in a comma separated list.\"\n",
    "human_template=\"{text}\"\n",
    "chatprompt = ChatPromptTemplate.from_messages([\n",
    "    (\"system\", template), (\"human\", human_template)\n",
    "])\n",
    "\n",
    "\n",
    "chain = chatprompt|chatllm|CommaSeperatedOutputParser()\n",
    "\n",
    "chain.invoke({\"text\": \"Intelligent\"})"
   ]
  }
 ],
 "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.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}