Spaces:
Running
Running
File size: 19,858 Bytes
adaea7c 63ae673 adaea7c 63ae673 adaea7c 63ae673 adaea7c 63ae673 adaea7c 63ae673 adaea7c 63ae673 adaea7c 63ae673 adaea7c 63ae673 adaea7c 63ae673 adaea7c 63ae673 adaea7c 63ae673 adaea7c 63ae673 adaea7c 986fa13 63ae673 986fa13 63ae673 986fa13 63ae673 986fa13 63ae673 986fa13 63ae673 986fa13 63ae673 986fa13 5f3a430 986fa13 5f3a430 986fa13 adaea7c 63ae673 adaea7c 63ae673 adaea7c 986fa13 adaea7c 986fa13 adaea7c 63ae673 adaea7c 986fa13 adaea7c 986fa13 adaea7c 63ae673 adaea7c 986fa13 adaea7c 986fa13 adaea7c 986fa13 adaea7c 5f3a430 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 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 |
{
"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": [
"<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->"
]
},
{
"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=<class '__main__.ConversationBot'>)"
],
"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=<class '__main__.ConversationBot'>)"
]
},
"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": [
"<div><iframe src=\"http://127.0.0.1:7860/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"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
}
|