├── langchain-types.png ├── README.md └── LangChain_Types_Explained.ipynb /langchain-types.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sugarforever/LangChain-Types-Explained/main/langchain-types.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LangChain Chain Types Explained 2 | 3 | 请在使用LangChain-Types-Explained.ipynb时,设置有效的OpenAI API Key 4 | 5 | ``` 6 | OPENAI_API_KEY = '' 7 | ``` 8 | 9 | ## 图解LangChain的不同Chain Type 10 | 11 | ![LangChain chain types](langchain-types.png) 12 | -------------------------------------------------------------------------------- /LangChain_Types_Explained.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "colab_type": "text", 7 | "id": "view-in-github" 8 | }, 9 | "source": [ 10 | "\"Open" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": null, 16 | "metadata": { 17 | "id": "Nifwi9FrKb3g" 18 | }, 19 | "outputs": [], 20 | "source": [ 21 | "%pip install openai\n", 22 | "%pip install pinecone-client\n", 23 | "%pip install langchain\n", 24 | "%pip install unstructured\n", 25 | "%pip install --upgrade pillow==8.0.0" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": null, 31 | "metadata": { 32 | "id": "MFwfRg3EnBw5" 33 | }, 34 | "outputs": [], 35 | "source": [ 36 | "%pip install pdfminer" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": null, 42 | "metadata": { 43 | "id": "Nr66ByBrnWTD" 44 | }, 45 | "outputs": [], 46 | "source": [ 47 | "%pip install \"unstructured[local-inference]\"" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": null, 53 | "metadata": { 54 | "id": "zQPh7UecoXjZ" 55 | }, 56 | "outputs": [], 57 | "source": [ 58 | "%pip install tiktoken" 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": null, 64 | "metadata": { 65 | "id": "-v53gJJkkMIs" 66 | }, 67 | "outputs": [], 68 | "source": [ 69 | "!wget https://resources.messari.io/pdf/messari-report-crypto-theses-for-2023.pdf -O messari-report-crypto-theses-for-2023.pdf\n", 70 | "!ls -alt messari-report-crypto-theses-for-2023.pdf" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": null, 76 | "metadata": { 77 | "id": "5xgbUBve0LuN" 78 | }, 79 | "outputs": [], 80 | "source": [ 81 | "from langchain.document_loaders import UnstructuredFileLoader" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": null, 87 | "metadata": { 88 | "id": "UpOBdhBrdaiU" 89 | }, 90 | "outputs": [], 91 | "source": [ 92 | "def load_pdf(pdf_path):\n", 93 | " loader = UnstructuredFileLoader(pdf_path)\n", 94 | " docs = loader.load()\n", 95 | "\n", 96 | " return docs" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": null, 102 | "metadata": { 103 | "id": "_-1itVpTY8Gz" 104 | }, 105 | "outputs": [], 106 | "source": [ 107 | "docs = load_pdf(\"./messari-report-crypto-theses-for-2023.pdf\")" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": null, 113 | "metadata": { 114 | "id": "33zAThYjY9gA" 115 | }, 116 | "outputs": [], 117 | "source": [ 118 | "print (f'You have {len(docs)} document(s) in your data')\n", 119 | "print (f'There are {len(docs[0].page_content)} characters in your document')" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": null, 125 | "metadata": { 126 | "id": "ziV20FzmZpm1" 127 | }, 128 | "outputs": [], 129 | "source": [ 130 | "from langchain.text_splitter import RecursiveCharacterTextSplitter\n", 131 | "\n", 132 | "text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n", 133 | "split_docs = text_splitter.split_documents(docs)" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": null, 139 | "metadata": { 140 | "id": "WlDqqN_6Z08T" 141 | }, 142 | "outputs": [], 143 | "source": [ 144 | "print (f'Now you have {len(split_docs)} documents')" 145 | ] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "execution_count": null, 150 | "metadata": { 151 | "id": "6IOlsXpbaIAw" 152 | }, 153 | "outputs": [], 154 | "source": [ 155 | "from langchain.vectorstores import Pinecone\n", 156 | "from langchain.embeddings.openai import OpenAIEmbeddings\n", 157 | "import pinecone" 158 | ] 159 | }, 160 | { 161 | "cell_type": "code", 162 | "execution_count": null, 163 | "metadata": { 164 | "id": "NPgBljfQaPM_" 165 | }, 166 | "outputs": [], 167 | "source": [ 168 | "OPENAI_API_KEY = ''" 169 | ] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "execution_count": null, 174 | "metadata": { 175 | "id": "yBAx1_X-beQp" 176 | }, 177 | "outputs": [], 178 | "source": [ 179 | "from langchain.llms import OpenAI\n", 180 | "from langchain.chains.summarize import load_summarize_chain" 181 | ] 182 | }, 183 | { 184 | "cell_type": "code", 185 | "execution_count": null, 186 | "metadata": { 187 | "id": "V8hds-zybhfc" 188 | }, 189 | "outputs": [], 190 | "source": [ 191 | "llm = OpenAI(temperature=0, openai_api_key=OPENAI_API_KEY)\n", 192 | "chain = load_summarize_chain(llm, chain_type=\"stuff\", verbose=True)" 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": null, 198 | "metadata": { 199 | "id": "K3SlreQ2haC4" 200 | }, 201 | "outputs": [], 202 | "source": [ 203 | "input_docs = split_docs[:20]" 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "execution_count": null, 209 | "metadata": { 210 | "id": "zdF03LqphOQY" 211 | }, 212 | "outputs": [], 213 | "source": [ 214 | "chain.run(input_documents=input_docs)" 215 | ] 216 | } 217 | ], 218 | "metadata": { 219 | "colab": { 220 | "authorship_tag": "ABX9TyOYgF8yGya/IxKz6XYqcgMe", 221 | "include_colab_link": true, 222 | "provenance": [] 223 | }, 224 | "kernelspec": { 225 | "display_name": "Python 3", 226 | "name": "python3" 227 | }, 228 | "language_info": { 229 | "codemirror_mode": { 230 | "name": "ipython", 231 | "version": 3 232 | }, 233 | "file_extension": ".py", 234 | "mimetype": "text/x-python", 235 | "name": "python", 236 | "nbconvert_exporter": "python", 237 | "pygments_lexer": "ipython3", 238 | "version": "3.9.16" 239 | } 240 | }, 241 | "nbformat": 4, 242 | "nbformat_minor": 0 243 | } 244 | --------------------------------------------------------------------------------