{ "cells": [ { "cell_type": "raw", "metadata": {}, "source": [ "---\n", "description: Gradio app.py\n", "output-file: app.html\n", "title: app\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "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": [ "#: eval: false\n", "load_dotenv()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Put the chat backend pieces together" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "language": "python" }, "outputs": [ { "data": { "text/markdown": [ "---\n", "\n", "### ConversationBufferMemory\n", "\n", "> ConversationBufferMemory\n", "> (chat_memory:langchain.schema.memory.BaseChatMe\n", "> ssageHistory=None,\n", "> output_key:Optional[str]=None,\n", "> input_key:Optional[str]=None,\n", "> return_messages:bool=False,\n", "> human_prefix:str='Human', ai_prefix:str='AI',\n", "> memory_key:str='history')\n", "\n", "Buffer for storing conversation memory." ], "text/plain": [ "---\n", "\n", "### ConversationBufferMemory\n", "\n", "> ConversationBufferMemory\n", "> (chat_memory:langchain.schema.memory.BaseChatMe\n", "> ssageHistory=None,\n", "> output_key:Optional[str]=None,\n", "> input_key:Optional[str]=None,\n", "> return_messages:bool=False,\n", "> human_prefix:str='Human', ai_prefix:str='AI',\n", "> memory_key:str='history')\n", "\n", "Buffer for storing conversation memory." ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#| echo: false\n", "#| output: asis\n", "show_doc(ConversationBufferMemory)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "language": "python" }, "outputs": [ { "data": { "text/markdown": [ "---\n", "\n", "### ChatMessageHistory\n", "\n", "> ChatMessageHistory\n", "> (messages:List[langchain.schema.messages.BaseMessage]\n", "> =[])\n", "\n", "In memory implementation of chat message history.\n", "\n", "Stores messages in an in memory list." ], "text/plain": [ "---\n", "\n", "### ChatMessageHistory\n", "\n", "> ChatMessageHistory\n", "> (messages:List[langchain.schema.messages.BaseMessage]\n", "> =[])\n", "\n", "In memory implementation of chat message history.\n", "\n", "Stores messages in an in memory list." ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#| echo: false\n", "#| output: asis\n", "show_doc(ChatMessageHistory)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "language": "python" }, "outputs": [ { "data": { "text/markdown": [ "---\n", "\n", "### ChatOpenAI\n", "\n", "> ChatOpenAI (cache:Optional[bool]=None, verbose:bool=None, callbacks:Union\n", "> [List[langchain.callbacks.base.BaseCallbackHandler],langchain\n", "> .callbacks.base.BaseCallbackManager,NoneType]=None, callback_\n", "> manager:Optional[langchain.callbacks.base.BaseCallbackManager\n", "> ]=None, tags:Optional[List[str]]=None,\n", "> metadata:Optional[Dict[str,Any]]=None, client:Any=None,\n", "> model:str='gpt-3.5-turbo', temperature:float=0.7,\n", "> model_kwargs:Dict[str,Any]=None,\n", "> openai_api_key:Optional[str]=None,\n", "> openai_api_base:Optional[str]=None,\n", "> openai_organization:Optional[str]=None,\n", "> openai_proxy:Optional[str]=None, request_timeout:Union[float,\n", "> Tuple[float,float],NoneType]=None, max_retries:int=6,\n", "> streaming:bool=False, n:int=1, max_tokens:Optional[int]=None,\n", "> tiktoken_model_name:Optional[str]=None)\n", "\n", "Wrapper around OpenAI Chat large language models.\n", "\n", "To use, you should have the ``openai`` python package installed, and the\n", "environment variable ``OPENAI_API_KEY`` set with your API key.\n", "\n", "Any parameters that are valid to be passed to the openai.create call can be passed\n", "in, even if not explicitly saved on this class.\n", "\n", "Example:\n", " .. code-block:: python\n", "\n", " from langchain.chat_models import ChatOpenAI\n", " openai = ChatOpenAI(model_name=\"gpt-3.5-turbo\")" ], "text/plain": [ "---\n", "\n", "### ChatOpenAI\n", "\n", "> ChatOpenAI (cache:Optional[bool]=None, verbose:bool=None, callbacks:Union\n", "> [List[langchain.callbacks.base.BaseCallbackHandler],langchain\n", "> .callbacks.base.BaseCallbackManager,NoneType]=None, callback_\n", "> manager:Optional[langchain.callbacks.base.BaseCallbackManager\n", "> ]=None, tags:Optional[List[str]]=None,\n", "> metadata:Optional[Dict[str,Any]]=None, client:Any=None,\n", "> model:str='gpt-3.5-turbo', temperature:float=0.7,\n", "> model_kwargs:Dict[str,Any]=None,\n", "> openai_api_key:Optional[str]=None,\n", "> openai_api_base:Optional[str]=None,\n", "> openai_organization:Optional[str]=None,\n", "> openai_proxy:Optional[str]=None, request_timeout:Union[float,\n", "> Tuple[float,float],NoneType]=None, max_retries:int=6,\n", "> streaming:bool=False, n:int=1, max_tokens:Optional[int]=None,\n", "> tiktoken_model_name:Optional[str]=None)\n", "\n", "Wrapper around OpenAI Chat large language models.\n", "\n", "To use, you should have the ``openai`` python package installed, and the\n", "environment variable ``OPENAI_API_KEY`` set with your API key.\n", "\n", "Any parameters that are valid to be passed to the openai.create call can be passed\n", "in, even if not explicitly saved on this class.\n", "\n", "Example:\n", " .. code-block:: python\n", "\n", " from langchain.chat_models import ChatOpenAI\n", " openai = ChatOpenAI(model_name=\"gpt-3.5-turbo\")" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#| echo: false\n", "#| output: asis\n", "show_doc(ChatOpenAI)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "language": "python" }, "outputs": [], "source": [ "#| eval: false\n", "llm = ChatOpenAI(temperature=1)\n", "MEMORY_KEY = \"chat_history\"\n", "chat_msgs = INIT_PROMPT.format_prompt(\n", " ingredients=\"tofu, brocolli\",\n", " allergies=\"\",\n", " recipe_freeform_input=\"The preparation time should be less than 30 minutes. I really love Thai food!\",\n", ")\n", "chat_msgs = chat_msgs.to_messages()\n", "results = llm.generate([chat_msgs])\n", "\n", "chat_msgs.append(results.generations[0][0].message)\n", "tools = [vegan_recipe_edamam_search]\n", "prompt = OpenAIFunctionsAgent.create_prompt(\n", " system_message=INIT_PROMPT.messages[0],\n", " extra_prompt_messages=chat_msgs + [MessagesPlaceholder(variable_name=MEMORY_KEY)],\n", ")\n", "memory = ConversationBufferMemory(\n", " chat_memory=ChatMessageHistory(messages=chat_msgs),\n", " return_messages=True,\n", " memory_key=MEMORY_KEY,\n", ")\n", "agent_executor = AgentExecutor(\n", " agent=OpenAIFunctionsAgent(llm=llm, tools=tools, prompt=prompt),\n", " tools=tools,\n", " memory=memory,\n", " verbose=True,\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "language": "python" }, "outputs": [], "source": [ "# Fails for a weird query\n", "# \"tofu, pickles, mustard, olives, tomatoes, lettuce, bell peppers, carrots, bread\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "language": "python" }, "outputs": [ { "data": { "text/plain": [ "[SystemMessage(content=\"The following is a conversation between a human and a friendly AI chef. \\nThe AI is compassionate to animals.\\nThe AI generates a simple concise keyword query for a vegan recipe, based on the ingredients, allergies, and other preferences the human has, to use in recipe APIs.\\nKnowledge: 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\\nLet's think step by step.\\nIf the human messages are unrelated to vegan recipes, remind them of your purpose to recipes.\\nOnly generate keyword queries as other tools should be used to fetch full recipes.\", additional_kwargs={}),\n", " AIMessage(content='What ingredients do you wish to cook with?', additional_kwargs={}, example=False),\n", " HumanMessage(content='Ingredients: tofu, brocolli', additional_kwargs={}, example=False),\n", " AIMessage(content='Do you have any allergies I should be aware of?', additional_kwargs={}, example=False),\n", " HumanMessage(content='Allergies: ', additional_kwargs={}, example=False),\n", " AIMessage(content='Do you have any preferences I should consider for the recipe such as preparation time, difficulty, or cuisine region?', additional_kwargs={}, example=False),\n", " HumanMessage(content=\"Generate a vegan recipe keyword query that is aligned with the user's allergies and contains at least a few of the ingredients provided (if any).\\nDraw some inspiration from the user's preferences delimited below if any are specified.\\n\\n###\\nPreferences: The preparation time should be less than 30 minutes. I really love Thai food!\\n###\", additional_kwargs={}, example=False),\n", " AIMessage(content='Based on the ingredients, allergies, and preferences you provided, here is a vegan recipe keyword query suggestion: \"vegan Thai tofu broccoli stir fry recipe\"', additional_kwargs={}, example=False)]" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#| eval: false\n", "memory.chat_memory.messages" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "language": "python" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "> Entering new AgentExecutor chain...\n", "\n", "Invoking: `vegan_recipe_edamam_search` with `{'query': 'vegan tofu broccoli'}`\n", "\n", "\n", "[{'label': 'Vegan BBQ teriyaki tofu', 'url': 'https://www.bbcgoodfood.com/recipes/teriyaki-tofu-vegan-barbecue', 'ingredientLines': ['4 tbsp low-salt soy sauce', '2 tbsp soft brown sugar', 'pinch ground ginger', '2 tbsp mirin', '3 tsp sesame oil', '350g block very firm tofu (see tip below) cut into thick slices', '½ tbsp rapeseed oil', '2 courgettes, sliced horizontally into strips', '200g Tenderstem broccoli', 'black and white sesame seeds, to serve'], 'totalTime': 25.0}, {'label': 'Vegan Crispy Stir-Fried Tofu With Broccoli Recipe', 'url': 'http://www.seriouseats.com/recipes/2014/02/vegan-experience-crispy-tofu-broccoli-stir-fry.html', 'ingredientLines': ['1 1/2 quarts vegetable or peanut oil', '1/2 cup plus 2 teaspoons cornstarch, divided', '1/2 cup all-purpose flour', '1/2 teaspoon baking powder', 'Kosher salt', '1/2 cup cold water', '1/2 cup vodka', '1 pound extra-firm tofu, cut into 1/2- by 2- by 1-inch slabs, carefully dried (see note above)', '1 pound broccoli, cut into 1-inch florets', '1/4 cup Xiaoshing wine or dry sherry', '1/4 cup homemade or store-bought low-sodium vegetable stock', '2 tablespoons soy sauce', '1 tablespoon fermented black bean sauce', '2 tablespoons sugar', '1 tablespoon toasted sesame oil', '2 (1-inch) segments lemon peel, plus 2 teaspoons lemon juice', '4 cloves garlic, minced (about 4 teaspoons)', '1 tablespoon minced or grated fresh ginger', '6 scallions, white and light green parts only, finely chopped', '2 tablespoons toasted sesame seeds, divided'], 'totalTime': 30.0}, {'label': 'Thai-Style Chopped Salad with Sriracha Tofu', 'url': 'http://www.eatingwell.com/recipe/276172/thai-style-chopped-salad-with-sriracha-tofu/', 'ingredientLines': ['1 (10 ounce) package kale, Brussels sprout, broccoli and cabbage salad mix', '1 (12 ounce) package frozen shelled edamame, thawed', '2 (7 ounce) packages Sriracha-flavored baked tofu, cubed', '1/2 cup spicy peanut vinaigrette'], 'totalTime': 10.0}]" ] } ], "source": [ "#| eval: false\n", "agent_executor.run(\"Search for vegan recipe\")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "---\n", "\n", "[source](https://gitlab.com/animalequality/lv-recipe-chatbot/blob/main/lv_recipe_chatbot/app.py#L42){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", "\n", "### ConversationBot\n", "\n", "> ConversationBot (verbose=True)\n", "\n", "Initialize self. See help(type(self)) for accurate signature." ], "text/plain": [ "---\n", "\n", "[source](https://gitlab.com/animalequality/lv-recipe-chatbot/blob/main/lv_recipe_chatbot/app.py#L42){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", "\n", "### ConversationBot\n", "\n", "> ConversationBot (verbose=True)\n", "\n", "Initialize self. See help(type(self)) for accurate signature." ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#| echo: false\n", "#| output: asis\n", "show_doc(ConversationBot)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "language": "python" }, "outputs": [ { "data": { "text/plain": [ "Path('/home/evylz/AnimalEquality/lv-recipe-chatbot/assets/images/vegan_ingredients')" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.listdir(SAMPLE_IMG_DIR)\n", "SAMPLE_IMG_DIR" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "language": "python" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 6.19 s, sys: 1.47 s, total: 7.66 s\n", "Wall time: 4.68 s\n" ] } ], "source": [ "#| eval: false" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "language": "python" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I uploaded an image that may contain vegan ingredients.\n", "The description of the image is: `a refrigerator with food inside`.\n", "The extracted ingredients are:\n", "```\n", "cabbage lettuce onion\n", "apples\n", "rice\n", "plant-based milk\n", "```\n", "\n", "CPU times: user 56.7 s, sys: 63.6 ms, total: 56.8 s\n", "Wall time: 5.95 s\n" ] } ], "source": [ "#| eval: false" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "---\n", "\n", "[source](https://gitlab.com/animalequality/lv-recipe-chatbot/blob/main/lv_recipe_chatbot/app.py#L126){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", "\n", "### create_demo\n", "\n", "> create_demo (bot=)" ], "text/plain": [ "---\n", "\n", "[source](https://gitlab.com/animalequality/lv-recipe-chatbot/blob/main/lv_recipe_chatbot/app.py#L126){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n", "\n", "### create_demo\n", "\n", "> create_demo (bot=)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#| echo: false\n", "#| output: asis\n", "show_doc(create_demo)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "language": "python" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Closing server running on port: 7860\n", "Running on local URL: http://127.0.0.1:7860\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#| eval: false\n", "if \"demo\" in globals():\n", " demo.close()\n", "demo = create_demo(bot)\n", "demo.launch()" ] } ], "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 }