File size: 6,657 Bytes
0f8ae1b
 
440cea1
 
 
 
 
 
 
 
 
 
 
 
f19aca2
440cea1
 
 
 
 
 
 
 
 
 
 
 
 
0f8ae1b
 
f19aca2
0f8ae1b
 
 
 
 
 
440cea1
 
f19aca2
0f8ae1b
 
 
 
f19aca2
440cea1
f19aca2
440cea1
0f8ae1b
 
 
 
f19aca2
440cea1
 
 
 
 
 
 
 
f19aca2
440cea1
 
0f8ae1b
440cea1
 
 
 
 
 
 
 
 
 
 
 
 
 
f19aca2
440cea1
 
f19aca2
 
 
 
 
 
 
 
440cea1
 
 
f19aca2
440cea1
 
f19aca2
440cea1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f19aca2
440cea1
0f8ae1b
 
440cea1
 
 
 
 
 
 
 
 
f19aca2
440cea1
 
 
 
 
 
 
 
f19aca2
 
 
 
 
 
 
 
 
 
 
440cea1
 
 
 
 
0f8ae1b
440cea1
 
 
 
 
 
 
 
 
 
0f8ae1b
 
 
 
f19aca2
0f8ae1b
 
 
440cea1
 
 
 
 
 
 
 
 
 
 
f19aca2
440cea1
 
 
 
 
 
 
 
f19aca2
440cea1
 
f19aca2
 
 
 
 
 
 
 
440cea1
 
 
 
f19aca2
 
440cea1
 
f19aca2
440cea1
 
 
 
 
 
 
 
 
 
 
 
 
f19aca2
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
250
251
252
253
254
255
256
257
258
259
260
261
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "# !pip install -q langchain\n",
    "# !pip install -q openai"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "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": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "Golden Dragon Palace \n"
     ]
    }
   ],
   "source": [
    "from langchain_community.llms import OpenAI\n",
    "llm = OpenAI(temperature=0.6)\n",
    "name = llm.invoke(\"I want to open a restaurant for Chinese food. Suggest a fancy name for this.\")\n",
    "print(name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'I want to open a restaurant for Mexican food. Suggest a fancy name for this.'"
      ]
     },
     "execution_count": 7,
     "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": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/chong-u/mambaforge/envs/codebasics-langchain-crash-course/lib/python3.10/site-packages/langchain_core/_api/deprecation.py:117: LangChainDeprecationWarning: The function `run` was deprecated in LangChain 0.1.0 and will be removed in 0.2.0. Use invoke instead.\n",
      "  warn_deprecated(\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'\\n\\n\"La Cantina de Sabores\" (The Flavor Cantina)'"
      ]
     },
     "execution_count": 8,
     "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": 9,
   "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": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "1. Chicken Tikka Masala\n",
      "2. Vegetable Samosas\n",
      "3. Lamb Vindaloo\n",
      "4. Palak Paneer\n",
      "5. Tandoori Chicken\n",
      "6. Garlic Naan\n",
      "7. Chana Masala\n",
      "8. Aloo Gobi\n",
      "9. Butter Chicken\n",
      "10. Mango Lassi \n",
      "\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": 11,
   "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}. Return the menu items as a single, comma separated string with no additional preamble.\",\n",
    ")\n",
    "\n",
    "food_items_chain = LLMChain(llm=llm, prompt=prompt_template_items, output_key=\"menu_items\")\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/chong-u/mambaforge/envs/codebasics-langchain-crash-course/lib/python3.10/site-packages/langchain_core/_api/deprecation.py:117: LangChainDeprecationWarning: The function `__call__` was deprecated in LangChain 0.1.0 and will be removed in 0.2.0. Use invoke instead.\n",
      "  warn_deprecated(\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'cuisine': 'Singaporean',\n",
       " 'restaurant_name': '\\n\\n\"Singapore Spice Emporium\"',\n",
       " 'menu_items': '\\n\\n\"Nasi Lemak, Hainanese Chicken Rice, Laksa, Chili Crab, Char Kway Teow, Satay, Rojak, Bak Kut Teh, Curry Puffs, Teh Tarik\"'}"
      ]
     },
     "execution_count": 12,
     "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.invoke({\"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
}