├── README.md ├── LICENSE ├── Caching.ipynb └── Blog_to_chatbot.ipynb /README.md: -------------------------------------------------------------------------------- 1 | # Langchain tutorials 2 | 3 | Overview and tutorial of the LangChain Library 4 | 5 | ### Getting Started 6 | 7 | Videos coming soon https://www.youtube.com/@AnilChandraNaiduMatcha 8 | .Subscribe to the channel to get latest content 9 | 10 | Follow [Anil Chandra Naidu Matcha](https://twitter.com/matchaman11) on twitter for updates 11 | 12 | Join our discord server for support https://discord.gg/FBpafqbbYF 13 | 14 | ### Also check 15 | 16 | [Langchain Course](https://github.com/SamurAIGPT/langchain-course) 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Anil Chandra Naidu Matcha 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Caching.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "authorship_tag": "ABX9TyOLHnR40BXyt6FenhWQ1cXr", 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "id": "view-in-github", 23 | "colab_type": "text" 24 | }, 25 | "source": [ 26 | "\"Open" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "source": [ 32 | "!pip install langchain gptcache openai" 33 | ], 34 | "metadata": { 35 | "id": "Mh4Z79kYT444" 36 | }, 37 | "execution_count": null, 38 | "outputs": [] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": 5, 43 | "metadata": { 44 | "id": "9aNFhMWzTZTp" 45 | }, 46 | "outputs": [], 47 | "source": [ 48 | "from gptcache import Cache\n", 49 | "from gptcache.adapter.api import init_similar_cache\n", 50 | "from langchain.cache import GPTCache\n", 51 | "import hashlib\n", 52 | "import langchain\n", 53 | "from langchain.llms import OpenAI\n", 54 | "\n", 55 | "def get_hashed_name(name):\n", 56 | " return hashlib.sha256(name.encode()).hexdigest()\n", 57 | "\n", 58 | "\n", 59 | "def init_gptcache(cache_obj: Cache, llm: str):\n", 60 | " hashed_llm = get_hashed_name(llm)\n", 61 | " init_similar_cache(cache_obj=cache_obj, data_dir=f\"similar_cache_{hashed_llm}\")\n", 62 | "\n", 63 | "\n", 64 | "langchain.llm_cache = GPTCache(init_gptcache)" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "source": [ 70 | "llm = OpenAI(model_name=\"text-davinci-002\", n=2, best_of=2, openai_api_key=\"api-key\")" 71 | ], 72 | "metadata": { 73 | "id": "8xL3ionzTzSZ" 74 | }, 75 | "execution_count": 6, 76 | "outputs": [] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "source": [ 81 | "import time\n", 82 | "start = time.time()\n", 83 | "llm(\"Tell me one rhyme\")\n", 84 | "print(\"Time taken\", time.time()-start)" 85 | ], 86 | "metadata": { 87 | "colab": { 88 | "base_uri": "https://localhost:8080/" 89 | }, 90 | "id": "oi5s5gnqUKeN", 91 | "outputId": "a1aca235-1e6f-479e-d654-87a92185d30c" 92 | }, 93 | "execution_count": 18, 94 | "outputs": [ 95 | { 96 | "output_type": "stream", 97 | "name": "stdout", 98 | "text": [ 99 | "Time taken 4.124613285064697\n" 100 | ] 101 | } 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "source": [ 107 | "start = time.time()\n", 108 | "llm(\"Tell me a rhyme\")\n", 109 | "print(\"Time taken\", time.time()-start)" 110 | ], 111 | "metadata": { 112 | "colab": { 113 | "base_uri": "https://localhost:8080/" 114 | }, 115 | "id": "K53Hsor_URzq", 116 | "outputId": "f8a9fd6f-2342-445b-eab2-1e22411ace60" 117 | }, 118 | "execution_count": 19, 119 | "outputs": [ 120 | { 121 | "output_type": "stream", 122 | "name": "stdout", 123 | "text": [ 124 | "Time taken 0.9025411605834961\n" 125 | ] 126 | } 127 | ] 128 | } 129 | ] 130 | } -------------------------------------------------------------------------------- /Blog_to_chatbot.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "authorship_tag": "ABX9TyOVvn6lfex8xyevRbi5evCu", 8 | "include_colab_link": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "id": "view-in-github", 23 | "colab_type": "text" 24 | }, 25 | "source": [ 26 | "\"Open" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "source": [ 32 | "!pip install requests langchain trafilatura openai BeautifulSoup" 33 | ], 34 | "metadata": { 35 | "id": "ygUPNWVWMMO1" 36 | }, 37 | "execution_count": null, 38 | "outputs": [] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "source": [ 43 | "import requests\n", 44 | "from bs4 import BeautifulSoup\n", 45 | "wunder = requests.get(\"https://uuki.live/sitemap.xml\")\n", 46 | "parcala = BeautifulSoup(wunder.content, \"xml\")\n", 47 | "\n", 48 | "urls = []\n", 49 | "\n", 50 | "loc_tags = parcala.find_all('loc')\n", 51 | "\n", 52 | "for loc in loc_tags:\n", 53 | " if 'learn/' in loc.get_text():\n", 54 | " urls.append(loc.get_text().strip())\n", 55 | "\n", 56 | "print(urls)\n" 57 | ], 58 | "metadata": { 59 | "colab": { 60 | "base_uri": "https://localhost:8080/" 61 | }, 62 | "id": "Y6sO5E9sMKk1", 63 | "outputId": "07e689bc-cb48-4c8f-d337-360fa3d78d15" 64 | }, 65 | "execution_count": 12, 66 | "outputs": [ 67 | { 68 | "output_type": "stream", 69 | "name": "stdout", 70 | "text": [ 71 | "['https://uuki.live/learn/10-best-ai-image-generators-in-2023', 'https://uuki.live/learn/10-best-discord-alternatives-for-better-experience', 'https://uuki.live/learn/5-best-online-community-platforms-for-2023', 'https://uuki.live/learn/5-tips-for-building-a-stronger-brand-community', 'https://uuki.live/learn/6-steps-to-hack-your-way-to-a-great-producthunt-launch', 'https://uuki.live/learn/7-best-solana-nft-marketplaces', 'https://uuki.live/learn/7-examples-of-captivating-community-welcome-emails', 'https://uuki.live/learn/7-tips-for-online-community-moderators', 'https://uuki.live/learn/8-tips-for-the-perfect-community-newsletter', 'https://uuki.live/learn/autogpt-vs-babyagi', 'https://uuki.live/learn/best-discord-servers-for-web3-developers', 'https://uuki.live/learn/community-app-the-text-messaging-app-for-creators-and-influencers', 'https://uuki.live/learn/create-your-own-social-network-like-facebook', 'https://uuki.live/learn/creative-membership-level-names-how-to-name-your-memberships-with-examples', 'https://uuki.live/learn/differences-between-social-media-manager-and-community-manager', 'https://uuki.live/learn/everything-you-need-to-know-about-ko-fi', 'https://uuki.live/learn/facebook-group-and-page-badges-explained', 'https://uuki.live/learn/how-can-moderators-make-your-community-a-better-place', 'https://uuki.live/learn/how-to-add-binance-smart-chain-to-metamask', 'https://uuki.live/learn/how-to-add-cronos-to-metamask', 'https://uuki.live/learn/how-to-add-polygon-matic-network-to-metamask', 'https://uuki.live/learn/how-to-add-slp-to-metamask', 'https://uuki.live/learn/how-to-build-a-community-of-practice', 'https://uuki.live/learn/how-to-build-an-online-community', 'https://uuki.live/learn/how-to-connect-uniswap-to-metamask', 'https://uuki.live/learn/how-to-create-a-ronin-wallet-to-play-axie-infinity', 'https://uuki.live/learn/how-to-get-your-community-indexed-on-google-faster', 'https://uuki.live/learn/how-to-grow-a-discord-community', 'https://uuki.live/learn/how-to-start-an-online-school', 'https://uuki.live/learn/how-to-transfer-bnb-from-trust-wallet-to-binance', 'https://uuki.live/learn/how-to-transfer-crypto-from-trust-wallet-to-coinbase', 'https://uuki.live/learn/how-to-transfer-ethereum-from-coinbase-to-metamask', 'https://uuki.live/learn/how-to-transfer-ethereum-from-metamask-to-coinbase', 'https://uuki.live/learn/how-to-write-guidelines-for-your-online-community-platform-forum', 'https://uuki.live/learn/kajabi-vs-thinkific', 'https://uuki.live/learn/messaging-app-vs-community-platform', 'https://uuki.live/learn/minigpt-4-open-source-model-for-complex-vision-language-tasks-like-gpt-4', 'https://uuki.live/learn/seo-tips-to-increase-traffic-to-your-community-and-forum', 'https://uuki.live/learn/should-you-use-slack-for-your-community', 'https://uuki.live/learn/the-5-best-online-community-management-courses', 'https://uuki.live/learn/the-best-guide-to-circle-so-and-the-5-best-alternatives-for-your-community', 'https://uuki.live/learn/the-best-gumroad-alternatives-for-2023', 'https://uuki.live/learn/the-best-tribe-alternatives-for-2023', 'https://uuki.live/learn/the-essential-seo-guide-for-online-community-and-forums', 'https://uuki.live/learn/the-top-20-side-hustles-to-make-extra-money-in-2022', 'https://uuki.live/learn/the-ultimate-guide-to-building-a-customer-community', 'https://uuki.live/learn/the-ultimate-guide-to-community-management', 'https://uuki.live/learn/twitter-vs-mastodon', 'https://uuki.live/learn/username-with-only-letters-numbers-and-underscore', 'https://uuki.live/learn/what-are-poap-nfts-and-why-are-they-important', 'https://uuki.live/learn/what-is-an-online-course-in-2022', 'https://uuki.live/learn/what-is-auto-gpt-and-what-can-it-do', 'https://uuki.live/learn/what-is-huggingchat-everything-you-need-to-know-about-this-open-source-ai-chatbot', 'https://uuki.live/learn/what-is-prompt-engineering-how-to-write-an-effective-gpt-3-5-or-gpt-4-prompt', 'https://uuki.live/learn/white-label-online-courses']\n" 72 | ] 73 | } 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": 19, 79 | "metadata": { 80 | "colab": { 81 | "base_uri": "https://localhost:8080/" 82 | }, 83 | "id": "ZQ6gKwYlIhjm", 84 | "outputId": "e3d716e2-21b5-4e48-dd03-3bdbd9616354" 85 | }, 86 | "outputs": [ 87 | { 88 | "output_type": "stream", 89 | "name": "stdout", 90 | "text": [ 91 | "{'output_text': 'According to a list compiled by Uuki.live, the best AI image generators include DALL-E 2, MidJourney, and WOMBO Dream.\\nSOURCES: https://uuki.live/learn/10-best-ai-image-generators-in-2023'}\n" 92 | ] 93 | } 94 | ], 95 | "source": [ 96 | "from langchain.tools import BaseTool\n", 97 | "from langchain.text_splitter import RecursiveCharacterTextSplitter\n", 98 | "from pydantic import Field\n", 99 | "from langchain.chains.qa_with_sources.loading import load_qa_with_sources_chain, BaseCombineDocumentsChain\n", 100 | "from langchain.chat_models import ChatOpenAI\n", 101 | "import os, asyncio, trafilatura\n", 102 | "from langchain.docstore.document import Document\n", 103 | "\n", 104 | "def _get_text_splitter():\n", 105 | " return RecursiveCharacterTextSplitter(\n", 106 | " # Set a really small chunk size, just to show.\n", 107 | " chunk_size = 500,\n", 108 | " chunk_overlap = 20,\n", 109 | " length_function = len,\n", 110 | " )\n", 111 | "\n", 112 | "class WebpageQATool(BaseTool):\n", 113 | " name = \"query_webpage\"\n", 114 | " description = \"Browse a webpage and retrieve the information relevant to the question.\"\n", 115 | " text_splitter: RecursiveCharacterTextSplitter = Field(default_factory=_get_text_splitter)\n", 116 | " qa_chain: BaseCombineDocumentsChain\n", 117 | "\n", 118 | " def _run(self, question: str) -> str:\n", 119 | " docs = []\n", 120 | " for url in urls[:2]:\n", 121 | " result = trafilatura.extract(trafilatura.fetch_url(url))\n", 122 | " docs.append(Document(page_content=result, metadata={\"source\": url}))\n", 123 | " #docs = [Document(page_content=result, metadata={\"source\": url})]\n", 124 | " web_docs = self.text_splitter.split_documents(docs)\n", 125 | " results = []\n", 126 | " for i in range(0, len(web_docs), 4):\n", 127 | " input_docs = web_docs[i:i+4]\n", 128 | " window_result = self.qa_chain({\"input_documents\": input_docs, \"question\": question}, return_only_outputs=True)\n", 129 | " results.append(f\"Response from window {i} - {window_result}\")\n", 130 | " results_docs = [Document(page_content=\"\\n\".join(results), metadata={\"source\": url})]\n", 131 | " return self.qa_chain({\"input_documents\": results_docs, \"question\": question}, return_only_outputs=True)\n", 132 | "\n", 133 | " async def _arun(self, url: str, question: str) -> str:\n", 134 | " raise NotImplementedError\n", 135 | "\n", 136 | "llm = ChatOpenAI(temperature=1.0, openai_api_key=\"your-openai-key\")\n", 137 | "query_website_tool = WebpageQATool(qa_chain=load_qa_with_sources_chain(llm))\n", 138 | "print(query_website_tool.run(\"What are the best AI image generators ?\"))" 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "source": [], 144 | "metadata": { 145 | "id": "IBCjAOk1NJuN" 146 | }, 147 | "execution_count": null, 148 | "outputs": [] 149 | } 150 | ] 151 | } --------------------------------------------------------------------------------