|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "0fc0309d-4d49-4bb5-bec0-bd92c6fddb28", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "## Fireworks.AI + LangChain + RAG\n", |
| 9 | + " \n", |
| 10 | + "[Fireworks AI](https://python.langchain.com/docs/integrations/llms/fireworks) wants to provide the best experience when working with LangChain, and here is an example of Fireworks + LangChain doing RAG\n", |
| 11 | + "\n", |
| 12 | + "See [our models page](https://fireworks.ai/models) for the full list of models. We use `accounts/fireworks/models/mixtral-8x7b-instruct` for RAG In this tutorial.\n", |
| 13 | + "\n", |
| 14 | + "For the RAG target, we will use the Gemma technical report https://storage.googleapis.com/deepmind-media/gemma/gemma-report.pdf " |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "code", |
| 19 | + "execution_count": 1, |
| 20 | + "id": "d12fb75a-f707-48d5-82a5-efe2d041813c", |
| 21 | + "metadata": {}, |
| 22 | + "outputs": [ |
| 23 | + { |
| 24 | + "name": "stdout", |
| 25 | + "output_type": "stream", |
| 26 | + "text": [ |
| 27 | + "\n", |
| 28 | + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.2.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.0\u001b[0m\n", |
| 29 | + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n", |
| 30 | + "Note: you may need to restart the kernel to use updated packages.\n", |
| 31 | + "Found existing installation: langchain-fireworks 0.0.1\n", |
| 32 | + "Uninstalling langchain-fireworks-0.0.1:\n", |
| 33 | + " Successfully uninstalled langchain-fireworks-0.0.1\n", |
| 34 | + "Note: you may need to restart the kernel to use updated packages.\n", |
| 35 | + "Obtaining file:///mnt/disks/data/langchain/libs/partners/fireworks\n", |
| 36 | + " Installing build dependencies ... \u001b[?25ldone\n", |
| 37 | + "\u001b[?25h Checking if build backend supports build_editable ... \u001b[?25ldone\n", |
| 38 | + "\u001b[?25h Getting requirements to build editable ... \u001b[?25ldone\n", |
| 39 | + "\u001b[?25h Preparing editable metadata (pyproject.toml) ... \u001b[?25ldone\n", |
| 40 | + "\u001b[?25hRequirement already satisfied: aiohttp<4.0.0,>=3.9.1 in /mnt/disks/data/langchain/.venv/lib/python3.9/site-packages (from langchain-fireworks==0.0.1) (3.9.3)\n", |
| 41 | + "Requirement already satisfied: fireworks-ai<0.13.0,>=0.12.0 in /mnt/disks/data/langchain/.venv/lib/python3.9/site-packages (from langchain-fireworks==0.0.1) (0.12.0)\n", |
| 42 | + "Requirement already satisfied: langchain-core<0.2,>=0.1 in /mnt/disks/data/langchain/.venv/lib/python3.9/site-packages (from langchain-fireworks==0.0.1) (0.1.23)\n", |
| 43 | + "Requirement already satisfied: requests<3,>=2 in /mnt/disks/data/langchain/.venv/lib/python3.9/site-packages (from langchain-fireworks==0.0.1) (2.31.0)\n", |
| 44 | + "Requirement already satisfied: aiosignal>=1.1.2 in /mnt/disks/data/langchain/.venv/lib/python3.9/site-packages (from aiohttp<4.0.0,>=3.9.1->langchain-fireworks==0.0.1) (1.3.1)\n", |
| 45 | + "Requirement already satisfied: attrs>=17.3.0 in /mnt/disks/data/langchain/.venv/lib/python3.9/site-packages (from aiohttp<4.0.0,>=3.9.1->langchain-fireworks==0.0.1) (23.1.0)\n", |
| 46 | + "Requirement already satisfied: frozenlist>=1.1.1 in /mnt/disks/data/langchain/.venv/lib/python3.9/site-packages (from aiohttp<4.0.0,>=3.9.1->langchain-fireworks==0.0.1) (1.4.0)\n", |
| 47 | + "Requirement already satisfied: multidict<7.0,>=4.5 in /mnt/disks/data/langchain/.venv/lib/python3.9/site-packages (from aiohttp<4.0.0,>=3.9.1->langchain-fireworks==0.0.1) (6.0.4)\n", |
| 48 | + "Requirement already satisfied: yarl<2.0,>=1.0 in /mnt/disks/data/langchain/.venv/lib/python3.9/site-packages (from aiohttp<4.0.0,>=3.9.1->langchain-fireworks==0.0.1) (1.9.2)\n", |
| 49 | + "Requirement already satisfied: async-timeout<5.0,>=4.0 in /mnt/disks/data/langchain/.venv/lib/python3.9/site-packages (from aiohttp<4.0.0,>=3.9.1->langchain-fireworks==0.0.1) (4.0.3)\n", |
| 50 | + "Requirement already satisfied: httpx in /mnt/disks/data/langchain/.venv/lib/python3.9/site-packages (from fireworks-ai<0.13.0,>=0.12.0->langchain-fireworks==0.0.1) (0.26.0)\n", |
| 51 | + "Requirement already satisfied: httpx-sse in /mnt/disks/data/langchain/.venv/lib/python3.9/site-packages (from fireworks-ai<0.13.0,>=0.12.0->langchain-fireworks==0.0.1) (0.4.0)\n", |
| 52 | + "Requirement already satisfied: pydantic in /mnt/disks/data/langchain/.venv/lib/python3.9/site-packages (from fireworks-ai<0.13.0,>=0.12.0->langchain-fireworks==0.0.1) (2.4.2)\n", |
| 53 | + "Requirement already satisfied: Pillow in /mnt/disks/data/langchain/.venv/lib/python3.9/site-packages (from fireworks-ai<0.13.0,>=0.12.0->langchain-fireworks==0.0.1) (10.2.0)\n", |
| 54 | + "Requirement already satisfied: PyYAML>=5.3 in /mnt/disks/data/langchain/.venv/lib/python3.9/site-packages (from langchain-core<0.2,>=0.1->langchain-fireworks==0.0.1) (6.0.1)\n", |
| 55 | + "Requirement already satisfied: anyio<5,>=3 in /mnt/disks/data/langchain/.venv/lib/python3.9/site-packages (from langchain-core<0.2,>=0.1->langchain-fireworks==0.0.1) (3.7.1)\n", |
| 56 | + "Requirement already satisfied: jsonpatch<2.0,>=1.33 in /mnt/disks/data/langchain/.venv/lib/python3.9/site-packages (from langchain-core<0.2,>=0.1->langchain-fireworks==0.0.1) (1.33)\n", |
| 57 | + "Requirement already satisfied: langsmith<0.2.0,>=0.1.0 in /mnt/disks/data/langchain/.venv/lib/python3.9/site-packages (from langchain-core<0.2,>=0.1->langchain-fireworks==0.0.1) (0.1.5)\n", |
| 58 | + "Requirement already satisfied: packaging<24.0,>=23.2 in /mnt/disks/data/langchain/.venv/lib/python3.9/site-packages (from langchain-core<0.2,>=0.1->langchain-fireworks==0.0.1) (23.2)\n", |
| 59 | + "Requirement already satisfied: tenacity<9.0.0,>=8.1.0 in /mnt/disks/data/langchain/.venv/lib/python3.9/site-packages (from langchain-core<0.2,>=0.1->langchain-fireworks==0.0.1) (8.2.3)\n", |
| 60 | + "Requirement already satisfied: charset-normalizer<4,>=2 in /mnt/disks/data/langchain/.venv/lib/python3.9/site-packages (from requests<3,>=2->langchain-fireworks==0.0.1) (3.3.0)\n", |
| 61 | + "Requirement already satisfied: idna<4,>=2.5 in /mnt/disks/data/langchain/.venv/lib/python3.9/site-packages (from requests<3,>=2->langchain-fireworks==0.0.1) (3.4)\n", |
| 62 | + "Requirement already satisfied: urllib3<3,>=1.21.1 in /mnt/disks/data/langchain/.venv/lib/python3.9/site-packages (from requests<3,>=2->langchain-fireworks==0.0.1) (2.0.6)\n", |
| 63 | + "Requirement already satisfied: certifi>=2017.4.17 in /mnt/disks/data/langchain/.venv/lib/python3.9/site-packages (from requests<3,>=2->langchain-fireworks==0.0.1) (2023.7.22)\n", |
| 64 | + "Requirement already satisfied: sniffio>=1.1 in /mnt/disks/data/langchain/.venv/lib/python3.9/site-packages (from anyio<5,>=3->langchain-core<0.2,>=0.1->langchain-fireworks==0.0.1) (1.3.0)\n", |
| 65 | + "Requirement already satisfied: exceptiongroup in /mnt/disks/data/langchain/.venv/lib/python3.9/site-packages (from anyio<5,>=3->langchain-core<0.2,>=0.1->langchain-fireworks==0.0.1) (1.1.3)\n", |
| 66 | + "Requirement already satisfied: jsonpointer>=1.9 in /mnt/disks/data/langchain/.venv/lib/python3.9/site-packages (from jsonpatch<2.0,>=1.33->langchain-core<0.2,>=0.1->langchain-fireworks==0.0.1) (2.4)\n", |
| 67 | + "Requirement already satisfied: annotated-types>=0.4.0 in /mnt/disks/data/langchain/.venv/lib/python3.9/site-packages (from pydantic->fireworks-ai<0.13.0,>=0.12.0->langchain-fireworks==0.0.1) (0.5.0)\n", |
| 68 | + "Requirement already satisfied: pydantic-core==2.10.1 in /mnt/disks/data/langchain/.venv/lib/python3.9/site-packages (from pydantic->fireworks-ai<0.13.0,>=0.12.0->langchain-fireworks==0.0.1) (2.10.1)\n", |
| 69 | + "Requirement already satisfied: typing-extensions>=4.6.1 in /mnt/disks/data/langchain/.venv/lib/python3.9/site-packages (from pydantic->fireworks-ai<0.13.0,>=0.12.0->langchain-fireworks==0.0.1) (4.8.0)\n", |
| 70 | + "Requirement already satisfied: httpcore==1.* in /mnt/disks/data/langchain/.venv/lib/python3.9/site-packages (from httpx->fireworks-ai<0.13.0,>=0.12.0->langchain-fireworks==0.0.1) (1.0.2)\n", |
| 71 | + "Requirement already satisfied: h11<0.15,>=0.13 in /mnt/disks/data/langchain/.venv/lib/python3.9/site-packages (from httpcore==1.*->httpx->fireworks-ai<0.13.0,>=0.12.0->langchain-fireworks==0.0.1) (0.14.0)\n", |
| 72 | + "Building wheels for collected packages: langchain-fireworks\n", |
| 73 | + " Building editable for langchain-fireworks (pyproject.toml) ... \u001b[?25ldone\n", |
| 74 | + "\u001b[?25h Created wheel for langchain-fireworks: filename=langchain_fireworks-0.0.1-py3-none-any.whl size=2228 sha256=564071b120b09ec31f2dc737733448a33bbb26e40b49fcde0c129ad26045259d\n", |
| 75 | + " Stored in directory: /tmp/pip-ephem-wheel-cache-oz368vdk/wheels/e0/ad/31/d7e76dd73d61905ff7f369f5b0d21a4b5e7af4d3cb7487aece\n", |
| 76 | + "Successfully built langchain-fireworks\n", |
| 77 | + "Installing collected packages: langchain-fireworks\n", |
| 78 | + "Successfully installed langchain-fireworks-0.0.1\n", |
| 79 | + "\n", |
| 80 | + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.2.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.0\u001b[0m\n", |
| 81 | + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n", |
| 82 | + "Note: you may need to restart the kernel to use updated packages.\n" |
| 83 | + ] |
| 84 | + } |
| 85 | + ], |
| 86 | + "source": [ |
| 87 | + "%pip install --quiet pypdf chromadb tiktoken openai \n", |
| 88 | + "%pip uninstall -y langchain-fireworks\n", |
| 89 | + "%pip install --editable /mnt/disks/data/langchain/libs/partners/fireworks" |
| 90 | + ] |
| 91 | + }, |
| 92 | + { |
| 93 | + "cell_type": "code", |
| 94 | + "execution_count": 3, |
| 95 | + "id": "cf719376", |
| 96 | + "metadata": {}, |
| 97 | + "outputs": [ |
| 98 | + { |
| 99 | + "name": "stdout", |
| 100 | + "output_type": "stream", |
| 101 | + "text": [ |
| 102 | + "<module 'fireworks' from '/mnt/disks/data/langchain/.venv/lib/python3.9/site-packages/fireworks/__init__.py'>\n" |
| 103 | + ] |
| 104 | + } |
| 105 | + ], |
| 106 | + "source": [ |
| 107 | + "import fireworks\n", |
| 108 | + "\n", |
| 109 | + "print(fireworks)\n", |
| 110 | + "import fireworks.client" |
| 111 | + ] |
| 112 | + }, |
| 113 | + { |
| 114 | + "cell_type": "code", |
| 115 | + "execution_count": null, |
| 116 | + "id": "9ab49327-0532-4480-804c-d066c302a322", |
| 117 | + "metadata": {}, |
| 118 | + "outputs": [], |
| 119 | + "source": [ |
| 120 | + "# Load\n", |
| 121 | + "import requests\n", |
| 122 | + "from langchain_community.document_loaders import PyPDFLoader\n", |
| 123 | + "\n", |
| 124 | + "# Download the PDF from a URL and save it to a temporary location\n", |
| 125 | + "url = \"https://storage.googleapis.com/deepmind-media/gemma/gemma-report.pdf\"\n", |
| 126 | + "response = requests.get(url, stream=True)\n", |
| 127 | + "file_name = \"temp_file.pdf\"\n", |
| 128 | + "with open(file_name, \"wb\") as pdf:\n", |
| 129 | + " pdf.write(response.content)\n", |
| 130 | + "\n", |
| 131 | + "loader = PyPDFLoader(file_name)\n", |
| 132 | + "data = loader.load()\n", |
| 133 | + "\n", |
| 134 | + "# Split\n", |
| 135 | + "from langchain.text_splitter import RecursiveCharacterTextSplitter\n", |
| 136 | + "\n", |
| 137 | + "text_splitter = RecursiveCharacterTextSplitter(chunk_size=2000, chunk_overlap=0)\n", |
| 138 | + "all_splits = text_splitter.split_documents(data)\n", |
| 139 | + "\n", |
| 140 | + "# Add to vectorDB\n", |
| 141 | + "from langchain_community.vectorstores import Chroma\n", |
| 142 | + "from langchain_fireworks.embeddings import FireworksEmbeddings\n", |
| 143 | + "\n", |
| 144 | + "vectorstore = Chroma.from_documents(\n", |
| 145 | + " documents=all_splits,\n", |
| 146 | + " collection_name=\"rag-chroma\",\n", |
| 147 | + " embedding=FireworksEmbeddings(),\n", |
| 148 | + ")\n", |
| 149 | + "\n", |
| 150 | + "retriever = vectorstore.as_retriever()" |
| 151 | + ] |
| 152 | + }, |
| 153 | + { |
| 154 | + "cell_type": "code", |
| 155 | + "execution_count": 3, |
| 156 | + "id": "4efaddd9-3dbb-455c-ba54-0ad7f2d2ce0f", |
| 157 | + "metadata": {}, |
| 158 | + "outputs": [], |
| 159 | + "source": [ |
| 160 | + "from langchain_core.output_parsers import StrOutputParser\n", |
| 161 | + "from langchain_core.prompts import ChatPromptTemplate\n", |
| 162 | + "from langchain_core.pydantic_v1 import BaseModel\n", |
| 163 | + "from langchain_core.runnables import RunnableParallel, RunnablePassthrough\n", |
| 164 | + "\n", |
| 165 | + "# RAG prompt\n", |
| 166 | + "template = \"\"\"Answer the question based only on the following context:\n", |
| 167 | + "{context}\n", |
| 168 | + "\n", |
| 169 | + "Question: {question}\n", |
| 170 | + "\"\"\"\n", |
| 171 | + "prompt = ChatPromptTemplate.from_template(template)\n", |
| 172 | + "\n", |
| 173 | + "# LLM\n", |
| 174 | + "from langchain_together import Together\n", |
| 175 | + "\n", |
| 176 | + "llm = Together(\n", |
| 177 | + " model=\"mistralai/Mixtral-8x7B-Instruct-v0.1\",\n", |
| 178 | + " temperature=0.0,\n", |
| 179 | + " max_tokens=2000,\n", |
| 180 | + " top_k=1,\n", |
| 181 | + ")\n", |
| 182 | + "\n", |
| 183 | + "# RAG chain\n", |
| 184 | + "chain = (\n", |
| 185 | + " RunnableParallel({\"context\": retriever, \"question\": RunnablePassthrough()})\n", |
| 186 | + " | prompt\n", |
| 187 | + " | llm\n", |
| 188 | + " | StrOutputParser()\n", |
| 189 | + ")" |
| 190 | + ] |
| 191 | + }, |
| 192 | + { |
| 193 | + "cell_type": "code", |
| 194 | + "execution_count": 4, |
| 195 | + "id": "88b1ee51-1b0f-4ebf-bb32-e50e843f0eeb", |
| 196 | + "metadata": {}, |
| 197 | + "outputs": [ |
| 198 | + { |
| 199 | + "data": { |
| 200 | + "text/plain": [ |
| 201 | + "'\\nAnswer: The architectural details of Mixtral are as follows:\\n- Dimension (dim): 4096\\n- Number of layers (n\\\\_layers): 32\\n- Dimension of each head (head\\\\_dim): 128\\n- Hidden dimension (hidden\\\\_dim): 14336\\n- Number of heads (n\\\\_heads): 32\\n- Number of kv heads (n\\\\_kv\\\\_heads): 8\\n- Context length (context\\\\_len): 32768\\n- Vocabulary size (vocab\\\\_size): 32000\\n- Number of experts (num\\\\_experts): 8\\n- Number of top k experts (top\\\\_k\\\\_experts): 2\\n\\nMixtral is based on a transformer architecture and uses the same modifications as described in [18], with the notable exceptions that Mixtral supports a fully dense context length of 32k tokens, and the feedforward block picks from a set of 8 distinct groups of parameters. At every layer, for every token, a router network chooses two of these groups (the “experts”) to process the token and combine their output additively. This technique increases the number of parameters of a model while controlling cost and latency, as the model only uses a fraction of the total set of parameters per token. Mixtral is pretrained with multilingual data using a context size of 32k tokens. It either matches or exceeds the performance of Llama 2 70B and GPT-3.5, over several benchmarks. In particular, Mixtral vastly outperforms Llama 2 70B on mathematics, code generation, and multilingual benchmarks.'" |
| 202 | + ] |
| 203 | + }, |
| 204 | + "execution_count": 4, |
| 205 | + "metadata": {}, |
| 206 | + "output_type": "execute_result" |
| 207 | + } |
| 208 | + ], |
| 209 | + "source": [ |
| 210 | + "chain.invoke(\"What are the Architectural details of Mixtral?\")" |
| 211 | + ] |
| 212 | + }, |
| 213 | + { |
| 214 | + "cell_type": "markdown", |
| 215 | + "id": "755cf871-26b7-4e30-8b91-9ffd698470f4", |
| 216 | + "metadata": {}, |
| 217 | + "source": [ |
| 218 | + "Trace: \n", |
| 219 | + "\n", |
| 220 | + "https://smith.langchain.com/public/935fd642-06a6-4b42-98e3-6074f93115cd/r" |
| 221 | + ] |
| 222 | + } |
| 223 | + ], |
| 224 | + "metadata": { |
| 225 | + "kernelspec": { |
| 226 | + "display_name": "Python 3 (ipykernel)", |
| 227 | + "language": "python", |
| 228 | + "name": "python3" |
| 229 | + }, |
| 230 | + "language_info": { |
| 231 | + "codemirror_mode": { |
| 232 | + "name": "ipython", |
| 233 | + "version": 3 |
| 234 | + }, |
| 235 | + "file_extension": ".py", |
| 236 | + "mimetype": "text/x-python", |
| 237 | + "name": "python", |
| 238 | + "nbconvert_exporter": "python", |
| 239 | + "pygments_lexer": "ipython3", |
| 240 | + "version": "3.9.12" |
| 241 | + } |
| 242 | + }, |
| 243 | + "nbformat": 4, |
| 244 | + "nbformat_minor": 5 |
| 245 | +} |
0 commit comments