File size: 5,763 Bytes
adaea7c
 
 
986fa13
adaea7c
 
986fa13
adaea7c
 
ae2bbf3
63ae673
 
 
 
 
 
 
 
 
ae2bbf3
adaea7c
63ae673
ae2bbf3
adaea7c
ae2bbf3
 
 
986fa13
ae2bbf3
986fa13
 
 
 
 
 
 
 
63ae673
adaea7c
33d21bf
ae2bbf3
33d21bf
 
 
 
 
 
 
 
 
986fa13
33d21bf
 
 
986fa13
ae2bbf3
986fa13
 
 
ae2bbf3
 
 
 
 
 
 
33d21bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae2bbf3
33d21bf
ae2bbf3
 
 
 
986fa13
 
 
 
 
 
 
 
 
 
33d21bf
986fa13
 
 
 
ae2bbf3
 
 
 
 
 
 
 
 
 
 
 
986fa13
ae2bbf3
 
 
 
 
33d21bf
 
 
 
 
ae2bbf3
8f49005
ae2bbf3
 
33d21bf
 
986fa13
 
 
 
 
 
 
 
 
adaea7c
986fa13
 
 
 
 
 
 
 
 
adaea7c
 
 
ae2bbf3
adaea7c
986fa13
2ffe248
 
 
 
 
 
 
 
 
adaea7c
 
 
986fa13
 
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
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/01_app.ipynb.

# %% auto 0
__all__ = ['ConversationBot', 'create_demo']

# %% ../nbs/01_app.ipynb 3
import copy
import os

import gradio as gr
from langchain import LLMChain, OpenAI, PromptTemplate
from langchain.agents import (
    AgentExecutor,
    AgentType,
    OpenAIFunctionsAgent,
    Tool,
    initialize_agent,
    load_tools,
)
from langchain.chains import ConversationChain
from langchain.chat_models import ChatOpenAI
from langchain.memory import ChatMessageHistory, ConversationBufferMemory
from langchain.prompts.chat import (
    ChatPromptTemplate,
    HumanMessagePromptTemplate,
    MessagesPlaceholder,
)
from PIL import Image

import constants
from .engineer_prompt import INIT_PROMPT
from lv_recipe_chatbot.ingredient_vision import (
    SAMPLE_IMG_DIR,
    BlipImageCaptioning,
    VeganIngredientFinder,
    format_image,
)
from .vegan_recipe_tools import vegan_recipe_edamam_search

# %% ../nbs/01_app.ipynb 16
class ConversationBot:
    memory_key: str = "chat_history"

    def __init__(
        self,
        vegan_ingred_finder: VeganIngredientFinder,
        img_cap: BlipImageCaptioning,
        verbose: bool = True,
    ):
        self.llm = ChatOpenAI(temperature=0.1, verbose=verbose)
        self.init_prompt = copy.deepcopy(INIT_PROMPT)
        self.img_cap = img_cap
        self.vegan_ingred_finder = vegan_ingred_finder
        self.verbose = verbose
        init_prompt_msgs = self.init_prompt.messages
        self.ai_prompt_questions = {
            "ingredients": init_prompt_msgs[1],
            "allergies": init_prompt_msgs[3],
            "recipe_open_params": init_prompt_msgs[5],
        }

    def respond(self, user_msg, chat_history):
        response = self._get_bot_response(user_msg, chat_history)
        chat_history.append((user_msg, response))
        return "", chat_history

    def init_agent_executor(self, chat_msgs):
        tools = [vegan_recipe_edamam_search]
        prompt = OpenAIFunctionsAgent.create_prompt(
            system_message=self.init_prompt.messages[0],
            extra_prompt_messages=chat_msgs
            + [MessagesPlaceholder(variable_name=self.memory_key)],
        )
        self.memory = ConversationBufferMemory(
            chat_memory=ChatMessageHistory(messages=chat_msgs),
            return_messages=True,
            memory_key=self.memory_key,
        )
        self.agent_executor = AgentExecutor(
            agent=OpenAIFunctionsAgent(llm=self.llm, tools=tools, prompt=prompt),
            tools=tools,
            memory=self.memory,
            verbose=True,
        )

    def reset(self):
        self.memory.clear()
        self.init_prompt = copy.deepcopy(INIT_PROMPT)

    def run_img(self, image: str):
        desc = self.img_cap.inference(format_image(image))
        answer = self.vegan_ingred_finder.list_ingredients(image)
        msg = f"""I uploaded an image that may contain vegan ingredients.
The description of the image is: `{desc}`.
The extracted ingredients are:
```
{answer}
```"""
        base_prompt = INIT_PROMPT.messages[2].prompt.template
        new_prompt = f"{msg}I may type some more ingredients below.\n{base_prompt}"
        self.init_prompt.messages[2].prompt.template = new_prompt
        return msg

    def _get_bot_response(self, user_msg: str, chat_history) -> str:
        if len(chat_history) < 2:
            return self.ai_prompt_questions["allergies"].prompt.template

        if len(chat_history) < 3:
            return self.ai_prompt_questions["recipe_open_params"].prompt.template

        if len(chat_history) < 4:
            user = 0
            ai = 1
            user_msgs = [msg_pair[user] for msg_pair in chat_history[1:]]
            f_init_prompt = self.init_prompt.format_prompt(
                ingredients=user_msgs[0],
                allergies=user_msgs[1],
                recipe_freeform_input=user_msg,
            )
            chat_msgs = f_init_prompt.to_messages()
            results = self.llm.generate([chat_msgs])
            chat_msgs.append(results.generations[0][0].message)
            # prepare the agent to takeover from this point
            self.init_agent_executor(chat_msgs)
            return self.agent_executor.run("Search for a vegan recipe with that query")

        response = self.agent_executor.run(input=user_msg)
        return response

# %% ../nbs/01_app.ipynb 20
def create_demo(bot: ConversationBot):
    sample_images = []
    all_imgs = [f"{SAMPLE_IMG_DIR}/{img}" for img in os.listdir(SAMPLE_IMG_DIR)]
    for i, img in enumerate(all_imgs):
        if i in [
            1,
            2,
            3,
        ]:
            sample_images.append(img)
    with gr.Blocks() as demo:
        gr_img = gr.Image(type="filepath")
        btn = gr.Button(value="Submit image")
        ingredients_msg = gr.Text(label="Ingredients from image")
        btn.click(bot.run_img, inputs=[gr_img], outputs=[ingredients_msg])
        gr.Examples(
            examples=sample_images,
            inputs=gr_img,
        )

        chatbot = gr.Chatbot(
            value=[(None, bot.ai_prompt_questions["ingredients"].prompt.template)]
        )

        msg = gr.Textbox()
        # clear = gr.Button("Clear")
        gr.Markdown(
            """
        **🔃Refresh the page to start from scratch🔃**  
        
        Recipe search tool powered by the [Edamam API](https://www.edamam.com/)  
        
        ![Edamam Logo](https://www.edamam.com/assets/img/small-logo.png)
        """
        )
        msg.submit(
            fn=bot.respond, inputs=[msg, chatbot], outputs=[msg, chatbot], queue=False
        )
        # clear.click(lambda: None, None, chatbot, queue=False).then(bot.reset)
        return demo