├── .gitignore ├── README.md ├── attention.pdf ├── crewAI_RAG_Qdrant.ipynb ├── requirements.txt ├── researcher_analysis.md └── writer_summary.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | share/python-wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .nox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *.cover 49 | *.py,cover 50 | .hypothesis/ 51 | .pytest_cache/ 52 | cover/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | .pybuilder/ 76 | target/ 77 | 78 | # Jupyter Notebook 79 | .ipynb_checkpoints 80 | 81 | # IPython 82 | profile_default/ 83 | ipython_config.py 84 | 85 | # pyenv 86 | # For a library or package, you might want to ignore these files since the code is 87 | # intended to run in multiple environments; otherwise, check them in: 88 | # .python-version 89 | 90 | # pipenv 91 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 92 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 93 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 94 | # install all needed dependencies. 95 | #Pipfile.lock 96 | 97 | # poetry 98 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 99 | # This is especially recommended for binary packages to ensure reproducibility, and is more 100 | # commonly ignored for libraries. 101 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 102 | #poetry.lock 103 | 104 | # pdm 105 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. 106 | #pdm.lock 107 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it 108 | # in version control. 109 | # https://pdm.fming.dev/#use-with-ide 110 | .pdm.toml 111 | 112 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm 113 | __pypackages__/ 114 | 115 | # Celery stuff 116 | celerybeat-schedule 117 | celerybeat.pid 118 | 119 | # SageMath parsed files 120 | *.sage.py 121 | 122 | # Environments 123 | .env 124 | .venv 125 | env/ 126 | venv/ 127 | ENV/ 128 | env.bak/ 129 | venv.bak/ 130 | 131 | # Spyder project settings 132 | .spyderproject 133 | .spyproject 134 | 135 | # Rope project settings 136 | .ropeproject 137 | 138 | # mkdocs documentation 139 | /site 140 | 141 | # mypy 142 | .mypy_cache/ 143 | .dmypy.json 144 | dmypy.json 145 | 146 | # Pyre type checker 147 | .pyre/ 148 | 149 | # pytype static type analyzer 150 | .pytype/ 151 | 152 | # Cython debug symbols 153 | cython_debug/ 154 | 155 | # PyCharm 156 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 157 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 158 | # and can be added to the global gitignore or merged into this file. For a more nuclear 159 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 160 | #.idea/ 161 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CREWAI RAG LANGCHAIN QDRANT 🤼 2 | 3 |

4 | crewai 5 |

6 | 7 | This repository contains a full Q&A pipeline using LangChain framework, Qdrant as vector database and CrewAI as Agents. The data used is "The Attention Mechanism" research paper, but the RAG pipeline is structure to analyze research papers and provide an analysis and summary. Therefore it can use any other research paper. Use this **[Link](https://nbviewer.org/github/benitomartin/crewai-rag-langchain-qdrant/blob/main/crewAI_RAG_Qdrant.ipynb)** if the notebook cannot be opened. 8 | 9 | The main steps taken to build the RAG pipeline can be summarize as follows: 10 | 11 | * **Data Ingestion**: load data from pdf file 12 | 13 | * **Indexing**: RecursiveCharacterTextSplitter for indexing in chunks 14 | 15 | * **Vector Store**: Qdrant inserting metadata 16 | 17 | * **QA Chain Retrieval**: RetrievalQA 18 | 19 | * **CrewAI**: Researcher and Writer Agents to analyze and summarize the paper 20 | 21 | Feel free to ⭐ and clone this repo 😉 22 | 23 | ## 👨‍💻 **Tech Stack** 24 | 25 | 26 | ![Visual Studio Code](https://img.shields.io/badge/Visual%20Studio%20Code-0078d7.svg?style=for-the-badge&logo=visual-studio-code&logoColor=white) 27 | ![Jupyter Notebook](https://img.shields.io/badge/jupyter-%23FA0F00.svg?style=for-the-badge&logo=jupyter&logoColor=white) 28 | ![Python](https://img.shields.io/badge/python-3670A0?style=for-the-badge&logo=python&logoColor=ffdd54) 29 | ![OpenAI](https://img.shields.io/badge/OpenAI-74aa9c?style=for-the-badge&logo=openai&logoColor=white) 30 | ![Linux](https://img.shields.io/badge/Linux-FCC624?style=for-the-badge&logo=linux&logoColor=black) 31 | ![Git](https://img.shields.io/badge/git-%23F05033.svg?style=for-the-badge&logo=git&logoColor=white) 32 | 33 | 34 | ## 📐 Set Up 35 | 36 | In the initial project phase, the documents are loaded using **PyPDFLoader** and indexed. Indexing is a fundamental process for storing and organizing data from diverse sources into a vector store, a structure essential for efficient storage and retrieval. This process involves the following steps: 37 | 38 | - Select a splitting method and its hyperparameters: we will use the **RecursiveCharacterTextSplitter**. 39 | 40 | - Select the embeddings model: in our case the **OpenAI** 41 | 42 | - Select a Vector Store: **Qdrant**. 43 | 44 | Storing text chunks along with their corresponding embedding representations, capturing the semantic meaning of the text. These embeddings facilitate easy retrieval of chunks based on their semantic similarity. 45 | 46 | After indexing, a QA Chain Retrieval Pipeline is set up in order to check the Q&A functioning and performance. 47 | 48 | The final step includes CrewAI Agents to generate two documents: 49 | 50 | - An analysis of the paper provided by the research agent 51 | 52 | - A summary of the key findings provided by the writer 53 | 54 | 55 | ## 🌊 QA Chain Retrieval Pipeline 56 | 57 | The pipeline created contains the main llm model, the QA chain and the agents. A custom tool which uses the vector store as search engine has been implemented. The researcher looks for the paper and generates a detailed [analysis](https://github.com/benitomartin/crewai-rag-langchain-qdrant/blob/main/researcher_analysis.md) as md file. Finally the writer generates a short [summary](https://github.com/benitomartin/crewai-rag-langchain-qdrant/blob/main/writer_summary.md) with the key findings of the paper, trying to write it in a way that it is comprehesive for computer science students. 58 | 59 | ``` 60 | ### RESEARCHER ### 61 | 62 | # Agent 63 | reasarcher = Agent( 64 | role="Computer Science Researcher", 65 | goal='Understanding the published research paper and extract the main insights', 66 | backstory="""You work at a Computer Science school. 67 | A new paper has been published and you need to extract 68 | the main information and provide the main insights to 69 | the students.You have a knack for dissecting complex 70 | research papers""", 71 | verbose=True, 72 | tools=[tool_instance], 73 | allow_delegation=False 74 | ) 75 | 76 | 77 | # Tasks 78 | task_research = Task( 79 | description="""Conduct an in-depth analysis of the published research paper. 80 | Identify the key components and try to understand how each works.""", 81 | expected_output="""Your final answer must be a full analysis report of the research paper""", 82 | agent=reasarcher, 83 | output_file='researcher_analysis.md' 84 | 85 | ) 86 | ``` 87 | 88 | ``` 89 | ### WRITER ### 90 | 91 | # Agent 92 | writer = Agent( 93 | role="Computer Science Writer", 94 | goal= 'Craft easy to understand content for Computer Science students', 95 | backstory="""You are working on writing a summary of a research paper. 96 | Use the content of the Computer Science Researcher to develop 97 | a short comprehensive summary of the research paper""", 98 | verbose=True, 99 | allow_delegation=False, 100 | ) 101 | 102 | # Tasks 103 | task_writer = Task( 104 | description="""Extract the most important topics and key components of the report 105 | developed by the Computer Science Researcher.""", 106 | expected_output="""Your final answer must be a short comprehensive summary 107 | to understand report for Computer Science students""", 108 | agent=writer, 109 | output_file='writer_summary.md' 110 | 111 | ) 112 | ``` 113 | -------------------------------------------------------------------------------- /attention.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benitomartin/crewai-rag-langchain-qdrant/d96fc6bbb20574b36897abcf1233abfa12d4e382/attention.pdf -------------------------------------------------------------------------------- /crewAI_RAG_Qdrant.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "id": "LB_gQIe0XQgt" 7 | }, 8 | "source": [ 9 | "# Import Packages" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 3, 15 | "metadata": { 16 | "id": "AFRaCqZWkolC" 17 | }, 18 | "outputs": [], 19 | "source": [ 20 | "from uuid import uuid4\n", 21 | "import os\n", 22 | "from dotenv import load_dotenv\n", 23 | "\n", 24 | "from langchain_community.document_loaders import PyPDFLoader\n", 25 | "from langchain.text_splitter import RecursiveCharacterTextSplitter\n", 26 | "from langchain_openai import OpenAIEmbeddings, ChatOpenAI\n", 27 | "from langchain.vectorstores import Qdrant\n", 28 | "from langchain import hub\n", 29 | "from langchain.chains import RetrievalQA\n", 30 | "from langchain.prompts import ChatPromptTemplate\n", 31 | "from langchain.callbacks import StdOutCallbackHandler\n", 32 | "\n", 33 | "from qdrant_client import QdrantClient\n", 34 | "from qdrant_client.models import Distance, VectorParams\n", 35 | "from qdrant_client.models import PointStruct\n", 36 | "\n", 37 | "from crewai_tools import BaseTool\n", 38 | "from crewai import Agent, Task, Crew" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 18, 44 | "metadata": {}, 45 | "outputs": [], 46 | "source": [ 47 | "# Load environmental variables from a .env file\n", 48 | "load_dotenv()\n", 49 | "\n", 50 | "OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')\n", 51 | "QDRANT_API_KEY = os.getenv('QDRANT_API_KEY')\n", 52 | "QDRANT_URL = os.getenv('QDRANT_URL')" 53 | ] 54 | }, 55 | { 56 | "cell_type": "markdown", 57 | "metadata": {}, 58 | "source": [ 59 | "# Load Documents" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 12, 65 | "metadata": { 66 | "id": "XH-vbBUQoG93" 67 | }, 68 | "outputs": [], 69 | "source": [ 70 | "# Load documents\n", 71 | "\n", 72 | "loader = PyPDFLoader(\"attention.pdf\")" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 13, 78 | "metadata": { 79 | "id": "ZWUH5E4Oq7qk" 80 | }, 81 | "outputs": [], 82 | "source": [ 83 | "# Split documents\n", 84 | "\n", 85 | "text_splitter = RecursiveCharacterTextSplitter(chunk_size=512, chunk_overlap=50)\n", 86 | "document = loader.load_and_split(text_splitter)" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": 14, 92 | "metadata": { 93 | "colab": { 94 | "base_uri": "https://localhost:8080/" 95 | }, 96 | "id": "dcdJOnejrJ7A", 97 | "outputId": "1dfd765f-f9a9-4f88-d0ef-3563147e994d" 98 | }, 99 | "outputs": [ 100 | { 101 | "data": { 102 | "text/plain": [ 103 | "Document(page_content='Provided proper attribution is provided, Google hereby grants permission to\\nreproduce the tables and figures in this paper solely for use in journalistic or\\nscholarly works.\\nAttention Is All You Need\\nAshish Vaswani∗\\nGoogle Brain\\navaswani@google.comNoam Shazeer∗\\nGoogle Brain\\nnoam@google.comNiki Parmar∗\\nGoogle Research\\nnikip@google.comJakob Uszkoreit∗\\nGoogle Research\\nusz@google.com\\nLlion Jones∗\\nGoogle Research\\nllion@google.comAidan N. Gomez∗ †\\nUniversity of Toronto\\naidan@cs.toronto.eduŁukasz Kaiser∗', metadata={'source': 'attention.pdf', 'page': 0})" 104 | ] 105 | }, 106 | "execution_count": 14, 107 | "metadata": {}, 108 | "output_type": "execute_result" 109 | } 110 | ], 111 | "source": [ 112 | "document[0]" 113 | ] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "execution_count": 15, 118 | "metadata": { 119 | "colab": { 120 | "base_uri": "https://localhost:8080/" 121 | }, 122 | "id": "9eG-OjJ7rb6B", 123 | "outputId": "3aa2038e-7a2b-4c50-9951-cb24623b4489" 124 | }, 125 | "outputs": [ 126 | { 127 | "data": { 128 | "text/plain": [ 129 | "92" 130 | ] 131 | }, 132 | "execution_count": 15, 133 | "metadata": {}, 134 | "output_type": "execute_result" 135 | } 136 | ], 137 | "source": [ 138 | "len(document)" 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": 48, 144 | "metadata": {}, 145 | "outputs": [ 146 | { 147 | "data": { 148 | "text/plain": [ 149 | "Document(page_content='tion models in various tasks, allowing modeling of dependencies without regard to their distance in\\nthe input or output sequences [ 2,19]. In all but a few cases [ 27], however, such attention mechanisms\\nare used in conjunction with a recurrent network.\\nIn this work we propose the Transformer, a model architecture eschewing recurrence and instead\\nrelying entirely on an attention mechanism to draw global dependencies between input and output.', metadata={'source': 'attention.pdf', 'page': 1})" 150 | ] 151 | }, 152 | "execution_count": 48, 153 | "metadata": {}, 154 | "output_type": "execute_result" 155 | } 156 | ], 157 | "source": [ 158 | "document[10]" 159 | ] 160 | }, 161 | { 162 | "cell_type": "markdown", 163 | "metadata": {}, 164 | "source": [ 165 | "# Vector Store" 166 | ] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": 24, 171 | "metadata": { 172 | "id": "63FoOb3mrqe_" 173 | }, 174 | "outputs": [], 175 | "source": [ 176 | "# Create collection in vector store\n", 177 | "\n", 178 | "client = QdrantClient(\n", 179 | " url=QDRANT_URL,\n", 180 | " api_key=QDRANT_API_KEY)\n", 181 | "\n", 182 | "\n", 183 | "client.create_collection(\n", 184 | " collection_name=\"attention\",\n", 185 | " vectors_config={\n", 186 | " \"content\": VectorParams(size=1536, distance=Distance.COSINE)\n", 187 | " }\n", 188 | " )" 189 | ] 190 | }, 191 | { 192 | "cell_type": "code", 193 | "execution_count": 25, 194 | "metadata": { 195 | "id": "8AHjREffuZxi" 196 | }, 197 | "outputs": [], 198 | "source": [ 199 | "# Upsert documents in vector store\n", 200 | "\n", 201 | "embedding = OpenAIEmbeddings()\n", 202 | "\n", 203 | "def chunked_metadata(data, client=client, collection_name=\"attention\"):\n", 204 | "\n", 205 | " chunked_metadata = []\n", 206 | "\n", 207 | " for item in data:\n", 208 | "\n", 209 | " id = str(uuid4())\n", 210 | " content = item.page_content\n", 211 | " source = item.metadata[\"source\"]\n", 212 | " page = item.metadata[\"page\"]\n", 213 | "\n", 214 | " content_vector = embedding.embed_documents([content])[0]\n", 215 | " vector_dict = {\"content\": content_vector}\n", 216 | "\n", 217 | " payload = {\n", 218 | " \"page_content\": content,\n", 219 | " \"metadata\": {\n", 220 | " \"id\": id,\n", 221 | " \"page_content\": content,\n", 222 | " \"source\": source,\n", 223 | " \"page\": page,\n", 224 | " }\n", 225 | " }\n", 226 | "\n", 227 | "\n", 228 | " metadata = PointStruct(id=id, vector=vector_dict, payload=payload)\n", 229 | " chunked_metadata.append(metadata)\n", 230 | "\n", 231 | " client.upsert(\n", 232 | " collection_name=collection_name,\n", 233 | " wait=True,\n", 234 | " points=chunked_metadata)" 235 | ] 236 | }, 237 | { 238 | "cell_type": "code", 239 | "execution_count": 26, 240 | "metadata": { 241 | "id": "zXYk8tvzuZ0U" 242 | }, 243 | "outputs": [], 244 | "source": [ 245 | "chunked_metadata(document[:10])" 246 | ] 247 | }, 248 | { 249 | "cell_type": "code", 250 | "execution_count": 27, 251 | "metadata": { 252 | "colab": { 253 | "base_uri": "https://localhost:8080/" 254 | }, 255 | "id": "FCDI12xeuZ3E", 256 | "outputId": "4dbe1cf5-57db-488b-8124-70e14b3916e2" 257 | }, 258 | "outputs": [ 259 | { 260 | "data": { 261 | "text/plain": [ 262 | "CollectionsResponse(collections=[CollectionDescription(name='attention')])" 263 | ] 264 | }, 265 | "execution_count": 27, 266 | "metadata": {}, 267 | "output_type": "execute_result" 268 | } 269 | ], 270 | "source": [ 271 | "client.get_collections()" 272 | ] 273 | }, 274 | { 275 | "cell_type": "code", 276 | "execution_count": 28, 277 | "metadata": { 278 | "colab": { 279 | "base_uri": "https://localhost:8080/" 280 | }, 281 | "id": "oBI8-d3P92uA", 282 | "outputId": "a8cade9e-a359-4722-9e94-ca20ed51ee58" 283 | }, 284 | "outputs": [ 285 | { 286 | "name": "stdout", 287 | "output_type": "stream", 288 | "text": [ 289 | "Points: 90 \n" 290 | ] 291 | } 292 | ], 293 | "source": [ 294 | "attention_collection = client.get_collection(\"attention\")\n", 295 | "\n", 296 | "print(f\"Points: {attention_collection.points_count} \")" 297 | ] 298 | }, 299 | { 300 | "cell_type": "code", 301 | "execution_count": 12, 302 | "metadata": { 303 | "id": "zmETDxM_-iC0" 304 | }, 305 | "outputs": [], 306 | "source": [ 307 | "# client.delete_collection(collection_name=\"attention\")" 308 | ] 309 | }, 310 | { 311 | "cell_type": "code", 312 | "execution_count": 29, 313 | "metadata": { 314 | "colab": { 315 | "base_uri": "https://localhost:8080/" 316 | }, 317 | "id": "vN6CJRNI-8-m", 318 | "outputId": "7a7b23b2-c43e-4b1d-a22b-503142d5fa38" 319 | }, 320 | "outputs": [ 321 | { 322 | "data": { 323 | "text/plain": [ 324 | "[ScoredPoint(id='511d4b4b-72ea-42e4-954a-d5fa8decc60c', version=0, score=0.77504903, payload={'page_content': 'architectures [38, 24, 15].\\nRecurrent models typically factor computation along the symbol positions of the input and output\\nsequences. Aligning the positions to steps in computation time, they generate a sequence of hidden\\nstates ht, as a function of the previous hidden state ht−1and the input for position t. This inherently\\nsequential nature precludes parallelization within training examples, which becomes critical at longer'}, vector=None, shard_key=None),\n", 325 | " ScoredPoint(id='0b73789c-151b-42e8-bc58-57aa00c299dd', version=1, score=0.77504903, payload={'page_content': 'architectures [38, 24, 15].\\nRecurrent models typically factor computation along the symbol positions of the input and output\\nsequences. Aligning the positions to steps in computation time, they generate a sequence of hidden\\nstates ht, as a function of the previous hidden state ht−1and the input for position t. This inherently\\nsequential nature precludes parallelization within training examples, which becomes critical at longer'}, vector=None, shard_key=None),\n", 326 | " ScoredPoint(id='0c1978bb-726c-45c7-9e5d-a702bdbbf346', version=2, score=0.77504903, payload={'page_content': 'architectures [38, 24, 15].\\nRecurrent models typically factor computation along the symbol positions of the input and output\\nsequences. Aligning the positions to steps in computation time, they generate a sequence of hidden\\nstates ht, as a function of the previous hidden state ht−1and the input for position t. This inherently\\nsequential nature precludes parallelization within training examples, which becomes critical at longer'}, vector=None, shard_key=None),\n", 327 | " ScoredPoint(id='0828ae47-0155-4202-b0a8-bcd2eb0d88cb', version=7, score=0.77504903, payload={'page_content': 'architectures [38, 24, 15].\\nRecurrent models typically factor computation along the symbol positions of the input and output\\nsequences. Aligning the positions to steps in computation time, they generate a sequence of hidden\\nstates ht, as a function of the previous hidden state ht−1and the input for position t. This inherently\\nsequential nature precludes parallelization within training examples, which becomes critical at longer'}, vector=None, shard_key=None),\n", 328 | " ScoredPoint(id='c059f737-c539-4c50-8e00-9dac8456c1aa', version=4, score=0.77504903, payload={'page_content': 'architectures [38, 24, 15].\\nRecurrent models typically factor computation along the symbol positions of the input and output\\nsequences. Aligning the positions to steps in computation time, they generate a sequence of hidden\\nstates ht, as a function of the previous hidden state ht−1and the input for position t. This inherently\\nsequential nature precludes parallelization within training examples, which becomes critical at longer'}, vector=None, shard_key=None)]" 329 | ] 330 | }, 331 | "execution_count": 29, 332 | "metadata": {}, 333 | "output_type": "execute_result" 334 | } 335 | ], 336 | "source": [ 337 | "# Search most similar documents to \"Positional Encoding\"\n", 338 | "\n", 339 | "client.search(\n", 340 | " collection_name=\"attention\",\n", 341 | " query_vector=(\"content\", embedding.embed_documents([\"Positional Encoding\"])[0]),\n", 342 | " with_payload=[\"page_content\", \"source\"],\n", 343 | " limit=5\n", 344 | ")" 345 | ] 346 | }, 347 | { 348 | "cell_type": "markdown", 349 | "metadata": {}, 350 | "source": [ 351 | "# Initialize Vector Store" 352 | ] 353 | }, 354 | { 355 | "cell_type": "code", 356 | "execution_count": 30, 357 | "metadata": { 358 | "id": "dDZrcp8U-9Wd" 359 | }, 360 | "outputs": [], 361 | "source": [ 362 | "# Initialize vector store\n", 363 | "\n", 364 | "vectorstore = Qdrant(client=client,\n", 365 | " collection_name=\"attention\",\n", 366 | " embeddings=embedding,\n", 367 | " vector_name=\"content\")" 368 | ] 369 | }, 370 | { 371 | "cell_type": "code", 372 | "execution_count": 31, 373 | "metadata": { 374 | "id": "29ICQc4TH7Hx" 375 | }, 376 | "outputs": [], 377 | "source": [ 378 | "# Downlad prompt template\n", 379 | "\n", 380 | "prompt = hub.pull(\"rlm/rag-prompt\")" 381 | ] 382 | }, 383 | { 384 | "cell_type": "code", 385 | "execution_count": 32, 386 | "metadata": { 387 | "colab": { 388 | "base_uri": "https://localhost:8080/" 389 | }, 390 | "id": "jn99i_7-IoVH", 391 | "outputId": "c62065be-bf52-4b87-cfee-4b04726abc62" 392 | }, 393 | "outputs": [ 394 | { 395 | "name": "stdout", 396 | "output_type": "stream", 397 | "text": [ 398 | "[HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=['context', 'question'], template=\"You are an assistant for question-answering tasks. Use the following pieces of retrieved context to answer the question. If you don't know the answer, just say that you don't know. Use three sentences maximum and keep the answer concise.\\nQuestion: {question} \\nContext: {context} \\nAnswer:\"))]\n" 399 | ] 400 | } 401 | ], 402 | "source": [ 403 | "print(prompt.messages)" 404 | ] 405 | }, 406 | { 407 | "cell_type": "code", 408 | "execution_count": 33, 409 | "metadata": { 410 | "colab": { 411 | "base_uri": "https://localhost:8080/" 412 | }, 413 | "id": "PFolcjm-JHLW", 414 | "outputId": "b046609e-3e3e-497a-de3e-a88f7849dcf2" 415 | }, 416 | "outputs": [ 417 | { 418 | "name": "stdout", 419 | "output_type": "stream", 420 | "text": [ 421 | "You are an assistant for question-answering tasks. Use the following pieces of retrieved context to answer the question. If you don't know the answer, just say that you don't know. Use three sentences maximum and keep the answer concise.\n", 422 | "Question: {question} \n", 423 | "Context: {context} \n", 424 | "Answer:\n" 425 | ] 426 | } 427 | ], 428 | "source": [ 429 | "print(prompt.messages[0].prompt.template)" 430 | ] 431 | }, 432 | { 433 | "cell_type": "markdown", 434 | "metadata": {}, 435 | "source": [ 436 | "# Define RAG Pipeline" 437 | ] 438 | }, 439 | { 440 | "cell_type": "code", 441 | "execution_count": 34, 442 | "metadata": { 443 | "id": "cuKc7sXhHN1g" 444 | }, 445 | "outputs": [], 446 | "source": [ 447 | "# Main parameters\n", 448 | "\n", 449 | "template = \"\"\"\n", 450 | "\n", 451 | "You are an assistant for question-answering tasks. \\\n", 452 | "Use the following pieces of retrieved context to answer the question. \\\n", 453 | "If you don't know the answer, just say that you don't know. \\\n", 454 | "\n", 455 | "\n", 456 | "Question: {question}\n", 457 | "Context: {context}\n", 458 | "\n", 459 | "Answer:\n", 460 | "\n", 461 | "\"\"\"\n", 462 | "\n", 463 | "retriever = vectorstore.as_retriever()\n", 464 | "\n", 465 | "prompt = ChatPromptTemplate.from_template(template)\n", 466 | "\n", 467 | "handler = StdOutCallbackHandler()" 468 | ] 469 | }, 470 | { 471 | "cell_type": "code", 472 | "execution_count": 37, 473 | "metadata": {}, 474 | "outputs": [], 475 | "source": [ 476 | "# LLM gpt-3.5-turbo\n", 477 | "\n", 478 | "llm35 = ChatOpenAI(temperature=0.0,\n", 479 | " model=\"gpt-3.5-turbo\",\n", 480 | " max_tokens=512)\n", 481 | "\n", 482 | "\n", 483 | "# Retrieval qa chain\n", 484 | "qa_d35 = RetrievalQA.from_chain_type(\n", 485 | " llm=llm35,\n", 486 | " chain_type=\"stuff\",\n", 487 | " callbacks=[handler],\n", 488 | " retriever=retriever)" 489 | ] 490 | }, 491 | { 492 | "cell_type": "code", 493 | "execution_count": 38, 494 | "metadata": {}, 495 | "outputs": [ 496 | { 497 | "name": "stdout", 498 | "output_type": "stream", 499 | "text": [ 500 | "\n", 501 | "\n", 502 | "\u001b[1m> Entering new RetrievalQA chain...\u001b[0m\n", 503 | "\n", 504 | "\u001b[1m> Finished chain.\u001b[0m\n" 505 | ] 506 | }, 507 | { 508 | "data": { 509 | "text/plain": [ 510 | "'The attention mechanism is a component used in sequence modeling and transduction tasks. It allows models to focus on different parts of the input sequence when making predictions, giving more weight to relevant information. This mechanism has become essential for improving the performance of models dealing with sequential data.'" 511 | ] 512 | }, 513 | "execution_count": 38, 514 | "metadata": {}, 515 | "output_type": "execute_result" 516 | } 517 | ], 518 | "source": [ 519 | "# Invoke qa_chain gpt-3.5-turbo\n", 520 | "\n", 521 | "qa_d35.invoke({\"query\": \"What is the attention mechanism?\"})[\"result\"]" 522 | ] 523 | }, 524 | { 525 | "cell_type": "code", 526 | "execution_count": 39, 527 | "metadata": { 528 | "id": "NLF2lOpVKN5t" 529 | }, 530 | "outputs": [], 531 | "source": [ 532 | "# LLM gpt-4o\n", 533 | "\n", 534 | "llm4o = ChatOpenAI(temperature=0.0,\n", 535 | " model=\"gpt-4o\",\n", 536 | " max_tokens=512)\n", 537 | "\n", 538 | "# Retrieval qa chain\n", 539 | "qa_db4o = RetrievalQA.from_chain_type(\n", 540 | " llm=llm4o,\n", 541 | " chain_type=\"stuff\",\n", 542 | " callbacks=[handler],\n", 543 | " retriever=retriever)" 544 | ] 545 | }, 546 | { 547 | "cell_type": "code", 548 | "execution_count": 40, 549 | "metadata": { 550 | "colab": { 551 | "base_uri": "https://localhost:8080/", 552 | "height": 161 553 | }, 554 | "id": "yIU1lxVyLd69", 555 | "outputId": "be09d984-d5f7-4984-8218-9644be8450dd" 556 | }, 557 | "outputs": [ 558 | { 559 | "name": "stdout", 560 | "output_type": "stream", 561 | "text": [ 562 | "\n", 563 | "\n", 564 | "\u001b[1m> Entering new RetrievalQA chain...\u001b[0m\n", 565 | "\n", 566 | "\u001b[1m> Finished chain.\u001b[0m\n", 567 | "The attention mechanism is a technique used in sequence modeling and transduction tasks, such as machine translation, text summarization, and image captioning. It allows the model to focus on different parts of the input sequence when generating each part of the output sequence. This mechanism helps the model to selectively concentrate on relevant parts of the input, thereby improving performance and efficiency.\n", 568 | "\n", 569 | "In essence, the attention mechanism assigns different weights to different parts of the input sequence, indicating their importance for the current step of the output sequence generation. This way, the model can dynamically adjust its focus based on the context, leading to better handling of long-range dependencies and more accurate predictions.\n", 570 | "\n", 571 | "Attention mechanisms have become a fundamental component in many state-of-the-art models, including the Transformer architecture, which has been highly successful in various natural language processing tasks.\n" 572 | ] 573 | } 574 | ], 575 | "source": [ 576 | "print(qa_db4o.invoke({\"query\": \"What is the attention mechanism?\"})[\"result\"])" 577 | ] 578 | }, 579 | { 580 | "cell_type": "code", 581 | "execution_count": 41, 582 | "metadata": { 583 | "colab": { 584 | "base_uri": "https://localhost:8080/" 585 | }, 586 | "id": "Z3GGnBkoUsC2", 587 | "outputId": "2b5ec1e5-3df1-425d-b744-a2303f4e6a84" 588 | }, 589 | "outputs": [ 590 | { 591 | "name": "stdout", 592 | "output_type": "stream", 593 | "text": [ 594 | "\n", 595 | "\n", 596 | "\u001b[1m> Entering new RetrievalQA chain...\u001b[0m\n", 597 | "\n", 598 | "\u001b[1m> Finished chain.\u001b[0m\n", 599 | "Certainly! The research paper \"Attention Is All You Need\" by Ashish Vaswani et al. introduces the Transformer model, which has become a foundational architecture in the field of natural language processing (NLP). Here are some key points and analysis of the paper:\n", 600 | "\n", 601 | "### Key Contributions:\n", 602 | "1. **Transformer Architecture**:\n", 603 | " - The paper introduces the Transformer model, which relies entirely on self-attention mechanisms, dispensing with recurrence and convolutions entirely.\n", 604 | " - This architecture allows for more parallelization during training, leading to significant speed improvements.\n", 605 | "\n", 606 | "2. **Self-Attention Mechanism**:\n", 607 | " - The core innovation is the self-attention mechanism, which computes the representation of a word in a sequence by attending to all other words in the sequence.\n", 608 | " - This mechanism allows the model to capture long-range dependencies more effectively than RNNs or CNNs.\n", 609 | "\n", 610 | "3. **Positional Encoding**:\n", 611 | " - Since the Transformer model does not have a built-in notion of word order (unlike RNNs), the authors introduce positional encodings to inject information about the relative or absolute position of tokens in the sequence.\n", 612 | "\n", 613 | "4. **Multi-Head Attention**:\n", 614 | " - The model uses multi-head attention to allow the model to jointly attend to information from different representation subspaces at different positions.\n", 615 | "\n", 616 | "5. **Layer Normalization and Residual Connections**:\n", 617 | " - The architecture includes layer normalization and residual connections, which help in stabilizing the training and improving the gradient flow.\n", 618 | "\n", 619 | "### Impact:\n", 620 | "- The Transformer model has revolutionized NLP, leading to the development of models like BERT, GPT, and T5, which have set new benchmarks in various NLP tasks.\n", 621 | "- The architecture's ability to handle long-range dependencies and its efficiency in training have made it the go-to model for many NLP applications.\n", 622 | "\n", 623 | "### Strengths:\n", 624 | "- **Efficiency**: The parallelizable nature of the Transformer allows for faster training times compared to RNN-based models.\n", 625 | "- **Performance**: The model achieves state-of-the-art results on several NLP tasks.\n", 626 | "- **Flexibility**: The self-attention mechanism can be adapted to various tasks beyond NLP, such as image processing.\n", 627 | "\n", 628 | "### Weaknesses:\n", 629 | "- **Computationally Intensive**: Despite its efficiency in training, the Transformer can be computationally intensive, especially for very long sequences, due to the quadratic complexity of the self-attention mechanism.\n", 630 | "- **Data Hungry**: The model requires a large amount of data to train effectively, which can be a limitation in scenarios with limited data availability.\n", 631 | "\n", 632 | "### Conclusion:\n", 633 | "\"Attention\n" 634 | ] 635 | } 636 | ], 637 | "source": [ 638 | "print(qa_db4o.invoke({\"query\": \"Can you analyze the research paper?\"})[\"result\"])" 639 | ] 640 | }, 641 | { 642 | "cell_type": "markdown", 643 | "metadata": {}, 644 | "source": [ 645 | "# CrewAI Agents and Tools" 646 | ] 647 | }, 648 | { 649 | "cell_type": "markdown", 650 | "metadata": {}, 651 | "source": [ 652 | "The following steps are performed:\n", 653 | "\n", 654 | "- Search the research paper in the vector store as tool \n", 655 | "- Provide a detailed report with the researcher agent\n", 656 | "- Provide a short summary with the writer agent" 657 | ] 658 | }, 659 | { 660 | "cell_type": "code", 661 | "execution_count": 57, 662 | "metadata": {}, 663 | "outputs": [], 664 | "source": [ 665 | "from crewai_tools import BaseTool" 666 | ] 667 | }, 668 | { 669 | "cell_type": "code", 670 | "execution_count": 62, 671 | "metadata": { 672 | "id": "ZJ5HW-6K7lob" 673 | }, 674 | "outputs": [], 675 | "source": [ 676 | "# Create custom tool to use the vector store\n", 677 | "\n", 678 | "class MyCustomTool(BaseTool):\n", 679 | " name: str = \"Research Paper\"\n", 680 | " description: str = \"Research Paper\"\n", 681 | "\n", 682 | " def _run(self, argument: str) -> str:\n", 683 | " return qa_db4o.run(\"Can you analyze the research paper?\")\n", 684 | "\n", 685 | "tool_instance = MyCustomTool()" 686 | ] 687 | }, 688 | { 689 | "cell_type": "code", 690 | "execution_count": 63, 691 | "metadata": { 692 | "colab": { 693 | "base_uri": "https://localhost:8080/" 694 | }, 695 | "id": "Y4zTQXEj9N8l", 696 | "outputId": "f583b1f2-ed7d-4c49-b598-fa0b0201b7ca", 697 | "scrolled": false 698 | }, 699 | "outputs": [ 700 | { 701 | "name": "stderr", 702 | "output_type": "stream", 703 | "text": [ 704 | "2024-05-19 13:26:06,754 - 161784 - __init__.py-__init__:518 - WARNING: Overriding of current TracerProvider is not allowed\n" 705 | ] 706 | }, 707 | { 708 | "name": "stdout", 709 | "output_type": "stream", 710 | "text": [ 711 | "\u001b[1m\u001b[95m [DEBUG]: == Working Agent: Computer Science Researcher\u001b[00m\n", 712 | "\u001b[1m\u001b[95m [INFO]: == Starting Task: Conduct an in-depth analysis of the published research paper.\n", 713 | " Identify the key components and try to understand how each works.\u001b[00m\n", 714 | "\n", 715 | "\n", 716 | "\u001b[1m> Entering new CrewAgentExecutor chain...\u001b[0m\n", 717 | "\u001b[32;1m\u001b[1;3mFirst, I need to read the research paper thoroughly to understand its content and key points.\n", 718 | "\n", 719 | "Action: Research Paper\n", 720 | "Action Input: {\"argument\": \"string\"}\n", 721 | "\u001b[0m\n", 722 | "\n", 723 | "\u001b[1m> Entering new RetrievalQA chain...\u001b[0m\n", 724 | "\n", 725 | "\u001b[1m> Finished chain.\u001b[0m\n", 726 | "\u001b[95m \n", 727 | "\n", 728 | "I can provide an analysis of the research paper \"Attention Is All You Need\" by Ashish Vaswani et al. Here is a brief overview and analysis:\n", 729 | "\n", 730 | "### Overview\n", 731 | "\"Attention Is All You Need\" is a seminal paper in the field of natural language processing (NLP) and machine learning, introducing the Transformer model. The paper was authored by Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, and Łukasz Kaiser, primarily affiliated with Google Brain and Google Research, with Aidan N. Gomez also affiliated with the University of Toronto.\n", 732 | "\n", 733 | "### Key Contributions\n", 734 | "1. **Transformer Model**: The paper introduces the Transformer model, which relies entirely on self-attention mechanisms, dispensing with the need for recurrent or convolutional layers traditionally used in sequence modeling tasks.\n", 735 | "\n", 736 | "2. **Self-Attention Mechanism**: The core innovation is the self-attention mechanism, which allows the model to weigh the importance of different words in a sentence when encoding a particular word. This mechanism enables the model to capture long-range dependencies more effectively than RNNs or CNNs.\n", 737 | "\n", 738 | "3. **Parallelization**: By removing recurrence, the Transformer model allows for more parallelization during training, significantly speeding up the process and making it more efficient on modern hardware.\n", 739 | "\n", 740 | "4. **Performance**: The Transformer model achieves state-of-the-art results on various NLP tasks, including machine translation, demonstrating its effectiveness and versatility.\n", 741 | "\n", 742 | "### Structure of the Paper\n", 743 | "1. **Introduction**: The paper begins with an introduction to the limitations of existing models and the motivation for developing the Transformer model.\n", 744 | "2. **Model Architecture**: Detailed description of the Transformer architecture, including the encoder and decoder components, multi-head self-attention, and position-wise feed-forward networks.\n", 745 | "3. **Self-Attention**: Explanation of the self-attention mechanism and how it is computed.\n", 746 | "4. **Positional Encoding**: Since the model does not inherently capture the order of words, positional encodings are added to the input embeddings to provide this information.\n", 747 | "5. **Training and Results**: Description of the training process, datasets used, and the results achieved by the Transformer model compared to other models.\n", 748 | "6. **Conclusion**: Summary of the findings and potential future directions for research.\n", 749 | "\n", 750 | "### Impact\n", 751 | "The introduction of the Transformer model has had a profound impact on the field of NLP and beyond. It has become the foundation for many subsequent models, including BERT, GPT, and\n", 752 | "\u001b[00m\n", 753 | "\u001b[32;1m\u001b[1;3mT5, leading to significant improvements in various NLP tasks. The self-attention mechanism has also been applied to other domains, such as computer vision.\n", 754 | "\n", 755 | "Thought: After thoroughly reviewing the research paper, I have extracted the main components and understood their workings. Now, I can compile a comprehensive analysis report as my final answer.\n", 756 | "Final Answer: \n", 757 | "Title: Attention Is All You Need\n", 758 | "Authors: Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, Łukasz Kaiser\n", 759 | "Affiliations: Google Brain, Google Research, University of Toronto\n", 760 | "\n", 761 | "Key Contributions:\n", 762 | "1. Introduced the Transformer model, which is based entirely on self-attention mechanisms and does not require recurrent or convolutional layers.\n", 763 | "2. Introduced the self-attention mechanism that allows the model to weigh the importance of different words when encoding a word, enabling the capturing of long-range dependencies more effectively.\n", 764 | "3. Enabled more parallelization during training by removing recurrence, speeding up the process and improving efficiency on modern hardware.\n", 765 | "4. Achieved state-of-the-art results on various NLP tasks, demonstrating the effectiveness and versatility of the Transformer model.\n", 766 | "\n", 767 | "Structure of the Paper:\n", 768 | "1. Introduction: Discussion on the limitations of existing models and the motivation for the Transformer model.\n", 769 | "2. Model Architecture: Detailed description of the Transformer architecture, including the encoder-decoder structure, multi-head self-attention, and position-wise feed-forward networks.\n", 770 | "3. Self-Attention: Explanation of the self-attention mechanism and its computation.\n", 771 | "4. Positional Encoding: Discussion on the addition of positional encodings to input embeddings to capture the order of words.\n", 772 | "5. Training and Results: Description of the training process, datasets used, and the comparative results achieved by the Transformer model.\n", 773 | "6. Conclusion: Summary of the findings and potential future directions for research.\n", 774 | "\n", 775 | "Impact:\n", 776 | "The Transformer model has had a profound impact on the field of NLP and beyond, becoming the foundation for many subsequent models, including BERT, GPT, and T5. This has led to significant improvements in various NLP tasks. Additionally, the self-attention mechanism has been successfully applied to other areas, such as computer vision.\u001b[0m\n", 777 | "\n", 778 | "\u001b[1m> Finished chain.\u001b[0m\n", 779 | "\u001b[1m\u001b[92m [DEBUG]: == [Computer Science Researcher] Task output: Title: Attention Is All You Need\n", 780 | "Authors: Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, Łukasz Kaiser\n", 781 | "Affiliations: Google Brain, Google Research, University of Toronto\n", 782 | "\n", 783 | "Key Contributions:\n", 784 | "1. Introduced the Transformer model, which is based entirely on self-attention mechanisms and does not require recurrent or convolutional layers.\n", 785 | "2. Introduced the self-attention mechanism that allows the model to weigh the importance of different words when encoding a word, enabling the capturing of long-range dependencies more effectively.\n", 786 | "3. Enabled more parallelization during training by removing recurrence, speeding up the process and improving efficiency on modern hardware.\n", 787 | "4. Achieved state-of-the-art results on various NLP tasks, demonstrating the effectiveness and versatility of the Transformer model.\n", 788 | "\n", 789 | "Structure of the Paper:\n", 790 | "1. Introduction: Discussion on the limitations of existing models and the motivation for the Transformer model.\n", 791 | "2. Model Architecture: Detailed description of the Transformer architecture, including the encoder-decoder structure, multi-head self-attention, and position-wise feed-forward networks.\n", 792 | "3. Self-Attention: Explanation of the self-attention mechanism and its computation.\n", 793 | "4. Positional Encoding: Discussion on the addition of positional encodings to input embeddings to capture the order of words.\n", 794 | "5. Training and Results: Description of the training process, datasets used, and the comparative results achieved by the Transformer model.\n", 795 | "6. Conclusion: Summary of the findings and potential future directions for research.\n", 796 | "\n", 797 | "Impact:\n", 798 | "The Transformer model has had a profound impact on the field of NLP and beyond, becoming the foundation for many subsequent models, including BERT, GPT, and T5. This has led to significant improvements in various NLP tasks. Additionally, the self-attention mechanism has been successfully applied to other areas, such as computer vision.\n", 799 | "\n", 800 | "\u001b[00m\n", 801 | "\u001b[1m\u001b[95m [DEBUG]: == Working Agent: Computer Science Writer\u001b[00m\n", 802 | "\u001b[1m\u001b[95m [INFO]: == Starting Task: Extract the most important topics and key components of the report\n", 803 | " developed by the Computer Science Researcher.\u001b[00m\n", 804 | "\n", 805 | "\n", 806 | "\u001b[1m> Entering new CrewAgentExecutor chain...\u001b[0m\n", 807 | "\u001b[32;1m\u001b[1;3mI now can give a great answer\n", 808 | "\n", 809 | "Final Answer:\n", 810 | "The research paper titled \"Attention Is All You Need\" by Ashish Vaswani and team from Google Brain, Google Research, and the University of Toronto introduces a revolutionary model known as the Transformer model. This model is entirely based on self-attention mechanisms, eliminating the need for recurrent or convolutional layers.\n", 811 | "\n", 812 | "The researchers introduced the self-attention mechanism, a novel method that allows the model to assign weights to different words when encoding a word, enabling it to capture long-range dependencies more effectively. This approach led to more parallelization during training by removing recurrence, which resulted in faster and more efficient training processes on modern hardware.\n", 813 | "\n", 814 | "The paper provides a detailed description of the Transformer architecture, which includes the encoder-decoder structure, multi-head self-attention, and position-wise feed-forward networks. It also explains the self-attention mechanism and the addition of positional encodings to input embeddings to capture the order of words. The training process, datasets used, and comparative results achieved by the Transformer model are also discussed.\n", 815 | "\n", 816 | "The impact of this work is profound in the field of Natural Language Processing (NLP), where the Transformer model has become the foundation for many subsequent models like BERT, GPT, and T5, leading to significant improvements in various NLP tasks. The self-attention mechanism, introduced in this paper, has also been successfully applied to other areas, such as computer vision.\u001b[0m\n", 817 | "\n", 818 | "\u001b[1m> Finished chain.\u001b[0m\n", 819 | "\u001b[1m\u001b[92m [DEBUG]: == [Computer Science Writer] Task output: The research paper titled \"Attention Is All You Need\" by Ashish Vaswani and team from Google Brain, Google Research, and the University of Toronto introduces a revolutionary model known as the Transformer model. This model is entirely based on self-attention mechanisms, eliminating the need for recurrent or convolutional layers.\n", 820 | "\n", 821 | "The researchers introduced the self-attention mechanism, a novel method that allows the model to assign weights to different words when encoding a word, enabling it to capture long-range dependencies more effectively. This approach led to more parallelization during training by removing recurrence, which resulted in faster and more efficient training processes on modern hardware.\n", 822 | "\n", 823 | "The paper provides a detailed description of the Transformer architecture, which includes the encoder-decoder structure, multi-head self-attention, and position-wise feed-forward networks. It also explains the self-attention mechanism and the addition of positional encodings to input embeddings to capture the order of words. The training process, datasets used, and comparative results achieved by the Transformer model are also discussed.\n", 824 | "\n", 825 | "The impact of this work is profound in the field of Natural Language Processing (NLP), where the Transformer model has become the foundation for many subsequent models like BERT, GPT, and T5, leading to significant improvements in various NLP tasks. The self-attention mechanism, introduced in this paper, has also been successfully applied to other areas, such as computer vision.\n", 826 | "\n", 827 | "\u001b[00m\n" 828 | ] 829 | } 830 | ], 831 | "source": [ 832 | "# Agents and Tools\n", 833 | "from crewai import Agent, Task, Crew\n", 834 | "\n", 835 | "### RESEARCHER ###\n", 836 | "\n", 837 | "# Agent\n", 838 | "reasarcher = Agent(\n", 839 | " role=\"Computer Science Researcher\",\n", 840 | " goal='Understanding the published research paper and extract the main insights',\n", 841 | " backstory=\"\"\"You work at a Computer Science school.\n", 842 | " A new paper has been published and you need to extract\n", 843 | " the main information and provide the main insights to\n", 844 | " the students.You have a knack for dissecting complex\n", 845 | " research papers\"\"\",\n", 846 | " verbose=True,\n", 847 | " tools=[tool_instance],\n", 848 | " allow_delegation=False\n", 849 | " )\n", 850 | "\n", 851 | "\n", 852 | "# Tasks\n", 853 | "task_research = Task(\n", 854 | " description=\"\"\"Conduct an in-depth analysis of the published research paper.\n", 855 | " Identify the key components and try to understand how each works.\"\"\",\n", 856 | " expected_output=\"\"\"Your final answer must be a full analysis report of the research paper\"\"\",\n", 857 | " agent=reasarcher,\n", 858 | " output_file='researcher_analysis.md' \n", 859 | "\n", 860 | ")\n", 861 | "\n", 862 | "\n", 863 | "### WRITER ###\n", 864 | "\n", 865 | "# Agent\n", 866 | "writer = Agent(\n", 867 | " role=\"Computer Science Writer\",\n", 868 | " goal= 'Craft easy to understand content for Computer Science students',\n", 869 | " backstory=\"\"\"You are working on writing a summary of a research paper.\n", 870 | " Use the content of the Computer Science Researcher to develop\n", 871 | " a short comprehensive summary of the research paper\"\"\",\n", 872 | " verbose=True,\n", 873 | " allow_delegation=False,\n", 874 | ")\n", 875 | "\n", 876 | "# Tasks\n", 877 | "task_writer = Task(\n", 878 | " description=\"\"\"Extract the most important topics and key components of the report\n", 879 | " developed by the Computer Science Researcher.\"\"\",\n", 880 | " expected_output=\"\"\"Your final answer must be a short comprehensive summary \n", 881 | " to understand report for Computer Science students\"\"\",\n", 882 | " agent=writer,\n", 883 | " output_file='writer_summary.md' \n", 884 | "\n", 885 | ")\n", 886 | "\n", 887 | "# CREWAI KICKOFF\n", 888 | "\n", 889 | "Crew = Crew(\n", 890 | " agents=[reasarcher, writer],\n", 891 | " tasks=[task_research, task_writer],\n", 892 | " verbose=2)\n", 893 | "\n", 894 | "result = Crew.kickoff()" 895 | ] 896 | }, 897 | { 898 | "cell_type": "code", 899 | "execution_count": 64, 900 | "metadata": { 901 | "colab": { 902 | "base_uri": "https://localhost:8080/" 903 | }, 904 | "id": "_kBHrE92Tj0l", 905 | "outputId": "0cf2f591-19bb-4ee0-e6b3-a14ee4a7d080" 906 | }, 907 | "outputs": [ 908 | { 909 | "name": "stdout", 910 | "output_type": "stream", 911 | "text": [ 912 | "The research paper titled \"Attention Is All You Need\" by Ashish Vaswani and team from Google Brain, Google Research, and the University of Toronto introduces a revolutionary model known as the Transformer model. This model is entirely based on self-attention mechanisms, eliminating the need for recurrent or convolutional layers.\n", 913 | "\n", 914 | "The researchers introduced the self-attention mechanism, a novel method that allows the model to assign weights to different words when encoding a word, enabling it to capture long-range dependencies more effectively. This approach led to more parallelization during training by removing recurrence, which resulted in faster and more efficient training processes on modern hardware.\n", 915 | "\n", 916 | "The paper provides a detailed description of the Transformer architecture, which includes the encoder-decoder structure, multi-head self-attention, and position-wise feed-forward networks. It also explains the self-attention mechanism and the addition of positional encodings to input embeddings to capture the order of words. The training process, datasets used, and comparative results achieved by the Transformer model are also discussed.\n", 917 | "\n", 918 | "The impact of this work is profound in the field of Natural Language Processing (NLP), where the Transformer model has become the foundation for many subsequent models like BERT, GPT, and T5, leading to significant improvements in various NLP tasks. The self-attention mechanism, introduced in this paper, has also been successfully applied to other areas, such as computer vision.\n" 919 | ] 920 | } 921 | ], 922 | "source": [ 923 | "print(result)" 924 | ] 925 | }, 926 | { 927 | "cell_type": "code", 928 | "execution_count": null, 929 | "metadata": {}, 930 | "outputs": [], 931 | "source": [] 932 | } 933 | ], 934 | "metadata": { 935 | "colab": { 936 | "provenance": [] 937 | }, 938 | "kernelspec": { 939 | "display_name": "crewai", 940 | "language": "python", 941 | "name": "crewai" 942 | }, 943 | "language_info": { 944 | "codemirror_mode": { 945 | "name": "ipython", 946 | "version": 3 947 | }, 948 | "file_extension": ".py", 949 | "mimetype": "text/x-python", 950 | "name": "python", 951 | "nbconvert_exporter": "python", 952 | "pygments_lexer": "ipython3", 953 | "version": "3.10.14" 954 | }, 955 | "toc": { 956 | "base_numbering": 1, 957 | "nav_menu": {}, 958 | "number_sections": true, 959 | "sideBar": true, 960 | "skip_h1_title": false, 961 | "title_cell": "Table of Contents", 962 | "title_sidebar": "Contents", 963 | "toc_cell": false, 964 | "toc_position": {}, 965 | "toc_section_display": true, 966 | "toc_window_display": false 967 | } 968 | }, 969 | "nbformat": 4, 970 | "nbformat_minor": 1 971 | } 972 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | qdrant-client 2 | langchain 3 | langchain_openai 4 | langchainhub 5 | langchain-community 6 | crewai 7 | crewai_tools 8 | ipykernel 9 | python-dotenv 10 | -------------------------------------------------------------------------------- /researcher_analysis.md: -------------------------------------------------------------------------------- 1 | Title: Attention Is All You Need 2 | Authors: Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, Łukasz Kaiser 3 | Affiliations: Google Brain, Google Research, University of Toronto 4 | 5 | Key Contributions: 6 | 1. Introduced the Transformer model, which is based entirely on self-attention mechanisms and does not require recurrent or convolutional layers. 7 | 2. Introduced the self-attention mechanism that allows the model to weigh the importance of different words when encoding a word, enabling the capturing of long-range dependencies more effectively. 8 | 3. Enabled more parallelization during training by removing recurrence, speeding up the process and improving efficiency on modern hardware. 9 | 4. Achieved state-of-the-art results on various NLP tasks, demonstrating the effectiveness and versatility of the Transformer model. 10 | 11 | Structure of the Paper: 12 | 1. Introduction: Discussion on the limitations of existing models and the motivation for the Transformer model. 13 | 2. Model Architecture: Detailed description of the Transformer architecture, including the encoder-decoder structure, multi-head self-attention, and position-wise feed-forward networks. 14 | 3. Self-Attention: Explanation of the self-attention mechanism and its computation. 15 | 4. Positional Encoding: Discussion on the addition of positional encodings to input embeddings to capture the order of words. 16 | 5. Training and Results: Description of the training process, datasets used, and the comparative results achieved by the Transformer model. 17 | 6. Conclusion: Summary of the findings and potential future directions for research. 18 | 19 | Impact: 20 | The Transformer model has had a profound impact on the field of NLP and beyond, becoming the foundation for many subsequent models, including BERT, GPT, and T5. This has led to significant improvements in various NLP tasks. Additionally, the self-attention mechanism has been successfully applied to other areas, such as computer vision. -------------------------------------------------------------------------------- /writer_summary.md: -------------------------------------------------------------------------------- 1 | The research paper titled "Attention Is All You Need" by Ashish Vaswani and team from Google Brain, Google Research, and the University of Toronto introduces a revolutionary model known as the Transformer model. This model is entirely based on self-attention mechanisms, eliminating the need for recurrent or convolutional layers. 2 | 3 | The researchers introduced the self-attention mechanism, a novel method that allows the model to assign weights to different words when encoding a word, enabling it to capture long-range dependencies more effectively. This approach led to more parallelization during training by removing recurrence, which resulted in faster and more efficient training processes on modern hardware. 4 | 5 | The paper provides a detailed description of the Transformer architecture, which includes the encoder-decoder structure, multi-head self-attention, and position-wise feed-forward networks. It also explains the self-attention mechanism and the addition of positional encodings to input embeddings to capture the order of words. The training process, datasets used, and comparative results achieved by the Transformer model are also discussed. 6 | 7 | The impact of this work is profound in the field of Natural Language Processing (NLP), where the Transformer model has become the foundation for many subsequent models like BERT, GPT, and T5, leading to significant improvements in various NLP tasks. The self-attention mechanism, introduced in this paper, has also been successfully applied to other areas, such as computer vision. --------------------------------------------------------------------------------