File size: 6,625 Bytes
0f8ae1b
 
440cea1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0f8ae1b
 
 
 
 
 
 
 
 
440cea1
 
 
0f8ae1b
 
 
 
440cea1
 
 
 
0f8ae1b
 
 
 
440cea1
 
 
 
 
 
 
 
 
 
 
 
0f8ae1b
440cea1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0f8ae1b
 
440cea1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0f8ae1b
440cea1
 
 
 
 
 
 
 
 
 
0f8ae1b
 
 
 
440cea1
0f8ae1b
 
 
440cea1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0f8ae1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "# !pip install -q langchain\n",
    "# !pip install -q openai"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "notebookRunGroups": {
     "groupValue": ""
    }
   },
   "outputs": [],
   "source": [
    "from keys import openapi_key\n",
    "import os\n",
    "\n",
    "os.environ['OPENAI_API_KEY'] = openapi_key\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\"Imperial Dragon Palace\"\n"
     ]
    }
   ],
   "source": [
    "from langchain.llms import OpenAI\n",
    "llm = OpenAI(temperature=0.6)\n",
    "name = llm(\"I want to open a restaurant for Chinese food. Suggest a fancy name for this.\")\n",
    "print(name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'I want to open a restaurant for Mexican food. Suggest a fancy name for this.'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain.prompts import PromptTemplate\n",
    "\n",
    "prompt_template_name = PromptTemplate(\n",
    "    input_variables = [\"cuisine\"],\n",
    "    template=\"I want to open a restaurant for {cuisine} food. Suggest a fancy name for this.\",\n",
    ")\n",
    "\n",
    "prompt_template_name.format(cuisine=\"Mexican\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n\\n\"La Cantina de Oro\"'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain.chains import LLMChain\n",
    "\n",
    "name_chain = LLMChain(llm=llm, prompt=prompt_template_name)\n",
    "name_chain.run(cuisine=\"Mexican\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Sequential Chains"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt_template_items = PromptTemplate(\n",
    "    input_variables = [\"restaurant_name\"],\n",
    "    template=\"Suggest some menu items for {restaurant_name}. Return it as a comma separated list.\",\n",
    ")\n",
    "food_items_chain = LLMChain(llm=llm, prompt=prompt_template_items)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "1. Spicy Chicken Curry\n",
      "2. Lamb Vindaloo\n",
      "3. Vegetable Samosas\n",
      "4. Tandoori Shrimp\n",
      "5. Naan Bread\n",
      "6. Chicken Tikka Masala\n",
      "7. Aloo Gobi (Potato and Cauliflower Curry)\n",
      "8. Palak Paneer (Spinach and Cheese Curry)\n",
      "9. Mango Lassi\n",
      "10. Biryani Rice\n",
      "11. Chana Masala (Chickpea Curry)\n",
      "12. Chicken Korma\n",
      "13. Garlic Naan\n",
      "14. Masala Dosa (Stuffed Crepe)\n",
      "15. Gulab Jamun (Sweet Fried Dumplings)\n"
     ]
    }
   ],
   "source": [
    "from langchain.chains import SimpleSequentialChain\n",
    "\n",
    "chain = SimpleSequentialChain(chains=[name_chain, food_items_chain])\n",
    "response = chain.run(\"Indian\")\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Sequential Chain"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "llm = OpenAI(temperature=0.7)\n",
    "\n",
    "prompt_template_name = PromptTemplate(\n",
    "    input_variables=[\"cuisine\"],\n",
    "    template = \"I want to open a restaurant for {cuisine} food. Suggest a fancy name for this.\",\n",
    ")\n",
    "\n",
    "name_chain = LLMChain(llm=llm, prompt=prompt_template_name, output_key=\"restaurant_name\")\n",
    "\n",
    "prompt_template_items = PromptTemplate(\n",
    "    input_variables=[\"restaurant_name\"],\n",
    "    template = \"Suggest some menu items for {restaurant_name}.\",\n",
    ")\n",
    "\n",
    "food_items_chain = LLMChain(llm=llm, prompt=prompt_template_items, output_key=\"menu_items\")\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'cuisine': 'Singaporean',\n",
       " 'restaurant_name': '\\n\\n\"Singapore Spice House\"',\n",
       " 'menu_items': '\\n\\n1. Singaporean Chili Crab\\n2. Hainanese Chicken Rice\\n3. Laksa Soup\\n4. Char Kway Teow (Stir-fried noodles with shrimp, egg, and sausage)\\n5. Satay Skewers (grilled marinated meat on skewers)\\n6. Nasi Lemak (coconut rice dish with fried chicken, fried anchovies, peanuts, and sambal chili sauce)\\n7. Roti Prata (Indian-style flatbread served with curry)\\n8. Bak Kut Teh (pork rib soup with herbs and spices)\\n9. Murtabak (stuffed pancake with chicken or mutton)\\n10. Singaporean-style Fried Rice\\n11. Beef Rendang (spicy braised beef in coconut milk)\\n12. Chilli Prawns\\n13. Gado Gado (Indonesian salad with peanut sauce)\\n14. Sambal Stingray (grilled stingray with spicy sambal sauce)\\n15. Biryani Rice (fragrant rice dish with spices and choice of meat)'}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain.chains import SequentialChain\n",
    "\n",
    "chain = SequentialChain(\n",
    "    chains=[name_chain, food_items_chain], \n",
    "    input_variables=[\"cuisine\"], \n",
    "    output_variables = [\"restaurant_name\", \"menu_items\"]\n",
    ")\n",
    "\n",
    "chain({\"cuisine\": \"Singaporean\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "codebasics-langchain-crash-course",
   "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.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}