{ "cells": [ { "cell_type": "raw", "metadata": {}, "source": [ "---\n", "description: Engineering prompts\n", "output-file: engineer_prompt.html\n", "title: engineer_prompt\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Setup env" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "language": "python" }, "outputs": [], "source": [ "from dotenv import load_dotenv" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "language": "python" }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "load_dotenv()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Evaluate chat backend " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "language": "python" }, "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", "> Entering new ConversationChain chain...\n", "Prompt after formatting:\n", "System: 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", "- \n", "\n", "Steps (detailed):\n", "1.\n", "2.\n", "3.\n", "...\n", "AI: Sure! How about a vegan Thai tofu lettuce wraps recipe? It should take less than 30 minutes to prepare.\n", "\n", "**Vegan Thai Tofu Lettuce Wraps**\n", "Preparation time: 25 minutes\n", "\n", "Ingredients:\n", "- 1 package firm tofu\n", "- 1/4 cup vegan fish sauce (you can use soy sauce as a substitute)\n", "- 2 tablespoons maple syrup\n", "- 1 tablespoon lime juice\n", "- 1 teaspoon chili paste\n", "- 1 bell pepper, sliced\n", "- 2 carrots, julienned\n", "- 1/4 cup sliced pickles\n", "- 1/4 cup sliced olives\n", "- 6 large lettuce leaves\n", "- 1/4 cup chopped fresh cilantro\n", "\n", "Steps:\n", "1. Preheat a skillet over medium-high heat and add cubed tofu. Cook for about 5-7 minutes, flipping occasionally, until all the pieces are golden brown. Set aside.\n", "2. In a mixing bowl, whisk together the fish sauce (or soy sauce), maple syrup, lime juice, and chili paste until combined.\n", "3. In a large mixing bowl, add the bell pepper, carrots, pickles, olives, and cooked tofu. Pour the dressing over the ingredients, and toss until everything is evenly coated with the sauce.\n", "4. Assemble the lettuce wraps by separating the lettuce leaves and spooning the tofu mixture into each one. Top with fresh cilantro and serve immediately. Enjoy your delicious vegan Thai tofu lettuce wraps!\n", "Human: Recommend a different recipe please.\n", "\n", "> Finished chain.\n", "Of course! How about a Mediterranean-inspired sandwich that includes tofu, olives, and tomatoes?\n", "\n", "**Mediterranean Tofu Sandwich**\n", "Preparation time: 20 minutes\n", "\n", "Ingredients:\n", "- 1 package extra-firm tofu, drained and sliced into 1/2-inch slices\n", "- 2 tablespoons olive oil\n", "- 1 teaspoon dried oregano\n", "- Salt and pepper\n", "- 1/4 cup vegan mayo\n", "- 1 garlic clove, minced\n", "- 2 teaspoons lemon juice\n", "- 1 large tomato, sliced\n", "- 1/4 cup sliced kalamata olives\n", "- 1/4 cup chopped lettuce\n", "- 1/4 cup sliced pickles\n", "- Bread of your choice (I recommend ciabatta)\n", "\n", "Steps:\n", "1. Preheat a skillet over medium-high heat and add the olive oil.\n", "2. Season the tofu slices with oregano, salt, and pepper, then add them to the skillet. Cook for about 5-7 minutes on each side or until golden brown. Set aside.\n", "3. In a small bowl, mix the vegan mayo, minced garlic, and lemon juice. Spread the mixture onto the slices of bread.\n", "4. Layer the tomato slices, cooked tofu, olives, lettuce, and pickles onto the bread. Add salt and pepper to taste.\n", "5. Top with the other slice of bread, slice in half, and enjoy your delicious Mediterranean tofu sandwich!\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)" ] } ], "metadata": { "kernelspec": { "display_name": "python3", "language": "python", "name": "python3" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 4 }