{ "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", "import openai\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": [ "#| eval: false\n", "load_dotenv()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#| export\n", "INIT_PROMPT = ChatPromptTemplate.from_messages(\n", " [\n", " SystemMessagePromptTemplate.from_template(\n", " \"\"\"The following is a conversation between a human and a friendly AI chef. \n", "The AI is compassionate to animals.\n", "The AI generates a vegan recipe name, based on the ingredients, allergies, and other preferences the human has, to query recipe APIs.\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 recipes.\n", "Only generate names as tools should be used to fetch full recipes.\"\"\"\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", " \"\"\"Come up with a vegan recipe name to use the user's allergies and contains at least a few of the ingredients provided (if any).\n", "Try to follow the user's preferences delimited below if any are specified.\n", "\n", "###\n", "Preferences: {recipe_freeform_input}\n", "###\n", "\n", "\"\"\"\n", " ),\n", " ]\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Evaluate chat backend " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "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.\n", "The AI generates a vegan recipe name, based on the ingredients, allergies, and other preferences the human has, to query recipe APIs.\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 recipes.\n", "Only generate names as tools should be used to fetch full 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: Come up with a vegan recipe name to use the user's allergies and contains at least a few of the ingredients provided (if any).\n", "Try to follow the user's preferences delimited below if any are specified.\n", "\n", "###\n", "Preferences: The preparation time should be less than 30 minutes. I really love Thai food!\n", "###\n", "\n", "\n", "AI: Based on your preferences and the provided ingredients, I have come up with a vegan recipe name: \"Thai-inspired Tofu Banh Mi\". This recipe combines the flavors of Thai cuisine with the ingredients you mentioned, including tofu, pickles, tomatoes, and bread. It's a delicious and quick vegan sandwich that can be prepared in less than 30 minutes. Enjoy!\n", "Human: Recommend a different recipe please.\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n", "\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.\n", "The AI generates a vegan recipe name, based on the ingredients, allergies, and other preferences the human has, to query recipe APIs.\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 recipes.\n", "Only generate names as tools should be used to fetch full 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: Come up with a vegan recipe name to use the user's allergies and contains at least a few of the ingredients provided (if any).\n", "Try to follow the user's preferences delimited below if any are specified.\n", "\n", "###\n", "Preferences: The preparation time should be less than 30 minutes. I really love Thai food!\n", "###\n", "\n", "\n", "AI: Based on your preferences and the provided ingredients, I have come up with a vegan recipe name: \"Thai-inspired Tofu Banh Mi\". This recipe combines the flavors of Thai cuisine with the ingredients you mentioned, including tofu, pickles, tomatoes, and bread. It's a delicious and quick vegan sandwich that can be prepared in less than 30 minutes. Enjoy!\n", "Human: Recommend a different recipe please.\n", "AI: Based on your preferences and the provided ingredients, another vegan recipe name I suggest is \"Mediterranean Tofu Salad with Tangy Pickle Dressing\". This recipe combines the freshness of Mediterranean flavors with the ingredients you mentioned, including tofu, pickles, olives, tomatoes, lettuce, and bell peppers. The tangy pickle dressing adds a delicious twist to the salad. It's a light and flavorful option that can be prepared in less than 30 minutes. Enjoy!\n", "Human: Aactually how about italian instead?\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n", "Absolutely! How about the recipe name \"Italian Tofu Caprese Sandwich\"? This recipe combines the Italian flavors with tofu and some of the ingredients you mentioned, including tomatoes and bread. It's a vegan twist on the classic Caprese sandwich, featuring fresh tomatoes, tofu, and a drizzle of balsamic glaze. With its vibrant colors and delicious flavors, this Italian-inspired sandwich is sure to satisfy your taste buds. Enjoy!\n" ] } ], "source": [ "#| eval: false\n", "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", "result = conversation.predict(input=\"Aactually how about italian instead?\")\n", "print(result)" ] }, { "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 }