├── README.md └── RestaurantChatbot_Gemma.ipynb /README.md: -------------------------------------------------------------------------------- 1 | # Restaurant menu ordering Chatbot using Gemma , Langchain and Chroma DB 2 | 3 | Interested in developing a customized chatbot tailored to your business. This article offers a solution to address exactly those challenges! A restaurant chatbot serves various purposes, enhancing customer experience and streamlining operations. Firstly, it facilitates reservations, allowing users to book tables conveniently. Secondly, it provides menu information, catering to dietary preferences and enabling informed ordering. Additionally, it offers assistance with frequently asked questions about operating hours, location, and special offers, reducing the burden on staff. Moreover, it can handle feedback and complaints promptly, improving customer satisfaction and retention. Overall, integrating a restaurant chatbot optimizes service delivery, increases efficiency, and enhances overall customer engagement. 4 | 5 | 6 | We would be leveraging the power of LLMs. Google recently launched Gemma and we going to use it. Gemma, a series of lightweight and advanced open models developed by Google, shares the research and technology foundation with the Gemini models. These models, which are decoder-only large language models designed for text-to-text tasks, come in English variants with open weights, pre-trained versions, and instruction-tuned adaptations. Gemma models are adept at various text generation tasks like question answering, summarization, and reasoning. Their compact size enables deployment in resource-constrained environments such as laptops, desktops, or personal cloud infrastructure, thus democratizing access to cutting-edge AI models and promoting innovation for all. 7 | 8 | 9 | Achieving the capability for a machine to fully comprehend the various ways humans might inquire about something and respond in natural language akin to a human represents, in my view, a comprehensive goal within the realm of Natural Language Processing (NLP). 10 | 11 | ![Gemma](https://github.com/ushareng/Restaurant-Menu-ordering-chatbot-using-Gemma-Langchain-and-Chroma-DB/assets/34335028/c63bfd00-9694-4410-881c-dbb92a88c98d) 12 | 13 | Demo at HuggingFace spaces 14 | https://huggingface.co/spaces/tensorgirl/Restaurant-Chatbot 15 | 16 | ![2365c6f3](https://github.com/ushareng/Restaurant-Menu-ordering-chatbot-using-Gemma-Langchain-and-Chroma-DB/assets/34335028/c9292e3a-4bec-4206-a68f-be9215911588) 17 | -------------------------------------------------------------------------------- /RestaurantChatbot_Gemma.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "gpuType": "T4" 8 | }, 9 | "kernelspec": { 10 | "name": "python3", 11 | "display_name": "Python 3" 12 | }, 13 | "language_info": { 14 | "name": "python" 15 | }, 16 | "accelerator": "GPU" 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "source": [ 22 | "Interested in developing a customized chatbot tailored to your business. This article offers a solution to address exactly those challenges! A restaurant chatbot serves various purposes, enhancing customer experience and streamlining operations. Firstly, it facilitates reservations, allowing users to book tables conveniently. Secondly, it provides menu information, catering to dietary preferences and enabling informed ordering. Additionally, it offers assistance with frequently asked questions about operating hours, location, and special offers, reducing the burden on staff. Moreover, it can handle feedback and complaints promptly, improving customer satisfaction and retention. Overall, integrating a restaurant chatbot optimizes service delivery, increases efficiency, and enhances overall customer engagement.\n", 23 | "\n", 24 | "We would be leveraging the power of LLMs. Google recently launched Gemma and we going to use it. Gemma, a series of lightweight and advanced open models developed by Google, shares the research and technology foundation with the Gemini models. These models, which are decoder-only large language models designed for text-to-text tasks, come in English variants with open weights, pre-trained versions, and instruction-tuned adaptations. Gemma models are adept at various text generation tasks like question answering, summarization, and reasoning. Their compact size enables deployment in resource-constrained environments such as laptops, desktops, or personal cloud infrastructure, thus democratizing access to cutting-edge AI models and promoting innovation for all.\n", 25 | "\n", 26 | "\n", 27 | "Achieving the capability for a machine to fully comprehend the various ways humans might inquire about something and respond in natural language akin to a human represents, in my view, a comprehensive goal within the realm of Natural Language Processing (NLP)." 28 | ], 29 | "metadata": { 30 | "id": "7tofznQOzGjY" 31 | } 32 | }, 33 | { 34 | "cell_type": "markdown", 35 | "source": [ 36 | "We will use langchain as Orchestration tools and chromadb as the vector database" 37 | ], 38 | "metadata": { 39 | "id": "PZWxN-85zmYT" 40 | } 41 | }, 42 | { 43 | "cell_type": "markdown", 44 | "source": [ 45 | "Langchain is a versatile natural language processing (NLP) framework designed for building conversational AI systems. It offers a range of tools and components to facilitate the development of language-based applications. With its modular architecture, Langchain enables developers to construct customized pipelines for tasks such as text generation, sentiment analysis, and named entity recognition. Its flexibility allows integration with various machine learning models and libraries, providing scalability and adaptability to diverse projects. Langchain aims to simplify NLP development by providing pre-built modules and a user-friendly interface, empowering developers to create sophisticated language-based applications efficiently." 46 | ], 47 | "metadata": { 48 | "id": "pzeplfEve5NH" 49 | } 50 | }, 51 | { 52 | "cell_type": "markdown", 53 | "source": [ 54 | "ChromaDB for LLM usage refers to integrating ChromaDB, a versatile database management system, with Large Language Models (LLMs) for various applications beyond genomics. This integration enables users to leverage LLMs for tasks such as natural language understanding, text generation, and information retrieval using data stored in ChromaDB. By combining ChromaDB's efficient data storage and retrieval capabilities with the language processing power of LLMs, users can develop sophisticated AI applications for diverse domains, including customer service, content recommendation, and data analysis. This integration streamlines the development process and enhances the functionality of AI systems by providing access to structured data stored in ChromaDB for training and inference tasks performed by LLMs. Overall, ChromaDB's integration with LLMs offers a versatile solution for building intelligent applications across various industries and use cases." 55 | ], 56 | "metadata": { 57 | "id": "dt0T5FlmhrYn" 58 | } 59 | }, 60 | { 61 | "cell_type": "markdown", 62 | "source": [ 63 | "Let's start with installing the necessary libraries." 64 | ], 65 | "metadata": { 66 | "id": "3bCy-g0ghpvO" 67 | } 68 | }, 69 | { 70 | "cell_type": "code", 71 | "source": [ 72 | "!pip install langchain\n", 73 | "!pip install chromadb" 74 | ], 75 | "metadata": { 76 | "colab": { 77 | "base_uri": "https://localhost:8080/" 78 | }, 79 | "id": "CAHPUTiT_6Yo", 80 | "outputId": "757701d3-3844-42b1-b786-e111920b5f44" 81 | }, 82 | "execution_count": null, 83 | "outputs": [ 84 | { 85 | "output_type": "stream", 86 | "name": "stdout", 87 | "text": [ 88 | "Collecting langchain\n", 89 | " Downloading langchain-0.1.9-py3-none-any.whl (816 kB)\n", 90 | "\u001b[?25l \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/817.0 kB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K \u001b[91m━━━━━━━━━━━━\u001b[0m\u001b[90m╺\u001b[0m\u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m245.8/817.0 kB\u001b[0m \u001b[31m7.3 MB/s\u001b[0m eta \u001b[36m0:00:01\u001b[0m\r\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m817.0/817.0 kB\u001b[0m \u001b[31m14.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 91 | "\u001b[?25hRequirement already satisfied: PyYAML>=5.3 in /usr/local/lib/python3.10/dist-packages (from langchain) (6.0.1)\n", 92 | "Requirement already satisfied: SQLAlchemy<3,>=1.4 in /usr/local/lib/python3.10/dist-packages (from langchain) (2.0.27)\n", 93 | "Requirement already satisfied: aiohttp<4.0.0,>=3.8.3 in /usr/local/lib/python3.10/dist-packages (from langchain) (3.9.3)\n", 94 | "Requirement already satisfied: async-timeout<5.0.0,>=4.0.0 in /usr/local/lib/python3.10/dist-packages (from langchain) (4.0.3)\n", 95 | "Collecting dataclasses-json<0.7,>=0.5.7 (from langchain)\n", 96 | " Downloading dataclasses_json-0.6.4-py3-none-any.whl (28 kB)\n", 97 | "Collecting jsonpatch<2.0,>=1.33 (from langchain)\n", 98 | " Downloading jsonpatch-1.33-py2.py3-none-any.whl (12 kB)\n", 99 | "Collecting langchain-community<0.1,>=0.0.21 (from langchain)\n", 100 | " Downloading langchain_community-0.0.24-py3-none-any.whl (1.7 MB)\n", 101 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.7/1.7 MB\u001b[0m \u001b[31m42.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 102 | "\u001b[?25hCollecting langchain-core<0.2,>=0.1.26 (from langchain)\n", 103 | " Downloading langchain_core-0.1.26-py3-none-any.whl (246 kB)\n", 104 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m246.4/246.4 kB\u001b[0m \u001b[31m26.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 105 | "\u001b[?25hCollecting langsmith<0.2.0,>=0.1.0 (from langchain)\n", 106 | " Downloading langsmith-0.1.7-py3-none-any.whl (62 kB)\n", 107 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m62.1/62.1 kB\u001b[0m \u001b[31m7.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 108 | "\u001b[?25hRequirement already satisfied: numpy<2,>=1 in /usr/local/lib/python3.10/dist-packages (from langchain) (1.25.2)\n", 109 | "Requirement already satisfied: pydantic<3,>=1 in /usr/local/lib/python3.10/dist-packages (from langchain) (2.6.1)\n", 110 | "Requirement already satisfied: requests<3,>=2 in /usr/local/lib/python3.10/dist-packages (from langchain) (2.31.0)\n", 111 | "Requirement already satisfied: tenacity<9.0.0,>=8.1.0 in /usr/local/lib/python3.10/dist-packages (from langchain) (8.2.3)\n", 112 | "Requirement already satisfied: aiosignal>=1.1.2 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.3.1)\n", 113 | "Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (23.2.0)\n", 114 | "Requirement already satisfied: frozenlist>=1.1.1 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.4.1)\n", 115 | "Requirement already satisfied: multidict<7.0,>=4.5 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (6.0.5)\n", 116 | "Requirement already satisfied: yarl<2.0,>=1.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.9.4)\n", 117 | "Collecting marshmallow<4.0.0,>=3.18.0 (from dataclasses-json<0.7,>=0.5.7->langchain)\n", 118 | " Downloading marshmallow-3.20.2-py3-none-any.whl (49 kB)\n", 119 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m49.4/49.4 kB\u001b[0m \u001b[31m6.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 120 | "\u001b[?25hCollecting typing-inspect<1,>=0.4.0 (from dataclasses-json<0.7,>=0.5.7->langchain)\n", 121 | " Downloading typing_inspect-0.9.0-py3-none-any.whl (8.8 kB)\n", 122 | "Collecting jsonpointer>=1.9 (from jsonpatch<2.0,>=1.33->langchain)\n", 123 | " Downloading jsonpointer-2.4-py2.py3-none-any.whl (7.8 kB)\n", 124 | "Requirement already satisfied: anyio<5,>=3 in /usr/local/lib/python3.10/dist-packages (from langchain-core<0.2,>=0.1.26->langchain) (3.7.1)\n", 125 | "Requirement already satisfied: packaging<24.0,>=23.2 in /usr/local/lib/python3.10/dist-packages (from langchain-core<0.2,>=0.1.26->langchain) (23.2)\n", 126 | "Collecting orjson<4.0.0,>=3.9.14 (from langsmith<0.2.0,>=0.1.0->langchain)\n", 127 | " Downloading orjson-3.9.15-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (138 kB)\n", 128 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m138.5/138.5 kB\u001b[0m \u001b[31m13.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 129 | "\u001b[?25hRequirement already satisfied: annotated-types>=0.4.0 in /usr/local/lib/python3.10/dist-packages (from pydantic<3,>=1->langchain) (0.6.0)\n", 130 | "Requirement already satisfied: pydantic-core==2.16.2 in /usr/local/lib/python3.10/dist-packages (from pydantic<3,>=1->langchain) (2.16.2)\n", 131 | "Requirement already satisfied: typing-extensions>=4.6.1 in /usr/local/lib/python3.10/dist-packages (from pydantic<3,>=1->langchain) (4.9.0)\n", 132 | "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2->langchain) (3.3.2)\n", 133 | "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2->langchain) (3.6)\n", 134 | "Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2->langchain) (2.0.7)\n", 135 | "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2->langchain) (2024.2.2)\n", 136 | "Requirement already satisfied: greenlet!=0.4.17 in /usr/local/lib/python3.10/dist-packages (from SQLAlchemy<3,>=1.4->langchain) (3.0.3)\n", 137 | "Requirement already satisfied: sniffio>=1.1 in /usr/local/lib/python3.10/dist-packages (from anyio<5,>=3->langchain-core<0.2,>=0.1.26->langchain) (1.3.0)\n", 138 | "Requirement already satisfied: exceptiongroup in /usr/local/lib/python3.10/dist-packages (from anyio<5,>=3->langchain-core<0.2,>=0.1.26->langchain) (1.2.0)\n", 139 | "Collecting mypy-extensions>=0.3.0 (from typing-inspect<1,>=0.4.0->dataclasses-json<0.7,>=0.5.7->langchain)\n", 140 | " Downloading mypy_extensions-1.0.0-py3-none-any.whl (4.7 kB)\n", 141 | "Installing collected packages: orjson, mypy-extensions, marshmallow, jsonpointer, typing-inspect, jsonpatch, langsmith, dataclasses-json, langchain-core, langchain-community, langchain\n", 142 | "Successfully installed dataclasses-json-0.6.4 jsonpatch-1.33 jsonpointer-2.4 langchain-0.1.9 langchain-community-0.0.24 langchain-core-0.1.26 langsmith-0.1.7 marshmallow-3.20.2 mypy-extensions-1.0.0 orjson-3.9.15 typing-inspect-0.9.0\n", 143 | "Collecting chromadb\n", 144 | " Downloading chromadb-0.4.23-py3-none-any.whl (521 kB)\n", 145 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m521.7/521.7 kB\u001b[0m \u001b[31m10.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 146 | "\u001b[?25hRequirement already satisfied: build>=1.0.3 in /usr/local/lib/python3.10/dist-packages (from chromadb) (1.0.3)\n", 147 | "Requirement already satisfied: requests>=2.28 in /usr/local/lib/python3.10/dist-packages (from chromadb) (2.31.0)\n", 148 | "Requirement already satisfied: pydantic>=1.9 in /usr/local/lib/python3.10/dist-packages (from chromadb) (2.6.1)\n", 149 | "Collecting chroma-hnswlib==0.7.3 (from chromadb)\n", 150 | " Downloading chroma_hnswlib-0.7.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.4 MB)\n", 151 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.4/2.4 MB\u001b[0m \u001b[31m27.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 152 | "\u001b[?25hCollecting fastapi>=0.95.2 (from chromadb)\n", 153 | " Downloading fastapi-0.109.2-py3-none-any.whl (92 kB)\n", 154 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m92.1/92.1 kB\u001b[0m \u001b[31m12.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 155 | "\u001b[?25hCollecting uvicorn[standard]>=0.18.3 (from chromadb)\n", 156 | " Downloading uvicorn-0.27.1-py3-none-any.whl (60 kB)\n", 157 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m60.8/60.8 kB\u001b[0m \u001b[31m5.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 158 | "\u001b[?25hRequirement already satisfied: numpy>=1.22.5 in /usr/local/lib/python3.10/dist-packages (from chromadb) (1.25.2)\n", 159 | "Collecting posthog>=2.4.0 (from chromadb)\n", 160 | " Downloading posthog-3.4.2-py2.py3-none-any.whl (41 kB)\n", 161 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m41.2/41.2 kB\u001b[0m \u001b[31m5.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 162 | "\u001b[?25hRequirement already satisfied: typing-extensions>=4.5.0 in /usr/local/lib/python3.10/dist-packages (from chromadb) (4.9.0)\n", 163 | "Collecting pulsar-client>=3.1.0 (from chromadb)\n", 164 | " Downloading pulsar_client-3.4.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.4 MB)\n", 165 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m5.4/5.4 MB\u001b[0m \u001b[31m47.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 166 | "\u001b[?25hCollecting onnxruntime>=1.14.1 (from chromadb)\n", 167 | " Downloading onnxruntime-1.17.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (6.8 MB)\n", 168 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m6.8/6.8 MB\u001b[0m \u001b[31m52.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 169 | "\u001b[?25hCollecting opentelemetry-api>=1.2.0 (from chromadb)\n", 170 | " Downloading opentelemetry_api-1.23.0-py3-none-any.whl (58 kB)\n", 171 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m58.4/58.4 kB\u001b[0m \u001b[31m5.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 172 | "\u001b[?25hCollecting opentelemetry-exporter-otlp-proto-grpc>=1.2.0 (from chromadb)\n", 173 | " Downloading opentelemetry_exporter_otlp_proto_grpc-1.23.0-py3-none-any.whl (18 kB)\n", 174 | "Collecting opentelemetry-instrumentation-fastapi>=0.41b0 (from chromadb)\n", 175 | " Downloading opentelemetry_instrumentation_fastapi-0.44b0-py3-none-any.whl (11 kB)\n", 176 | "Collecting opentelemetry-sdk>=1.2.0 (from chromadb)\n", 177 | " Downloading opentelemetry_sdk-1.23.0-py3-none-any.whl (105 kB)\n", 178 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m105.7/105.7 kB\u001b[0m \u001b[31m12.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 179 | "\u001b[?25hRequirement already satisfied: tokenizers>=0.13.2 in /usr/local/lib/python3.10/dist-packages (from chromadb) (0.15.2)\n", 180 | "Collecting pypika>=0.48.9 (from chromadb)\n", 181 | " Downloading PyPika-0.48.9.tar.gz (67 kB)\n", 182 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m67.3/67.3 kB\u001b[0m \u001b[31m8.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 183 | "\u001b[?25h Installing build dependencies ... \u001b[?25l\u001b[?25hdone\n", 184 | " Getting requirements to build wheel ... \u001b[?25l\u001b[?25hdone\n", 185 | " Preparing metadata (pyproject.toml) ... \u001b[?25l\u001b[?25hdone\n", 186 | "Requirement already satisfied: tqdm>=4.65.0 in /usr/local/lib/python3.10/dist-packages (from chromadb) (4.66.2)\n", 187 | "Collecting overrides>=7.3.1 (from chromadb)\n", 188 | " Downloading overrides-7.7.0-py3-none-any.whl (17 kB)\n", 189 | "Requirement already satisfied: importlib-resources in /usr/local/lib/python3.10/dist-packages (from chromadb) (6.1.1)\n", 190 | "Requirement already satisfied: grpcio>=1.58.0 in /usr/local/lib/python3.10/dist-packages (from chromadb) (1.60.1)\n", 191 | "Collecting bcrypt>=4.0.1 (from chromadb)\n", 192 | " Downloading bcrypt-4.1.2-cp39-abi3-manylinux_2_28_x86_64.whl (698 kB)\n", 193 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m698.9/698.9 kB\u001b[0m \u001b[31m54.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 194 | "\u001b[?25hRequirement already satisfied: typer>=0.9.0 in /usr/local/lib/python3.10/dist-packages (from chromadb) (0.9.0)\n", 195 | "Collecting kubernetes>=28.1.0 (from chromadb)\n", 196 | " Downloading kubernetes-29.0.0-py2.py3-none-any.whl (1.6 MB)\n", 197 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.6/1.6 MB\u001b[0m \u001b[31m59.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 198 | "\u001b[?25hRequirement already satisfied: tenacity>=8.2.3 in /usr/local/lib/python3.10/dist-packages (from chromadb) (8.2.3)\n", 199 | "Requirement already satisfied: PyYAML>=6.0.0 in /usr/local/lib/python3.10/dist-packages (from chromadb) (6.0.1)\n", 200 | "Collecting mmh3>=4.0.1 (from chromadb)\n", 201 | " Downloading mmh3-4.1.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (67 kB)\n", 202 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m67.6/67.6 kB\u001b[0m \u001b[31m8.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 203 | "\u001b[?25hRequirement already satisfied: orjson>=3.9.12 in /usr/local/lib/python3.10/dist-packages (from chromadb) (3.9.15)\n", 204 | "Requirement already satisfied: packaging>=19.0 in /usr/local/lib/python3.10/dist-packages (from build>=1.0.3->chromadb) (23.2)\n", 205 | "Requirement already satisfied: pyproject_hooks in /usr/local/lib/python3.10/dist-packages (from build>=1.0.3->chromadb) (1.0.0)\n", 206 | "Requirement already satisfied: tomli>=1.1.0 in /usr/local/lib/python3.10/dist-packages (from build>=1.0.3->chromadb) (2.0.1)\n", 207 | "Collecting starlette<0.37.0,>=0.36.3 (from fastapi>=0.95.2->chromadb)\n", 208 | " Downloading starlette-0.36.3-py3-none-any.whl (71 kB)\n", 209 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m71.5/71.5 kB\u001b[0m \u001b[31m8.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 210 | "\u001b[?25hRequirement already satisfied: certifi>=14.05.14 in /usr/local/lib/python3.10/dist-packages (from kubernetes>=28.1.0->chromadb) (2024.2.2)\n", 211 | "Requirement already satisfied: six>=1.9.0 in /usr/local/lib/python3.10/dist-packages (from kubernetes>=28.1.0->chromadb) (1.16.0)\n", 212 | "Requirement already satisfied: python-dateutil>=2.5.3 in /usr/local/lib/python3.10/dist-packages (from kubernetes>=28.1.0->chromadb) (2.8.2)\n", 213 | "Requirement already satisfied: google-auth>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from kubernetes>=28.1.0->chromadb) (2.27.0)\n", 214 | "Requirement already satisfied: websocket-client!=0.40.0,!=0.41.*,!=0.42.*,>=0.32.0 in /usr/local/lib/python3.10/dist-packages (from kubernetes>=28.1.0->chromadb) (1.7.0)\n", 215 | "Requirement already satisfied: requests-oauthlib in /usr/local/lib/python3.10/dist-packages (from kubernetes>=28.1.0->chromadb) (1.3.1)\n", 216 | "Requirement already satisfied: oauthlib>=3.2.2 in /usr/local/lib/python3.10/dist-packages (from kubernetes>=28.1.0->chromadb) (3.2.2)\n", 217 | "Requirement already satisfied: urllib3>=1.24.2 in /usr/local/lib/python3.10/dist-packages (from kubernetes>=28.1.0->chromadb) (2.0.7)\n", 218 | "Collecting coloredlogs (from onnxruntime>=1.14.1->chromadb)\n", 219 | " Downloading coloredlogs-15.0.1-py2.py3-none-any.whl (46 kB)\n", 220 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m46.0/46.0 kB\u001b[0m \u001b[31m5.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 221 | "\u001b[?25hRequirement already satisfied: flatbuffers in /usr/local/lib/python3.10/dist-packages (from onnxruntime>=1.14.1->chromadb) (23.5.26)\n", 222 | "Requirement already satisfied: protobuf in /usr/local/lib/python3.10/dist-packages (from onnxruntime>=1.14.1->chromadb) (3.20.3)\n", 223 | "Requirement already satisfied: sympy in /usr/local/lib/python3.10/dist-packages (from onnxruntime>=1.14.1->chromadb) (1.12)\n", 224 | "Collecting deprecated>=1.2.6 (from opentelemetry-api>=1.2.0->chromadb)\n", 225 | " Downloading Deprecated-1.2.14-py2.py3-none-any.whl (9.6 kB)\n", 226 | "Collecting importlib-metadata<7.0,>=6.0 (from opentelemetry-api>=1.2.0->chromadb)\n", 227 | " Downloading importlib_metadata-6.11.0-py3-none-any.whl (23 kB)\n", 228 | "Requirement already satisfied: googleapis-common-protos~=1.52 in /usr/local/lib/python3.10/dist-packages (from opentelemetry-exporter-otlp-proto-grpc>=1.2.0->chromadb) (1.62.0)\n", 229 | "Collecting opentelemetry-exporter-otlp-proto-common==1.23.0 (from opentelemetry-exporter-otlp-proto-grpc>=1.2.0->chromadb)\n", 230 | " Downloading opentelemetry_exporter_otlp_proto_common-1.23.0-py3-none-any.whl (17 kB)\n", 231 | "Collecting opentelemetry-proto==1.23.0 (from opentelemetry-exporter-otlp-proto-grpc>=1.2.0->chromadb)\n", 232 | " Downloading opentelemetry_proto-1.23.0-py3-none-any.whl (50 kB)\n", 233 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m50.8/50.8 kB\u001b[0m \u001b[31m5.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 234 | "\u001b[?25hCollecting opentelemetry-instrumentation-asgi==0.44b0 (from opentelemetry-instrumentation-fastapi>=0.41b0->chromadb)\n", 235 | " Downloading opentelemetry_instrumentation_asgi-0.44b0-py3-none-any.whl (14 kB)\n", 236 | "Collecting opentelemetry-instrumentation==0.44b0 (from opentelemetry-instrumentation-fastapi>=0.41b0->chromadb)\n", 237 | " Downloading opentelemetry_instrumentation-0.44b0-py3-none-any.whl (28 kB)\n", 238 | "Collecting opentelemetry-semantic-conventions==0.44b0 (from opentelemetry-instrumentation-fastapi>=0.41b0->chromadb)\n", 239 | " Downloading opentelemetry_semantic_conventions-0.44b0-py3-none-any.whl (36 kB)\n", 240 | "Collecting opentelemetry-util-http==0.44b0 (from opentelemetry-instrumentation-fastapi>=0.41b0->chromadb)\n", 241 | " Downloading opentelemetry_util_http-0.44b0-py3-none-any.whl (6.9 kB)\n", 242 | "Requirement already satisfied: setuptools>=16.0 in /usr/local/lib/python3.10/dist-packages (from opentelemetry-instrumentation==0.44b0->opentelemetry-instrumentation-fastapi>=0.41b0->chromadb) (67.7.2)\n", 243 | "Requirement already satisfied: wrapt<2.0.0,>=1.0.0 in /usr/local/lib/python3.10/dist-packages (from opentelemetry-instrumentation==0.44b0->opentelemetry-instrumentation-fastapi>=0.41b0->chromadb) (1.14.1)\n", 244 | "Collecting asgiref~=3.0 (from opentelemetry-instrumentation-asgi==0.44b0->opentelemetry-instrumentation-fastapi>=0.41b0->chromadb)\n", 245 | " Downloading asgiref-3.7.2-py3-none-any.whl (24 kB)\n", 246 | "Collecting monotonic>=1.5 (from posthog>=2.4.0->chromadb)\n", 247 | " Downloading monotonic-1.6-py2.py3-none-any.whl (8.2 kB)\n", 248 | "Collecting backoff>=1.10.0 (from posthog>=2.4.0->chromadb)\n", 249 | " Downloading backoff-2.2.1-py3-none-any.whl (15 kB)\n", 250 | "Requirement already satisfied: annotated-types>=0.4.0 in /usr/local/lib/python3.10/dist-packages (from pydantic>=1.9->chromadb) (0.6.0)\n", 251 | "Requirement already satisfied: pydantic-core==2.16.2 in /usr/local/lib/python3.10/dist-packages (from pydantic>=1.9->chromadb) (2.16.2)\n", 252 | "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests>=2.28->chromadb) (3.3.2)\n", 253 | "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests>=2.28->chromadb) (3.6)\n", 254 | "Requirement already satisfied: huggingface_hub<1.0,>=0.16.4 in /usr/local/lib/python3.10/dist-packages (from tokenizers>=0.13.2->chromadb) (0.20.3)\n", 255 | "Requirement already satisfied: click<9.0.0,>=7.1.1 in /usr/local/lib/python3.10/dist-packages (from typer>=0.9.0->chromadb) (8.1.7)\n", 256 | "Collecting h11>=0.8 (from uvicorn[standard]>=0.18.3->chromadb)\n", 257 | " Downloading h11-0.14.0-py3-none-any.whl (58 kB)\n", 258 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m58.3/58.3 kB\u001b[0m \u001b[31m6.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 259 | "\u001b[?25hCollecting httptools>=0.5.0 (from uvicorn[standard]>=0.18.3->chromadb)\n", 260 | " Downloading httptools-0.6.1-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (341 kB)\n", 261 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m341.4/341.4 kB\u001b[0m \u001b[31m24.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 262 | "\u001b[?25hCollecting python-dotenv>=0.13 (from uvicorn[standard]>=0.18.3->chromadb)\n", 263 | " Downloading python_dotenv-1.0.1-py3-none-any.whl (19 kB)\n", 264 | "Collecting uvloop!=0.15.0,!=0.15.1,>=0.14.0 (from uvicorn[standard]>=0.18.3->chromadb)\n", 265 | " Downloading uvloop-0.19.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.4 MB)\n", 266 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m3.4/3.4 MB\u001b[0m \u001b[31m57.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 267 | "\u001b[?25hCollecting watchfiles>=0.13 (from uvicorn[standard]>=0.18.3->chromadb)\n", 268 | " Downloading watchfiles-0.21.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.3 MB)\n", 269 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.3/1.3 MB\u001b[0m \u001b[31m54.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 270 | "\u001b[?25hCollecting websockets>=10.4 (from uvicorn[standard]>=0.18.3->chromadb)\n", 271 | " Downloading websockets-12.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (130 kB)\n", 272 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m130.2/130.2 kB\u001b[0m \u001b[31m11.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 273 | "\u001b[?25hRequirement already satisfied: cachetools<6.0,>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from google-auth>=1.0.1->kubernetes>=28.1.0->chromadb) (5.3.2)\n", 274 | "Requirement already satisfied: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.10/dist-packages (from google-auth>=1.0.1->kubernetes>=28.1.0->chromadb) (0.3.0)\n", 275 | "Requirement already satisfied: rsa<5,>=3.1.4 in /usr/local/lib/python3.10/dist-packages (from google-auth>=1.0.1->kubernetes>=28.1.0->chromadb) (4.9)\n", 276 | "Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from huggingface_hub<1.0,>=0.16.4->tokenizers>=0.13.2->chromadb) (3.13.1)\n", 277 | "Requirement already satisfied: fsspec>=2023.5.0 in /usr/local/lib/python3.10/dist-packages (from huggingface_hub<1.0,>=0.16.4->tokenizers>=0.13.2->chromadb) (2023.6.0)\n", 278 | "Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.10/dist-packages (from importlib-metadata<7.0,>=6.0->opentelemetry-api>=1.2.0->chromadb) (3.17.0)\n", 279 | "Requirement already satisfied: anyio<5,>=3.4.0 in /usr/local/lib/python3.10/dist-packages (from starlette<0.37.0,>=0.36.3->fastapi>=0.95.2->chromadb) (3.7.1)\n", 280 | "Collecting humanfriendly>=9.1 (from coloredlogs->onnxruntime>=1.14.1->chromadb)\n", 281 | " Downloading humanfriendly-10.0-py2.py3-none-any.whl (86 kB)\n", 282 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m86.8/86.8 kB\u001b[0m \u001b[31m7.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 283 | "\u001b[?25hRequirement already satisfied: mpmath>=0.19 in /usr/local/lib/python3.10/dist-packages (from sympy->onnxruntime>=1.14.1->chromadb) (1.3.0)\n", 284 | "Requirement already satisfied: sniffio>=1.1 in /usr/local/lib/python3.10/dist-packages (from anyio<5,>=3.4.0->starlette<0.37.0,>=0.36.3->fastapi>=0.95.2->chromadb) (1.3.0)\n", 285 | "Requirement already satisfied: exceptiongroup in /usr/local/lib/python3.10/dist-packages (from anyio<5,>=3.4.0->starlette<0.37.0,>=0.36.3->fastapi>=0.95.2->chromadb) (1.2.0)\n", 286 | "Requirement already satisfied: pyasn1<0.6.0,>=0.4.6 in /usr/local/lib/python3.10/dist-packages (from pyasn1-modules>=0.2.1->google-auth>=1.0.1->kubernetes>=28.1.0->chromadb) (0.5.1)\n", 287 | "Building wheels for collected packages: pypika\n", 288 | " Building wheel for pypika (pyproject.toml) ... \u001b[?25l\u001b[?25hdone\n", 289 | " Created wheel for pypika: filename=PyPika-0.48.9-py2.py3-none-any.whl size=53723 sha256=777b9f33f7eedebb04a2b1e673f09c8c5f6ad2a8edddcc28ea1df5e2ad3927d7\n", 290 | " Stored in directory: /root/.cache/pip/wheels/e1/26/51/d0bffb3d2fd82256676d7ad3003faea3bd6dddc9577af665f4\n", 291 | "Successfully built pypika\n", 292 | "Installing collected packages: pypika, monotonic, mmh3, websockets, uvloop, python-dotenv, pulsar-client, overrides, opentelemetry-util-http, opentelemetry-semantic-conventions, opentelemetry-proto, importlib-metadata, humanfriendly, httptools, h11, deprecated, chroma-hnswlib, bcrypt, backoff, asgiref, watchfiles, uvicorn, starlette, posthog, opentelemetry-exporter-otlp-proto-common, opentelemetry-api, coloredlogs, opentelemetry-sdk, opentelemetry-instrumentation, onnxruntime, kubernetes, fastapi, opentelemetry-instrumentation-asgi, opentelemetry-exporter-otlp-proto-grpc, opentelemetry-instrumentation-fastapi, chromadb\n", 293 | " Attempting uninstall: importlib-metadata\n", 294 | " Found existing installation: importlib-metadata 7.0.1\n", 295 | " Uninstalling importlib-metadata-7.0.1:\n", 296 | " Successfully uninstalled importlib-metadata-7.0.1\n", 297 | "Successfully installed asgiref-3.7.2 backoff-2.2.1 bcrypt-4.1.2 chroma-hnswlib-0.7.3 chromadb-0.4.23 coloredlogs-15.0.1 deprecated-1.2.14 fastapi-0.109.2 h11-0.14.0 httptools-0.6.1 humanfriendly-10.0 importlib-metadata-6.11.0 kubernetes-29.0.0 mmh3-4.1.0 monotonic-1.6 onnxruntime-1.17.0 opentelemetry-api-1.23.0 opentelemetry-exporter-otlp-proto-common-1.23.0 opentelemetry-exporter-otlp-proto-grpc-1.23.0 opentelemetry-instrumentation-0.44b0 opentelemetry-instrumentation-asgi-0.44b0 opentelemetry-instrumentation-fastapi-0.44b0 opentelemetry-proto-1.23.0 opentelemetry-sdk-1.23.0 opentelemetry-semantic-conventions-0.44b0 opentelemetry-util-http-0.44b0 overrides-7.7.0 posthog-3.4.2 pulsar-client-3.4.0 pypika-0.48.9 python-dotenv-1.0.1 starlette-0.36.3 uvicorn-0.27.1 uvloop-0.19.0 watchfiles-0.21.0 websockets-12.0\n" 298 | ] 299 | } 300 | ] 301 | }, 302 | { 303 | "cell_type": "markdown", 304 | "source": [ 305 | "The data we are going to use is a menu of a restaurant which is in JSON format. Thus, we will first load the json file and extract the individual dishes and store them in .txt file" 306 | ], 307 | "metadata": { 308 | "id": "hiYVDeFN0qQJ" 309 | } 310 | }, 311 | { 312 | "cell_type": "code", 313 | "source": [ 314 | "import json\n", 315 | "\n", 316 | "json_file = \"menu.json\"\n", 317 | "with open(json_file, \"r\") as f:\n", 318 | " json_data = json.load(f)" 319 | ], 320 | "metadata": { 321 | "id": "pc9Md6RXC0EZ" 322 | }, 323 | "execution_count": null, 324 | "outputs": [] 325 | }, 326 | { 327 | "cell_type": "code", 328 | "source": [ 329 | "import os\n", 330 | "count = 0\n", 331 | "folder_path = \"/content/Data\"\n", 332 | "\n", 333 | "for dish in json_data:\n", 334 | " file_path = os.path.join(folder_path,\"{}.txt\".format(count))\n", 335 | " f = open(file_path, \"w\")\n", 336 | " for key, value in dish.items():\n", 337 | " f.write(f\"{key}: {value}\\n\")\n", 338 | " f.close()\n", 339 | " count+=1" 340 | ], 341 | "metadata": { 342 | "id": "5BjYan9GFDin" 343 | }, 344 | "execution_count": null, 345 | "outputs": [] 346 | }, 347 | { 348 | "cell_type": "markdown", 349 | "source": [ 350 | "Once the .txt files are created, the next task is to load the data using langchain Loaders. All the files are loaded and stored in the *docs*" 351 | ], 352 | "metadata": { 353 | "id": "-i7kxLoY1RbE" 354 | } 355 | }, 356 | { 357 | "cell_type": "code", 358 | "source": [ 359 | "from langchain_community.document_loaders import TextLoader\n", 360 | "\n", 361 | "loaders = []\n", 362 | "\n", 363 | "for i in range(12):\n", 364 | " file_path = os.path.join(folder_path,\"{}.txt\".format(i))\n", 365 | " loaders.append(TextLoader(file_path))\n", 366 | "\n", 367 | "docs = []\n", 368 | "for loader in loaders:\n", 369 | " docs.extend(loader.load())" 370 | ], 371 | "metadata": { 372 | "id": "xXkvqImuYyfg" 373 | }, 374 | "execution_count": null, 375 | "outputs": [] 376 | }, 377 | { 378 | "cell_type": "markdown", 379 | "source": [ 380 | "Next task is to create the Vector Database. We would create the embeddings using the Hugging Face Inference API Embeddings function and store them in the Vector Database" 381 | ], 382 | "metadata": { 383 | "id": "IENmCSso3A3-" 384 | } 385 | }, 386 | { 387 | "cell_type": "code", 388 | "execution_count": null, 389 | "metadata": { 390 | "id": "wRy_puem-jDP" 391 | }, 392 | "outputs": [], 393 | "source": [ 394 | "from langchain.vectorstores import Chroma\n", 395 | "from langchain_community.embeddings import HuggingFaceInferenceAPIEmbeddings\n", 396 | "\n", 397 | "inference_api_key = \"WRITE_YOUR_API_KEY\"\n", 398 | "\n", 399 | "embeddings = HuggingFaceInferenceAPIEmbeddings(\n", 400 | " api_key=inference_api_key, model_name=\"sentence-transformers/all-mpnet-base-v2\"\n", 401 | ")\n", 402 | "\n", 403 | "vectordb = Chroma.from_documents(\n", 404 | " documents=docs,\n", 405 | " embedding=embeddings\n", 406 | ")" 407 | ] 408 | }, 409 | { 410 | "cell_type": "markdown", 411 | "source": [ 412 | "It's time now to invoke the llm. We would be using gemma-2b-it which was launched by google. We set temperature to 0.1 so that we get outputs that are more direct, less creative, and expected. We don't want our chatbot to give unnecessary info. It will use the top 5 results for a given query and then refine it according to the prompt" 413 | ], 414 | "metadata": { 415 | "id": "D_KvYVs345rw" 416 | } 417 | }, 418 | { 419 | "cell_type": "code", 420 | "source": [ 421 | "from langchain_community.llms import HuggingFaceHub\n", 422 | "\n", 423 | "llm = HuggingFaceHub(\n", 424 | " repo_id=\"google/gemma-2b-it\",\n", 425 | " task=\"text-generation\",\n", 426 | " model_kwargs={\n", 427 | " \"max_new_tokens\": 512,\n", 428 | " \"top_k\": 5,\n", 429 | " \"temperature\": 0.1,\n", 430 | " \"repetition_penalty\": 1.03,\n", 431 | " },\n", 432 | " huggingfacehub_api_token = \"WRITE_YOUR_API_KEY\"\n", 433 | ")" 434 | ], 435 | "metadata": { 436 | "colab": { 437 | "base_uri": "https://localhost:8080/" 438 | }, 439 | "id": "c2XWnrbNdemF", 440 | "outputId": "2b13c9f9-cd8b-4e30-b7df-abbf6f6b8667" 441 | }, 442 | "execution_count": null, 443 | "outputs": [ 444 | { 445 | "output_type": "stream", 446 | "name": "stderr", 447 | "text": [ 448 | "/usr/local/lib/python3.10/dist-packages/langchain_core/_api/deprecation.py:117: LangChainDeprecationWarning: The class `langchain_community.llms.huggingface_hub.HuggingFaceHub` was deprecated in langchain-community 0.0.21 and will be removed in 0.2.0. Use HuggingFaceEndpoint instead.\n", 449 | " warn_deprecated(\n" 450 | ] 451 | } 452 | ] 453 | }, 454 | { 455 | "cell_type": "markdown", 456 | "source": [ 457 | "In the prompt, we mention that it is for a restaurant and give some information about are dataset." 458 | ], 459 | "metadata": { 460 | "id": "g6iBzQ_Q6NaE" 461 | } 462 | }, 463 | { 464 | "cell_type": "code", 465 | "source": [ 466 | "from langchain.prompts import PromptTemplate\n", 467 | "\n", 468 | "template = \"\"\"You are a Chatbot at a Restaurant. Help the customer pick the right dish to order. The items in the context are dishes. The field below the item is the cost of the dish. About is the description of the dish. Use the context below to answe the questions\n", 469 | "{context}\n", 470 | "Question: {question}\n", 471 | "Helpful Answer:\"\"\"\n", 472 | "QA_CHAIN_PROMPT = PromptTemplate(input_variables=[\"context\", \"question\"],template=template,)\n" 473 | ], 474 | "metadata": { 475 | "id": "cMZYVZl7hFfV" 476 | }, 477 | "execution_count": null, 478 | "outputs": [] 479 | }, 480 | { 481 | "cell_type": "markdown", 482 | "source": [ 483 | "As this is a chatbot, we want the chatbot to remember the previous conversation. Thus, we initialize the Memory function." 484 | ], 485 | "metadata": { 486 | "id": "gXkLZ22i6jrI" 487 | } 488 | }, 489 | { 490 | "cell_type": "code", 491 | "source": [ 492 | "from langchain.memory import ConversationBufferMemory\n", 493 | "memory = ConversationBufferMemory(\n", 494 | " memory_key=\"chat_history\",\n", 495 | " return_messages=True\n", 496 | ")" 497 | ], 498 | "metadata": { 499 | "id": "HHTnmWVtiDXk" 500 | }, 501 | "execution_count": null, 502 | "outputs": [] 503 | }, 504 | { 505 | "cell_type": "markdown", 506 | "source": [ 507 | "We define the retriever to fetch the data from the database. We also initalise the Conversational RAG chain with parameters of llm, retriver and memory" 508 | ], 509 | "metadata": { 510 | "id": "0h8yrkj-AEk7" 511 | } 512 | }, 513 | { 514 | "cell_type": "code", 515 | "source": [ 516 | "from langchain.chains import ConversationalRetrievalChain\n", 517 | "\n", 518 | "retriever = vectordb.as_retriever()\n", 519 | "qa = ConversationalRetrievalChain.from_llm(\n", 520 | " llm,\n", 521 | " retriever=retriever,\n", 522 | " memory=memory,\n", 523 | ")" 524 | ], 525 | "metadata": { 526 | "id": "CR8IjHVFiqVf" 527 | }, 528 | "execution_count": null, 529 | "outputs": [] 530 | }, 531 | { 532 | "cell_type": "markdown", 533 | "source": [ 534 | "We imports necessary modules and defines a prompt for contextualizing user questions based on chat history. It utilizes langchain_core components to formulate standalone questions. Finally, it applies a language model (llm) and a string output parser to process the contextualized questions." 535 | ], 536 | "metadata": { 537 | "id": "YdLzZEybBRsC" 538 | } 539 | }, 540 | { 541 | "cell_type": "code", 542 | "source": [ 543 | "from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder\n", 544 | "from langchain_core.output_parsers import StrOutputParser\n", 545 | "from langchain_core.runnables import RunnablePassthrough\n", 546 | "\n", 547 | "contextualize_q_system_prompt = \"\"\"Given a chat history and the latest user question \\\n", 548 | "which might reference context in the chat history, formulate a standalone question \\\n", 549 | "which can be understood without the chat history. Do NOT answer the question, \\\n", 550 | "just reformulate it if needed and otherwise return it as is.\"\"\"\n", 551 | "contextualize_q_prompt = ChatPromptTemplate.from_messages(\n", 552 | " [\n", 553 | " (\"system\", contextualize_q_system_prompt),\n", 554 | " MessagesPlaceholder(variable_name=\"chat_history\"),\n", 555 | " (\"human\", \"{question}\"),\n", 556 | " ]\n", 557 | ")\n", 558 | "contextualize_q_chain = contextualize_q_prompt | llm | StrOutputParser()" 559 | ], 560 | "metadata": { 561 | "id": "8K3hsKlvm1oX" 562 | }, 563 | "execution_count": null, 564 | "outputs": [] 565 | }, 566 | { 567 | "cell_type": "markdown", 568 | "source": [ 569 | "This function `contextualized_question` checks if `chat_history` is present in the input dictionary. If it is, it returns the contextualize_q_chain, otherwise, it returns the value of the \"question\" key. The `rag_chain` incorporates this function along with a retriever and a language model to process a series of operations." 570 | ], 571 | "metadata": { 572 | "id": "2hYa70UdBiO9" 573 | } 574 | }, 575 | { 576 | "cell_type": "code", 577 | "source": [ 578 | "def contextualized_question(input: dict):\n", 579 | " if input.get(\"chat_history\"):\n", 580 | " return contextualize_q_chain\n", 581 | " else:\n", 582 | " return input[\"question\"]\n", 583 | "\n", 584 | "\n", 585 | "rag_chain = (\n", 586 | " RunnablePassthrough.assign(\n", 587 | " context=contextualized_question | retriever\n", 588 | " )\n", 589 | " | QA_CHAIN_PROMPT\n", 590 | " | llm\n", 591 | ")" 592 | ], 593 | "metadata": { 594 | "id": "ms3yrmUtmRGI" 595 | }, 596 | "execution_count": null, 597 | "outputs": [] 598 | }, 599 | { 600 | "cell_type": "markdown", 601 | "source": [ 602 | "We initializes a conversation loop where the user can input questions until they type \"exit.\" Each question is processed by the 'rag_chain' function, which combines various operations to generate an AI response. The AI response is printed, and the conversation history is updated with the user question and the AI response." 603 | ], 604 | "metadata": { 605 | "id": "I9lRPCKoBoie" 606 | } 607 | }, 608 | { 609 | "cell_type": "code", 610 | "source": [ 611 | "import wandb\n", 612 | "wandb.login(key='WRITE_YOUR_API_KEY')" 613 | ], 614 | "metadata": { 615 | "colab": { 616 | "base_uri": "https://localhost:8080/" 617 | }, 618 | "id": "ezyINzKUkbvy", 619 | "outputId": "5edde0ae-eac5-41d6-931e-b0c60d4b9c0e" 620 | }, 621 | "execution_count": null, 622 | "outputs": [ 623 | { 624 | "output_type": "stream", 625 | "name": "stdout", 626 | "text": [ 627 | "Collecting wandb\n", 628 | " Downloading wandb-0.16.3-py3-none-any.whl (2.2 MB)\n", 629 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.2/2.2 MB\u001b[0m \u001b[31m29.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 630 | "\u001b[?25hRequirement already satisfied: Click!=8.0.0,>=7.1 in /usr/local/lib/python3.10/dist-packages (from wandb) (8.1.7)\n", 631 | "Collecting GitPython!=3.1.29,>=1.0.0 (from wandb)\n", 632 | " Downloading GitPython-3.1.42-py3-none-any.whl (195 kB)\n", 633 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m195.4/195.4 kB\u001b[0m \u001b[31m18.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 634 | "\u001b[?25hRequirement already satisfied: requests<3,>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from wandb) (2.31.0)\n", 635 | "Requirement already satisfied: psutil>=5.0.0 in /usr/local/lib/python3.10/dist-packages (from wandb) (5.9.5)\n", 636 | "Collecting sentry-sdk>=1.0.0 (from wandb)\n", 637 | " Downloading sentry_sdk-1.40.5-py2.py3-none-any.whl (258 kB)\n", 638 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m258.5/258.5 kB\u001b[0m \u001b[31m27.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 639 | "\u001b[?25hCollecting docker-pycreds>=0.4.0 (from wandb)\n", 640 | " Downloading docker_pycreds-0.4.0-py2.py3-none-any.whl (9.0 kB)\n", 641 | "Requirement already satisfied: PyYAML in /usr/local/lib/python3.10/dist-packages (from wandb) (6.0.1)\n", 642 | "Collecting setproctitle (from wandb)\n", 643 | " Downloading setproctitle-1.3.3-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (30 kB)\n", 644 | "Requirement already satisfied: setuptools in /usr/local/lib/python3.10/dist-packages (from wandb) (67.7.2)\n", 645 | "Requirement already satisfied: appdirs>=1.4.3 in /usr/local/lib/python3.10/dist-packages (from wandb) (1.4.4)\n", 646 | "Requirement already satisfied: protobuf!=4.21.0,<5,>=3.19.0 in /usr/local/lib/python3.10/dist-packages (from wandb) (3.20.3)\n", 647 | "Requirement already satisfied: six>=1.4.0 in /usr/local/lib/python3.10/dist-packages (from docker-pycreds>=0.4.0->wandb) (1.16.0)\n", 648 | "Collecting gitdb<5,>=4.0.1 (from GitPython!=3.1.29,>=1.0.0->wandb)\n", 649 | " Downloading gitdb-4.0.11-py3-none-any.whl (62 kB)\n", 650 | "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m62.7/62.7 kB\u001b[0m \u001b[31m8.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", 651 | "\u001b[?25hRequirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2.0.0->wandb) (3.3.2)\n", 652 | "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2.0.0->wandb) (3.6)\n", 653 | "Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2.0.0->wandb) (2.0.7)\n", 654 | "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2.0.0->wandb) (2024.2.2)\n", 655 | "Collecting smmap<6,>=3.0.1 (from gitdb<5,>=4.0.1->GitPython!=3.1.29,>=1.0.0->wandb)\n", 656 | " Downloading smmap-5.0.1-py3-none-any.whl (24 kB)\n", 657 | "Installing collected packages: smmap, setproctitle, sentry-sdk, docker-pycreds, gitdb, GitPython, wandb\n", 658 | "Successfully installed GitPython-3.1.42 docker-pycreds-0.4.0 gitdb-4.0.11 sentry-sdk-1.40.5 setproctitle-1.3.3 smmap-5.0.1 wandb-0.16.3\n" 659 | ] 660 | }, 661 | { 662 | "output_type": "stream", 663 | "name": "stderr", 664 | "text": [ 665 | "\u001b[34m\u001b[1mwandb\u001b[0m: W&B API key is configured. Use \u001b[1m`wandb login --relogin`\u001b[0m to force relogin\n", 666 | "\u001b[34m\u001b[1mwandb\u001b[0m: \u001b[33mWARNING\u001b[0m If you're specifying your api key in code, ensure this code is not shared publicly.\n", 667 | "\u001b[34m\u001b[1mwandb\u001b[0m: \u001b[33mWARNING\u001b[0m Consider setting the WANDB_API_KEY environment variable, or running `wandb login` from the command line.\n", 668 | "\u001b[34m\u001b[1mwandb\u001b[0m: Appending key for api.wandb.ai to your netrc file: /root/.netrc\n" 669 | ] 670 | }, 671 | { 672 | "output_type": "execute_result", 673 | "data": { 674 | "text/plain": [ 675 | "True" 676 | ] 677 | }, 678 | "metadata": {}, 679 | "execution_count": 16 680 | } 681 | ] 682 | }, 683 | { 684 | "cell_type": "code", 685 | "source": [ 686 | "from langchain_core.messages import AIMessage, HumanMessage\n", 687 | "\n", 688 | "os.environ[\"LANGCHAIN_WANDB_TRACING\"] = \"true\"\n", 689 | "os.environ[\"WANDB_PROJECT\"] = \"Restaurant_ChatBot\"\n", 690 | "\n", 691 | "print(\"Welcome to the Restaurant. How can I help you today?\")\n", 692 | "chat_history = []\n", 693 | "\n", 694 | "def predict(message, history):\n", 695 | " ai_msg = rag_chain.invoke({\"question\": message, \"chat_history\": chat_history})\n", 696 | " idx = ai_msg.find(\"Answer\")\n", 697 | " chat_history.extend([HumanMessage(content=message), ai_msg])\n", 698 | "\n", 699 | " return ai_msg[idx:]" 700 | ], 701 | "metadata": { 702 | "colab": { 703 | "base_uri": "https://localhost:8080/" 704 | }, 705 | "id": "NqmiJJrzq6zY", 706 | "outputId": "e3990aad-0f1c-4cf3-ec5a-985b00d1752f" 707 | }, 708 | "execution_count": null, 709 | "outputs": [ 710 | { 711 | "output_type": "stream", 712 | "name": "stdout", 713 | "text": [ 714 | "Welcome to the Restaurant. How can I help you today?\n" 715 | ] 716 | } 717 | ] 718 | }, 719 | { 720 | "cell_type": "markdown", 721 | "source": [ 722 | "And that’s how we build a simple LLM chatbot with a very limited amount of data!\n" 723 | ], 724 | "metadata": { 725 | "id": "7zAWOBUlCJfQ" 726 | } 727 | }, 728 | { 729 | "cell_type": "code", 730 | "source": [ 731 | "import gradio as gr\n", 732 | "\n", 733 | "gr.ChatInterface(predict).launch()" 734 | ], 735 | "metadata": { 736 | "colab": { 737 | "base_uri": "https://localhost:8080/", 738 | "height": 646 739 | }, 740 | "id": "ffJntX1CnJCz", 741 | "outputId": "11c4593d-f2e2-41a4-b7fc-54d1b451804f" 742 | }, 743 | "execution_count": null, 744 | "outputs": [ 745 | { 746 | "output_type": "stream", 747 | "name": "stdout", 748 | "text": [ 749 | "Setting queue=True in a Colab notebook requires sharing enabled. Setting `share=True` (you can turn this off by setting `share=False` in `launch()` explicitly).\n", 750 | "\n", 751 | "Colab notebook detected. To show errors in colab notebook, set debug=True in launch()\n", 752 | "Running on public URL: https://a8b026c1b3c18915f2.gradio.live\n", 753 | "\n", 754 | "This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)\n" 755 | ] 756 | }, 757 | { 758 | "output_type": "display_data", 759 | "data": { 760 | "text/plain": [ 761 | "" 762 | ], 763 | "text/html": [ 764 | "
" 765 | ] 766 | }, 767 | "metadata": {} 768 | }, 769 | { 770 | "output_type": "execute_result", 771 | "data": { 772 | "text/plain": [] 773 | }, 774 | "metadata": {}, 775 | "execution_count": 22 776 | } 777 | ] 778 | } 779 | ] 780 | } --------------------------------------------------------------------------------