|
25 | 25 | }, |
26 | 26 | { |
27 | 27 | "cell_type": "code", |
28 | | - "execution_count": 16, |
| 28 | + "execution_count": 20, |
29 | 29 | "id": "35289cea", |
30 | 30 | "metadata": {}, |
31 | 31 | "outputs": [], |
|
39 | 39 | "id": "ba880453", |
40 | 40 | "metadata": {}, |
41 | 41 | "source": [ |
42 | | - "## API Configuation\n", |
| 42 | + "## API Configuation and Deployed Model Setup\n", |
43 | 43 | "\n", |
44 | 44 | "After installing the necessary libraies, the API must be configured. The code below shows how to configure the API directly in your Python environment. \n" |
45 | 45 | ] |
46 | 46 | }, |
47 | 47 | { |
48 | 48 | "cell_type": "code", |
49 | | - "execution_count": 25, |
| 49 | + "execution_count": 21, |
50 | 50 | "id": "a9752fda", |
51 | 51 | "metadata": { |
52 | 52 | "scrolled": true |
53 | 53 | }, |
54 | 54 | "outputs": [], |
55 | 55 | "source": [ |
56 | | - "from langchain.llms import AzureOpenAI \n", |
57 | 56 | "import openai\n", |
58 | 57 | "import json\n", |
59 | 58 | "import os\n", |
| 59 | + "from langchain.chat_models import AzureChatOpenAI\n", |
| 60 | + "from langchain.schema import HumanMessage\n", |
| 61 | + "from langchain import LLMChain\n", |
| 62 | + "\n", |
60 | 63 | "\n", |
61 | 64 | "# Load config values\n", |
62 | 65 | "with open(r'config.json') as config_file:\n", |
63 | 66 | " config_details = json.load(config_file)\n", |
64 | 67 | "\n", |
65 | | - "# Setting up the deployment name\n", |
66 | | - "deployment_name = config_details['DEPLOYMENT_NAME']\n", |
| 68 | + "# The base URL for your Azure OpenAI resource. e.g. \"https://<your resource name>.openai.azure.com\"\n", |
| 69 | + "openai_api_base=config_details['OPENAI_API_BASE']\n", |
| 70 | + " \n", |
| 71 | + "# API version e.g. \"2023-07-01-preview\"\n", |
| 72 | + "openai_api_version=config_details['OPENAI_API_VERSION']\n", |
67 | 73 | "\n", |
68 | | - "# This is set to `azure`\n", |
69 | | - "openai.api_type = \"azure\"\n", |
| 74 | + "# The name of your Azure OpenAI deployment chat model. e.g. \"gpt-3-turbo-0613\"\n", |
| 75 | + "deployment_name=config_details['DEPLOYMENT_NAME']\n", |
70 | 76 | "\n", |
71 | 77 | "# The API key for your Azure OpenAI resource.\n", |
72 | | - "openai.api_key = os.getenv(\"OPENAI_API_KEY\")\n", |
| 78 | + "openai_api_key = os.getenv(\"OPENAI_API_KEY\")\n", |
73 | 79 | "\n", |
74 | | - "# The base URL for your Azure OpenAI resource. e.g. \"https://<your resource name>.openai.azure.com\"\n", |
75 | | - "openai.api_base = config_details['OPENAI_API_BASE']\n", |
76 | | - "\n", |
77 | | - "# Currently Chat Completion API have the following versions available: 2023-07-01-preview\n", |
78 | | - "openai.api_version = config_details['OPENAI_API_VERSION']" |
| 80 | + "# This is set to `azure`\n", |
| 81 | + "openai_api_type=\"azure\"" |
79 | 82 | ] |
80 | 83 | }, |
81 | 84 | { |
82 | 85 | "cell_type": "markdown", |
83 | | - "id": "ab4779e5", |
| 86 | + "id": "52da840e", |
84 | 87 | "metadata": {}, |
85 | 88 | "source": [ |
86 | | - "## Deployed Model Setup\n", |
87 | | - "\n", |
88 | | - "Azure OpenAI allows users to create and manage their own model deployments. To call the API, you must specify the deployment you want to use by passing in the deployment name and model name. " |
| 89 | + "## Deployed Model Setup" |
89 | 90 | ] |
90 | 91 | }, |
91 | 92 | { |
92 | 93 | "cell_type": "code", |
93 | | - "execution_count": 33, |
94 | | - "id": "6ea3e99f", |
| 94 | + "execution_count": 22, |
| 95 | + "id": "041f0a56", |
95 | 96 | "metadata": {}, |
96 | 97 | "outputs": [ |
97 | 98 | { |
98 | 99 | "data": { |
99 | 100 | "text/plain": [ |
100 | | - "\"\\n\\n\\n\\n\\nThe world is a beautiful place,\\nThe colors are so bright and true,\\nAnd I feel so free and free,\\nWhen I'm away from here.\\n\\nThe sky is so blue,\\nAnd the sun is so warm,\\nAnd I feel so free and free,\\nWhen I'm away from here.\"" |
| 101 | + "AIMessage(content=\"In a world of dreams, where words take flight,\\nI'll weave a poem, to bring you delight.\\nWith ink and rhythm, I'll paint a scene,\\nWhere emotions dance in colors unseen.\\n\\nIn twilight's embrace, the stars ignite,\\nAs moonbeams whisper, a poet's plight.\\nEach syllable, a gentle caress,\\nUnveiling secrets, in poetic finesse.\\n\\nThrough nature's canvas, I'll take you there,\\nWhere sunsets melt, in hues so rare.\\nA symphony of birds, their melodies blend,\\nAs whispers of wind, through tall trees ascend.\\n\\nIn the depths of silence, where echoes reside,\\nI'll find the stories that time can't hide.\\nUnearth forgotten tales, from ancient lore,\\nAnd breathe life into them, like never before.\\n\\nWith passion's fire, my verses will ignite,\\nEmbracing love's joy, and sorrow's might.\\nI'll capture the essence, of a tender touch,\\nAnd the ache of longing, that hurts so much.\\n\\nThrough valleys of hope, and mountains of despair,\\nI'll guide you gently, with tender care.\\nWith each word penned, a journey will start,\\nTo touch your soul, and mend a broken heart.\\n\\nSo let these lines, like whispered breeze,\\nWrap around your heart, with gentle ease.\\nFor in this poem, I'll pour my soul,\\nAnd hope its essence makes you whole.\", additional_kwargs={}, example=False)" |
101 | 102 | ] |
102 | 103 | }, |
103 | | - "execution_count": 33, |
| 104 | + "execution_count": 22, |
104 | 105 | "metadata": {}, |
105 | 106 | "output_type": "execute_result" |
106 | 107 | } |
107 | 108 | ], |
108 | 109 | "source": [ |
109 | | - "# Create an instance of Azure OpenAI\n", |
110 | | - "\n", |
111 | | - "# Replace the deployment name and model name with your own\n", |
112 | | - "llm = AzureOpenAI(\n", |
113 | | - " deployment_name= deployment_name,\n", |
114 | | - " model_name=\"text-davinci-002\", \n", |
| 110 | + "# Create an instance of chat llm\n", |
| 111 | + "llm = AzureChatOpenAI(\n", |
| 112 | + " openai_api_base=openai_api_base,\n", |
| 113 | + " openai_api_version=openai_api_version,\n", |
| 114 | + " deployment_name=deployment_name,\n", |
| 115 | + " openai_api_key=openai_api_key,\n", |
| 116 | + " openai_api_type=openai_api_type,\n", |
115 | 117 | ")\n", |
116 | 118 | "\n", |
117 | | - "# Run the LLM\n", |
118 | | - "llm(\"Write me a poem\")" |
| 119 | + "llm([HumanMessage(content=\"Write me a poem\")])" |
119 | 120 | ] |
120 | 121 | }, |
121 | 122 | { |
|
130 | 131 | }, |
131 | 132 | { |
132 | 133 | "cell_type": "code", |
133 | | - "execution_count": 40, |
| 134 | + "execution_count": 23, |
134 | 135 | "id": "927d4bac", |
135 | 136 | "metadata": {}, |
136 | 137 | "outputs": [ |
137 | 138 | { |
138 | 139 | "name": "stdout", |
139 | 140 | "output_type": "stream", |
140 | 141 | "text": [ |
| 142 | + "Example #1:\n", |
| 143 | + "content=\"A good face wash for acne-prone skin is one that contains ingredients like salicylic acid or benzoyl peroxide. These ingredients are effective in combating acne by unclogging pores, reducing inflammation, and killing acne-causing bacteria. Here are a few recommendations:\\n\\n1. Neutrogena Oil-Free Acne Wash: This face wash contains salicylic acid, which helps to clear breakouts and prevent new ones. It is gentle on the skin and doesn't leave it feeling dry or tight.\\n\\n2. La Roche-Posay Effaclar Medicated Gel Cleanser: This cleanser is formulated with 2% salicylic acid and helps to remove excess oil and impurities from the skin. It is suitable for sensitive skin and helps to reduce acne breakouts.\\n\\n3. CeraVe Acne Foaming Cream Cleanser: This face wash is formulated with benzoyl peroxide to combat acne and promote a clear complexion. It also contains ceramides and hyaluronic acid to moisturize and protect the skin barrier.\\n\\nRemember, it's important to choose a face wash that suits your specific skin type and concerns. If you have severe acne or any underlying skin conditions, it's best to consult a dermatologist for personalized recommendations.\" additional_kwargs={} example=False\n", |
| 144 | + "\n", |
| 145 | + "\n", |
| 146 | + "Example #2:\n", |
| 147 | + "If you are looking for warm weather and beautiful beaches in December, here are some top destinations to consider:\n", |
| 148 | + "\n", |
| 149 | + "1. Maldives: Known for its crystal-clear waters and luxurious resorts, the Maldives is a perfect winter getaway. With average temperatures of around 27°C (81°F), you can enjoy sunbathing, snorkeling, and diving in the stunning coral reefs.\n", |
| 150 | + "\n", |
| 151 | + "2. Thailand: Thailand offers a variety of beach destinations such as Phuket, Krabi, and Koh Samui. With temperatures ranging from 28-32°C (82-89°F), you can relax on the white sandy beaches, explore vibrant marine life, and indulge in delicious Thai cuisine.\n", |
| 152 | + "\n", |
| 153 | + "3. Bali, Indonesia: Bali is a tropical paradise with warm temperatures around 30°C (86°F) in December. It offers a mix of stunning beaches, lush landscapes, and vibrant culture. You can enjoy surfing, snorkeling, or simply unwind in luxury beach resorts.\n", |
| 154 | + "\n", |
| 155 | + "4. Seychelles: Located in the Indian Ocean, Seychelles is known for its pristine white sand beaches and turquoise waters. With temperatures averaging around 29°C (84°F), you can enjoy swimming, snorkeling, and even spot rare wildlife in the national parks.\n", |
| 156 | + "\n", |
| 157 | + "5. Caribbean Islands: The Caribbean is a popular choice for warm weather and beach vacations in December. Destinations like Barbados, Jamaica, and the Dominican Republic offer beautiful beaches, water sports, and a vibrant island atmosphere.\n", |
141 | 158 | "\n", |
142 | | - "There are a number of good face washes that can help with acne prone skin. A few popular options include the Neutrogena Oil-Free Acne Wash, the Cetaphil Dermatological Gentle Skin Cleanser, and the La Roche-Posay Effaclar Medicated Gel Cleanser.\n" |
| 159 | + "Remember to check travel advisories and local COVID-19 guidelines before planning your trip.\n" |
143 | 160 | ] |
144 | 161 | } |
145 | 162 | ], |
146 | 163 | "source": [ |
147 | 164 | "from langchain import PromptTemplate\n", |
148 | 165 | "\n", |
| 166 | + "from langchain.prompts.chat import (\n", |
| 167 | + " ChatPromptTemplate,\n", |
| 168 | + " SystemMessagePromptTemplate,\n", |
| 169 | + " HumanMessagePromptTemplate,\n", |
| 170 | + ")\n", |
| 171 | + "\n", |
| 172 | + "\n", |
| 173 | + "# First Example\n", |
149 | 174 | "template = \"\"\"\n", |
150 | 175 | "You are a skin care consulant that recommends products based on customer\n", |
151 | 176 | "needs and preferences.\n", |
|
158 | 183 | "template=template,\n", |
159 | 184 | ")\n", |
160 | 185 | "\n", |
161 | | - "print(llm(\n", |
162 | | - " prompt.format(\n", |
| 186 | + "print(\"Example #1:\")\n", |
| 187 | + "print(llm([HumanMessage(content=prompt.format(\n", |
163 | 188 | " product_type=\"face wash\",\n", |
164 | 189 | " customer_request = \"acne prone skin\"\n", |
165 | | - " )\n", |
166 | | - "))" |
| 190 | + " ))]\n", |
| 191 | + "))\n", |
| 192 | + "print(\"\\n\")\n", |
| 193 | + "\n", |
| 194 | + "# Second Example\n", |
| 195 | + "system_message = \"You are an AI assistant travel assistant that provides vacation recommendations.\"\n", |
| 196 | + "\n", |
| 197 | + "system_message_prompt = SystemMessagePromptTemplate.from_template(system_message)\n", |
| 198 | + "human_template=\"{text}\"\n", |
| 199 | + "human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)\n", |
| 200 | + "chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])\n", |
| 201 | + "chain = LLMChain(llm=llm, prompt=chat_prompt)\n", |
| 202 | + "result = chain.run(f\"Where should I go on vaction in Decemember for warm weather and beaches?\")\n", |
| 203 | + "print(\"Example #2:\")\n", |
| 204 | + "print(result)" |
167 | 205 | ] |
168 | 206 | }, |
169 | 207 | { |
|
180 | 218 | }, |
181 | 219 | { |
182 | 220 | "cell_type": "code", |
183 | | - "execution_count": 46, |
| 221 | + "execution_count": 24, |
184 | 222 | "id": "af7c236f", |
185 | 223 | "metadata": {}, |
186 | 224 | "outputs": [], |
187 | 225 | "source": [ |
188 | | - "#from langchain.llms import AzureOpenAI\n", |
189 | | - "from langchain.chains import LLMChain\n", |
190 | | - "# from langchain.prompts import PromptTemplate\n", |
191 | 226 | "from langchain.chains import SimpleSequentialChain" |
192 | 227 | ] |
193 | 228 | }, |
194 | 229 | { |
195 | 230 | "cell_type": "code", |
196 | | - "execution_count": 51, |
| 231 | + "execution_count": 25, |
197 | 232 | "id": "2a4a32f0", |
198 | 233 | "metadata": {}, |
199 | 234 | "outputs": [], |
|
212 | 247 | }, |
213 | 248 | { |
214 | 249 | "cell_type": "code", |
215 | | - "execution_count": 55, |
| 250 | + "execution_count": 26, |
216 | 251 | "id": "6eec47ff", |
217 | 252 | "metadata": {}, |
218 | 253 | "outputs": [], |
|
231 | 266 | }, |
232 | 267 | { |
233 | 268 | "cell_type": "code", |
234 | | - "execution_count": 56, |
| 269 | + "execution_count": 27, |
235 | 270 | "id": "84a15aea", |
236 | 271 | "metadata": {}, |
237 | 272 | "outputs": [], |
|
241 | 276 | }, |
242 | 277 | { |
243 | 278 | "cell_type": "code", |
244 | | - "execution_count": 57, |
| 279 | + "execution_count": 28, |
245 | 280 | "id": "15928f72", |
246 | 281 | "metadata": {}, |
247 | 282 | "outputs": [ |
|
251 | 286 | "text": [ |
252 | 287 | "\n", |
253 | 288 | "\n", |
254 | | - "\u001b[1m> Entering new SimpleSequentialChain chain...\u001b[0m\n" |
255 | | - ] |
256 | | - }, |
257 | | - { |
258 | | - "name": "stdout", |
259 | | - "output_type": "stream", |
260 | | - "text": [ |
261 | | - "\u001b[36;1m\u001b[1;3m\n", |
262 | | - "A simple and fun DIY project for a 5-year-old girl is to make a paper doll. All you need is some paper, scissors, and crayons or markers. First, cut out a paper doll shape from the paper. Then, decorate the paper doll with clothes, hair, and facial features. You can also cut out paper furniture and accessories to create a paper doll scene.\u001b[0m\n", |
263 | | - "\u001b[33;1m\u001b[1;3m\n", |
264 | | - "1. Cut out a paper doll shape from the paper.\n", |
265 | | - "2. Decorate the paper doll with clothes, hair, and facial features.\n", |
266 | | - "3. Cut out paper furniture and accessories to create a paper doll scene.\n", |
| 289 | + "\u001b[1m> Entering new SimpleSequentialChain chain...\u001b[0m\n", |
| 290 | + "\u001b[36;1m\u001b[1;3mDIY Princess Crown Craft Kit: \n", |
| 291 | + "\n", |
| 292 | + "Materials needed:\n", |
| 293 | + "- Cardboard or foam sheet\n", |
| 294 | + "- Scissors\n", |
| 295 | + "- Glue\n", |
| 296 | + "- Decorative items (such as glitter, stickers, feathers, beads, sequins)\n", |
| 297 | + "- Elastic string or ribbon\n", |
| 298 | + "\n", |
| 299 | + "Instructions:\n", |
| 300 | + "1. Cut a strip of cardboard or foam sheet that is long enough to fit around the child's head.\n", |
| 301 | + "2. Measure the strip to the child's head size and cut to adjust if needed.\n", |
| 302 | + "3. Let the child decorate the strip with glue and decorative items of their choice. They can use glitter, stickers, feathers, beads, sequins, or anything else they find appealing.\n", |
| 303 | + "4. Once the decorations are complete, let the glue dry completely.\n", |
| 304 | + "5. Attach elastic string or ribbon to both ends of the strip to create a crown that can be worn on the head.\n", |
| 305 | + "6. Help the child tie the elastic string or ribbon securely, making sure it is not too tight or too loose.\n", |
| 306 | + "7. The princess crown is now ready to be worn and enjoyed! The child can wear it during imaginative play, dress-up sessions, or even for a princess-themed birthday party.\n", |
| 307 | + "\n", |
| 308 | + "Note: Adult supervision may be required for cutting and tying the elastic string/ribbon.\u001b[0m\n", |
| 309 | + "\u001b[33;1m\u001b[1;3mDIY Princess Crown Craft Kit:\n", |
267 | 310 | "\n", |
268 | 311 | "Materials needed:\n", |
| 312 | + "- Cardboard or foam sheet\n", |
| 313 | + "- Scissors\n", |
| 314 | + "- Glue\n", |
| 315 | + "- Decorative items (such as glitter, stickers, feathers, beads, sequins)\n", |
| 316 | + "- Elastic string or ribbon\n", |
| 317 | + "\n", |
| 318 | + "Instructions:\n", |
| 319 | + "1. Cut a strip of cardboard or foam sheet that is long enough to fit around the child's head.\n", |
| 320 | + "2. Measure the strip to the child's head size and cut to adjust if needed.\n", |
| 321 | + "3. Let the child decorate the strip with glue and decorative items of their choice. They can use glitter, stickers, feathers, beads, sequins, or anything else they find appealing.\n", |
| 322 | + "4. Once the decorations are complete, let the glue dry completely.\n", |
| 323 | + "5. Attach elastic string or ribbon to both ends of the strip to create a crown that can be worn on the head.\n", |
| 324 | + "6. Help the child tie the elastic string or ribbon securely, making sure it is not too tight or too loose.\n", |
| 325 | + "7. The princess crown is now ready to be worn and enjoyed! The child can wear it during imaginative play, dress-up sessions, or even for a princess-themed birthday party.\n", |
269 | 326 | "\n", |
270 | | - "-Paper\n", |
271 | | - "-Scissors\n", |
272 | | - "-Crayons or markers\u001b[0m\n", |
| 327 | + "Note: Adult supervision may be required for cutting and tying the elastic string/ribbon.\u001b[0m\n", |
273 | 328 | "\n", |
274 | 329 | "\u001b[1m> Finished chain.\u001b[0m\n" |
275 | 330 | ] |
|
0 commit comments