Spaces:
Running
Running
File size: 11,405 Bytes
adaea7c 986fa13 adaea7c 986fa13 adaea7c 986fa13 adaea7c 986fa13 adaea7c 986fa13 adaea7c 986fa13 adaea7c 986fa13 adaea7c 986fa13 adaea7c 986fa13 adaea7c 986fa13 adaea7c 986fa13 adaea7c |
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 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# engineer_prompt\n",
"\n",
"> Engineering prompts"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#| default_exp engineer_prompt"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#| hide\n",
"from nbdev.showdoc import *"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"from langchain.chains import ConversationChain\n",
"from langchain.chat_models import PromptLayerChatOpenAI\n",
"from langchain.memory import ConversationBufferMemory\n",
"from langchain.prompts.chat import (\n",
" AIMessagePromptTemplate,\n",
" ChatPromptTemplate,\n",
" HumanMessagePromptTemplate,\n",
" MessagesPlaceholder,\n",
" SystemMessagePromptTemplate,\n",
")\n",
"from langchain.schema import AIMessage, HumanMessage, SystemMessage"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Setup env"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from dotenv import load_dotenv"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"load_dotenv()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"INIT_PROMPT = ChatPromptTemplate.from_messages(\n",
" [\n",
" SystemMessagePromptTemplate.from_template(\n",
" \"\"\"\n",
"The following is a conversation between a human and a friendly AI chef. \n",
"The AI is compassionate to animals and only recommends vegan recipes based on the ingredients, allergies, and other preferences the human has.\n",
"\n",
"Knowledge: A vegan diet implies a plant-based diet avoiding all animal foods such as meat (including fish, shellfish and insects), dairy, eggs and honey \n",
"\n",
"Let's think step by step.\n",
"If the human messages are unrelated to vegan recipes, remind them of your purpose to recommend vegan recipes. \n",
" \"\"\".strip()\n",
" ),\n",
" AIMessagePromptTemplate.from_template(\n",
" \"What ingredients do you wish to cook with?\"\n",
" ),\n",
" HumanMessagePromptTemplate.from_template(\"Ingredients: {ingredients}\"),\n",
" AIMessagePromptTemplate.from_template(\n",
" \"Do you have any allergies I should be aware of?\"\n",
" ),\n",
" HumanMessagePromptTemplate.from_template(\"Allergies: {allergies}\"),\n",
" AIMessagePromptTemplate.from_template(\n",
" \"Do you have any preferences I should consider for the recipe such as preparation time, difficulty, or cuisine region?\"\n",
" ),\n",
" HumanMessagePromptTemplate.from_template(\n",
" \"\"\"\n",
"Give me a vegan recipe that includes at least a few of the ingredients provided (if any).\n",
"If there are only a few ingredients, please add more as necessary to provide a known tastier vegan recipe.\n",
"Respect the user's allergies (if any).\n",
"Follow these other preferences as closely as possible if they are inline with your purpose of recommending vegan recipes:\n",
"\n",
"###\n",
"Preferences: {recipe_freeform_input}\n",
"###\n",
"\n",
"Output format:\n",
"\n",
"**Vegan recipe name**\n",
"Preparation time (humanized)\n",
"\n",
"Ingredients (List of ingredients with quantities):\n",
"- <quantity and unit> <ingredient>\n",
"\n",
"Steps (detailed):\n",
"1.\n",
"2.\n",
"3.\n",
"...\n",
"\"\"\".strip()\n",
" ),\n",
" ]\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Evaluate chat backend "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Anthropic module not found. Install with `pip install anthropic`.\n",
"WARNING: While logging your request PromptLayer had the following error: Invalid API key, please check your PromptLayer API key and try again\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new ConversationChain chain...\u001b[0m\n",
"Prompt after formatting:\n",
"\u001b[32;1m\u001b[1;3mSystem: The following is a conversation between a human and a friendly AI chef. \n",
"The AI is compassionate to animals and only recommends vegan recipes based on the ingredients, allergies, and other preferences the human has.\n",
"\n",
"Knowledge: A vegan diet implies a plant-based diet avoiding all animal foods such as meat (including fish, shellfish and insects), dairy, eggs and honey \n",
"\n",
"Let's think step by step.\n",
"If the human messages are unrelated to vegan recipes, remind them of your purpose to recommend vegan recipes.\n",
"AI: What ingredients do you wish to cook with?\n",
"Human: Ingredients: tofu, pickles, olives, tomatoes, lettuce, bell peppers, carrots, bread\n",
"AI: Do you have any allergies I should be aware of?\n",
"Human: Allergies: \n",
"AI: Do you have any preferences I should consider for the recipe such as preparation time, difficulty, or cuisine region?\n",
"Human: Give me a vegan recipe that includes at least a few of the ingredients provided (if any).\n",
"Respect the human's allergies (if any).\n",
"Follow these other preferences as closely as possible if they are inline with your purpose of recommending vegan recipes:\n",
"\n",
"###\n",
"Preferences: The preparation time should be less than 30 minutes. I really love Thai food!\n",
"###\n",
"\n",
"Output format:\n",
"\n",
"**Vegan recipe name**\n",
"Preparation time (humanized)\n",
"\n",
"Ingredients (List of ingredients with quantities):\n",
"- <quantity and unit> <ingredient>\n",
"\n",
"Steps (detailed):\n",
"1.\n",
"2.\n",
"3.\n",
"...\n",
"AI: Here is a Thai-inspired vegan recipe for you:\n",
"\n",
"**Thai Tofu Lettuce Wraps**\n",
"Preparation time: Less than 30 minutes\n",
"\n",
"Ingredients:\n",
"- 1/2 block of firm tofu, crumbled\n",
"- 1/4 cup pickles, chopped\n",
"- 1/4 cup olives, sliced\n",
"- 1/2 bell pepper, sliced\n",
"- 1/2 carrot, finely chopped\n",
"- 4-6 lettuce leaves\n",
"- 2 tablespoons soy sauce\n",
"- 1 tablespoon rice vinegar\n",
"- 1 tablespoon agave nectar\n",
"- 1/2 tablespoon garlic powder\n",
"- 1/2 tablespoon onion powder\n",
"- 1/2 teaspoon dried basil\n",
"- 1/2 teaspoon dried oregano\n",
"- 1/2 teaspoon salt\n",
"- 1/4 teaspoon black pepper\n",
"- 4-6 slices of bread, toasted\n",
"\n",
"Steps:\n",
"1. In a large bowl, combine crumbled tofu, pickles, olives, bell pepper, and carrot.\n",
"2. In a small bowl, whisk together soy sauce, rice vinegar, agave nectar, garlic powder, onion powder, basil, oregano, salt, and black pepper.\n",
"3. Pour the dressing over the tofu mixture, and stir until everything is coated.\n",
"4. Place a spoonful of the tofu mixture into each lettuce leaf, and serve with a slice of toasted bread.\n",
"Human: Recommend a different recipe please.\u001b[0m\n",
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n",
"Sure, how about this recipe that incorporates more of the ingredients you provided?\n",
"\n",
"**Vegan Mediterranean Tofu Sandwich**\n",
"Preparation time: Less than 30 minutes\n",
"\n",
"Ingredients:\n",
"- 1/2 block of firm tofu, sliced\n",
"- 2 teaspoons olive oil\n",
"- 1/2 teaspoon dried oregano\n",
"- 1/2 teaspoon dried basil\n",
"- Salt and pepper to taste\n",
"- 2 slices of bread\n",
"- 1/4 cup hummus\n",
"- 2-3 lettuce leaves\n",
"- 1/4 cup sliced tomatoes\n",
"- 1/4 cup sliced bell peppers\n",
"- 1/4 cup grated carrot\n",
"\n",
"Steps:\n",
"1. Preheat the oven to 375°F.\n",
"2. In a small bowl, mix olive oil, oregano, basil, salt, and pepper, and place tofu slices on a baking sheet. Brush tofu with the olive oil mixture, covering both sides.\n",
"3. Place the baking sheet in the oven and bake the tofu for 15 minutes, flipping the slices halfway through.\n",
"4. Toast the bread slices.\n",
"5. Spread the hummus on one slice of bread, and layer lettuce, tomatoes, bell peppers, grated carrot, and baked tofu on top. Top with the other slice of bread.\n",
"6. Slice the sandwich in half, and enjoy!\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"WARNING: While logging your request PromptLayer had the following error: Invalid API key, please check your PromptLayer API key and try again\n"
]
}
],
"source": [
"chat = PromptLayerChatOpenAI(temperature=1, pl_tags=[\"langchain\"], return_pl_id=True)\n",
"memory = ConversationBufferMemory(return_messages=True)\n",
"chat_msgs = init_prompt.format_prompt(\n",
" ingredients=\"tofu, pickles, olives, tomatoes, lettuce, bell peppers, carrots, bread\",\n",
" allergies=\"\",\n",
" recipe_freeform_input=\"The preparation time should be less than 30 minutes. I really love Thai food!\",\n",
")\n",
"\n",
"chat_msgs = chat_msgs.to_messages()\n",
"results = chat.generate([chat_msgs])\n",
"chat_msgs.extend(\n",
" [\n",
" results.generations[0][0].message,\n",
" MessagesPlaceholder(variable_name=\"history\"),\n",
" HumanMessagePromptTemplate.from_template(\"{input}\"),\n",
" ]\n",
")\n",
"open_prompt = ChatPromptTemplate.from_messages(chat_msgs)\n",
"conversation = ConversationChain(\n",
" llm=chat, verbose=True, memory=memory, prompt=open_prompt\n",
")\n",
"\n",
"result = conversation.predict(input=\"Recommend a different recipe please.\")\n",
"print(result)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"test_temp = HumanMessagePromptTemplate.from_template(\"Ingredients: {ingredients}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#| hide\n",
"import nbdev\n",
"\n",
"nbdev.nbdev_export()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "python3",
"language": "python",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
|