{ "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": [ "\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": [ "" ] }, "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 }