File size: 25,354 Bytes
b5100ba
 
f098c5a
 
 
 
 
 
 
b5100ba
 
72b38a2
b5100ba
 
 
 
 
 
 
 
 
 
 
 
72b38a2
b5100ba
 
 
 
 
 
 
 
72b38a2
b5100ba
72b38a2
b5100ba
 
 
 
 
 
 
72b38a2
cf6b047
 
 
f098c5a
 
 
cf6b047
 
 
 
 
 
 
 
 
 
f098c5a
 
 
 
cf6b047
 
 
 
72b38a2
b5100ba
 
 
 
 
 
 
 
cf6b047
b5100ba
 
 
 
 
 
 
72b38a2
b5100ba
 
 
 
f098c5a
cf6b047
b5100ba
f098c5a
b5100ba
 
 
cf6b047
72b38a2
b5100ba
cf6b047
b5100ba
f098c5a
 
cf6b047
 
 
 
72b38a2
cf6b047
 
 
f098c5a
b5100ba
 
 
f098c5a
b5100ba
 
f098c5a
b5100ba
 
 
 
72b38a2
cf6b047
 
 
f098c5a
 
 
 
cf6b047
 
 
 
72b38a2
b5100ba
 
72b38a2
 
 
 
 
 
 
 
b5100ba
 
 
f098c5a
 
 
 
 
72b38a2
f098c5a
 
 
 
 
 
 
 
 
 
 
 
72b38a2
 
b5100ba
 
72b38a2
b5100ba
 
 
 
 
f098c5a
 
 
 
 
72b38a2
f098c5a
 
 
 
 
 
 
 
72b38a2
f098c5a
 
 
 
 
 
72b38a2
f098c5a
72b38a2
 
f098c5a
72b38a2
 
f098c5a
72b38a2
 
f098c5a
72b38a2
 
f098c5a
72b38a2
 
 
 
 
f098c5a
72b38a2
 
 
 
 
 
 
 
 
f098c5a
 
 
 
 
 
 
 
 
 
72b38a2
f098c5a
 
 
 
 
 
 
 
72b38a2
f098c5a
 
 
 
 
 
 
 
 
72b38a2
 
 
f098c5a
72b38a2
f098c5a
72b38a2
 
 
 
 
f098c5a
72b38a2
 
f098c5a
72b38a2
f098c5a
 
 
 
 
 
cf6b047
 
 
 
 
 
f098c5a
cf6b047
 
 
 
 
 
 
72b38a2
cf6b047
 
 
 
 
 
 
 
 
 
b5100ba
 
 
 
 
 
 
 
 
 
 
72b38a2
b5100ba
 
 
 
 
 
cf6b047
b5100ba
f098c5a
 
 
b5100ba
 
 
 
 
 
f098c5a
b5100ba
 
 
cf6b047
72b38a2
b5100ba
f098c5a
 
 
 
 
 
 
 
 
b5100ba
f098c5a
cf6b047
 
f098c5a
 
 
 
 
 
 
 
72b38a2
f098c5a
 
 
b5100ba
f098c5a
b5100ba
 
 
 
72b38a2
b5100ba
cf6b047
 
f098c5a
 
 
 
 
cf6b047
 
b5100ba
f098c5a
 
b5100ba
 
 
 
72b38a2
b5100ba
f098c5a
 
 
 
 
 
 
 
72b38a2
f098c5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5100ba
f098c5a
 
 
 
 
 
 
 
 
b5100ba
 
 
 
72b38a2
b5100ba
72b38a2
 
 
 
 
 
 
 
 
 
b5100ba
cf6b047
f098c5a
cf6b047
 
f098c5a
cf6b047
 
 
 
 
b5100ba
 
72b38a2
b5100ba
f098c5a
 
 
 
72b38a2
f098c5a
 
72b38a2
f098c5a
 
 
 
b5100ba
cf6b047
 
 
 
f098c5a
cf6b047
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f098c5a
b5100ba
 
 
 
72b38a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5100ba
f098c5a
b5100ba
cf6b047
 
b5100ba
 
 
 
72b38a2
cf6b047
 
 
 
 
 
 
 
 
72b38a2
b5100ba
 
 
 
 
 
cf6b047
 
 
 
 
 
 
 
72b38a2
 
 
 
cf6b047
 
 
b5100ba
 
 
 
cf6b047
 
 
 
b5100ba
 
 
 
72b38a2
cf6b047
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72b38a2
b5100ba
 
 
cf6b047
 
 
 
 
 
 
72b38a2
f098c5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72b38a2
f098c5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf6b047
72b38a2
cf6b047
f098c5a
 
72b38a2
f098c5a
 
72b38a2
 
f098c5a
 
 
72b38a2
 
 
 
f098c5a
cf6b047
 
 
b5100ba
 
 
f098c5a
b5100ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11c18c4
b5100ba
 
 
 
 
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Adobe research unit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from getpass import getpass\n",
    "\n",
    "from dotenv import load_dotenv\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pstuts_rag"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from uuid import uuid4\n",
    "\n",
    "unique_id = uuid4().hex[0:8]\n",
    "\n",
    "load_dotenv()\n",
    "\n",
    "def set_api_key_if_not_present(key_name, prompt_message=\"\"):\n",
    "    if len(prompt_message) == 0:\n",
    "        prompt_message=key_name\n",
    "    if key_name not in os.environ or not os.environ[key_name]:\n",
    "        os.environ[key_name] = getpass.getpass(prompt_message)\n",
    "\n",
    "set_api_key_if_not_present(\"OPENAI_API_KEY\")\n",
    "set_api_key_if_not_present(\"TAVILY_API_KEY\")\n",
    "os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n",
    "os.environ[\"LANGCHAIN_PROJECT\"] = f\"AIE - MBUDISIC - CERT - {unique_id}\"\n",
    "set_api_key_if_not_present(\"LANGCHAIN_API_KEY\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataclasses import dataclass\n",
    "@dataclass\n",
    "class ApplicationParameters:\n",
    "    filename = \"data/test.json\"\n",
    "    embedding_model = \"text-embedding-3-small\"\n",
    "    tool_calling_model = \"gpt-4.1-mini\"\n",
    "    n_context_docs = 2\n",
    "\n",
    "params = ApplicationParameters()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "from langchain_core.messages import  HumanMessage\n",
    "from langchain_openai.chat_models import ChatOpenAI\n",
    "\n",
    "from pstuts_rag.agents import PsTutsTeamState\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_query = PsTutsTeamState(messages=[HumanMessage(content=\"What are layers?\")],\n",
    "                             team_members=[], next=\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "llm_tool_calling = ChatOpenAI(model=params.tool_calling_model,temperature=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Tavily"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pstuts_rag.agent_tavily import create_tavily_node\n",
    "\n",
    "\n",
    "adobe_help_node, adobe_help_agent,adobe_search = create_tavily_node(llm=llm_tool_calling,name=\"AdobeHelp\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_7954/3445616894.py:1: LangChainDeprecationWarning: The method `BaseTool.__call__` was deprecated in langchain-core 0.1.47 and will be removed in 1.0. Use :meth:`~invoke` instead.\n",
      "  adobe_search(\"What is crop?\")\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[{'title': 'Top 4 reasons to crop your photo',\n",
       "  'url': 'https://helpx.adobe.com/lv/photoshop/how-to/cropping-photo-basics.html',\n",
       "  'content': 'Cropping is the easiest way to remove unwanted objects or people at the edges of a photograph. Anything outside the crop boundary will disappear from your image',\n",
       "  'score': 0.585789},\n",
       " {'title': 'Crop, resize, and resample images in Photoshop Elements',\n",
       "  'url': 'https://helpx.adobe.com/photoshop-elements/kb/crop-resize-resample-photoshop-elements.html',\n",
       "  'content': 'When you crop an image, you trim away material from the edges to show a smaller area, often for artistic reasons.',\n",
       "  'score': 0.585789},\n",
       " {'title': 'How to crop and straighten photos in Photoshop',\n",
       "  'url': 'https://helpx.adobe.com/photoshop/using/crop-straighten-photos.html',\n",
       "  'content': \"Try it in the app\\nYou'll get a sample file to follow along with as you learn how to crop photos.\\nOpen Photoshop\\nCropping is the process of removing portions of a photo to create focus or strengthen the composition. Use the Crop tool to crop and straighten photos in Photoshop. The Crop tool is non-destructive, and you can choose to retain the cropped pixels to optimize the crop boundaries later. The Crop tool also provides intuitive methods to straighten a photo while cropping. [...] Press Enter (Windows) or Return (Mac OS) to crop the photo.\\n\\n\\nContent-Aware Fill on Crop\\nIntroduced in Photoshop CC 2015.5 release\\nPhotoshop now uses content-aware technology to intelligently fill in the gaps when you use the Crop tool for straightening or rotating an image, or expanding your canvas beyond the image's original size.  \\nFollow these steps:\\n\\n\\nFrom the toolbar, select the Crop Tool (). Crop borders display on the edges of the photo. [...] See the video Adjust perspective in a photo for more information.\\nResize the canvas using the Crop tool\\nYou can use the Crop tool to resize the image canvas.\\n\\n\\nFrom the toolbar, select the Crop Tool\\xa0. Crop borders display on the edges of the image.\\n\\n\\nDrag the crop handles outwards to enlarge the canvas. Use the Alt/Option modifier key to enlarge from all sides.\\n\\n\\nPress Enter (Windows) or Return (Mac OS) to confirm the action.\",\n",
       "  'score': 0.5826579},\n",
       " {'title': 'Crop images in Photoshop Elements',\n",
       "  'url': 'https://helpx.adobe.com/ca/photoshop-elements/using/cropping.html',\n",
       "  'content': 'The Crop tool removes the part of an image surrounding the selection. Crop to remove distractive background elements and create a focus on',\n",
       "  'score': 0.5367749},\n",
       " {'title': 'Crop a photo using the Crop tool',\n",
       "  'url': 'https://helpx.adobe.com/photoshop/using/tool-techniques/crop-tool.html',\n",
       "  'content': 'The Crop tool allows you to select an area of a photo and remove or crop everything outside the selected area.',\n",
       "  'score': 0.49465415}]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "adobe_search(\"What is crop?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "retval = adobe_help_agent.invoke({\"input\":\"What is crop?\",\"messages\":[],\"team_members\":[],\"next\":[]}),\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('To create a new layer in Adobe Photoshop, you can do any of the following:\\n'\n",
      " '\\n'\n",
      " '1. Click the \"Create a New Layer\" button at the bottom of the Layers panel. '\n",
      " 'This creates a new transparent layer above the currently selected layer.\\n'\n",
      " '\\n'\n",
      " '2. Choose Layer > New > Layer from the top menu. This opens a dialog box '\n",
      " 'where you can name the layer and set options before creating it.\\n'\n",
      " '\\n'\n",
      " '3. Alt-click (Windows) or Option-click (Mac) the \"Create a New Layer\" button '\n",
      " 'to open the New Layer dialog box and set layer options.\\n'\n",
      " '\\n'\n",
      " '4. Ctrl-click (Windows) or Command-click (Mac) the \"Create a New Layer\" '\n",
      " 'button to add a new layer below the currently selected layer.\\n'\n",
      " '\\n'\n",
      " 'Additionally, you can create a new layer from a selection by making a '\n",
      " 'selection and then choosing:\\n'\n",
      " '- Layer > New > Layer Via Copy (copies the selection to a new layer)\\n'\n",
      " '- Layer > New > Layer Via Cut (cuts the selection and pastes it into a new '\n",
      " 'layer)\\n'\n",
      " '\\n'\n",
      " 'A new layer appears above the selected layer or within the selected group in '\n",
      " 'the Layers panel.\\n'\n",
      " '\\n'\n",
      " \"For more details, you can visit Adobe's official help page on creating and \"\n",
      " 'managing layers:\\n'\n",
      " 'https://helpx.adobe.com/photoshop/using/create-layers-groups.html\\n'\n",
      " '\\n'\n",
      " '**URL**\\n'\n",
      " 'https://helpx.adobe.com/photoshop/using/create-layers-groups.html')\n"
     ]
    }
   ],
   "source": [
    "from pprint import pp\n",
    "pp(retval[0][\"output\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "output = adobe_help_node(test_query)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "================================\u001b[1m Human Message \u001b[0m=================================\n",
      "Name: AdobeHelp\n",
      "\n",
      "Layers in Adobe Photoshop are fundamental elements used for editing and composing images. They are like stacked, transparent sheets of glass, each containing images, text, effects, or objects. Layers allow you to work on different parts of an image independently without affecting other layers. This makes editing nondestructive and flexible.\n",
      "\n",
      "You can think of layers as separate pieces of content stacked on top of each other. The Layers panel in Photoshop shows these layers arranged in a stack, where you can manage their order, visibility, and properties such as opacity and blending modes. Layers enable you to composite multiple images, add text, apply filters, and make adjustments selectively.\n",
      "\n",
      "The bottommost layer is usually the Background layer, which is locked by default but can be converted into a regular layer for more editing options.\n",
      "\n",
      "In summary, layers help you:\n",
      "- Edit parts of an image independently\n",
      "- Stack multiple images or elements\n",
      "- Apply effects and adjustments nondestructively\n",
      "- Manage complex compositions easily\n",
      "\n",
      "For more detailed information, you can visit Adobe's official help page on layers:\n",
      "https://helpx.adobe.com/photoshop/web/edit-images/manage-layers/about-layers.html\n",
      "\n",
      "**URL**\n",
      "https://helpx.adobe.com/photoshop/web/edit-images/manage-layers/about-layers.html\n"
     ]
    }
   ],
   "source": [
    "output[\"messages\"][-1].pretty_print()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Data Preparation\n",
    "\n",
    "First, we will read in the transcripts of the videos and convert them to Documents\n",
    "with appropriate metadata."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ast import Dict\n",
    "import json\n",
    "\n",
    "from pstuts_rag.loader import load_json_files\n",
    "filename = [\"../data/test.json\"]\n",
    "from typing import List, Dict, Any\n",
    "data:List[Dict[str,Any]] = await load_json_files(filename)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's hit it with a semantic chunker."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pstuts_rag.datastore import DatastoreManager\n",
    "from qdrant_client import QdrantClient\n",
    "\n",
    "client = QdrantClient(path=\":memory:\")\n",
    "\n",
    "datastore_manager = DatastoreManager(qdrant_client=client,name=\"local_test\")\n",
    "if datastore_manager.count_docs() == 0:\n",
    "    await datastore_manager.populate_database(raw_docs=data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## RAG Agent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<built-in function repr>\n"
     ]
    }
   ],
   "source": [
    "from pstuts_rag.agent_rag import create_rag_node\n",
    "from langchain_openai import ChatOpenAI\n",
    "from langchain_core.tools import tool\n",
    "rag_node, rag_search = create_rag_node(retriever=datastore_manager.get_retriever(),\n",
    "                                         llm=ChatOpenAI(model=\"gpt-4.1-mini\",temperature=0),\n",
    "                                         name=\"VideoArchiveSearch\" )\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "retval = rag_search(\"What is Seinfeld?\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\"I don't know. This isn’t covered in the training videos.\"\n"
     ]
    }
   ],
   "source": [
    "from pprint import pp\n",
    "pp(retval)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "================================\u001b[1m Human Message \u001b[0m=================================\n",
      "Name: VideoArchiveSearch\n",
      "\n",
      "Layers are the building blocks of any image in Photoshop CC. You can think of layers like separate flat panes of glass stacked on top of each other, with each layer containing separate pieces of content. Some parts of a layer can be transparent, allowing you to see through to the layers below. This setup lets you edit parts of an image independently without affecting the rest of the image. You manage and work with layers in the Layers panel, where you can toggle their visibility on and off using the Eye icon. (See explanation around 0:28 to 1:00 and 1:25 to 2:32) 🎨🖼️\n",
      "**REFERENCES**\n",
      "[\n",
      "  {\n",
      "    \"title\": \"Understand layers\",\n",
      "    \"source\": \"https://images-tv.adobe.com/avp/vr/b758b4c4-2a74-41f4-8e67-e2f2eab83c6a/f810fc5b-2b04-4e23-8fa4-5c532e7de6f8/e268fe4d-e5c7-415c-9f5c-d34d024b14d8_20170727011753.1280x720at2400_h264.mp4\",\n",
      "    \"start\": 0.47,\n",
      "    \"stop\": 62.14\n",
      "  },\n",
      "  {\n",
      "    \"title\": \"Understand layers\",\n",
      "    \"source\": \"https://images-tv.adobe.com/avp/vr/b758b4c4-2a74-41f4-8e67-e2f2eab83c6a/f810fc5b-2b04-4e23-8fa4-5c532e7de6f8/e268fe4d-e5c7-415c-9f5c-d34d024b14d8_20170727011753.1280x720at2400_h264.mp4\",\n",
      "    \"start\": 85.75,\n",
      "    \"stop\": 152.97\n",
      "  }\n",
      "]\n"
     ]
    }
   ],
   "source": [
    "rag_output = rag_node(test_query)\n",
    "rag_output[\"messages\"][-1].pretty_print()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Graph Creation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/mbudisic/Documents/PsTuts-RAG/pstuts_rag/pstuts_rag/agents.py:79: LangChainDeprecationWarning: The method `BaseChatOpenAI.bind_functions` was deprecated in langchain-openai 0.2.1 and will be removed in 1.0.0. Use :meth:`~langchain_openai.chat_models.base.ChatOpenAI.bind_tools` instead.\n",
      "  | llm.bind_functions(functions=[function_def], function_call=\"route\")\n"
     ]
    }
   ],
   "source": [
    "from pstuts_rag.agents import create_team_supervisor\n",
    "from pstuts_rag.prompt_templates import SUPERVISOR_SYSTEM\n",
    "supervisor_agent = create_team_supervisor(\n",
    "    llm_tool_calling,\n",
    "    SUPERVISOR_SYSTEM,\n",
    "    [\"VideoArchiveSearch\", \"AdobeHelp\"],\n",
    ")\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<langgraph.graph.state.StateGraph at 0x794d6582ecf0>"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langgraph.graph import END, StateGraph\n",
    "\n",
    "adobe_help_graph = StateGraph(PsTutsTeamState)\n",
    "\n",
    "adobe_help_graph.add_node(\"VideoArchiveSearch\", rag_node)\n",
    "adobe_help_graph.add_node(\"AdobeHelp\", adobe_help_node)\n",
    "adobe_help_graph.add_node(\"supervisor\", supervisor_agent)\n",
    "\n",
    "edges = [\n",
    "    [\"VideoArchiveSearch\",\"supervisor\"],\n",
    "    [\"AdobeHelp\",\"supervisor\"],\n",
    "]\n",
    "\n",
    "[adobe_help_graph.add_edge(*p) for p in edges]\n",
    "\n",
    "adobe_help_graph.add_conditional_edges(\n",
    "    \"supervisor\",\n",
    "    lambda x:x[\"next\"],\n",
    "    {\"VideoArchiveSearch\":\"VideoArchiveSearch\",\n",
    "    \"AdobeHelp\":\"AdobeHelp\",    \n",
    "    \"FINISH\": END},\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['VideoArchiveSearch', 'AdobeHelp', 'supervisor'])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "adobe_help_graph.nodes.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "adobe_help_graph.set_entry_point(\"supervisor\")\n",
    "compiled_research_graph = adobe_help_graph.compile()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "import nest_asyncio\n",
    "nest_asyncio.apply()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                            +-----------+                             \n",
      "                            | __start__ |                             \n",
      "                            +-----------+                             \n",
      "                                  *                                   \n",
      "                                  *                                   \n",
      "                                  *                                   \n",
      "                            +------------+                            \n",
      "                            | supervisor |                            \n",
      "                       *****+------------+.....                       \n",
      "                   ****            .           ....                   \n",
      "              *****                .               .....              \n",
      "           ***                     .                    ...           \n",
      "+-----------+           +--------------------+           +---------+  \n",
      "| AdobeHelp |           | VideoArchiveSearch |           | __end__ |  \n",
      "+-----------+           +--------------------+           +---------+  \n"
     ]
    }
   ],
   "source": [
    "from langchain_core.runnables.graph_ascii import draw_ascii\n",
    "\n",
    "graph_data = compiled_research_graph.get_graph()\n",
    "print(graph_data.draw_ascii())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "def enter_chain(message: str):\n",
    "    results = {\n",
    "        \"messages\": [HumanMessage(content=message)],\n",
    "        \"team_members\": [\"VideoArchiveSearch\", \"AdobeHelp\"],\n",
    "    }\n",
    "    return results\n",
    "\n",
    "research_chain = enter_chain | compiled_research_graph\n",
    "\n",
    "def demo_research_chain(query:str):\n",
    "    \n",
    "    for s in research_chain.stream(\n",
    "        query, \n",
    "        {\"recursion_limit\": 20}\n",
    "    ):\n",
    "        if \"__end__\" not in s:\n",
    "            if 'supervisor' not in s.keys():\n",
    "                for response in s.values():\n",
    "                    for msg in response['messages']:\n",
    "                        msg.pretty_print()\n",
    "            print(\"---\")\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "---\n",
      "================================\u001b[1m Human Message \u001b[0m=================================\n",
      "Name: VideoArchiveSearch\n",
      "\n",
      "Layers are the building blocks of any image in Photoshop CC. You can think of layers like separate flat panes of glass stacked on top of each other, where each layer contains separate pieces of content. Some parts of a layer can be transparent, allowing you to see through to the layers below. This setup lets you edit parts of an image independently without affecting the rest of the image. You manage and work with layers in the Layers panel, where you can toggle their visibility on and off using the Eye icon. (See 0:28–1:03 and 1:25–2:32) 🎨🖼️\n",
      "**REFERENCES**\n",
      "[\n",
      "  {\n",
      "    \"title\": \"Understand layers\",\n",
      "    \"source\": \"https://images-tv.adobe.com/avp/vr/b758b4c4-2a74-41f4-8e67-e2f2eab83c6a/f810fc5b-2b04-4e23-8fa4-5c532e7de6f8/e268fe4d-e5c7-415c-9f5c-d34d024b14d8_20170727011753.1280x720at2400_h264.mp4\",\n",
      "    \"start\": 0.47,\n",
      "    \"stop\": 62.14\n",
      "  },\n",
      "  {\n",
      "    \"title\": \"Understand layers\",\n",
      "    \"source\": \"https://images-tv.adobe.com/avp/vr/b758b4c4-2a74-41f4-8e67-e2f2eab83c6a/f810fc5b-2b04-4e23-8fa4-5c532e7de6f8/e268fe4d-e5c7-415c-9f5c-d34d024b14d8_20170727011753.1280x720at2400_h264.mp4\",\n",
      "    \"start\": 85.75,\n",
      "    \"stop\": 152.97\n",
      "  }\n",
      "]\n",
      "---\n",
      "---\n"
     ]
    }
   ],
   "source": [
    "demo_research_chain(\"What are layers?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "---\n",
      "================================\u001b[1m Human Message \u001b[0m=================================\n",
      "Name: VideoArchiveSearch\n",
      "\n",
      "I don't know. This isn’t covered in the training videos.\n",
      "---\n",
      "---\n",
      "================================\u001b[1m Human Message \u001b[0m=================================\n",
      "Name: AdobeHelp\n",
      "\n",
      "To crop a layer in Adobe Photoshop, you can use the Crop tool in a way that targets the active layer or a selection within that layer. Here's how you can do it:\n",
      "\n",
      "1. Select the layer you want to crop by clicking its thumbnail in the Layers panel.\n",
      "2. Choose the Crop tool from the toolbar.\n",
      "3. A bounding box will appear around the active layer or the selection.\n",
      "4. Drag the handles of the bounding box to set the crop size.\n",
      "5. You can also rotate the layer by moving the rotation dial below the bounding box if needed.\n",
      "6. Apply touch gestures or mouse actions to pan, zoom, or reset the crop area.\n",
      "7. When satisfied, confirm the crop to apply it to the layer.\n",
      "\n",
      "This method allows you to crop and rotate an active layer or the contents of a selection non-destructively.\n",
      "\n",
      "For more detailed instructions, you can visit Adobe's official help page on cropping and rotating layers:\n",
      "https://helpx.adobe.com/photoshop/using/crop-move-rotate-photos.html\n",
      "\n",
      "**URL**\n",
      "https://helpx.adobe.com/photoshop/using/crop-move-rotate-photos.html\n",
      "---\n",
      "---\n"
     ]
    }
   ],
   "source": [
    "demo_research_chain(\"How do we crop a layer?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}