File size: 13,140 Bytes
adaea7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b5c559
986fa13
adaea7c
 
986fa13
cf2b422
 
 
 
63ae673
cf2b422
 
6e5b58a
 
63ae673
 
 
 
 
 
 
 
2ffe248
cf2b422
63ae673
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e5b58a
63ae673
 
 
 
 
 
 
cf2b422
 
 
63ae673
 
6e5b58a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63ae673
 
 
 
cf2b422
63ae673
cf2b422
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e5b58a
cf2b422
 
 
 
6e5b58a
63ae673
 
 
 
 
 
 
 
6e5b58a
 
 
 
 
 
 
 
 
 
 
adaea7c
 
63ae673
 
 
 
cf2b422
63ae673
cf2b422
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63ae673
 
6e5b58a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63ae673
 
 
 
 
33d21bf
 
 
 
6e5b58a
33d21bf
 
 
 
6e5b58a
cf2b422
33d21bf
6e5b58a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf2b422
6e5b58a
cf2b422
6e5b58a
 
cf2b422
6e5b58a
 
 
cf2b422
 
 
 
 
 
 
33d21bf
6e5b58a
 
 
cf2b422
 
 
6e5b58a
cf2b422
 
 
6e5b58a
 
 
 
 
adaea7c
6e5b58a
 
 
 
986fa13
 
 
 
 
 
6e5b58a
986fa13
6e5b58a
 
 
 
 
 
cf2b422
 
 
 
 
 
 
 
 
6e5b58a
 
 
 
 
 
 
 
2ffe248
6e5b58a
2ffe248
 
 
cf2b422
2ffe248
6e5b58a
986fa13
 
adaea7c
 
6e5b58a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
adaea7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf2b422
adaea7c
cf2b422
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
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# app\n",
    "\n",
    "> Gradio app.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#| default_exp app"
   ]
  },
  {
   "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 copy\n",
    "import os\n",
    "import gradio as gr\n",
    "import constants\n",
    "from lv_recipe_chatbot.vegan_recipe_assistant import (\n",
    "    SYSTEM_PROMPT,\n",
    "    vegan_recipe_edamam_search,\n",
    "    VEGAN_RECIPE_SEARCH_TOOL_SCHEMA,\n",
    ")\n",
    "from openai import OpenAI, AssistantEventHandler\n",
    "from typing_extensions import override\n",
    "import json\n",
    "from functools import partial"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#| hide\n",
    "import time\n",
    "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": "markdown",
   "metadata": {},
   "source": [
    "[GPT4 streaming output example on hugging face 🤗](https://huggingface.co/spaces/ysharma/ChatGPT4/blob/main/app.pyhttps://huggingface.co/spaces/ysharma/ChatGPT4/blob/main/app.py)  \n",
    "[Gradio lite let's you insert Gradio app in browser JS](https://www.gradio.app/guides/gradio-litehttps://www.gradio.app/guides/gradio-lite)  \n",
    "[Streaming output](https://www.gradio.app/main/guides/streaming-outputshttps://www.gradio.app/main/guides/streaming-outputs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#| eval: false\n",
    "client = OpenAI()\n",
    "assistant = client.beta.assistants.create(\n",
    "    name=\"Vegan Recipe Finder\",\n",
    "    instructions=SYSTEM_PROMPT,\n",
    "    # + \"\\nChoose the best single matching recipe to the user's query out of the vegan recipe search returned recipes\",\n",
    "    model=\"gpt-4o\",\n",
    "    tools=[VEGAN_RECIPE_SEARCH_TOOL_SCHEMA],\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class EventHandler(AssistantEventHandler):\n",
    "    @override\n",
    "    def on_event(self, event):\n",
    "        # Retrieve events that are denoted with 'requires_action'\n",
    "        # since these will have our tool_calls\n",
    "        if event.event == \"thread.run.requires_action\":\n",
    "            run_id = event.data.id  # Retrieve the run ID from the event data\n",
    "            self.handle_requires_action(event.data, run_id)\n",
    "\n",
    "    def handle_requires_action(self, data, run_id):\n",
    "        tool_outputs = []\n",
    "        for tool_call in data.required_action.submit_tool_outputs.tool_calls:\n",
    "            if tool_call.function.name == \"vegan_recipe_edamam_search\":\n",
    "                fn_args = json.loads(tool_call.function.arguments)\n",
    "                data = vegan_recipe_edamam_search(\n",
    "                    query=fn_args.get(\"query\"),\n",
    "                )\n",
    "                tool_outputs.append({\"tool_call_id\": tool_call.id, \"output\": data})\n",
    "\n",
    "        self.submit_tool_outputs(tool_outputs, run_id)\n",
    "\n",
    "    def submit_tool_outputs(self, tool_outputs, run_id):\n",
    "        client.beta.threads.runs.submit_tool_outputs_stream(\n",
    "            thread_id=self.current_run.thread_id,\n",
    "            run_id=self.current_run.id,\n",
    "            tool_outputs=tool_outputs,\n",
    "            event_handler=EventHandler(),\n",
    "        )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#| export\n",
    "def handle_requires_action(data):\n",
    "    tool_outputs = []\n",
    "    for tool_call in data.required_action.submit_tool_outputs.tool_calls:\n",
    "        if tool_call.function.name == \"vegan_recipe_edamam_search\":\n",
    "            fn_args = json.loads(tool_call.function.arguments)\n",
    "            data = vegan_recipe_edamam_search(\n",
    "                query=fn_args.get(\"query\"),\n",
    "            )\n",
    "            tool_outputs.append({\"tool_call_id\": tool_call.id, \"output\": data})\n",
    "    return tool_outputs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def run_conversation() -> str:\n",
    "    run = client.beta.threads.runs.create_and_poll(\n",
    "        thread_id=thread.id,\n",
    "        assistant_id=assistant.id,\n",
    "    )\n",
    "    while True:\n",
    "        tool_outputs = []\n",
    "        tool_calls = (\n",
    "            []\n",
    "            if not run.required_action\n",
    "            else run.required_action.submit_tool_outputs.tool_calls\n",
    "        )\n",
    "\n",
    "        for tool_call in tool_calls:\n",
    "            if tool_call.function.name == \"vegan_recipe_edamam_search\":\n",
    "                fn_args = json.loads(tool_call.function.arguments)\n",
    "                data = vegan_recipe_edamam_search(\n",
    "                    query=fn_args.get(\"query\"),\n",
    "                )\n",
    "                tool_outputs.append({\"tool_call_id\": tool_call.id, \"output\": data})\n",
    "\n",
    "        if tool_outputs:\n",
    "            try:\n",
    "                run = client.beta.threads.runs.submit_tool_outputs_and_poll(\n",
    "                    thread_id=thread.id,\n",
    "                    run_id=run.id,\n",
    "                    tool_outputs=tool_outputs,\n",
    "                )\n",
    "                print(\"Tool outputs submitted successfully.\")\n",
    "\n",
    "            except Exception as e:\n",
    "                print(\"Failed to submit tool outputs:\", e)\n",
    "                return \"Sorry failed to run tools. Try again with a different query.\"\n",
    "\n",
    "        if run.status == \"completed\":\n",
    "            messages = client.beta.threads.messages.list(thread_id=thread.id)\n",
    "            data = messages.data\n",
    "            content = data[0].content\n",
    "            return content[0].text.value\n",
    "        time.sleep(0.05)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#| export\n",
    "def run_convo_stream(thread, content: str, client: OpenAI, assistant):\n",
    "    message = client.beta.threads.messages.create(\n",
    "        thread_id=thread.id,\n",
    "        role=\"user\",\n",
    "        content=content,\n",
    "    )\n",
    "    stream = client.beta.threads.runs.create(\n",
    "        thread_id=thread.id,\n",
    "        assistant_id=assistant.id,\n",
    "        stream=True,\n",
    "    )\n",
    "    for event in stream:\n",
    "        if event.event == \"thread.message.delta\":\n",
    "            yield event.data.delta.content[0].text.value\n",
    "\n",
    "        if event.event == \"thread.run.requires_action\":\n",
    "            tool_outputs = handle_requires_action(event.data)\n",
    "            stream = client.beta.threads.runs.submit_tool_outputs(\n",
    "                run_id=event.data.id,\n",
    "                thread_id=thread.id,\n",
    "                tool_outputs=tool_outputs,\n",
    "                stream=True,\n",
    "            )\n",
    "            for event in stream:\n",
    "                if event.event == \"thread.message.delta\":\n",
    "                    yield event.data.delta.content[0].text.value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "skip\n"
     ]
    }
   ],
   "source": [
    "%%script echo  skip\n",
    "thread = client.beta.threads.create()\n",
    "\n",
    "test_msgs = [\n",
    "    \"Hello\",\n",
    "    \"What can I make with tempeh, whole wheat bread, and lettuce?\",\n",
    "]\n",
    "for m in test_msgs:\n",
    "    for txt in run_convo_stream(thread, m, client, assistant):\n",
    "        print(txt, end=\"\")\n",
    "    print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#| export\n",
    "def predict(message, history, client: OpenAI, assistant, thread):\n",
    "    # note that history is a flat list of text messages\n",
    "    reply = \"\"\n",
    "    files = message[\"files\"]\n",
    "    txt = message[\"text\"]\n",
    "\n",
    "    if files:\n",
    "        if files[-1].split(\".\")[-1] not in [\"jpg\", \"png\", \"jpeg\", \"webp\"]:\n",
    "            return \"Sorry only accept image files\"\n",
    "\n",
    "        file = message[\"files\"][-1]\n",
    "        file = client.files.create(\n",
    "            file=open(\n",
    "                file,\n",
    "                \"rb\",\n",
    "            ),\n",
    "            purpose=\"vision\",\n",
    "        )\n",
    "\n",
    "        for reply_txt in run_convo_stream(\n",
    "            thread,\n",
    "            content=[\n",
    "                {\n",
    "                    \"type\": \"text\",\n",
    "                    \"text\": \"What vegan ingredients do you see in this image? Also list out a few combinations of the ingredients that go well together. Lastly, suggest a recipe based on one of those combos using the vegan recipe seach tool.\",\n",
    "                },\n",
    "                {\"type\": \"image_file\", \"image_file\": {\"file_id\": file.id}},\n",
    "            ],\n",
    "            client=client,\n",
    "            assistant=assistant,\n",
    "        ):\n",
    "            reply += reply_txt\n",
    "            yield reply\n",
    "\n",
    "    elif txt:\n",
    "        for reply_txt in run_convo_stream(thread, txt, client, assistant):\n",
    "            reply += reply_txt\n",
    "            yield reply"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#| export\n",
    "def create_demo(client: OpenAI, assistant):\n",
    "    # https://www.gradio.app/main/guides/creating-a-chatbot-fast#customizing-your-chatbot\n",
    "    # on chatbot start/ first msg after clear\n",
    "    thread = client.beta.threads.create()\n",
    "\n",
    "    # sample_images = []\n",
    "    # all_imgs = [f\"{SAMPLE_IMG_DIR}/{img}\" for img in os.listdir(SAMPLE_IMG_DIR)]\n",
    "    # for i, img in enumerate(all_imgs):\n",
    "    #     if i in [\n",
    "    #         1,\n",
    "    #         2,\n",
    "    #         3,\n",
    "    #     ]:\n",
    "    #         sample_images.append(img)\n",
    "    pred = partial(predict, client=client, assistant=assistant, thread=thread)\n",
    "    with gr.ChatInterface(\n",
    "        fn=pred,\n",
    "        multimodal=True,\n",
    "        chatbot=gr.Chatbot(\n",
    "            placeholder=\"Hello!\\nI am a animal advocate AI that is capable of recommending vegan recipes.\\nUpload an image or write a message below to get started!\"\n",
    "        ),\n",
    "    ) as demo:\n",
    "        gr.Markdown(\n",
    "            \"\"\"🔃 **Refresh the page to start from scratch**  \n",
    "        \n",
    "        Recipe search tool powered by the [Edamam API](https://www.edamam.com/)  \n",
    "        \n",
    "        ![Edamam Logo](https://www.edamam.com/assets/img/small-logo.png)\"\"\"\n",
    "        )\n",
    "\n",
    "        # clear.click(lambda: None, None, chatbot, queue=False).then(bot.reset)\n",
    "        return demo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "skip\n"
     ]
    }
   ],
   "source": [
    "%%script echo skip\n",
    "if \"demo\" in globals():\n",
    "    demo.close()\n",
    "\n",
    "demo = create_demo(client, assistant)\n",
    "demo.launch()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#| hide\n",
    "import nbdev\n",
    "\n",
    "nbdev.nbdev_export()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "local-lv-chatbot",
   "language": "python",
   "name": "local-lv-chatbot"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}