├── .gitignore ├── 0.0-basics └── getting_started.ipynb ├── 0.0-ollama └── chat_with_ollama_.py ├── 1.0-data_ingestion ├── 1.1-data_ingestion.ipynb └── data │ ├── GenAi Assignment.pdf │ └── speech.txt ├── 2.0-data-transformation ├── 2.1-recursive_character_text_splitter.ipynb ├── 2.2-character_text_splitter.ipynb ├── 2.3-HTML_text_splitter.ipynb ├── 2.4-recursive_Json_Splitter.ipynb └── data │ ├── attention.pdf │ └── speech.txt ├── 3.0-embeddings ├── 3.1-openai-embedding.ipynb ├── 3.2-ollama_embedding.ipynb ├── 3.3-huggingface_embedding.ipynb └── data │ ├── attention.pdf │ └── speech.txt ├── 4.0-vector_store_db ├── 4.1-faiss.ipynb ├── 4.2-chromadb.ipynb ├── chroma_db │ ├── bfca9c1c-3c72-4c8f-9f79-0acc72382eab │ │ ├── data_level0.bin │ │ ├── header.bin │ │ ├── length.bin │ │ └── link_lists.bin │ └── chroma.sqlite3 ├── data │ └── speech.txt └── faiss_db │ ├── index.faiss │ └── index.pkl ├── README.md └── requirements.txt /.gitignore: -------------------------------------------------------------------------------- 1 | venv/ 2 | .env -------------------------------------------------------------------------------- /0.0-basics/getting_started.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Getting started with langchain and Open AI\n", 8 | "\n", 9 | "Outline:\n", 10 | "- Setup Langchain, langsmith and Langserve✅\n", 11 | "- Use the basic and common component of LagChain, primpt template, models and output parser✅\n", 12 | "- Buils a simple application with langchain✅\n", 13 | "- Trace the application with langsmith✅\n", 14 | "- serve the application with langserve✅" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "import os \n", 24 | "from dotenv import load_dotenv\n", 25 | "load_dotenv()\n", 26 | "\n", 27 | "os.environ[\"OPENAI_API_KEY\"] = os.getenv(\"OPENAI_API_KEY\")\n", 28 | "\n", 29 | "# For langchain to work, you need to have the following environment variables set:\n", 30 | "os.environ[\"LANGCHAIN_API_KEY\"] = os.getenv(\"LANGCHAIN_API_KEY\")\n", 31 | "os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n", 32 | "os.environ[\"LANGCHAIN_PROJECT\"] = os.getenv(\"LANGCHAIN_PROJECT\")" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": 6, 38 | "metadata": {}, 39 | "outputs": [ 40 | { 41 | "data": { 42 | "text/plain": [ 43 | "ChatOpenAI(client=, async_client=, root_client=, root_async_client=, model_name='gpt-4o', model_kwargs={}, openai_api_key=SecretStr('**********'))" 44 | ] 45 | }, 46 | "execution_count": 6, 47 | "metadata": {}, 48 | "output_type": "execute_result" 49 | } 50 | ], 51 | "source": [ 52 | "from langchain_openai import ChatOpenAI\n", 53 | "llm = ChatOpenAI(model=\"gpt-4o\")\n", 54 | "llm" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 7, 60 | "metadata": {}, 61 | "outputs": [], 62 | "source": [ 63 | "# input and output parser\n", 64 | "result = llm.invoke(\"Who is the president of Nigeria?\")\n" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": 8, 70 | "metadata": {}, 71 | "outputs": [ 72 | { 73 | "name": "stdout", 74 | "output_type": "stream", 75 | "text": [ 76 | "content='As of October 2023, the president of Nigeria is Bola Ahmed Tinubu, who has been in office since May 29, 2023.' additional_kwargs={'refusal': None} response_metadata={'token_usage': {'completion_tokens': 32, 'prompt_tokens': 14, 'total_tokens': 46, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}}, 'model_name': 'gpt-4o-2024-08-06', 'system_fingerprint': 'fp_4691090a87', 'finish_reason': 'stop', 'logprobs': None} id='run-2e35d1b6-08c4-472a-ac0e-3250a4b1544f-0' usage_metadata={'input_tokens': 14, 'output_tokens': 32, 'total_tokens': 46, 'input_token_details': {'audio': 0, 'cache_read': 0}, 'output_token_details': {'audio': 0, 'reasoning': 0}}\n" 77 | ] 78 | } 79 | ], 80 | "source": [ 81 | "print(result)" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": 9, 87 | "metadata": {}, 88 | "outputs": [], 89 | "source": [ 90 | "result2 = llm.invoke(\"What is mars\")" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 10, 96 | "metadata": {}, 97 | "outputs": [ 98 | { 99 | "data": { 100 | "text/plain": [ 101 | "AIMessage(content='Mars is the fourth planet from the Sun in our solar system. It is often referred to as the \"Red Planet\" due to its reddish appearance, which comes from iron oxide (rust) on its surface. Here are some key points about Mars:\\n\\n1. **Size and Distance:** Mars is about half the size of Earth, with a diameter of approximately 6,792 kilometers (about 4,220 miles). It is located about 227.9 million kilometers (about 141.6 million miles) from the Sun.\\n\\n2. **Surface and Atmosphere:** Mars has a varied surface with features such as volcanoes, valleys, deserts, and polar ice caps. Olympus Mons, the tallest volcano in the solar system, and Valles Marineris, one of the largest canyons, are located on Mars. The thin atmosphere is composed mostly of carbon dioxide, with trace amounts of nitrogen and argon.\\n\\n3. **Moons:** Mars has two small moons, Phobos and Deimos, which are thought to be captured asteroids.\\n\\n4. **Temperature:** The surface temperature on Mars can vary significantly, ranging from about -125 degrees Celsius (-195 degrees Fahrenheit) during the winter at the poles to 20 degrees Celsius (68 degrees Fahrenheit) in the summer at its equator.\\n\\n5. **Exploration:** Mars has been a focal point for exploration due to the possibility of past life. Numerous missions by NASA and other space agencies have been sent to study Mars, including orbiters, landers, and rovers. Notable missions include the Mars rovers Spirit, Opportunity, Curiosity, and Perseverance.\\n\\n6. **Potential for Life:** While no definitive evidence of life on Mars has been found, scientists continue to study its surface and climate for signs of past microbial life. The presence of water ice and the detection of seasonal methane fluctuations are of particular interest.\\n\\n7. **Human Exploration:** Mars is considered a prime candidate for future human exploration due to its similarities to Earth. Efforts are ongoing to develop the technology needed for human missions to Mars, with plans for eventual manned missions.\\n\\nMars continues to be a subject of interest in astronomy and planetary science due to its unique characteristics and potential for human habitation in the distant future.', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 457, 'prompt_tokens': 10, 'total_tokens': 467, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}}, 'model_name': 'gpt-4o-2024-08-06', 'system_fingerprint': 'fp_4691090a87', 'finish_reason': 'stop', 'logprobs': None}, id='run-19db8eae-e266-4b09-b9b4-1d79fe0fbc67-0', usage_metadata={'input_tokens': 10, 'output_tokens': 457, 'total_tokens': 467, 'input_token_details': {'audio': 0, 'cache_read': 0}, 'output_token_details': {'audio': 0, 'reasoning': 0}})" 102 | ] 103 | }, 104 | "execution_count": 10, 105 | "metadata": {}, 106 | "output_type": "execute_result" 107 | } 108 | ], 109 | "source": [ 110 | "result2" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": 12, 116 | "metadata": {}, 117 | "outputs": [ 118 | { 119 | "data": { 120 | "text/plain": [ 121 | "ChatPromptTemplate(input_variables=['input'], input_types={}, partial_variables={}, messages=[SystemMessagePromptTemplate(prompt=PromptTemplate(input_variables=[], input_types={}, partial_variables={}, template='Act like an expert AI Engineer, Provide me answers based on the questions'), additional_kwargs={}), HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=['input'], input_types={}, partial_variables={}, template='{input}'), additional_kwargs={})])" 122 | ] 123 | }, 124 | "execution_count": 12, 125 | "metadata": {}, 126 | "output_type": "execute_result" 127 | } 128 | ], 129 | "source": [ 130 | "# Chat prompt template\n", 131 | "from langchain_core.prompts import ChatPromptTemplate\n", 132 | "\n", 133 | "prompt = ChatPromptTemplate.from_messages(\n", 134 | " [\n", 135 | " (\"system\", \"Act like an expert AI Engineer, Provide me answers based on the questions\"),\n", 136 | " (\"user\", \"{input}\")\n", 137 | " ]\n", 138 | ")\n", 139 | "prompt" 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": 13, 145 | "metadata": {}, 146 | "outputs": [ 147 | { 148 | "name": "stdout", 149 | "output_type": "stream", 150 | "text": [ 151 | "content='As of my last update in October 2023, there isn\\'t any widely recognized technologies or frameworks explicitly named \"Langsmith\" and \"Langserve\" in the context of artificial intelligence or software engineering. It\\'s possible that these could be internal tools, newly released products, or lesser-known initiatives. If they have been introduced or gained popularity after my last update, I would recommend checking official websites, recent technology news articles, or relevant technology forums for the most up-to-date information. Additionally, understanding the context or the domain (e.g., natural language processing, software development tools, etc.) in which these terms are used could provide further clues.' additional_kwargs={'refusal': None} response_metadata={'token_usage': {'completion_tokens': 129, 'prompt_tokens': 34, 'total_tokens': 163, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}}, 'model_name': 'gpt-4o-2024-08-06', 'system_fingerprint': 'fp_50cad350e4', 'finish_reason': 'stop', 'logprobs': None} id='run-e7b05b6e-1204-44c7-9516-5e481d2f8a15-0' usage_metadata={'input_tokens': 34, 'output_tokens': 129, 'total_tokens': 163, 'input_token_details': {'audio': 0, 'cache_read': 0}, 'output_token_details': {'audio': 0, 'reasoning': 0}}\n" 152 | ] 153 | } 154 | ], 155 | "source": [ 156 | "## Chain prompt with LLM\n", 157 | "chain = prompt|llm\n", 158 | "\n", 159 | "response = chain.invoke({\"input\": \"Tell me about langsmith and langserve.\"})\n", 160 | "print(response)" 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": 14, 166 | "metadata": {}, 167 | "outputs": [ 168 | { 169 | "name": "stdout", 170 | "output_type": "stream", 171 | "text": [ 172 | "content=\"Generative Adversarial Networks (GANs) are a class of machine learning frameworks invented by Ian Goodfellow and his colleagues in 2014. GANs are particularly known for their capability to generate new, synthetic instances of data that can pass for real data. They are widely used in image generation, video generation, and voice synthesis, among other applications.\\n\\nA GAN consists of two primary components: the generator and the discriminator, which are neural networks that contest with each other in a zero-sum game framework.\\n\\n1. **Generator:** This network is responsible for creating new data instances. It starts from a random noise and tries to generate data that resembles the real dataset. The generator's goal is to produce data that is indistinguishable from the real data by the discriminator.\\n\\n2. **Discriminator:** This network evaluates the authenticity of the data it receives. It decides whether a given instance of data is real (from the dataset) or fake (generated by the generator). The discriminator is essentially a binary classifier designed to correctly categorize input data as real or fake.\\n\\nThe training process of GANs is a competition between these two networks. The generator aims to produce increasingly realistic data to fool the discriminator, while the discriminator improves its ability to distinguish real data from fake data. This adversarial nature leads to a minimax optimization framework where:\\n\\n- The generator tries to minimize the probability of the discriminator distinguishing the real from the fake.\\n- The discriminator tries to maximize the probability of correctly identifying real and fake data.\\n\\nThe process can be mathematically described by the following value function \\\\(V(G, D)\\\\):\\n\\n\\\\[ \\\\min_G \\\\max_D V(D, G) = \\\\mathbb{E}_{x \\\\sim p_{data}}[\\\\log D(x)] + \\\\mathbb{E}_{z \\\\sim p_z}[\\\\log(1 - D(G(z)))] \\\\]\\n\\nHere, \\\\(p_{data}\\\\) is the data distribution, \\\\(p_z\\\\) is the input noise distribution, and \\\\(z\\\\) is the noise vector used by the generator.\\n\\nAlthough GANs are powerful, they are notoriously difficult to train due to issues like mode collapse, non-convergence, and gradient instability. However, advances such as Wasserstein GANs (WGANs), Conditional GANs (CGANs), and StyleGANs have improved the stability and quality of data generated by GANs.\" additional_kwargs={'refusal': None} response_metadata={'token_usage': {'completion_tokens': 483, 'prompt_tokens': 35, 'total_tokens': 518, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}}, 'model_name': 'gpt-4o-2024-08-06', 'system_fingerprint': 'fp_0f8c83e59b', 'finish_reason': 'stop', 'logprobs': None} id='run-1920032e-9386-46e3-bc9b-521634960b04-0' usage_metadata={'input_tokens': 35, 'output_tokens': 483, 'total_tokens': 518, 'input_token_details': {'audio': 0, 'cache_read': 0}, 'output_token_details': {'audio': 0, 'reasoning': 0}}\n" 173 | ] 174 | } 175 | ], 176 | "source": [ 177 | "chain = prompt|llm\n", 178 | "\n", 179 | "response = chain.invoke({\"input\": \"Tell me about Generative Adversarial Network.\"})\n", 180 | "print(response)" 181 | ] 182 | }, 183 | { 184 | "cell_type": "markdown", 185 | "metadata": {}, 186 | "source": [ 187 | "Generative Adversarial Networks (GANs) are a class of machine learning frameworks invented by Ian Goodfellow and his colleagues in 2014. GANs are particularly known for their capability to generate new, synthetic instances of data that can pass for real data. They are widely used in image generation, video generation, and voice synthesis, among other applications.\n", 188 | "\n", 189 | "A GAN consists of two primary components: the generator and the discriminator, which are neural networks that contest with each other in a zero-sum game framework.\n", 190 | "\n", 191 | "1. **Generator:** This network is responsible for creating new data instances. It starts from a random noise and tries to generate data that resembles the real dataset. The generator's goal is to produce data that is indistinguishable from the real data by the discriminator.\n", 192 | "\n", 193 | "2. **Discriminator:** This network evaluates the authenticity of the data it receives. It decides whether a given instance of data is real (from the dataset) or fake (generated by the generator). The discriminator is essentially a binary classifier designed to correctly categorize input data as real or fake.\n", 194 | "\n", 195 | "The training process of GANs is a competition between these two networks. The generator aims to produce increasingly realistic data to fool the discriminator, while the discriminator improves its ability to distinguish real data from fake data. This adversarial nature leads to a minimax optimization framework where:\n", 196 | "\n", 197 | "- The generator tries to minimize the probability of the discriminator distinguishing the real from the fake.\n", 198 | "- The discriminator tries to maximize the probability of correctly identifying real and fake data.\n", 199 | "\n", 200 | "The process can be mathematically described by the following value function \\(V(G, D)\\):\n", 201 | "\n", 202 | "\\[ \\min_G \\max_D V(D, G) = \\mathbb{E}_{x \\sim p_{data}}[\\log D(x)] + \\mathbb{E}_{z \\sim p_z}[\\log(1 - D(G(z)))] \\]\n", 203 | "\n", 204 | "Here, \\(p_{data}\\) is the data distribution, \\(p_z\\) is the input noise distribution, and \\(z\\) is the noise vector used by the generator.\n", 205 | "\n", 206 | "Although GANs are powerful, they are notoriously difficult to train due to issues like mode collapse, non-convergence, and gradient instability. However, advances such as Wasserstein GANs (WGANs), Conditional GANs (CGANs), and StyleGANs have improved the stability and quality of data generated by GANs." 207 | ] 208 | }, 209 | { 210 | "cell_type": "code", 211 | "execution_count": 15, 212 | "metadata": {}, 213 | "outputs": [ 214 | { 215 | "data": { 216 | "text/plain": [ 217 | "langchain_core.messages.ai.AIMessage" 218 | ] 219 | }, 220 | "execution_count": 15, 221 | "metadata": {}, 222 | "output_type": "execute_result" 223 | } 224 | ], 225 | "source": [ 226 | "type(response)" 227 | ] 228 | }, 229 | { 230 | "cell_type": "code", 231 | "execution_count": 16, 232 | "metadata": {}, 233 | "outputs": [ 234 | { 235 | "name": "stdout", 236 | "output_type": "stream", 237 | "text": [ 238 | "Variational Autoencoders (VAEs) are a type of generative model that combine principles from deep learning and Bayesian inference to generate new data similar to a given dataset. They are an advanced version of traditional autoencoders, and they were introduced by Kingma and Welling in 2013.\n", 239 | "\n", 240 | "### Key Components of VAEs:\n", 241 | "\n", 242 | "1. **Encoder Network**: The encoder takes an input and maps it to a distribution over a latent space. Unlike traditional autoencoders, which encode an input as a fixed point in latent space, VAEs map inputs to a distribution, usually a Gaussian. The encoder outputs the mean and the logarithm of the variance of this distribution, representing the parameters of the Gaussian.\n", 243 | "\n", 244 | "2. **Latent Space**: Instead of a deterministic encoding, VAEs introduce stochasticity by representing each input as a Gaussian distribution in the latent space. A sample is drawn from this distribution during training, introducing some variability. This stochastic choice is crucial for generating diverse outputs.\n", 245 | "\n", 246 | "3. **Decoder Network**: The decoder takes a latent variable sampled from the Gaussian distribution and reconstructs the input data. The decoder is typically a neural network that learns to map latent variables back to the original data distribution.\n", 247 | "\n", 248 | "4. **Loss Function**: The loss function for a VAE consists of two parts:\n", 249 | " - **Reconstruction Loss**: Measures how well the decoder reconstructs the input from the latent representation. This is usually computed as mean squared error for continuous data or cross-entropy for binary data.\n", 250 | " - **KL Divergence**: A regularization term that ensures the learned latent space distribution is close to a prior distribution, typically a standard normal distribution. This encourages smoothness in the latent space and allows for meaningful sampling.\n", 251 | "\n", 252 | "5. **Reparameterization Trick**: A technique used to backpropagate through the sampling process. It involves expressing the sampling operation in a way that allows gradients to flow through the network by introducing a deterministic node and a noise variable.\n", 253 | "\n", 254 | "### Advantages of VAEs:\n", 255 | "\n", 256 | "- **Generative Capability**: Can generate new, similar data samples by sampling latent variables from the learned latent space.\n", 257 | "- **Continuous Latent Space**: Provides smooth interpolations between data points in the latent space, which can be useful for tasks such as morphing one data point into another.\n", 258 | "- **Unsupervised Learning**: VAEs can be trained without labeled data, making them suitable for unsupervised learning scenarios.\n", 259 | "\n", 260 | "### Applications:\n", 261 | "\n", 262 | "- Image generation and inpainting\n", 263 | "- Semi-supervised learning\n", 264 | "- Data denoising\n", 265 | "- Anomaly detection\n", 266 | "\n", 267 | "VAEs are a powerful framework for unsupervised learning and have been widely used in many applications, especially in scenarios where high-dimensional data needs to be compressed into a lower-dimensional space without losing much information.\n" 268 | ] 269 | } 270 | ], 271 | "source": [ 272 | "# Print out just the sting answer only\n", 273 | "\n", 274 | "from langchain_core.output_parsers import StrOutputParser\n", 275 | "output_parser = StrOutputParser()\n", 276 | "chain = prompt|llm|output_parser\n", 277 | "\n", 278 | "response2 = chain.invoke({\"input\": \"Tell me about Variation auto encodersk.\"})\n", 279 | "print(response2)" 280 | ] 281 | }, 282 | { 283 | "cell_type": "markdown", 284 | "metadata": {}, 285 | "source": [ 286 | "{\n", 287 | " \"output\": \"Variational Autoencoders (VAEs) are a type of generative model that combine principles from deep learning and Bayesian inference to generate new data similar to a given dataset. They are an advanced version of traditional autoencoders, and they were introduced by Kingma and Welling in 2013.\\n\\n### Key Components of VAEs:\\n\\n1. **Encoder Network**: The encoder takes an input and maps it to a distribution over a latent space. Unlike traditional autoencoders, which encode an input as a fixed point in latent space, VAEs map inputs to a distribution, usually a Gaussian. The encoder outputs the mean and the logarithm of the variance of this distribution, representing the parameters of the Gaussian.\\n\\n2. **Latent Space**: Instead of a deterministic encoding, VAEs introduce stochasticity by representing each input as a Gaussian distribution in the latent space. A sample is drawn from this distribution during training, introducing some variability. This stochastic choice is crucial for generating diverse outputs.\\n\\n3. **Decoder Network**: The decoder takes a latent variable sampled from the Gaussian distribution and reconstructs the input data. The decoder is typically a neural network that learns to map latent variables back to the original data distribution.\\n\\n4. **Loss Function**: The loss function for a VAE consists of two parts:\\n - **Reconstruction Loss**: Measures how well the decoder reconstructs the input from the latent representation. This is usually computed as mean squared error for continuous data or cross-entropy for binary data.\\n - **KL Divergence**: A regularization term that ensures the learned latent space distribution is close to a prior distribution, typically a standard normal distribution. This encourages smoothness in the latent space and allows for meaningful sampling.\\n\\n5. **Reparameterization Trick**: A technique used to backpropagate through the sampling process. It involves expressing the sampling operation in a way that allows gradients to flow through the network by introducing a deterministic node and a noise variable.\\n\\n### Advantages of VAEs:\\n\\n- **Generative Capability**: Can generate new, similar data samples by sampling latent variables from the learned latent space.\\n- **Continuous Latent Space**: Provides smooth interpolations between data points in the latent space, which can be useful for tasks such as morphing one data point into another.\\n- **Unsupervised Learning**: VAEs can be trained without labeled data, making them suitable for unsupervised learning scenarios.\\n\\n### Applications:\\n\\n- Image generation and inpainting\\n- Semi-supervised learning\\n- Data denoising\\n- Anomaly detection\\n\\nVAEs are a powerful framework for unsupervised learning and have been widely used in many applications, especially in scenarios where high-dimensional data needs to be compressed into a lower-dimensional space without losing much information.\"\n", 288 | "}" 289 | ] 290 | } 291 | ], 292 | "metadata": { 293 | "kernelspec": { 294 | "display_name": "Python 3", 295 | "language": "python", 296 | "name": "python3" 297 | }, 298 | "language_info": { 299 | "codemirror_mode": { 300 | "name": "ipython", 301 | "version": 3 302 | }, 303 | "file_extension": ".py", 304 | "mimetype": "text/x-python", 305 | "name": "python", 306 | "nbconvert_exporter": "python", 307 | "pygments_lexer": "ipython3", 308 | "version": "3.10.0" 309 | } 310 | }, 311 | "nbformat": 4, 312 | "nbformat_minor": 2 313 | } 314 | -------------------------------------------------------------------------------- /0.0-ollama/chat_with_ollama_.py: -------------------------------------------------------------------------------- 1 | import os 2 | from dotenv import load_dotenv 3 | from langchain_community.llms import Ollama 4 | from langchain_core.prompts import ChatPromptTemplate 5 | from langchain_core.output_parsers import StrOutputParser 6 | import streamlit as st 7 | 8 | 9 | load_dotenv() 10 | 11 | os.environ["LANGCHAIN_API_KEY"] = os.getenv("LANGCHAIN_API_KEY") 12 | os.environ["LANGCHAIN_TRACING_V2"] = "true" 13 | os.environ["LANGCHAIN_PROJECT"] = os.getenv("LANGCHAIN_PROJECT") 14 | 15 | # Create out chat prompt template 16 | prompt = ChatPromptTemplate.from_messages( 17 | [ 18 | ("system", "You are a helpful assistant Kindly respond to the question asked."), 19 | ("user", "Question: {question}"), 20 | ] 21 | ) 22 | 23 | # Stramlit interface 24 | st.title("Chat with Ollama:gamma2:2b") 25 | input_text = st.text_input("Enter your question here: ") 26 | 27 | llm = Ollama(model="gemma2:2b") 28 | output_parser = StrOutputParser() 29 | chain = prompt|llm|output_parser 30 | 31 | if input_text: 32 | st.write(chain.invoke({"question": input_text}) 33 | ) -------------------------------------------------------------------------------- /1.0-data_ingestion/1.1-data_ingestion.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Data ingestion\n", 8 | "\n", 9 | "Langchain DOcument Loader Documentation: https://python.langchain.com/v0.2/docs/integrations/document_loaders/" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 1, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "from langchain_community.document_loaders import TextLoader" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 2, 24 | "metadata": {}, 25 | "outputs": [], 26 | "source": [ 27 | "# Loading your text file\n", 28 | "loader = TextLoader(\"./data/speech.txt\")" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 3, 34 | "metadata": {}, 35 | "outputs": [], 36 | "source": [ 37 | "text_document = loader.load()" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": 4, 43 | "metadata": {}, 44 | "outputs": [ 45 | { 46 | "data": { 47 | "text/plain": [ 48 | "[Document(metadata={'source': './data/speech.txt'}, page_content='Hello!\\nMy name is DAVEWORLD, and I am excited to be your tutor for this program.\\n\\nI am a Data Scientist, Open-source Contributor, and Frontend Engineer with years of experience in the tech industry. Throughout my career, I’ve been privileged to build innovative products, solve real-world problems, and empower over 5,000 individuals to grow into better versions of themselves. My work and insights have also connected me with an audience of over 10,000 followers across social media platforms.\\nLooking ahead, my vision is to create groundbreaking projects and support millions—if not billions—of people in achieving their goals and thriving in the tech industry.\\n\\nIn this program, I am here to guide you every step of the way, ensuring you have an unforgettable learning experience.\\nLet’s embark on this transformative journey together!')]" 49 | ] 50 | }, 51 | "execution_count": 4, 52 | "metadata": {}, 53 | "output_type": "execute_result" 54 | } 55 | ], 56 | "source": [ 57 | "text_document" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": 5, 63 | "metadata": {}, 64 | "outputs": [ 65 | { 66 | "data": { 67 | "text/plain": [ 68 | "list" 69 | ] 70 | }, 71 | "execution_count": 5, 72 | "metadata": {}, 73 | "output_type": "execute_result" 74 | } 75 | ], 76 | "source": [ 77 | "type(text_document)" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 6, 83 | "metadata": {}, 84 | "outputs": [ 85 | { 86 | "data": { 87 | "text/plain": [ 88 | "'Hello!\\nMy name is DAVEWORLD, and I am excited to be your tutor for this program.\\n\\nI am a Data Scientist, Open-source Contributor, and Frontend Engineer with years of experience in the tech industry. Throughout my career, I’ve been privileged to build innovative products, solve real-world problems, and empower over 5,000 individuals to grow into better versions of themselves. My work and insights have also connected me with an audience of over 10,000 followers across social media platforms.\\nLooking ahead, my vision is to create groundbreaking projects and support millions—if not billions—of people in achieving their goals and thriving in the tech industry.\\n\\nIn this program, I am here to guide you every step of the way, ensuring you have an unforgettable learning experience.\\nLet’s embark on this transformative journey together!'" 89 | ] 90 | }, 91 | "execution_count": 6, 92 | "metadata": {}, 93 | "output_type": "execute_result" 94 | } 95 | ], 96 | "source": [ 97 | "text_document[0].page_content" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 7, 103 | "metadata": {}, 104 | "outputs": [], 105 | "source": [ 106 | "# Load in pdf files\n", 107 | "from langchain_community.document_loaders import PyPDFLoader" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": 8, 113 | "metadata": {}, 114 | "outputs": [], 115 | "source": [ 116 | "loader = PyPDFLoader(\"./data/GenAi Assignment.pdf\") # Make sure to add your pdf assignment to data folder" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": 9, 122 | "metadata": {}, 123 | "outputs": [], 124 | "source": [ 125 | "docs = loader.load()" 126 | ] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": 10, 131 | "metadata": {}, 132 | "outputs": [ 133 | { 134 | "data": { 135 | "text/plain": [ 136 | "[Document(metadata={'source': './data/GenAi Assignment.pdf', 'page': 0}, page_content='SAIL Generative AI Programme\\nASSIGNMENT FOR MODULE 1\\nGenerative Artificial Intelligence\\nOdelola Solomon Oluwatobiloba\\nDecember 11, 2024'),\n", 137 | " Document(metadata={'source': './data/GenAi Assignment.pdf', 'page': 1}, page_content='Contents\\n1 Short Answer Questions 3\\n1.1 What is Artificial Intelligence . . . . . . . . . . . . . . . . . . . . . . . . . 3\\n1.2 Explain the difference between machine learning and deep learning . . . 3\\n1.3 Describe each type of machine learning . . . . . . . . . . . . . . . . . . . 4\\n1.4 Give an example of where Deep Learning might be more effective than\\ntraditional Machine Learning . . . . . . . . . . . . . . . . . . . . . . . . . 4\\n1.5 Write a brief definition for discriminative models and generative models 5\\n2 Algorithm Exploration 6\\n3 Practical Exercise 7\\n4 Reflection 9'),\n", 138 | " Document(metadata={'source': './data/GenAi Assignment.pdf', 'page': 2}, page_content='C H A P T E R1\\nShort Answer Questions\\n1.1 What is Artificial Intelligence\\nArtificial intelligence is a branch of computer science that leverages the mimicking\\nof human intelligence to build applications that makes life easy and problem solving\\nefficient. AI is also a branch of computer science that embodies machine learning and\\nits sub-domains like deep learning. AI has of recent been the reason most businesses\\nhave been making a lot of income.\\n1.2 Explain the difference between machine learning and\\ndeep learning\\nFirstly, machine learning is a broader field than deep learning and deep learning is a\\nbranch of machine learning while machine learning itself is a branch of AI. Machine\\nlearning is therefore a branch of AI that involves the use of statistical models in making\\npredictions and forecasts by either exposing the models to labelled data or unlabelled\\ndata.\\nOn the other hand, deep learning is a branch of machine learning that uses a large\\nnumber of layers containing neurons to learn patters in complex data in order to make\\naccurate predictions.\\nDeep learning is majorly subdivided into artificial neural networks(ANN), recurrent'),\n", 139 | " Document(metadata={'source': './data/GenAi Assignment.pdf', 'page': 3}, page_content='4 Short Answer Questions\\nneural networks(RNN), and convolutional neural networks(CNN).\\nANN is a type of deep-learning that uses simpler layers of neurons to make predic-\\ntions, an example of an ANN model is multilayer perceptron(MLP). RNN is a type of\\ndeep learning model that is used for textual or sequential data. CNN is a type of deep\\nlearning model that is used for image classification, object detection\\n1.3 Describe each type of machine learning\\n1. Supervised Learning Supervised learning is a type of machine learning in which\\nmachine learning models are trained on labeled data, i.e., data whose target or\\ndependent variable is present alongside the independent variables. Examples of\\nsuch models are: Logistic regression, random forest , linear regression models,\\nsvm\\n2. Unsupervised Learning deals with unlabeled data, where the goal is to find\\npatterns or structures within the data. This is often used in clustering and dimen-\\nsionality reduction. A real-world example is customer segmentation in marketing,\\nwhere users are grouped based on their behavior or preferences without prede-\\nfined categories.\\n3. Reinforcement Learning focuses on training agents to make sequential decisions\\nby interacting with an environment. The agent learns by receiving rewards or\\npenalties for its actions. A practical example is training autonomous vehicles to\\nnavigate roads safely, optimizing for efficiency and avoiding collisions.\\n1.4 Give an example of where Deep Learning might be\\nmore effective than traditional Machine Learning\\nDeep Learning can be more effective than traditional Machine Learning when dealing\\nwith high-dimensional and unstructured data, such as images, audio, or text. For\\ninstance, in facial recognition tasks, deep learning models like Convolutional Neural\\nNetworks (CNNs) outperform traditional methods by automatically extracting hierar-\\nchical features from images, which would be challenging and less effective with manual'),\n", 140 | " Document(metadata={'source': './data/GenAi Assignment.pdf', 'page': 4}, page_content='1.5 Write a brief definition for discriminative models and generative models 5\\nfeature engineering used in traditional approaches\\n1.5 Write a brief definition for discriminative models and\\ngenerative models\\n1. Discriminative models : Discriminative models focus on learning the decision\\nboundary between classes by directly modeling the conditional probability\\nP(y/x) (1.1)\\nwhere y represents the label and x represents the input data. These models are\\nprimarily used for classification or regression tasks. Examples include Logistic\\nRegression and Support Vector Machines (SVM).\\n2. Generative Models: Generative models, on the other hand, aim to model the joint\\nprobability distribution\\nP(x, y) (1.2)\\nor the distribution of the input data\\nP(x) (1.3)\\nThey learn how the data is generated, which allows them to create new data\\nsamples similar to the training data. Examples include Gaussian Mixture Models\\n(GMM), Variational Autoencoders (VAE), and Generative Adversarial Networks\\n(GANs).'),\n", 141 | " Document(metadata={'source': './data/GenAi Assignment.pdf', 'page': 5}, page_content='C H A P T E R2\\nAlgorithm Exploration\\nLogistic Regression is a supervised learning algorithm used to classify data into cate-\\ngories. It works by analyzing input data and predicting the likelihood of an outcome\\nbelonging to a particular class. The model uses a function that compresses outputs into\\nprobabilities between 0 and 1, making it suitable for binary classification tasks. Based\\non these probabilities, it assigns the data to one of the two categories by applying a\\nthreshold, usually 0.5.\\nA practical example is email spam detection. Logistic Regression can classify emails\\nas \"spam\" or \"not spam\" by examining features such as word usage, sender details, and\\nmessage formatting. Its simplicity and effectiveness with structured data make it ideal\\nfor such binary classification problems.'),\n", 142 | " Document(metadata={'source': './data/GenAi Assignment.pdf', 'page': 6}, page_content='C H A P T E R3\\nPractical Exercise\\n1. Dataset: The Iris dataset from the UCI Machine Learning Repository. The Iris\\ndataset is a small, open-source dataset containing 150 samples of iris flowers,\\nclassified into three species: Setosa, Versicolor, and Virginica. It has four features:\\nsepal length, sepal width, petal length, and petal width.\\n2. Task Explanation This dataset is well-suited for supervised learning because it\\ncontains labeled data, where each flower sample is associated with its correspond-\\ning species. Using this dataset, a supervised learning model can learn to classify\\niris flowers based on their features. Insights include identifying patterns in flower\\nspecies based on measurements, which can help in botanical studies or automated\\nclassification systems.\\n3. Real-World Examples of Discriminative and Generative Models Discriminative\\nModel Example: Email spam classification A discriminative model like Logistic\\nRegression or Support Vector Machines (SVM) analyzes email content and predicts\\nwhether it is spam or not. It focuses on learning the decision boundary between\\n\"spam\" and \"not spam\" classes directly, making it efficient for binary classification\\ntasks.\\n4. Generative Model Example: Image generation with GANs (Generative Adversar-\\nial Networks) GANs are used to create realistic images, such as generating human\\nfaces for art or entertainment. They learn the distribution of the training data and'),\n", 143 | " Document(metadata={'source': './data/GenAi Assignment.pdf', 'page': 7}, page_content='8 Practical Exercise\\ncan generate entirely new data samples that resemble the original data. This fits\\nthe generative model type because GANs learn to simulate the underlying data\\ndistribution rather than simply classifying inputs.'),\n", 144 | " Document(metadata={'source': './data/GenAi Assignment.pdf', 'page': 8}, page_content='C H A P T E R4\\nReflection\\n1. Understanding the distinctions between AI, Machine Learning, and Deep Learning\\nis crucial in the field of Generative AI because each layer builds on the other.\\nAI represents the overarching goal of creating systems capable of simulating\\nintelligence. Machine Learning focuses on enabling systems to learn from data,\\nand Deep Learning, as a subset of Machine Learning, specializes in using neural\\nnetworks for complex, high-dimensional data tasks. Generative AI, often reliant\\non Deep Learning models like GANs or transformers, benefits from this layered\\nunderstanding as it enables developers to choose the right tools and techniques for\\ntasks like image synthesis or text generation. This knowledge ensures solutions\\nare optimized for performance, accuracy, and scalability.\\n2. The art industry benefits significantly from generative AI. Tools like DALL-E and\\nMidJourney democratize creativity by enabling anyone to produce high-quality\\ndigital art. They empower artists to prototype ideas, enhance creativity, and\\nexplore new aesthetic territories, while reducing the time and cost of traditional\\nmethods.\\n3. In marketing, generative AI is revolutionizing content creation. It is applied to\\ngenerate personalized advertisements, draft engaging social media content, and\\ndevelop email campaigns tailored to specific customer preferences. Tools like\\nChatGPT and Jasper AI allow marketers to produce persuasive copy quickly,\\nenhancing outreach effectiveness. Furthermore, generative AI models create syn-'),\n", 145 | " Document(metadata={'source': './data/GenAi Assignment.pdf', 'page': 9}, page_content='10 Reflection\\nthetic customer personas based on historical data, enabling businesses to predict\\ntrends and better target their audiences. This application drives engagement,\\nsaves resources, and increases the overall ROI of marketing strategies.')]" 146 | ] 147 | }, 148 | "execution_count": 10, 149 | "metadata": {}, 150 | "output_type": "execute_result" 151 | } 152 | ], 153 | "source": [ 154 | "docs" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": 11, 160 | "metadata": {}, 161 | "outputs": [ 162 | { 163 | "data": { 164 | "text/plain": [ 165 | "list" 166 | ] 167 | }, 168 | "execution_count": 11, 169 | "metadata": {}, 170 | "output_type": "execute_result" 171 | } 172 | ], 173 | "source": [ 174 | "type(docs)" 175 | ] 176 | }, 177 | { 178 | "cell_type": "code", 179 | "execution_count": 12, 180 | "metadata": {}, 181 | "outputs": [ 182 | { 183 | "name": "stderr", 184 | "output_type": "stream", 185 | "text": [ 186 | "USER_AGENT environment variable not set, consider setting it to identify your requests.\n" 187 | ] 188 | } 189 | ], 190 | "source": [ 191 | "# Web based loader\n", 192 | "from langchain_community.document_loaders import WebBaseLoader\n", 193 | "import bs4\n", 194 | "\n", 195 | "web_loader = WebBaseLoader(web_paths=(\"https://lilianweng.github.io/posts/2023-06-23-agent/\", ),\n", 196 | " bs_kwargs=dict(parse_only=bs4.SoupStrainer(\n", 197 | " class_ = (\"post-title\", \"post-header\")\n", 198 | " )))" 199 | ] 200 | }, 201 | { 202 | "cell_type": "code", 203 | "execution_count": 13, 204 | "metadata": {}, 205 | "outputs": [ 206 | { 207 | "data": { 208 | "text/plain": [ 209 | "[Document(metadata={'source': 'https://lilianweng.github.io/posts/2023-06-23-agent/'}, page_content='\\n\\n LLM Powered Autonomous Agents\\n \\nDate: June 23, 2023 | Estimated Reading Time: 31 min | Author: Lilian Weng\\n\\n\\n')]" 210 | ] 211 | }, 212 | "execution_count": 13, 213 | "metadata": {}, 214 | "output_type": "execute_result" 215 | } 216 | ], 217 | "source": [ 218 | "web_loader.load()" 219 | ] 220 | }, 221 | { 222 | "cell_type": "code", 223 | "execution_count": 14, 224 | "metadata": {}, 225 | "outputs": [], 226 | "source": [ 227 | "# Reading Arxive document\n", 228 | "from langchain_community.document_loaders import ArxivLoader" 229 | ] 230 | }, 231 | { 232 | "cell_type": "code", 233 | "execution_count": null, 234 | "metadata": {}, 235 | "outputs": [], 236 | "source": [ 237 | "loader = ArxivLoader(\n", 238 | " query=\"1706.03762\"\n", 239 | ")\n" 240 | ] 241 | }, 242 | { 243 | "cell_type": "code", 244 | "execution_count": 20, 245 | "metadata": {}, 246 | "outputs": [ 247 | { 248 | "data": { 249 | "text/plain": [ 250 | "[Document(metadata={'Published': '2023-08-02', 'Title': 'Attention Is All You Need', 'Authors': 'Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, Lukasz Kaiser, Illia Polosukhin', 'Summary': 'The dominant sequence transduction models are based on complex recurrent or\\nconvolutional neural networks in an encoder-decoder configuration. The best\\nperforming models also connect the encoder and decoder through an attention\\nmechanism. We propose a new simple network architecture, the Transformer, based\\nsolely on attention mechanisms, dispensing with recurrence and convolutions\\nentirely. Experiments on two machine translation tasks show these models to be\\nsuperior in quality while being more parallelizable and requiring significantly\\nless time to train. Our model achieves 28.4 BLEU on the WMT 2014\\nEnglish-to-German translation task, improving over the existing best results,\\nincluding ensembles by over 2 BLEU. On the WMT 2014 English-to-French\\ntranslation task, our model establishes a new single-model state-of-the-art\\nBLEU score of 41.8 after training for 3.5 days on eight GPUs, a small fraction\\nof the training costs of the best models from the literature. We show that the\\nTransformer generalizes well to other tasks by applying it successfully to\\nEnglish constituency parsing both with large and limited training data.'}, 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.com\\nNoam Shazeer∗\\nGoogle Brain\\nnoam@google.com\\nNiki Parmar∗\\nGoogle Research\\nnikip@google.com\\nJakob Uszkoreit∗\\nGoogle Research\\nusz@google.com\\nLlion Jones∗\\nGoogle Research\\nllion@google.com\\nAidan N. Gomez∗†\\nUniversity of Toronto\\naidan@cs.toronto.edu\\nŁukasz Kaiser∗\\nGoogle Brain\\nlukaszkaiser@google.com\\nIllia Polosukhin∗‡\\nillia.polosukhin@gmail.com\\nAbstract\\nThe dominant sequence transduction models are based on complex recurrent or\\nconvolutional neural networks that include an encoder and a decoder. The best\\nperforming models also connect the encoder and decoder through an attention\\nmechanism. We propose a new simple network architecture, the Transformer,\\nbased solely on attention mechanisms, dispensing with recurrence and convolutions\\nentirely. Experiments on two machine translation tasks show these models to\\nbe superior in quality while being more parallelizable and requiring significantly\\nless time to train. Our model achieves 28.4 BLEU on the WMT 2014 English-\\nto-German translation task, improving over the existing best results, including\\nensembles, by over 2 BLEU. On the WMT 2014 English-to-French translation task,\\nour model establishes a new single-model state-of-the-art BLEU score of 41.8 after\\ntraining for 3.5 days on eight GPUs, a small fraction of the training costs of the\\nbest models from the literature. We show that the Transformer generalizes well to\\nother tasks by applying it successfully to English constituency parsing both with\\nlarge and limited training data.\\n∗Equal contribution. Listing order is random. Jakob proposed replacing RNNs with self-attention and started\\nthe effort to evaluate this idea. Ashish, with Illia, designed and implemented the first Transformer models and\\nhas been crucially involved in every aspect of this work. Noam proposed scaled dot-product attention, multi-head\\nattention and the parameter-free position representation and became the other person involved in nearly every\\ndetail. Niki designed, implemented, tuned and evaluated countless model variants in our original codebase and\\ntensor2tensor. Llion also experimented with novel model variants, was responsible for our initial codebase, and\\nefficient inference and visualizations. Lukasz and Aidan spent countless long days designing various parts of and\\nimplementing tensor2tensor, replacing our earlier codebase, greatly improving results and massively accelerating\\nour research.\\n†Work performed while at Google Brain.\\n‡Work performed while at Google Research.\\n31st Conference on Neural Information Processing Systems (NIPS 2017), Long Beach, CA, USA.\\narXiv:1706.03762v7 [cs.CL] 2 Aug 2023\\n1\\nIntroduction\\nRecurrent neural networks, long short-term memory [13] and gated recurrent [7] neural networks\\nin particular, have been firmly established as state of the art approaches in sequence modeling and\\ntransduction problems such as language modeling and machine translation [35, 2, 5]. Numerous\\nefforts have since continued to push the boundaries of recurrent language models and encoder-decoder\\narchitectures [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−1 and the input for position t. This inherently\\nsequential nature precludes parallelization within training examples, which becomes critical at longer\\nsequence lengths, as memory constraints limit batching across examples. Recent work has achieved\\nsignificant improvements in computational efficiency through factorization tricks [21] and conditional\\ncomputation [32], while also improving model performance in case of the latter. The fundamental\\nconstraint of sequential computation, however, remains.\\nAttention mechanisms have become an integral part of compelling sequence modeling and transduc-\\ntion 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.\\nThe Transformer allows for significantly more parallelization and can reach a new state of the art in\\ntranslation quality after being trained for as little as twelve hours on eight P100 GPUs.\\n2\\nBackground\\nThe goal of reducing sequential computation also forms the foundation of the Extended Neural GPU\\n[16], ByteNet [18] and ConvS2S [9], all of which use convolutional neural networks as basic building\\nblock, computing hidden representations in parallel for all input and output positions. In these models,\\nthe number of operations required to relate signals from two arbitrary input or output positions grows\\nin the distance between positions, linearly for ConvS2S and logarithmically for ByteNet. This makes\\nit more difficult to learn dependencies between distant positions [12]. In the Transformer this is\\nreduced to a constant number of operations, albeit at the cost of reduced effective resolution due\\nto averaging attention-weighted positions, an effect we counteract with Multi-Head Attention as\\ndescribed in section 3.2.\\nSelf-attention, sometimes called intra-attention is an attention mechanism relating different positions\\nof a single sequence in order to compute a representation of the sequence. Self-attention has been\\nused successfully in a variety of tasks including reading comprehension, abstractive summarization,\\ntextual entailment and learning task-independent sentence representations [4, 27, 28, 22].\\nEnd-to-end memory networks are based on a recurrent attention mechanism instead of sequence-\\naligned recurrence and have been shown to perform well on simple-language question answering and\\nlanguage modeling tasks [34].\\nTo the best of our knowledge, however, the Transformer is the first transduction model relying\\nentirely on self-attention to compute representations of its input and output without using sequence-\\naligned RNNs or convolution. In the following sections, we will describe the Transformer, motivate\\nself-attention and discuss its advantages over models such as [17, 18] and [9].\\n3\\nModel Architecture\\nMost competitive neural sequence transduction models have an encoder-decoder structure [5, 2, 35].\\nHere, the encoder maps an input sequence of symbol representations (x1, ..., xn) to a sequence\\nof continuous representations z = (z1, ..., zn). Given z, the decoder then generates an output\\nsequence (y1, ..., ym) of symbols one element at a time. At each step the model is auto-regressive\\n[10], consuming the previously generated symbols as additional input when generating the next.\\n2\\nFigure 1: The Transformer - model architecture.\\nThe Transformer follows this overall architecture using stacked self-attention and point-wise, fully\\nconnected layers for both the encoder and decoder, shown in the left and right halves of Figure 1,\\nrespectively.\\n3.1\\nEncoder and Decoder Stacks\\nEncoder:\\nThe encoder is composed of a stack of N = 6 identical layers. Each layer has two\\nsub-layers. The first is a multi-head self-attention mechanism, and the second is a simple, position-\\nwise fully connected feed-forward network. We employ a residual connection [11] around each of\\nthe two sub-layers, followed by layer normalization [1]. That is, the output of each sub-layer is\\nLayerNorm(x + Sublayer(x)), where Sublayer(x) is the function implemented by the sub-layer\\nitself. To facilitate these residual connections, all sub-layers in the model, as well as the embedding\\nlayers, produce outputs of dimension dmodel = 512.\\nDecoder:\\nThe decoder is also composed of a stack of N = 6 identical layers. In addition to the two\\nsub-layers in each encoder layer, the decoder inserts a third sub-layer, which performs multi-head\\nattention over the output of the encoder stack. Similar to the encoder, we employ residual connections\\naround each of the sub-layers, followed by layer normalization. We also modify the self-attention\\nsub-layer in the decoder stack to prevent positions from attending to subsequent positions. This\\nmasking, combined with fact that the output embeddings are offset by one position, ensures that the\\npredictions for position i can depend only on the known outputs at positions less than i.\\n3.2\\nAttention\\nAn attention function can be described as mapping a query and a set of key-value pairs to an output,\\nwhere the query, keys, values, and output are all vectors. The output is computed as a weighted sum\\n3\\nScaled Dot-Product Attention\\nMulti-Head Attention\\nFigure 2: (left) Scaled Dot-Product Attention. (right) Multi-Head Attention consists of several\\nattention layers running in parallel.\\nof the values, where the weight assigned to each value is computed by a compatibility function of the\\nquery with the corresponding key.\\n3.2.1\\nScaled Dot-Product Attention\\nWe call our particular attention \"Scaled Dot-Product Attention\" (Figure 2). The input consists of\\nqueries and keys of dimension dk, and values of dimension dv. We compute the dot products of the\\nquery with all keys, divide each by √dk, and apply a softmax function to obtain the weights on the\\nvalues.\\nIn practice, we compute the attention function on a set of queries simultaneously, packed together\\ninto a matrix Q. The keys and values are also packed together into matrices K and V . We compute\\nthe matrix of outputs as:\\nAttention(Q, K, V ) = softmax(QKT\\n√dk\\n)V\\n(1)\\nThe two most commonly used attention functions are additive attention [2], and dot-product (multi-\\nplicative) attention. Dot-product attention is identical to our algorithm, except for the scaling factor\\nof\\n1\\n√dk . Additive attention computes the compatibility function using a feed-forward network with\\na single hidden layer. While the two are similar in theoretical complexity, dot-product attention is\\nmuch faster and more space-efficient in practice, since it can be implemented using highly optimized\\nmatrix multiplication code.\\nWhile for small values of dk the two mechanisms perform similarly, additive attention outperforms\\ndot product attention without scaling for larger values of dk [3]. We suspect that for large values of\\ndk, the dot products grow large in magnitude, pushing the softmax function into regions where it has\\nextremely small gradients 4. To counteract this effect, we scale the dot products by\\n1\\n√dk .\\n3.2.2\\nMulti-Head Attention\\nInstead of performing a single attention function with dmodel-dimensional keys, values and queries,\\nwe found it beneficial to linearly project the queries, keys and values h times with different, learned\\nlinear projections to dk, dk and dv dimensions, respectively. On each of these projected versions of\\nqueries, keys and values we then perform the attention function in parallel, yielding dv-dimensional\\n4To illustrate why the dot products get large, assume that the components of q and k are independent random\\nvariables with mean 0 and variance 1. Then their dot product, q · k = Pdk\\ni=1 qiki, has mean 0 and variance dk.\\n4\\noutput values. These are concatenated and once again projected, resulting in the final values, as\\ndepicted in Figure 2.\\nMulti-head attention allows the model to jointly attend to information from different representation\\nsubspaces at different positions. With a single attention head, averaging inhibits this.\\nMultiHead(Q, K, V ) = Concat(head1, ..., headh)W O\\nwhere headi = Attention(QW Q\\ni , KW K\\ni , V W V\\ni )\\nWhere the projections are parameter matrices W Q\\ni\\n∈Rdmodel×dk, W K\\ni\\n∈Rdmodel×dk, W V\\ni\\n∈Rdmodel×dv\\nand W O ∈Rhdv×dmodel.\\nIn this work we employ h = 8 parallel attention layers, or heads. For each of these we use\\ndk = dv = dmodel/h = 64. Due to the reduced dimension of each head, the total computational cost\\nis similar to that of single-head attention with full dimensionality.\\n3.2.3\\nApplications of Attention in our Model\\nThe Transformer uses multi-head attention in three different ways:\\n• In \"encoder-decoder attention\" layers, the queries come from the previous decoder layer,\\nand the memory keys and values come from the output of the encoder. This allows every\\nposition in the decoder to attend over all positions in the input sequence. This mimics the\\ntypical encoder-decoder attention mechanisms in sequence-to-sequence models such as\\n[38, 2, 9].\\n• The encoder contains self-attention layers. In a self-attention layer all of the keys, values\\nand queries come from the same place, in this case, the output of the previous layer in the\\nencoder. Each position in the encoder can attend to all positions in the previous layer of the\\nencoder.\\n• Similarly, self-attention layers in the decoder allow each position in the decoder to attend to\\nall positions in the decoder up to and including that position. We need to prevent leftward\\ninformation flow in the decoder to preserve the auto-regressive property. We implement this\\ninside of scaled dot-product attention by masking out (setting to −∞) all values in the input\\nof the softmax which correspond to illegal connections. See Figure 2.\\n3.3\\nPosition-wise Feed-Forward Networks\\nIn addition to attention sub-layers, each of the layers in our encoder and decoder contains a fully\\nconnected feed-forward network, which is applied to each position separately and identically. This\\nconsists of two linear transformations with a ReLU activation in between.\\nFFN(x) = max(0, xW1 + b1)W2 + b2\\n(2)\\nWhile the linear transformations are the same across different positions, they use different parameters\\nfrom layer to layer. Another way of describing this is as two convolutions with kernel size 1.\\nThe dimensionality of input and output is dmodel = 512, and the inner-layer has dimensionality\\ndff = 2048.\\n3.4\\nEmbeddings and Softmax\\nSimilarly to other sequence transduction models, we use learned embeddings to convert the input\\ntokens and output tokens to vectors of dimension dmodel. We also use the usual learned linear transfor-\\nmation and softmax function to convert the decoder output to predicted next-token probabilities. In\\nour model, we share the same weight matrix between the two embedding layers and the pre-softmax\\nlinear transformation, similar to [30]. In the embedding layers, we multiply those weights by √dmodel.\\n5\\nTable 1: Maximum path lengths, per-layer complexity and minimum number of sequential operations\\nfor different layer types. n is the sequence length, d is the representation dimension, k is the kernel\\nsize of convolutions and r the size of the neighborhood in restricted self-attention.\\nLayer Type\\nComplexity per Layer\\nSequential\\nMaximum Path Length\\nOperations\\nSelf-Attention\\nO(n2 · d)\\nO(1)\\nO(1)\\nRecurrent\\nO(n · d2)\\nO(n)\\nO(n)\\nConvolutional\\nO(k · n · d2)\\nO(1)\\nO(logk(n))\\nSelf-Attention (restricted)\\nO(r · n · d)\\nO(1)\\nO(n/r)\\n3.5\\nPositional Encoding\\nSince our model contains no recurrence and no convolution, in order for the model to make use of the\\norder of the sequence, we must inject some information about the relative or absolute position of the\\ntokens in the sequence. To this end, we add \"positional encodings\" to the input embeddings at the\\nbottoms of the encoder and decoder stacks. The positional encodings have the same dimension dmodel\\nas the embeddings, so that the two can be summed. There are many choices of positional encodings,\\nlearned and fixed [9].\\nIn this work, we use sine and cosine functions of different frequencies:\\nPE(pos,2i) = sin(pos/100002i/dmodel)\\nPE(pos,2i+1) = cos(pos/100002i/dmodel)\\nwhere pos is the position and i is the dimension. That is, each dimension of the positional encoding\\ncorresponds to a sinusoid. The wavelengths form a geometric progression from 2π to 10000 · 2π. We\\nchose this function because we hypothesized it would allow the model to easily learn to attend by\\nrelative positions, since for any fixed offset k, PEpos+k can be represented as a linear function of\\nPEpos.\\nWe also experimented with using learned positional embeddings [9] instead, and found that the two\\nversions produced nearly identical results (see Table 3 row (E)). We chose the sinusoidal version\\nbecause it may allow the model to extrapolate to sequence lengths longer than the ones encountered\\nduring training.\\n4\\nWhy Self-Attention\\nIn this section we compare various aspects of self-attention layers to the recurrent and convolu-\\ntional layers commonly used for mapping one variable-length sequence of symbol representations\\n(x1, ..., xn) to another sequence of equal length (z1, ..., zn), with xi, zi ∈Rd, such as a hidden\\nlayer in a typical sequence transduction encoder or decoder. Motivating our use of self-attention we\\nconsider three desiderata.\\nOne is the total computational complexity per layer. Another is the amount of computation that can\\nbe parallelized, as measured by the minimum number of sequential operations required.\\nThe third is the path length between long-range dependencies in the network. Learning long-range\\ndependencies is a key challenge in many sequence transduction tasks. One key factor affecting the\\nability to learn such dependencies is the length of the paths forward and backward signals have to\\ntraverse in the network. The shorter these paths between any combination of positions in the input\\nand output sequences, the easier it is to learn long-range dependencies [12]. Hence we also compare\\nthe maximum path length between any two input and output positions in networks composed of the\\ndifferent layer types.\\nAs noted in Table 1, a self-attention layer connects all positions with a constant number of sequentially\\nexecuted operations, whereas a recurrent layer requires O(n) sequential operations. In terms of\\ncomputational complexity, self-attention layers are faster than recurrent layers when the sequence\\n6\\nlength n is smaller than the representation dimensionality d, which is most often the case with\\nsentence representations used by state-of-the-art models in machine translations, such as word-piece\\n[38] and byte-pair [31] representations. To improve computational performance for tasks involving\\nvery long sequences, self-attention could be restricted to considering only a neighborhood of size r in\\nthe input sequence centered around the respective output position. This would increase the maximum\\npath length to O(n/r). We plan to investigate this approach further in future work.\\nA single convolutional layer with kernel width k < n does not connect all pairs of input and output\\npositions. Doing so requires a stack of O(n/k) convolutional layers in the case of contiguous kernels,\\nor O(logk(n)) in the case of dilated convolutions [18], increasing the length of the longest paths\\nbetween any two positions in the network. Convolutional layers are generally more expensive than\\nrecurrent layers, by a factor of k. Separable convolutions [6], however, decrease the complexity\\nconsiderably, to O(k · n · d + n · d2). Even with k = n, however, the complexity of a separable\\nconvolution is equal to the combination of a self-attention layer and a point-wise feed-forward layer,\\nthe approach we take in our model.\\nAs side benefit, self-attention could yield more interpretable models. We inspect attention distributions\\nfrom our models and present and discuss examples in the appendix. Not only do individual attention\\nheads clearly learn to perform different tasks, many appear to exhibit behavior related to the syntactic\\nand semantic structure of the sentences.\\n5\\nTraining\\nThis section describes the training regime for our models.\\n5.1\\nTraining Data and Batching\\nWe trained on the standard WMT 2014 English-German dataset consisting of about 4.5 million\\nsentence pairs. Sentences were encoded using byte-pair encoding [3], which has a shared source-\\ntarget vocabulary of about 37000 tokens. For English-French, we used the significantly larger WMT\\n2014 English-French dataset consisting of 36M sentences and split tokens into a 32000 word-piece\\nvocabulary [38]. Sentence pairs were batched together by approximate sequence length. Each training\\nbatch contained a set of sentence pairs containing approximately 25000 source tokens and 25000\\ntarget tokens.\\n5.2\\nHardware and Schedule\\nWe trained our models on one machine with 8 NVIDIA P100 GPUs. For our base models using\\nthe hyperparameters described throughout the paper, each training step took about 0.4 seconds. We\\ntrained the base models for a total of 100,000 steps or 12 hours. For our big models,(described on the\\nbottom line of table 3), step time was 1.0 seconds. The big models were trained for 300,000 steps\\n(3.5 days).\\n5.3\\nOptimizer\\nWe used the Adam optimizer [20] with β1 = 0.9, β2 = 0.98 and ϵ = 10−9. We varied the learning\\nrate over the course of training, according to the formula:\\nlrate = d−0.5\\nmodel · min(step_num−0.5, step_num · warmup_steps−1.5)\\n(3)\\nThis corresponds to increasing the learning rate linearly for the first warmup_steps training steps,\\nand decreasing it thereafter proportionally to the inverse square root of the step number. We used\\nwarmup_steps = 4000.\\n5.4\\nRegularization\\nWe employ three types of regularization during training:\\n7\\nTable 2: The Transformer achieves better BLEU scores than previous state-of-the-art models on the\\nEnglish-to-German and English-to-French newstest2014 tests at a fraction of the training cost.\\nModel\\nBLEU\\nTraining Cost (FLOPs)\\nEN-DE\\nEN-FR\\nEN-DE\\nEN-FR\\nByteNet [18]\\n23.75\\nDeep-Att + PosUnk [39]\\n39.2\\n1.0 · 1020\\nGNMT + RL [38]\\n24.6\\n39.92\\n2.3 · 1019\\n1.4 · 1020\\nConvS2S [9]\\n25.16\\n40.46\\n9.6 · 1018\\n1.5 · 1020\\nMoE [32]\\n26.03\\n40.56\\n2.0 · 1019\\n1.2 · 1020\\nDeep-Att + PosUnk Ensemble [39]\\n40.4\\n8.0 · 1020\\nGNMT + RL Ensemble [38]\\n26.30\\n41.16\\n1.8 · 1020\\n1.1 · 1021\\nConvS2S Ensemble [9]\\n26.36\\n41.29\\n7.7 · 1019\\n1.2 · 1021\\nTransformer (base model)\\n27.3\\n38.1\\n3.3 · 1018\\nTransformer (big)\\n28.4\\n41.8\\n2.3 · 1019\\nResidual Dropout\\nWe apply dropout [33] to the output of each sub-layer, before it is added to the\\nsub-layer input and normalized. In addition, we apply dropout to the sums of the embeddings and the\\npositional encodings in both the encoder and decoder stacks. For the base model, we use a rate of\\nPdrop = 0.1.\\nLabel Smoothing\\nDuring training, we employed label smoothing of value ϵls = 0.1 [36]. This\\nhurts perplexity, as the model learns to be more unsure, but improves accuracy and BLEU score.\\n6\\nResults\\n6.1\\nMachine Translation\\nOn the WMT 2014 English-to-German translation task, the big transformer model (Transformer (big)\\nin Table 2) outperforms the best previously reported models (including ensembles) by more than 2.0\\nBLEU, establishing a new state-of-the-art BLEU score of 28.4. The configuration of this model is\\nlisted in the bottom line of Table 3. Training took 3.5 days on 8 P100 GPUs. Even our base model\\nsurpasses all previously published models and ensembles, at a fraction of the training cost of any of\\nthe competitive models.\\nOn the WMT 2014 English-to-French translation task, our big model achieves a BLEU score of 41.0,\\noutperforming all of the previously published single models, at less than 1/4 the training cost of the\\nprevious state-of-the-art model. The Transformer (big) model trained for English-to-French used\\ndropout rate Pdrop = 0.1, instead of 0.3.\\nFor the base models, we used a single model obtained by averaging the last 5 checkpoints, which\\nwere written at 10-minute intervals. For the big models, we averaged the last 20 checkpoints. We\\nused beam search with a beam size of 4 and length penalty α = 0.6 [38]. These hyperparameters\\nwere chosen after experimentation on the development set. We set the maximum output length during\\ninference to input length + 50, but terminate early when possible [38].\\nTable 2 summarizes our results and compares our translation quality and training costs to other model\\narchitectures from the literature. We estimate the number of floating point operations used to train a\\nmodel by multiplying the training time, the number of GPUs used, and an estimate of the sustained\\nsingle-precision floating-point capacity of each GPU 5.\\n6.2\\nModel Variations\\nTo evaluate the importance of different components of the Transformer, we varied our base model\\nin different ways, measuring the change in performance on English-to-German translation on the\\n5We used values of 2.8, 3.7, 6.0 and 9.5 TFLOPS for K80, K40, M40 and P100, respectively.\\n8\\nTable 3: Variations on the Transformer architecture. Unlisted values are identical to those of the base\\nmodel. All metrics are on the English-to-German translation development set, newstest2013. Listed\\nperplexities are per-wordpiece, according to our byte-pair encoding, and should not be compared to\\nper-word perplexities.\\nN\\ndmodel\\ndff\\nh\\ndk\\ndv\\nPdrop\\nϵls\\ntrain\\nPPL\\nBLEU\\nparams\\nsteps\\n(dev)\\n(dev)\\n×106\\nbase\\n6\\n512\\n2048\\n8\\n64\\n64\\n0.1\\n0.1\\n100K\\n4.92\\n25.8\\n65\\n(A)\\n1\\n512\\n512\\n5.29\\n24.9\\n4\\n128\\n128\\n5.00\\n25.5\\n16\\n32\\n32\\n4.91\\n25.8\\n32\\n16\\n16\\n5.01\\n25.4\\n(B)\\n16\\n5.16\\n25.1\\n58\\n32\\n5.01\\n25.4\\n60\\n(C)\\n2\\n6.11\\n23.7\\n36\\n4\\n5.19\\n25.3\\n50\\n8\\n4.88\\n25.5\\n80\\n256\\n32\\n32\\n5.75\\n24.5\\n28\\n1024\\n128\\n128\\n4.66\\n26.0\\n168\\n1024\\n5.12\\n25.4\\n53\\n4096\\n4.75\\n26.2\\n90\\n(D)\\n0.0\\n5.77\\n24.6\\n0.2\\n4.95\\n25.5\\n0.0\\n4.67\\n25.3\\n0.2\\n5.47\\n25.7\\n(E)\\npositional embedding instead of sinusoids\\n4.92\\n25.7\\nbig\\n6\\n1024\\n4096\\n16\\n0.3\\n300K\\n4.33\\n26.4\\n213\\ndevelopment set, newstest2013. We used beam search as described in the previous section, but no\\ncheckpoint averaging. We present these results in Table 3.\\nIn Table 3 rows (A), we vary the number of attention heads and the attention key and value dimensions,\\nkeeping the amount of computation constant, as described in Section 3.2.2. While single-head\\nattention is 0.9 BLEU worse than the best setting, quality also drops off with too many heads.\\nIn Table 3 rows (B), we observe that reducing the attention key size dk hurts model quality. This\\nsuggests that determining compatibility is not easy and that a more sophisticated compatibility\\nfunction than dot product may be beneficial. We further observe in rows (C) and (D) that, as expected,\\nbigger models are better, and dropout is very helpful in avoiding over-fitting. In row (E) we replace our\\nsinusoidal positional encoding with learned positional embeddings [9], and observe nearly identical\\nresults to the base model.\\n6.3\\nEnglish Constituency Parsing\\nTo evaluate if the Transformer can generalize to other tasks we performed experiments on English\\nconstituency parsing. This task presents specific challenges: the output is subject to strong structural\\nconstraints and is significantly longer than the input. Furthermore, RNN sequence-to-sequence\\nmodels have not been able to attain state-of-the-art results in small-data regimes [37].\\nWe trained a 4-layer transformer with dmodel = 1024 on the Wall Street Journal (WSJ) portion of the\\nPenn Treebank [25], about 40K training sentences. We also trained it in a semi-supervised setting,\\nusing the larger high-confidence and BerkleyParser corpora from with approximately 17M sentences\\n[37]. We used a vocabulary of 16K tokens for the WSJ only setting and a vocabulary of 32K tokens\\nfor the semi-supervised setting.\\nWe performed only a small number of experiments to select the dropout, both attention and residual\\n(section 5.4), learning rates and beam size on the Section 22 development set, all other parameters\\nremained unchanged from the English-to-German base translation model. During inference, we\\n9\\nTable 4: The Transformer generalizes well to English constituency parsing (Results are on Section 23\\nof WSJ)\\nParser\\nTraining\\nWSJ 23 F1\\nVinyals & Kaiser el al. (2014) [37]\\nWSJ only, discriminative\\n88.3\\nPetrov et al. (2006) [29]\\nWSJ only, discriminative\\n90.4\\nZhu et al. (2013) [40]\\nWSJ only, discriminative\\n90.4\\nDyer et al. (2016) [8]\\nWSJ only, discriminative\\n91.7\\nTransformer (4 layers)\\nWSJ only, discriminative\\n91.3\\nZhu et al. (2013) [40]\\nsemi-supervised\\n91.3\\nHuang & Harper (2009) [14]\\nsemi-supervised\\n91.3\\nMcClosky et al. (2006) [26]\\nsemi-supervised\\n92.1\\nVinyals & Kaiser el al. (2014) [37]\\nsemi-supervised\\n92.1\\nTransformer (4 layers)\\nsemi-supervised\\n92.7\\nLuong et al. (2015) [23]\\nmulti-task\\n93.0\\nDyer et al. (2016) [8]\\ngenerative\\n93.3\\nincreased the maximum output length to input length + 300. We used a beam size of 21 and α = 0.3\\nfor both WSJ only and the semi-supervised setting.\\nOur results in Table 4 show that despite the lack of task-specific tuning our model performs sur-\\nprisingly well, yielding better results than all previously reported models with the exception of the\\nRecurrent Neural Network Grammar [8].\\nIn contrast to RNN sequence-to-sequence models [37], the Transformer outperforms the Berkeley-\\nParser [29] even when training only on the WSJ training set of 40K sentences.\\n7\\nConclusion\\nIn this work, we presented the Transformer, the first sequence transduction model based entirely on\\nattention, replacing the recurrent layers most commonly used in encoder-decoder architectures with\\nmulti-headed self-attention.\\nFor translation tasks, the Transformer can be trained significantly faster than architectures based\\non recurrent or convolutional layers. On both WMT 2014 English-to-German and WMT 2014\\nEnglish-to-French translation tasks, we achieve a new state of the art. In the former task our best\\nmodel outperforms even all previously reported ensembles.\\nWe are excited about the future of attention-based models and plan to apply them to other tasks. We\\nplan to extend the Transformer to problems involving input and output modalities other than text and\\nto investigate local, restricted attention mechanisms to efficiently handle large inputs and outputs\\nsuch as images, audio and video. Making generation less sequential is another research goals of ours.\\nThe code we used to train and evaluate our models is available at https://github.com/\\ntensorflow/tensor2tensor.\\nAcknowledgements\\nWe are grateful to Nal Kalchbrenner and Stephan Gouws for their fruitful\\ncomments, corrections and inspiration.\\nReferences\\n[1] Jimmy Lei Ba, Jamie Ryan Kiros, and Geoffrey E Hinton. Layer normalization. arXiv preprint\\narXiv:1607.06450, 2016.\\n[2] Dzmitry Bahdanau, Kyunghyun Cho, and Yoshua Bengio. Neural machine translation by jointly\\nlearning to align and translate. CoRR, abs/1409.0473, 2014.\\n[3] Denny Britz, Anna Goldie, Minh-Thang Luong, and Quoc V. Le. Massive exploration of neural\\nmachine translation architectures. CoRR, abs/1703.03906, 2017.\\n[4] Jianpeng Cheng, Li Dong, and Mirella Lapata. Long short-term memory-networks for machine\\nreading. arXiv preprint arXiv:1601.06733, 2016.\\n10\\n[5] Kyunghyun Cho, Bart van Merrienboer, Caglar Gulcehre, Fethi Bougares, Holger Schwenk,\\nand Yoshua Bengio. Learning phrase representations using rnn encoder-decoder for statistical\\nmachine translation. CoRR, abs/1406.1078, 2014.\\n[6] Francois Chollet. Xception: Deep learning with depthwise separable convolutions. arXiv\\npreprint arXiv:1610.02357, 2016.\\n[7] Junyoung Chung, Çaglar Gülçehre, Kyunghyun Cho, and Yoshua Bengio. Empirical evaluation\\nof gated recurrent neural networks on sequence modeling. CoRR, abs/1412.3555, 2014.\\n[8] Chris Dyer, Adhiguna Kuncoro, Miguel Ballesteros, and Noah A. Smith. Recurrent neural\\nnetwork grammars. In Proc. of NAACL, 2016.\\n[9] Jonas Gehring, Michael Auli, David Grangier, Denis Yarats, and Yann N. Dauphin. Convolu-\\ntional sequence to sequence learning. arXiv preprint arXiv:1705.03122v2, 2017.\\n[10] Alex Graves.\\nGenerating sequences with recurrent neural networks.\\narXiv preprint\\narXiv:1308.0850, 2013.\\n[11] Kaiming He, Xiangyu Zhang, Shaoqing Ren, and Jian Sun. Deep residual learning for im-\\nage recognition. In Proceedings of the IEEE Conference on Computer Vision and Pattern\\nRecognition, pages 770–778, 2016.\\n[12] Sepp Hochreiter, Yoshua Bengio, Paolo Frasconi, and Jürgen Schmidhuber. Gradient flow in\\nrecurrent nets: the difficulty of learning long-term dependencies, 2001.\\n[13] Sepp Hochreiter and Jürgen Schmidhuber. Long short-term memory. Neural computation,\\n9(8):1735–1780, 1997.\\n[14] Zhongqiang Huang and Mary Harper. Self-training PCFG grammars with latent annotations\\nacross languages. In Proceedings of the 2009 Conference on Empirical Methods in Natural\\nLanguage Processing, pages 832–841. ACL, August 2009.\\n[15] Rafal Jozefowicz, Oriol Vinyals, Mike Schuster, Noam Shazeer, and Yonghui Wu. Exploring\\nthe limits of language modeling. arXiv preprint arXiv:1602.02410, 2016.\\n[16] Łukasz Kaiser and Samy Bengio. Can active memory replace attention? In Advances in Neural\\nInformation Processing Systems, (NIPS), 2016.\\n[17] Łukasz Kaiser and Ilya Sutskever. Neural GPUs learn algorithms. In International Conference\\non Learning Representations (ICLR), 2016.\\n[18] Nal Kalchbrenner, Lasse Espeholt, Karen Simonyan, Aaron van den Oord, Alex Graves, and Ko-\\nray Kavukcuoglu. Neural machine translation in linear time. arXiv preprint arXiv:1610.10099v2,\\n2017.\\n[19] Yoon Kim, Carl Denton, Luong Hoang, and Alexander M. Rush. Structured attention networks.\\nIn International Conference on Learning Representations, 2017.\\n[20] Diederik Kingma and Jimmy Ba. Adam: A method for stochastic optimization. In ICLR, 2015.\\n[21] Oleksii Kuchaiev and Boris Ginsburg. Factorization tricks for LSTM networks. arXiv preprint\\narXiv:1703.10722, 2017.\\n[22] Zhouhan Lin, Minwei Feng, Cicero Nogueira dos Santos, Mo Yu, Bing Xiang, Bowen\\nZhou, and Yoshua Bengio. A structured self-attentive sentence embedding. arXiv preprint\\narXiv:1703.03130, 2017.\\n[23] Minh-Thang Luong, Quoc V. Le, Ilya Sutskever, Oriol Vinyals, and Lukasz Kaiser. Multi-task\\nsequence to sequence learning. arXiv preprint arXiv:1511.06114, 2015.\\n[24] Minh-Thang Luong, Hieu Pham, and Christopher D Manning. Effective approaches to attention-\\nbased neural machine translation. arXiv preprint arXiv:1508.04025, 2015.\\n11\\n[25] Mitchell P Marcus, Mary Ann Marcinkiewicz, and Beatrice Santorini. Building a large annotated\\ncorpus of english: The penn treebank. Computational linguistics, 19(2):313–330, 1993.\\n[26] David McClosky, Eugene Charniak, and Mark Johnson. Effective self-training for parsing. In\\nProceedings of the Human Language Technology Conference of the NAACL, Main Conference,\\npages 152–159. ACL, June 2006.\\n[27] Ankur Parikh, Oscar Täckström, Dipanjan Das, and Jakob Uszkoreit. A decomposable attention\\nmodel. In Empirical Methods in Natural Language Processing, 2016.\\n[28] Romain Paulus, Caiming Xiong, and Richard Socher. A deep reinforced model for abstractive\\nsummarization. arXiv preprint arXiv:1705.04304, 2017.\\n[29] Slav Petrov, Leon Barrett, Romain Thibaux, and Dan Klein. Learning accurate, compact,\\nand interpretable tree annotation. In Proceedings of the 21st International Conference on\\nComputational Linguistics and 44th Annual Meeting of the ACL, pages 433–440. ACL, July\\n2006.\\n[30] Ofir Press and Lior Wolf. Using the output embedding to improve language models. arXiv\\npreprint arXiv:1608.05859, 2016.\\n[31] Rico Sennrich, Barry Haddow, and Alexandra Birch. Neural machine translation of rare words\\nwith subword units. arXiv preprint arXiv:1508.07909, 2015.\\n[32] Noam Shazeer, Azalia Mirhoseini, Krzysztof Maziarz, Andy Davis, Quoc Le, Geoffrey Hinton,\\nand Jeff Dean. Outrageously large neural networks: The sparsely-gated mixture-of-experts\\nlayer. arXiv preprint arXiv:1701.06538, 2017.\\n[33] Nitish Srivastava, Geoffrey E Hinton, Alex Krizhevsky, Ilya Sutskever, and Ruslan Salakhutdi-\\nnov. Dropout: a simple way to prevent neural networks from overfitting. Journal of Machine\\nLearning Research, 15(1):1929–1958, 2014.\\n[34] Sainbayar Sukhbaatar, Arthur Szlam, Jason Weston, and Rob Fergus. End-to-end memory\\nnetworks. In C. Cortes, N. D. Lawrence, D. D. Lee, M. Sugiyama, and R. Garnett, editors,\\nAdvances in Neural Information Processing Systems 28, pages 2440–2448. Curran Associates,\\nInc., 2015.\\n[35] Ilya Sutskever, Oriol Vinyals, and Quoc VV Le. Sequence to sequence learning with neural\\nnetworks. In Advances in Neural Information Processing Systems, pages 3104–3112, 2014.\\n[36] Christian Szegedy, Vincent Vanhoucke, Sergey Ioffe, Jonathon Shlens, and Zbigniew Wojna.\\nRethinking the inception architecture for computer vision. CoRR, abs/1512.00567, 2015.\\n[37] Vinyals & Kaiser, Koo, Petrov, Sutskever, and Hinton. Grammar as a foreign language. In\\nAdvances in Neural Information Processing Systems, 2015.\\n[38] Yonghui Wu, Mike Schuster, Zhifeng Chen, Quoc V Le, Mohammad Norouzi, Wolfgang\\nMacherey, Maxim Krikun, Yuan Cao, Qin Gao, Klaus Macherey, et al. Google’s neural machine\\ntranslation system: Bridging the gap between human and machine translation. arXiv preprint\\narXiv:1609.08144, 2016.\\n[39] Jie Zhou, Ying Cao, Xuguang Wang, Peng Li, and Wei Xu. Deep recurrent models with\\nfast-forward connections for neural machine translation. CoRR, abs/1606.04199, 2016.\\n[40] Muhua Zhu, Yue Zhang, Wenliang Chen, Min Zhang, and Jingbo Zhu. Fast and accurate\\nshift-reduce constituent parsing. In Proceedings of the 51st Annual Meeting of the ACL (Volume\\n1: Long Papers), pages 434–443. ACL, August 2013.\\n12\\nAttention Visualizations\\nIt\\nis\\nin\\nthis\\nspirit\\nthat\\na\\nmajority\\nof\\nAmerican\\ngovernments\\nhave\\npassed\\nnew\\nlaws\\nsince\\n2009\\nmaking\\nthe\\nregistration\\nor\\nvoting\\nprocess\\nmore\\ndifficult\\n.\\n\\n\\n\\n\\n\\n\\n\\nIt\\nis\\nin\\nthis\\nspirit\\nthat\\na\\nmajority\\nof\\nAmerican\\ngovernments\\nhave\\npassed\\nnew\\nlaws\\nsince\\n2009\\nmaking\\nthe\\nregistration\\nor\\nvoting\\nprocess\\nmore\\ndifficult\\n.\\n\\n\\n\\n\\n\\n\\n\\nFigure 3: An example of the attention mechanism following long-distance dependencies in the\\nencoder self-attention in layer 5 of 6. Many of the attention heads attend to a distant dependency of\\nthe verb ‘making’, completing the phrase ‘making...more difficult’. Attentions here shown only for\\nthe word ‘making’. Different colors represent different heads. Best viewed in color.\\n13\\nThe\\nLaw\\nwill\\nnever\\nbe\\nperfect\\n,\\nbut\\nits\\napplication\\nshould\\nbe\\njust\\n-\\nthis\\nis\\nwhat\\nwe\\nare\\nmissing\\n,\\nin\\nmy\\nopinion\\n.\\n\\n\\nThe\\nLaw\\nwill\\nnever\\nbe\\nperfect\\n,\\nbut\\nits\\napplication\\nshould\\nbe\\njust\\n-\\nthis\\nis\\nwhat\\nwe\\nare\\nmissing\\n,\\nin\\nmy\\nopinion\\n.\\n\\n\\nThe\\nLaw\\nwill\\nnever\\nbe\\nperfect\\n,\\nbut\\nits\\napplication\\nshould\\nbe\\njust\\n-\\nthis\\nis\\nwhat\\nwe\\nare\\nmissing\\n,\\nin\\nmy\\nopinion\\n.\\n\\n\\nThe\\nLaw\\nwill\\nnever\\nbe\\nperfect\\n,\\nbut\\nits\\napplication\\nshould\\nbe\\njust\\n-\\nthis\\nis\\nwhat\\nwe\\nare\\nmissing\\n,\\nin\\nmy\\nopinion\\n.\\n\\n\\nFigure 4: Two attention heads, also in layer 5 of 6, apparently involved in anaphora resolution. Top:\\nFull attentions for head 5. Bottom: Isolated attentions from just the word ‘its’ for attention heads 5\\nand 6. Note that the attentions are very sharp for this word.\\n14\\nThe\\nLaw\\nwill\\nnever\\nbe\\nperfect\\n,\\nbut\\nits\\napplication\\nshould\\nbe\\njust\\n-\\nthis\\nis\\nwhat\\nwe\\nare\\nmissing\\n,\\nin\\nmy\\nopinion\\n.\\n\\n\\nThe\\nLaw\\nwill\\nnever\\nbe\\nperfect\\n,\\nbut\\nits\\napplication\\nshould\\nbe\\njust\\n-\\nthis\\nis\\nwhat\\nwe\\nare\\nmissing\\n,\\nin\\nmy\\nopinion\\n.\\n\\n\\nThe\\nLaw\\nwill\\nnever\\nbe\\nperfect\\n,\\nbut\\nits\\napplication\\nshould\\nbe\\njust\\n-\\nthis\\nis\\nwhat\\nwe\\nare\\nmissing\\n,\\nin\\nmy\\nopinion\\n.\\n\\n\\nThe\\nLaw\\nwill\\nnever\\nbe\\nperfect\\n,\\nbut\\nits\\napplication\\nshould\\nbe\\njust\\n-\\nthis\\nis\\nwhat\\nwe\\nare\\nmissing\\n,\\nin\\nmy\\nopinion\\n.\\n\\n\\nFigure 5: Many of the attention heads exhibit behaviour that seems related to the structure of the\\nsentence. We give two such examples above, from two different heads from the encoder self-attention\\nat layer 5 of 6. The heads clearly learned to perform different tasks.\\n15\\n')]" 251 | ] 252 | }, 253 | "execution_count": 20, 254 | "metadata": {}, 255 | "output_type": "execute_result" 256 | } 257 | ], 258 | "source": [ 259 | "loader.load()" 260 | ] 261 | }, 262 | { 263 | "cell_type": "code", 264 | "execution_count": 21, 265 | "metadata": {}, 266 | "outputs": [], 267 | "source": [ 268 | "from langchain_community.document_loaders import WikipediaLoader" 269 | ] 270 | }, 271 | { 272 | "cell_type": "code", 273 | "execution_count": 22, 274 | "metadata": {}, 275 | "outputs": [], 276 | "source": [ 277 | "docs = WikipediaLoader(query=\"Generative AI\", load_max_docs=2)" 278 | ] 279 | }, 280 | { 281 | "cell_type": "code", 282 | "execution_count": 25, 283 | "metadata": {}, 284 | "outputs": [ 285 | { 286 | "data": { 287 | "text/plain": [ 288 | "[Document(metadata={'title': 'Generative artificial intelligence', 'summary': 'Generative artificial intelligence (generative AI, GenAI, or GAI) is a subset of artificial intelligence that uses generative models to produce text, images, videos, or other forms of data. These models learn the underlying patterns and structures of their training data and use them to produce new data based on the input, which often comes in the form of natural language prompts. \\nImprovements in transformer-based deep neural networks, particularly large language models (LLMs), enabled an AI boom of generative AI systems in the early 2020s. These include chatbots such as ChatGPT, Copilot, Gemini, and LLaMA; text-to-image artificial intelligence image generation systems such as Stable Diffusion, Midjourney, and DALL-E; and text-to-video AI generators such as Sora. Companies such as OpenAI, Anthropic, Microsoft, Google, and Baidu as well as numerous smaller firms have developed generative AI models.\\nGenerative AI has uses across a wide range of industries, including software development, healthcare, finance, entertainment, customer service, sales and marketing, art, writing, fashion, and product design. However, concerns have been raised about the potential misuse of generative AI such as cybercrime, the use of fake news or deepfakes to deceive or manipulate people, and the mass replacement of human jobs. Intellectual property law concerns also exist around generative models that are trained on and emulate copyrighted works of art.', 'source': 'https://en.wikipedia.org/wiki/Generative_artificial_intelligence'}, page_content='Generative artificial intelligence (generative AI, GenAI, or GAI) is a subset of artificial intelligence that uses generative models to produce text, images, videos, or other forms of data. These models learn the underlying patterns and structures of their training data and use them to produce new data based on the input, which often comes in the form of natural language prompts. \\nImprovements in transformer-based deep neural networks, particularly large language models (LLMs), enabled an AI boom of generative AI systems in the early 2020s. These include chatbots such as ChatGPT, Copilot, Gemini, and LLaMA; text-to-image artificial intelligence image generation systems such as Stable Diffusion, Midjourney, and DALL-E; and text-to-video AI generators such as Sora. Companies such as OpenAI, Anthropic, Microsoft, Google, and Baidu as well as numerous smaller firms have developed generative AI models.\\nGenerative AI has uses across a wide range of industries, including software development, healthcare, finance, entertainment, customer service, sales and marketing, art, writing, fashion, and product design. However, concerns have been raised about the potential misuse of generative AI such as cybercrime, the use of fake news or deepfakes to deceive or manipulate people, and the mass replacement of human jobs. Intellectual property law concerns also exist around generative models that are trained on and emulate copyrighted works of art.\\n\\n\\n== History ==\\n\\n\\n=== Early history ===\\nSince its inception, researchers in the field have raised philosophical and ethical arguments about the nature of the human mind and the consequences of creating artificial beings with human-like intelligence; these issues have previously been explored by myth, fiction and philosophy since antiquity. The concept of automated art dates back at least to the automata of ancient Greek civilization, where inventors such as Daedalus and Hero of Alexandria were described as having designed machines capable of writing text, generating sounds, and playing music. The tradition of creative automations has flourished throughout history, exemplified by Maillardet\\'s automaton created in the early 1800s. Markov chains have long been used to model natural languages since their development by Russian mathematician Andrey Markov in the early 20th century. Markov published his first paper on the topic in 1906, and analyzed the pattern of vowels and consonants in the novel Eugeny Onegin using Markov chains. Once a Markov chain is learned on a text corpus, it can then be used as a probabilistic text generator.\\n\\n\\n=== Academic artificial intelligence ===\\nThe academic discipline of artificial intelligence was established at a research workshop held at Dartmouth College in 1956 and has experienced several waves of advancement and optimism in the decades since. Artificial Intelligence research began in the 1950s with works like Computing Machinery and Intelligence (1950) and the 1956 Dartmouth Summer Research Project on AI. Since the 1950s, artists and researchers have used artificial intelligence to create artistic works. By the early 1970s, Harold Cohen was creating and exhibiting generative AI works created by AARON, the computer program Cohen created to generate paintings.\\nThe terms generative AI planning or generative planning were used in the 1980s and 1990s to refer to AI planning systems, especially computer-aided process planning, used to generate sequences of actions to reach a specified goal. Generative AI planning systems used symbolic AI methods such as state space search and constraint satisfaction and were a \"relatively mature\" technology by the early 1990s. They were used to generate crisis action plans for military use, process plans for manufacturing and decision plans such as in prototype autonomous spacecraft.\\n\\n\\n=== Generative neural nets (2014-2019) ===\\n\\nSince its inception, the field of machine learning used both discriminative models and generative models, to mode'),\n", 289 | " Document(metadata={'title': 'Generative AI pornography', 'summary': 'Generative AI pornography or simply AI pornography refers to digitally created explicit content produced through generative artificial intelligence (AI) technologies. Unlike traditional pornography, which involves real actors and cameras, this content is synthesized entirely by AI algorithms. These algorithms, including Generative adversarial network (GANs) and text-to-image models, generate lifelike images, videos, or animations from textual descriptions or datasets.\\n\\n', 'source': 'https://en.wikipedia.org/wiki/Generative_AI_pornography'}, page_content='Generative AI pornography or simply AI pornography refers to digitally created explicit content produced through generative artificial intelligence (AI) technologies. Unlike traditional pornography, which involves real actors and cameras, this content is synthesized entirely by AI algorithms. These algorithms, including Generative adversarial network (GANs) and text-to-image models, generate lifelike images, videos, or animations from textual descriptions or datasets.\\n\\n\\n== History ==\\nThe use of generative AI in the adult industry began in the late 2010s, initially focusing on AI-generated art, music, and visual content. This trend accelerated in 2022 with Stability AI\\'s release of Stable Diffusion (SD), an open-source text-to-image model that enables users to generate images, including NSFW content, from text prompts using the LAION-Aesthetics subset of the LAION-5B dataset. Despite Stability AI\\'s warnings against sexual imagery, SD\\'s public release led to dedicated communities exploring both artistic and explicit content, sparking ethical debates over open-access AI and its use in adult media. By 2020, AI tools had advanced to generate highly realistic adult content, amplifying calls for regulation.\\n\\n\\n=== AI-generated influencers ===\\nOne application of generative AI technology is the creation of AI-generated influencers on platforms such as OnlyFans and Instagram. These AI personas interact with users in ways that can mimic real human engagement, offering an entirely synthetic but convincing experience. While popular among niche audiences, these virtual influencers have prompted discussions about authenticity, consent, and the blurring line between human and AI-generated content, especially in adult entertainment.\\n\\n\\n=== The growth of AI porn sites ===\\nBy 2023, websites dedicated to AI-generated adult content had gained traction, catering to audiences seeking customizable experiences. These platforms allow users to create or view AI-generated pornography tailored to their preferences. These platforms enable users to create or view AI-generated adult content appealing to different preferences through prompts and tags, customizing body type, facial features, and art styles. Tags further refine the output, creating niche and diverse content. Many sites feature extensive image libraries and continuous content feeds, combining personalization with discovery and enhancing user engagement. AI porn sites, therefore, attract those seeking unique or niche experiences, sparking debates on creativity and the ethical boundaries of AI in adult media.\\n\\n\\n== Ethical concerns and misuse ==\\nThe growth of generative AI pornography has also attracted some cause for criticism. AI technology can be exploited to create non-consensual pornographic material, posing risks similar to those seen with deepfake revenge porn and AI-generated NCII (Non-Consensual Intimate Image). A 2023 analysis found that 98% of deepfake videos online are pornographic, with 99% of the victims being women. Some famous celebrities victims of deepfake include Scarlett Johansson, Taylor Swift, and Maisie Williams.\\nOpenAI is exploring whether NSFW content, such as erotica, can be responsibly generated in age-appropriate contexts while maintaining its ban on deepfakes. This proposal has attracted criticism from child safety campaigners who argue it undermines OpenAI\\'s mission to develop \"safe and beneficial\" AI. Additionally, the Internet Watch Foundation has raised concerns about AI being used to generate sexual abuse content involving children.\\n\\n\\n=== AI-generated NCII (AI Undress) ===\\nSeveral US states are taking actions against using deepfake apps and sharing them on the internet. In 2024, San Francisco filed a landmark lawsuit to shut down \"undress\" apps that allow users to generate non-consensual AI nude images, citing violations of state laws. The case aligns with California\\'s recent legislation—SB 926, SB 942, and SB 981—championed by Senators Aisha Wahab and Josh Becker an')]" 290 | ] 291 | }, 292 | "execution_count": 25, 293 | "metadata": {}, 294 | "output_type": "execute_result" 295 | } 296 | ], 297 | "source": [ 298 | "docs.load()" 299 | ] 300 | } 301 | ], 302 | "metadata": { 303 | "kernelspec": { 304 | "display_name": "Python 3", 305 | "language": "python", 306 | "name": "python3" 307 | }, 308 | "language_info": { 309 | "codemirror_mode": { 310 | "name": "ipython", 311 | "version": 3 312 | }, 313 | "file_extension": ".py", 314 | "mimetype": "text/x-python", 315 | "name": "python", 316 | "nbconvert_exporter": "python", 317 | "pygments_lexer": "ipython3", 318 | "version": "3.10.0" 319 | } 320 | }, 321 | "nbformat": 4, 322 | "nbformat_minor": 2 323 | } 324 | -------------------------------------------------------------------------------- /1.0-data_ingestion/data/GenAi Assignment.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EniolaAdemola/langchain-basics/d0c47b35f41ac7810b4ed21015bca04e8012baa8/1.0-data_ingestion/data/GenAi Assignment.pdf -------------------------------------------------------------------------------- /1.0-data_ingestion/data/speech.txt: -------------------------------------------------------------------------------- 1 | Hello! 2 | My name is DAVEWORLD, and I am excited to be your tutor for this program. 3 | 4 | I am a Data Scientist, Open-source Contributor, and Frontend Engineer with years of experience in the tech industry. Throughout my career, I’ve been privileged to build innovative products, solve real-world problems, and empower over 5,000 individuals to grow into better versions of themselves. My work and insights have also connected me with an audience of over 10,000 followers across social media platforms. 5 | Looking ahead, my vision is to create groundbreaking projects and support millions—if not billions—of people in achieving their goals and thriving in the tech industry. 6 | 7 | In this program, I am here to guide you every step of the way, ensuring you have an unforgettable learning experience. 8 | Let’s embark on this transformative journey together! -------------------------------------------------------------------------------- /2.0-data-transformation/2.2-character_text_splitter.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "#### How to split by character-Character Text Splitter\n", 8 | "This is the simplest method. This splits based on a given character sequence, which defaults to \"\\n\\n\". Chunk length is measured by number of characters.\n", 9 | "\n", 10 | "1. How the text is split: by single character separator.\n", 11 | "2. How the chunk size is measured: by number of characters." 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 6, 17 | "metadata": {}, 18 | "outputs": [ 19 | { 20 | "data": { 21 | "text/plain": [ 22 | "[Document(metadata={'source': './data/speech.txt'}, page_content='Hello!\\nMy name is DAVEWORLD, and I am excited to be your tutor for this program.\\n\\n\\nI am a Data Scientist, Open-source Contributor, and Frontend Engineer with years of experience in the tech industry. Throughout my career, I’ve been privileged to build innovative products, solve real-world problems, and empower over 5,000 individuals to grow into better versions of themselves. My work and insights have also connected me with an audience of over 10,000 followers across social media platforms.\\nLooking ahead, my vision is to create groundbreaking projects and support millions—if not billions—of people in achieving their goals and thriving in the tech industry.\\n\\n\\nIn this program, I am here to guide you every step of the way, ensuring you have an unforgettable learning experience.\\nLet’s embark on this transformative journey together!')]" 23 | ] 24 | }, 25 | "execution_count": 6, 26 | "metadata": {}, 27 | "output_type": "execute_result" 28 | } 29 | ], 30 | "source": [ 31 | "from langchain_community.document_loaders import TextLoader\n", 32 | "loader = TextLoader(\"./data/speech.txt\")\n", 33 | "text_document = loader.load()\n", 34 | "text_document" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": 10, 40 | "metadata": {}, 41 | "outputs": [ 42 | { 43 | "name": "stderr", 44 | "output_type": "stream", 45 | "text": [ 46 | "Created a chunk of size 412, which is longer than the specified 100\n", 47 | "Created a chunk of size 168, which is longer than the specified 100\n", 48 | "Created a chunk of size 118, which is longer than the specified 100\n" 49 | ] 50 | }, 51 | { 52 | "data": { 53 | "text/plain": [ 54 | "[Document(metadata={'source': './data/speech.txt'}, page_content='Hello!\\nMy name is DAVEWORLD, and I am excited to be your tutor for this program.'),\n", 55 | " Document(metadata={'source': './data/speech.txt'}, page_content='I am a Data Scientist, Open-source Contributor, and Frontend Engineer with years of experience in the tech industry. Throughout my career, I’ve been privileged to build innovative products, solve real-world problems, and empower over 5,000 individuals to grow into better versions of themselves. My work and insights have also connected me with an audience of over 10,000 followers across social media platforms.'),\n", 56 | " Document(metadata={'source': './data/speech.txt'}, page_content='Looking ahead, my vision is to create groundbreaking projects and support millions—if not billions—of people in achieving their goals and thriving in the tech industry.'),\n", 57 | " Document(metadata={'source': './data/speech.txt'}, page_content='In this program, I am here to guide you every step of the way, ensuring you have an unforgettable learning experience.'),\n", 58 | " Document(metadata={'source': './data/speech.txt'}, page_content='Let’s embark on this transformative journey together!')]" 59 | ] 60 | }, 61 | "execution_count": 10, 62 | "metadata": {}, 63 | "output_type": "execute_result" 64 | } 65 | ], 66 | "source": [ 67 | "from langchain_text_splitters import CharacterTextSplitter\n", 68 | "text_splitter = CharacterTextSplitter(separator=\"\\n\", chunk_size=100, chunk_overlap=20)\n", 69 | "text_splitter.split_documents(text_document)" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 14, 75 | "metadata": {}, 76 | "outputs": [ 77 | { 78 | "name": "stderr", 79 | "output_type": "stream", 80 | "text": [ 81 | "Created a chunk of size 581, which is longer than the specified 200\n" 82 | ] 83 | }, 84 | { 85 | "data": { 86 | "text/plain": [ 87 | "[Document(metadata={}, page_content='Hello!\\nMy name is DAVEWORLD, and I am excited to be your tutor for this program.'),\n", 88 | " Document(metadata={}, page_content='I am a Data Scientist, Open-source Contributor, and Frontend Engineer with years of experience in the tech industry. Throughout my career, I’ve been privileged to build innovative products, solve real-world problems, and empower over 5,000 individuals to grow into better versions of themselves. My work and insights have also connected me with an audience of over 10,000 followers across social media platforms.\\nLooking ahead, my vision is to create groundbreaking projects and support millions—if not billions—of people in achieving their goals and thriving in the tech industry.'),\n", 89 | " Document(metadata={}, page_content='In this program, I am here to guide you every step of the way, ensuring you have an unforgettable learning experience.\\nLet’s embark on this transformative journey together!')]" 90 | ] 91 | }, 92 | "execution_count": 14, 93 | "metadata": {}, 94 | "output_type": "execute_result" 95 | } 96 | ], 97 | "source": [ 98 | "speach = \"\"\n", 99 | "with open(\"./data/speech.txt\") as file:\n", 100 | " speach = file.read()\n", 101 | "text_spitter = CharacterTextSplitter(chunk_size=200, chunk_overlap=50)\n", 102 | "final_doc = text_spitter.create_documents([speach])\n", 103 | "final_doc" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": null, 109 | "metadata": {}, 110 | "outputs": [], 111 | "source": [] 112 | } 113 | ], 114 | "metadata": { 115 | "kernelspec": { 116 | "display_name": "Python 3", 117 | "language": "python", 118 | "name": "python3" 119 | }, 120 | "language_info": { 121 | "codemirror_mode": { 122 | "name": "ipython", 123 | "version": 3 124 | }, 125 | "file_extension": ".py", 126 | "mimetype": "text/x-python", 127 | "name": "python", 128 | "nbconvert_exporter": "python", 129 | "pygments_lexer": "ipython3", 130 | "version": "3.10.0" 131 | } 132 | }, 133 | "nbformat": 4, 134 | "nbformat_minor": 2 135 | } 136 | -------------------------------------------------------------------------------- /2.0-data-transformation/data/attention.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EniolaAdemola/langchain-basics/d0c47b35f41ac7810b4ed21015bca04e8012baa8/2.0-data-transformation/data/attention.pdf -------------------------------------------------------------------------------- /2.0-data-transformation/data/speech.txt: -------------------------------------------------------------------------------- 1 | Hello! 2 | My name is DAVEWORLD, and I am excited to be your tutor for this program. 3 | 4 | I am a Data Scientist, Open-source Contributor, and Frontend Engineer with years of experience in the tech industry. Throughout my career, I’ve been privileged to build innovative products, solve real-world problems, and empower over 5,000 individuals to grow into better versions of themselves. My work and insights have also connected me with an audience of over 10,000 followers across social media platforms. 5 | Looking ahead, my vision is to create groundbreaking projects and support millions—if not billions—of people in achieving their goals and thriving in the tech industry. 6 | 7 | In this program, I am here to guide you every step of the way, ensuring you have an unforgettable learning experience. 8 | Let’s embark on this transformative journey together! -------------------------------------------------------------------------------- /3.0-embeddings/3.3-huggingface_embedding.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "#### Embedding Techniques Using HuggingFace" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 2, 13 | "metadata": {}, 14 | "outputs": [ 15 | { 16 | "data": { 17 | "text/plain": [ 18 | "True" 19 | ] 20 | }, 21 | "execution_count": 2, 22 | "metadata": {}, 23 | "output_type": "execute_result" 24 | } 25 | ], 26 | "source": [ 27 | "import os\n", 28 | "from dotenv import load_dotenv\n", 29 | "load_dotenv() # load all the environment variables" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 3, 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [ 38 | "os.environ[\"HF_TOKEN\"] = os.getenv(\"HF_TOKEN\")" 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": {}, 44 | "source": [ 45 | "#### Sentence Transformers on Hugging Face\n", 46 | "Hugging Face sentence-transformers is a Python framework for state-of-the-art sentence, text and image embeddings. One of the embedding models is used in the HuggingFaceEmbeddings class. We have also added an alias for SentenceTransformerEmbeddings for users who are more familiar with directly using that package." 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 4, 52 | "metadata": {}, 53 | "outputs": [ 54 | { 55 | "name": "stderr", 56 | "output_type": "stream", 57 | "text": [ 58 | "c:\\Users\\HP\\Documents\\appliso-genai-class\\class-project\\langchain\\venv\\lib\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", 59 | " from .autonotebook import tqdm as notebook_tqdm\n" 60 | ] 61 | } 62 | ], 63 | "source": [ 64 | "from langchain_huggingface import HuggingFaceEmbeddings\n", 65 | "embeddings = HuggingFaceEmbeddings(model_name=\"all-MiniLM-L6-v2\")" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 5, 71 | "metadata": {}, 72 | "outputs": [], 73 | "source": [ 74 | "text = \"This is a test embedding\"\n", 75 | "query_result = embeddings.embed_query(text)" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": 6, 81 | "metadata": {}, 82 | "outputs": [ 83 | { 84 | "data": { 85 | "text/plain": [ 86 | "384" 87 | ] 88 | }, 89 | "execution_count": 6, 90 | "metadata": {}, 91 | "output_type": "execute_result" 92 | } 93 | ], 94 | "source": [ 95 | "len(query_result)" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": 9, 101 | "metadata": {}, 102 | "outputs": [ 103 | { 104 | "data": { 105 | "text/plain": [ 106 | "[-3.982232738053426e-05,\n", 107 | " -0.008389588445425034,\n", 108 | " 0.017159033566713333,\n", 109 | " -0.009672412648797035,\n", 110 | " 0.05390653386712074,\n", 111 | " 0.03168691694736481,\n", 112 | " -0.012321701273322105,\n", 113 | " -0.03883601352572441,\n", 114 | " -0.0013938791817054152,\n", 115 | " -0.03625251725316048,\n", 116 | " 0.02205708436667919,\n", 117 | " -0.002488198457285762,\n", 118 | " 0.04577368497848511,\n", 119 | " 0.057863567024469376,\n", 120 | " -0.10501978546380997,\n", 121 | " 0.0015720946248620749,\n", 122 | " 0.07229085266590118,\n", 123 | " 0.01180707011371851,\n", 124 | " -0.047921936959028244,\n", 125 | " -0.001890235231257975,\n", 126 | " -0.04576699808239937,\n", 127 | " 0.02663777582347393,\n", 128 | " 0.04789052903652191,\n", 129 | " -0.052035678178071976,\n", 130 | " 0.05804193764925003,\n", 131 | " -0.010897456668317318,\n", 132 | " -0.038650769740343094,\n", 133 | " 0.07309255003929138,\n", 134 | " 0.08525511622428894,\n", 135 | " -0.06628172844648361,\n", 136 | " 0.11747563630342484,\n", 137 | " -0.0187731571495533,\n", 138 | " -0.0013455996522679925,\n", 139 | " 0.0707893818616867,\n", 140 | " 0.0334489569067955,\n", 141 | " 0.008096144534647465,\n", 142 | " 0.021063588559627533,\n", 143 | " 0.031115349382162094,\n", 144 | " -0.02520374394953251,\n", 145 | " 0.04926156625151634,\n", 146 | " 0.02203265205025673,\n", 147 | " -0.04765094816684723,\n", 148 | " 0.054290689527988434,\n", 149 | " 0.0441928394138813,\n", 150 | " 0.01790175586938858,\n", 151 | " -4.29010578955058e-05,\n", 152 | " -0.04158930107951164,\n", 153 | " -0.03652743622660637,\n", 154 | " -0.06079954653978348,\n", 155 | " 0.008788670413196087,\n", 156 | " -0.04537130519747734,\n", 157 | " -0.030636265873908997,\n", 158 | " -0.08699008822441101,\n", 159 | " -0.0501871332526207,\n", 160 | " -0.03282628208398819,\n", 161 | " -0.027263218536973,\n", 162 | " -0.0005354031454771757,\n", 163 | " -0.07510843127965927,\n", 164 | " 0.056432172656059265,\n", 165 | " 0.03713793680071831,\n", 166 | " 0.06987880170345306,\n", 167 | " -0.043512362986803055,\n", 168 | " 0.05463732033967972,\n", 169 | " 0.03990713506937027,\n", 170 | " 0.05400236323475838,\n", 171 | " -0.01218034140765667,\n", 172 | " -0.019111264497041702,\n", 173 | " 0.0390915721654892,\n", 174 | " -0.027581369504332542,\n", 175 | " 8.155100658768788e-05,\n", 176 | " -0.03960272669792175,\n", 177 | " 0.038850780576467514,\n", 178 | " -0.04775379225611687,\n", 179 | " 0.026025976985692978,\n", 180 | " 0.01190735399723053,\n", 181 | " 0.03636711835861206,\n", 182 | " -0.04449765384197235,\n", 183 | " -0.12421391904354095,\n", 184 | " 0.13886255025863647,\n", 185 | " -0.07852920144796371,\n", 186 | " -0.01889309287071228,\n", 187 | " -0.09540576487779617,\n", 188 | " -0.019844181835651398,\n", 189 | " -0.001993434503674507,\n", 190 | " 0.08345229923725128,\n", 191 | " 0.09071779996156693,\n", 192 | " 0.07899980247020721,\n", 193 | " -0.07421126216650009,\n", 194 | " -0.11611834913492203,\n", 195 | " -0.019220691174268723,\n", 196 | " 0.021763546392321587,\n", 197 | " 0.000924809486605227,\n", 198 | " -0.044782184064388275,\n", 199 | " 0.05770300701260567,\n", 200 | " -0.026618560776114464,\n", 201 | " -0.01985950581729412,\n", 202 | " -0.003156122984364629,\n", 203 | " -0.016520636156201363,\n", 204 | " 0.02315826341509819,\n", 205 | " 0.13808368146419525,\n", 206 | " -0.029056387022137642,\n", 207 | " 0.010708917863667011,\n", 208 | " 0.090960294008255,\n", 209 | " -0.003035126719623804,\n", 210 | " -0.05726246535778046,\n", 211 | " -0.08792917430400848,\n", 212 | " 0.07052353024482727,\n", 213 | " -0.02229619212448597,\n", 214 | " 0.0009588834946043789,\n", 215 | " -0.058415014296770096,\n", 216 | " -0.051296502351760864,\n", 217 | " -0.061925265938043594,\n", 218 | " -0.018568843603134155,\n", 219 | " 0.037519726902246475,\n", 220 | " -0.020278897136449814,\n", 221 | " -0.08814726024866104,\n", 222 | " 0.005657498259097338,\n", 223 | " 0.029928648844361305,\n", 224 | " 0.013650081120431423,\n", 225 | " 0.012477613054215908,\n", 226 | " 0.07397845387458801,\n", 227 | " 0.05466856062412262,\n", 228 | " -0.03696698695421219,\n", 229 | " 0.00823430810123682,\n", 230 | " 0.03739957511425018,\n", 231 | " -0.05445488169789314,\n", 232 | " 0.013573396019637585,\n", 233 | " -3.238861625339727e-33,\n", 234 | " 0.02799445018172264,\n", 235 | " -0.06826857477426529,\n", 236 | " -0.019251540303230286,\n", 237 | " 0.07805021852254868,\n", 238 | " 0.05403478071093559,\n", 239 | " 0.024495646357536316,\n", 240 | " -0.08566156029701233,\n", 241 | " 0.10353649407625198,\n", 242 | " -0.07747086137533188,\n", 243 | " -0.015058095566928387,\n", 244 | " -0.0415959469974041,\n", 245 | " 0.009519764222204685,\n", 246 | " -0.014069483615458012,\n", 247 | " 0.08735539764165878,\n", 248 | " -0.004416861105710268,\n", 249 | " 0.04899677634239197,\n", 250 | " -0.0022297482937574387,\n", 251 | " 0.04959813505411148,\n", 252 | " -0.05701699107885361,\n", 253 | " 0.003256509080529213,\n", 254 | " -0.04002814367413521,\n", 255 | " -0.0020311991684138775,\n", 256 | " -0.035953156650066376,\n", 257 | " -0.11728297919034958,\n", 258 | " -0.09502940624952316,\n", 259 | " -0.058854494243860245,\n", 260 | " 0.03332493454217911,\n", 261 | " -0.05482638627290726,\n", 262 | " -0.06075582653284073,\n", 263 | " 0.021803036332130432,\n", 264 | " -0.10384271293878555,\n", 265 | " -0.025937851518392563,\n", 266 | " -0.04837459325790405,\n", 267 | " 0.030800960958003998,\n", 268 | " 0.029658550396561623,\n", 269 | " 0.003079761518165469,\n", 270 | " 0.06791012734174728,\n", 271 | " -0.0045179626904428005,\n", 272 | " -0.051944535225629807,\n", 273 | " 0.019817214459180832,\n", 274 | " -0.008171487599611282,\n", 275 | " -0.061849456280469894,\n", 276 | " 0.006351749412715435,\n", 277 | " -0.009956898167729378,\n", 278 | " -0.010044580325484276,\n", 279 | " 0.009328625164926052,\n", 280 | " 0.02864973247051239,\n", 281 | " -0.011597787030041218,\n", 282 | " -0.009050896391272545,\n", 283 | " -0.03734878823161125,\n", 284 | " 0.011802811175584793,\n", 285 | " 0.032057564705610275,\n", 286 | " 0.020887525752186775,\n", 287 | " -0.09473728388547897,\n", 288 | " 0.026082519441843033,\n", 289 | " -0.008845807984471321,\n", 290 | " -0.02265990525484085,\n", 291 | " 0.03663666918873787,\n", 292 | " -0.0014924074057489634,\n", 293 | " 0.0311404038220644,\n", 294 | " -0.08595141768455505,\n", 295 | " -0.014536670409142971,\n", 296 | " -0.018746422603726387,\n", 297 | " 0.017592482268810272,\n", 298 | " -0.0852465033531189,\n", 299 | " -0.02272128127515316,\n", 300 | " -0.03379029035568237,\n", 301 | " -0.06625084578990936,\n", 302 | " 0.09529098868370056,\n", 303 | " 0.07090650498867035,\n", 304 | " -0.010913383215665817,\n", 305 | " 0.03226915001869202,\n", 306 | " -0.0317811518907547,\n", 307 | " 0.01790858432650566,\n", 308 | " -0.07794017344713211,\n", 309 | " 0.002214992418885231,\n", 310 | " -0.05164162069559097,\n", 311 | " 0.005949240177869797,\n", 312 | " -0.02975139208137989,\n", 313 | " 0.008752788417041302,\n", 314 | " -0.008651845157146454,\n", 315 | " -0.1301809847354889,\n", 316 | " 0.02039877511560917,\n", 317 | " -0.0649644210934639,\n", 318 | " -0.04493653029203415,\n", 319 | " -0.12240216135978699,\n", 320 | " 0.014912579208612442,\n", 321 | " -0.13807429373264313,\n", 322 | " 0.01827366277575493,\n", 323 | " -0.04148923605680466,\n", 324 | " -0.034012775868177414,\n", 325 | " 0.015545093454420567,\n", 326 | " -0.02856813743710518,\n", 327 | " -0.055336397141218185,\n", 328 | " 0.11916743963956833,\n", 329 | " 2.0815773350506073e-33,\n", 330 | " -0.024176878854632378,\n", 331 | " 0.08730743080377579,\n", 332 | " -0.04175102338194847,\n", 333 | " 0.120825856924057,\n", 334 | " 0.03555309399962425,\n", 335 | " 0.007135098800063133,\n", 336 | " 0.10279736667871475,\n", 337 | " 0.08518263697624207,\n", 338 | " -0.0772886648774147,\n", 339 | " 0.13364852964878082,\n", 340 | " 0.009534682147204876,\n", 341 | " -0.039741866290569305,\n", 342 | " 0.012871588580310345,\n", 343 | " -0.04554315283894539,\n", 344 | " 0.0016407534712925553,\n", 345 | " 0.032261449843645096,\n", 346 | " -4.252193684806116e-05,\n", 347 | " -0.006645619869232178,\n", 348 | " 0.0038436325266957283,\n", 349 | " 0.0010526059195399284,\n", 350 | " -0.02668687142431736,\n", 351 | " 0.13362596929073334,\n", 352 | " 0.040329448878765106,\n", 353 | " 0.08542609959840775,\n", 354 | " -0.02976442687213421,\n", 355 | " 0.0938456654548645,\n", 356 | " 0.005219993647187948,\n", 357 | " -0.0014844386605545878,\n", 358 | " -0.013066734187304974,\n", 359 | " -0.0013150630984455347,\n", 360 | " 0.011112679727375507,\n", 361 | " -0.032797470688819885,\n", 362 | " -0.06623934209346771,\n", 363 | " 0.026126839220523834,\n", 364 | " -0.001078748726285994,\n", 365 | " 0.004938961006700993,\n", 366 | " 0.14063985645771027,\n", 367 | " 0.00486473273485899,\n", 368 | " -0.01777205802500248,\n", 369 | " 0.015878429636359215,\n", 370 | " 0.07886666804552078,\n", 371 | " 0.09637969732284546,\n", 372 | " -0.058684129267930984,\n", 373 | " 0.06573521345853806,\n", 374 | " 0.03201272711157799,\n", 375 | " -0.006719919387251139,\n", 376 | " 0.020651768893003464,\n", 377 | " -0.06645544618368149,\n", 378 | " 0.09720725566148758,\n", 379 | " 0.01983771100640297,\n", 380 | " -0.07045739144086838,\n", 381 | " 0.009721983224153519,\n", 382 | " -0.014323637820780277,\n", 383 | " 0.0024438733235001564,\n", 384 | " -0.02910926379263401,\n", 385 | " -0.00639130175113678,\n", 386 | " -0.06984380632638931,\n", 387 | " -0.018651029095053673,\n", 388 | " 0.012167496606707573,\n", 389 | " 0.019333936274051666,\n", 390 | " -0.038592420518398285,\n", 391 | " -0.0007712884689681232,\n", 392 | " 0.012427078559994698,\n", 393 | " -0.016147442162036896,\n", 394 | " 0.05892188847064972,\n", 395 | " -0.08908401429653168,\n", 396 | " -0.043482422828674316,\n", 397 | " 0.06348364800214767,\n", 398 | " -0.02782214619219303,\n", 399 | " 0.016217490658164024,\n", 400 | " 0.035559698939323425,\n", 401 | " 0.005008375272154808,\n", 402 | " -0.03674767166376114,\n", 403 | " -0.0027263795491307974,\n", 404 | " 0.07537440210580826,\n", 405 | " -0.07071912288665771,\n", 406 | " 0.06777496635913849,\n", 407 | " -0.0007328986539505422,\n", 408 | " 0.054387640208005905,\n", 409 | " -0.028612522408366203,\n", 410 | " -0.012822918593883514,\n", 411 | " -0.09669359773397446,\n", 412 | " 0.0264329481869936,\n", 413 | " 0.04288501292467117,\n", 414 | " 0.011468319222331047,\n", 415 | " 0.09469547122716904,\n", 416 | " 0.033904068171978,\n", 417 | " 0.08502475917339325,\n", 418 | " -0.008647147566080093,\n", 419 | " 0.0084849763661623,\n", 420 | " 0.02088925987482071,\n", 421 | " 0.030218353495001793,\n", 422 | " -0.04598240181803703,\n", 423 | " 0.035952307283878326,\n", 424 | " 0.042149703949689865,\n", 425 | " -1.7889414394289815e-08,\n", 426 | " -0.03766067698597908,\n", 427 | " -0.059617724269628525,\n", 428 | " 0.008411399088799953,\n", 429 | " -0.025091171264648438,\n", 430 | " -0.07347160577774048,\n", 431 | " -0.03483089432120323,\n", 432 | " 0.031146621331572533,\n", 433 | " -0.049455463886260986,\n", 434 | " -0.0346391461789608,\n", 435 | " -0.02936202846467495,\n", 436 | " 0.06484342366456985,\n", 437 | " 0.009327800944447517,\n", 438 | " -0.08251499384641647,\n", 439 | " 0.024225054308772087,\n", 440 | " 0.021541869267821312,\n", 441 | " -0.01132198516279459,\n", 442 | " -0.06059659644961357,\n", 443 | " 0.05189492180943489,\n", 444 | " 0.014483273960649967,\n", 445 | " 0.03894324228167534,\n", 446 | " -0.024379592388868332,\n", 447 | " 0.09330469369888306,\n", 448 | " 0.07694195210933685,\n", 449 | " 0.03136269375681877,\n", 450 | " -0.05563265085220337,\n", 451 | " 0.014230797067284584,\n", 452 | " -0.03880460932850838,\n", 453 | " 0.037391725927591324,\n", 454 | " 0.037480805069208145,\n", 455 | " 0.01522623561322689,\n", 456 | " 0.061950717121362686,\n", 457 | " 0.10594558715820312,\n", 458 | " -0.02448192983865738,\n", 459 | " -0.05928506329655647,\n", 460 | " -0.06597160547971725,\n", 461 | " 0.10984618216753006,\n", 462 | " 0.03938158601522446,\n", 463 | " -0.0285971537232399,\n", 464 | " -0.05850430577993393,\n", 465 | " -0.01563064567744732,\n", 466 | " -0.044457800686359406,\n", 467 | " -0.03671998158097267,\n", 468 | " 0.017169831320643425,\n", 469 | " -0.03980456665158272,\n", 470 | " 0.08060701191425323,\n", 471 | " -0.023166652768850327,\n", 472 | " 0.024017855525016785,\n", 473 | " -0.027038449421525,\n", 474 | " 0.014661993831396103,\n", 475 | " -0.04711359366774559,\n", 476 | " -0.008670343086123466,\n", 477 | " -0.04968971014022827,\n", 478 | " 0.0008851219317875803,\n", 479 | " 0.02413458377122879,\n", 480 | " 0.0002598866412881762,\n", 481 | " -0.016296731308102608,\n", 482 | " -0.008100338280200958,\n", 483 | " 0.023780453950166702,\n", 484 | " -0.05325911194086075,\n", 485 | " 0.031403012573719025,\n", 486 | " 0.02105952426791191,\n", 487 | " 0.034209128469228745,\n", 488 | " 0.07567431777715683,\n", 489 | " -0.012619723565876484]" 490 | ] 491 | }, 492 | "execution_count": 9, 493 | "metadata": {}, 494 | "output_type": "execute_result" 495 | } 496 | ], 497 | "source": [ 498 | "doc_result = embeddings.embed_documents([text, \"This is my second text document\"])\n", 499 | "doc_result[0]" 500 | ] 501 | } 502 | ], 503 | "metadata": { 504 | "kernelspec": { 505 | "display_name": "Python 3", 506 | "language": "python", 507 | "name": "python3" 508 | }, 509 | "language_info": { 510 | "codemirror_mode": { 511 | "name": "ipython", 512 | "version": 3 513 | }, 514 | "file_extension": ".py", 515 | "mimetype": "text/x-python", 516 | "name": "python", 517 | "nbconvert_exporter": "python", 518 | "pygments_lexer": "ipython3", 519 | "version": "3.10.0" 520 | } 521 | }, 522 | "nbformat": 4, 523 | "nbformat_minor": 2 524 | } 525 | -------------------------------------------------------------------------------- /3.0-embeddings/data/attention.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EniolaAdemola/langchain-basics/d0c47b35f41ac7810b4ed21015bca04e8012baa8/3.0-embeddings/data/attention.pdf -------------------------------------------------------------------------------- /3.0-embeddings/data/speech.txt: -------------------------------------------------------------------------------- 1 | Hello! 2 | My name is DAVEWORLD, and I am excited to be your tutor for this program. 3 | 4 | I am a Data Scientist, Open-source Contributor, and Frontend Engineer with years of experience in the tech industry. Throughout my career, I’ve been privileged to build innovative products, solve real-world problems, and empower over 5,000 individuals to grow into better versions of themselves. My work and insights have also connected me with an audience of over 10,000 followers across social media platforms. 5 | Looking ahead, my vision is to create groundbreaking projects and support millions—if not billions—of people in achieving their goals and thriving in the tech industry. 6 | 7 | In this program, I am here to guide you every step of the way, ensuring you have an unforgettable learning experience. 8 | Let’s embark on this transformative journey together! -------------------------------------------------------------------------------- /4.0-vector_store_db/4.1-faiss.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "#### Faiss\n", 8 | "Facebook AI Similarity Search (Faiss) is a library for efficient similarity search and clustering of dense vectors. It contains algorithms that search in sets of vectors of any size, up to ones that possibly do not fit in RAM. It also contains supporting code for evaluation and parameter tuning." 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 1, 14 | "metadata": {}, 15 | "outputs": [ 16 | { 17 | "name": "stderr", 18 | "output_type": "stream", 19 | "text": [ 20 | "Created a chunk of size 73, which is longer than the specified 50\n", 21 | "Created a chunk of size 412, which is longer than the specified 50\n", 22 | "Created a chunk of size 168, which is longer than the specified 50\n", 23 | "Created a chunk of size 118, which is longer than the specified 50\n" 24 | ] 25 | } 26 | ], 27 | "source": [ 28 | "from langchain_community.document_loaders import TextLoader\n", 29 | "from langchain_community.vectorstores import FAISS\n", 30 | "from langchain_community.embeddings import OllamaEmbeddings\n", 31 | "from langchain_text_splitters import CharacterTextSplitter\n", 32 | "\n", 33 | "loader=TextLoader(\"./data/speech.txt\")\n", 34 | "documents=loader.load()\n", 35 | "text_splitter=CharacterTextSplitter(separator=\"\\n\", chunk_size=50,chunk_overlap=20)\n", 36 | "docs=text_splitter.split_documents(documents)" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 2, 42 | "metadata": {}, 43 | "outputs": [ 44 | { 45 | "data": { 46 | "text/plain": [ 47 | "[Document(metadata={'source': './data/speech.txt'}, page_content='Hello!'),\n", 48 | " Document(metadata={'source': './data/speech.txt'}, page_content='My name is DAVEWORLD, and I am excited to be your tutor for this program.'),\n", 49 | " Document(metadata={'source': './data/speech.txt'}, page_content='I am a Data Scientist, Open-source Contributor, and Frontend Engineer with years of experience in the tech industry. Throughout my career, I’ve been privileged to build innovative products, solve real-world problems, and empower over 5,000 individuals to grow into better versions of themselves. My work and insights have also connected me with an audience of over 10,000 followers across social media platforms.'),\n", 50 | " Document(metadata={'source': './data/speech.txt'}, page_content='Looking ahead, my vision is to create groundbreaking projects and support millions—if not billions—of people in achieving their goals and thriving in the tech industry.'),\n", 51 | " Document(metadata={'source': './data/speech.txt'}, page_content='In this program, I am here to guide you every step of the way, ensuring you have an unforgettable learning experience.'),\n", 52 | " Document(metadata={'source': './data/speech.txt'}, page_content='Let’s embark on this transformative journey together!')]" 53 | ] 54 | }, 55 | "execution_count": 2, 56 | "metadata": {}, 57 | "output_type": "execute_result" 58 | } 59 | ], 60 | "source": [ 61 | "docs" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 4, 67 | "metadata": {}, 68 | "outputs": [], 69 | "source": [ 70 | "embeddings = (OllamaEmbeddings(model=\"gemma2:2b\")) # by defaults llamma2\n", 71 | "database = FAISS.from_documents(docs, embeddings)" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": 5, 77 | "metadata": {}, 78 | "outputs": [ 79 | { 80 | "data": { 81 | "text/plain": [ 82 | "" 83 | ] 84 | }, 85 | "execution_count": 5, 86 | "metadata": {}, 87 | "output_type": "execute_result" 88 | } 89 | ], 90 | "source": [ 91 | "database" 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": 6, 97 | "metadata": {}, 98 | "outputs": [ 99 | { 100 | "name": "stdout", 101 | "output_type": "stream", 102 | "text": [ 103 | "[Document(id='4ec1d08b-e438-41c0-9cbf-7a07324044b4', metadata={'source': './data/speech.txt'}, page_content='Looking ahead, my vision is to create groundbreaking projects and support millions—if not billions—of people in achieving their goals and thriving in the tech industry.'), Document(id='d497400c-27fa-46e6-9bce-d25782a8009a', metadata={'source': './data/speech.txt'}, page_content='In this program, I am here to guide you every step of the way, ensuring you have an unforgettable learning experience.'), Document(id='2a18d4c6-7942-4f8f-8002-49cee08ab4ac', metadata={'source': './data/speech.txt'}, page_content='I am a Data Scientist, Open-source Contributor, and Frontend Engineer with years of experience in the tech industry. Throughout my career, I’ve been privileged to build innovative products, solve real-world problems, and empower over 5,000 individuals to grow into better versions of themselves. My work and insights have also connected me with an audience of over 10,000 followers across social media platforms.'), Document(id='b5d474bd-302e-443a-9660-6e86de06e40e', metadata={'source': './data/speech.txt'}, page_content='My name is DAVEWORLD, and I am excited to be your tutor for this program.')]\n" 104 | ] 105 | } 106 | ], 107 | "source": [ 108 | "query = \"WHat's the speaker future ambition?\"\n", 109 | "retrived_result = database.similarity_search(query)\n", 110 | "print(retrived_result)" 111 | ] 112 | }, 113 | { 114 | "cell_type": "markdown", 115 | "metadata": {}, 116 | "source": [ 117 | "#### As a Retriever\n", 118 | "We can also convert the vectorstore into a Retriever class. This allows us to easily use it in other LangChain methods, which largely work with retrievers" 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": 10, 124 | "metadata": {}, 125 | "outputs": [ 126 | { 127 | "data": { 128 | "text/plain": [ 129 | "'Looking ahead, my vision is to create groundbreaking projects and support millions—if not billions—of people in achieving their goals and thriving in the tech industry.'" 130 | ] 131 | }, 132 | "execution_count": 10, 133 | "metadata": {}, 134 | "output_type": "execute_result" 135 | } 136 | ], 137 | "source": [ 138 | "retrieval = database.as_retriever()\n", 139 | "docs = retrieval.invoke(query)\n", 140 | "docs[0].page_content" 141 | ] 142 | }, 143 | { 144 | "cell_type": "markdown", 145 | "metadata": {}, 146 | "source": [ 147 | "#### Similarity Search with score\n", 148 | "There are some FAISS specific methods. One of them is similarity_search_with_score, which allows you to return not only the documents but also the distance score of the query to them. The returned distance score is L2 distance. Therefore, a lower score is better." 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "execution_count": 11, 154 | "metadata": {}, 155 | "outputs": [ 156 | { 157 | "data": { 158 | "text/plain": [ 159 | "[(Document(id='4ec1d08b-e438-41c0-9cbf-7a07324044b4', metadata={'source': './data/speech.txt'}, page_content='Looking ahead, my vision is to create groundbreaking projects and support millions—if not billions—of people in achieving their goals and thriving in the tech industry.'),\n", 160 | " 4627.6006),\n", 161 | " (Document(id='d497400c-27fa-46e6-9bce-d25782a8009a', metadata={'source': './data/speech.txt'}, page_content='In this program, I am here to guide you every step of the way, ensuring you have an unforgettable learning experience.'),\n", 162 | " 5418.37),\n", 163 | " (Document(id='2a18d4c6-7942-4f8f-8002-49cee08ab4ac', metadata={'source': './data/speech.txt'}, page_content='I am a Data Scientist, Open-source Contributor, and Frontend Engineer with years of experience in the tech industry. Throughout my career, I’ve been privileged to build innovative products, solve real-world problems, and empower over 5,000 individuals to grow into better versions of themselves. My work and insights have also connected me with an audience of over 10,000 followers across social media platforms.'),\n", 164 | " 5759.3496),\n", 165 | " (Document(id='b5d474bd-302e-443a-9660-6e86de06e40e', metadata={'source': './data/speech.txt'}, page_content='My name is DAVEWORLD, and I am excited to be your tutor for this program.'),\n", 166 | " 6570.869)]" 167 | ] 168 | }, 169 | "execution_count": 11, 170 | "metadata": {}, 171 | "output_type": "execute_result" 172 | } 173 | ], 174 | "source": [ 175 | "docs_and_score = database.similarity_search_with_score(query)\n", 176 | "docs_and_score" 177 | ] 178 | }, 179 | { 180 | "cell_type": "code", 181 | "execution_count": 12, 182 | "metadata": {}, 183 | "outputs": [ 184 | { 185 | "data": { 186 | "text/plain": [ 187 | "[-0.6297181844711304,\n", 188 | " 1.94210684299469,\n", 189 | " -1.4898511171340942,\n", 190 | " 0.6643972992897034,\n", 191 | " -1.1895604133605957,\n", 192 | " -0.551268458366394,\n", 193 | " 0.897462785243988,\n", 194 | " -0.9539811015129089,\n", 195 | " 0.7110637426376343,\n", 196 | " 1.0620583295822144,\n", 197 | " 1.026173710823059,\n", 198 | " -0.8261721730232239,\n", 199 | " -0.6739504933357239,\n", 200 | " -1.416894555091858,\n", 201 | " 0.3613138794898987,\n", 202 | " -3.6238253116607666,\n", 203 | " 0.16175216436386108,\n", 204 | " 0.7011933922767639,\n", 205 | " -4.665972709655762,\n", 206 | " 0.5434863567352295,\n", 207 | " 1.6604150533676147,\n", 208 | " -0.6790810227394104,\n", 209 | " -1.41616952419281,\n", 210 | " 0.43560418486595154,\n", 211 | " -0.10380357503890991,\n", 212 | " 0.04784208908677101,\n", 213 | " -0.4895695745944977,\n", 214 | " -1.5505367517471313,\n", 215 | " -1.039842963218689,\n", 216 | " 1.2034982442855835,\n", 217 | " 0.12905548512935638,\n", 218 | " -1.645477533340454,\n", 219 | " -0.16743314266204834,\n", 220 | " 0.07186418026685715,\n", 221 | " 0.42498087882995605,\n", 222 | " 1.666306495666504,\n", 223 | " 0.4298882484436035,\n", 224 | " 0.09445460140705109,\n", 225 | " 1.7989983558654785,\n", 226 | " -2.015619993209839,\n", 227 | " -0.2573263943195343,\n", 228 | " 2.4922964572906494,\n", 229 | " 0.6339044570922852,\n", 230 | " -0.3625108003616333,\n", 231 | " -3.1435229778289795,\n", 232 | " -1.8743040561676025,\n", 233 | " 0.6478239297866821,\n", 234 | " -2.8897180557250977,\n", 235 | " 2.3119049072265625,\n", 236 | " 1.961540937423706,\n", 237 | " -0.6805639266967773,\n", 238 | " 1.4126390218734741,\n", 239 | " -0.5199459195137024,\n", 240 | " 3.0665671825408936,\n", 241 | " -0.4951547384262085,\n", 242 | " -0.08559148013591766,\n", 243 | " 1.0529073476791382,\n", 244 | " 1.6275041103363037,\n", 245 | " -0.5304652452468872,\n", 246 | " -0.3426063060760498,\n", 247 | " 0.34220558404922485,\n", 248 | " -6.214635372161865,\n", 249 | " 1.184674620628357,\n", 250 | " 1.6532390117645264,\n", 251 | " -1.9081913232803345,\n", 252 | " 2.1418445110321045,\n", 253 | " -0.06467658281326294,\n", 254 | " -0.4231157898902893,\n", 255 | " -0.6958569884300232,\n", 256 | " -2.0289130210876465,\n", 257 | " -1.4233540296554565,\n", 258 | " 0.2444634586572647,\n", 259 | " -1.0265171527862549,\n", 260 | " 0.0076964073814451694,\n", 261 | " 1.682145357131958,\n", 262 | " -0.5226271152496338,\n", 263 | " -0.03347870707511902,\n", 264 | " 1.7335102558135986,\n", 265 | " -1.0894557237625122,\n", 266 | " -1.6806663274765015,\n", 267 | " 1.4313597679138184,\n", 268 | " -1.2113040685653687,\n", 269 | " -3.320441246032715,\n", 270 | " -0.4309166371822357,\n", 271 | " -0.23668605089187622,\n", 272 | " 0.500357985496521,\n", 273 | " -1.2329576015472412,\n", 274 | " -1.6816424131393433,\n", 275 | " -0.6858345866203308,\n", 276 | " -1.6821331977844238,\n", 277 | " -1.561043381690979,\n", 278 | " -0.04999246820807457,\n", 279 | " -2.4260294437408447,\n", 280 | " 1.3094966411590576,\n", 281 | " -1.2921072244644165,\n", 282 | " -1.7896438837051392,\n", 283 | " 1.6489330530166626,\n", 284 | " -0.7554736733436584,\n", 285 | " -0.9746853709220886,\n", 286 | " 0.6486099362373352,\n", 287 | " 0.8126263618469238,\n", 288 | " 0.45838016271591187,\n", 289 | " 0.04307258129119873,\n", 290 | " -0.5479816198348999,\n", 291 | " 0.7430949211120605,\n", 292 | " -1.8209333419799805,\n", 293 | " -0.914669930934906,\n", 294 | " -0.3257955014705658,\n", 295 | " -1.2181406021118164,\n", 296 | " -0.1279335469007492,\n", 297 | " -0.6371833682060242,\n", 298 | " -0.38756439089775085,\n", 299 | " -0.3796147108078003,\n", 300 | " 1.5417330265045166,\n", 301 | " -1.488925814628601,\n", 302 | " -2.44594407081604,\n", 303 | " 1.4124945402145386,\n", 304 | " -3.1233584880828857,\n", 305 | " -0.07948365807533264,\n", 306 | " 0.45045965909957886,\n", 307 | " 1.2435137033462524,\n", 308 | " -0.23410791158676147,\n", 309 | " -1.436476469039917,\n", 310 | " -1.7543251514434814,\n", 311 | " 1.023201823234558,\n", 312 | " 0.4088285565376282,\n", 313 | " -0.21886135637760162,\n", 314 | " 0.017414160072803497,\n", 315 | " -1.3565962314605713,\n", 316 | " -1.115206241607666,\n", 317 | " 1.6772934198379517,\n", 318 | " 0.851812481880188,\n", 319 | " 0.5056002736091614,\n", 320 | " 0.7880306839942932,\n", 321 | " 0.3834030330181122,\n", 322 | " -1.0392627716064453,\n", 323 | " 0.9816275835037231,\n", 324 | " 0.03730392083525658,\n", 325 | " -1.139460563659668,\n", 326 | " -0.11942020058631897,\n", 327 | " 10.975912094116211,\n", 328 | " -0.8088572025299072,\n", 329 | " -0.25920984148979187,\n", 330 | " -0.02932194247841835,\n", 331 | " -0.5671954154968262,\n", 332 | " 0.5317320823669434,\n", 333 | " -1.1306465864181519,\n", 334 | " -0.2354477345943451,\n", 335 | " 2.2581756114959717,\n", 336 | " 1.175461769104004,\n", 337 | " 0.4753134548664093,\n", 338 | " -1.26810622215271,\n", 339 | " -1.0846747159957886,\n", 340 | " 1.6102770566940308,\n", 341 | " 1.3275156021118164,\n", 342 | " 2.0818538665771484,\n", 343 | " 2.705718517303467,\n", 344 | " 1.087443232536316,\n", 345 | " -0.4557741582393646,\n", 346 | " 1.0306795835494995,\n", 347 | " -3.576873779296875,\n", 348 | " -0.1690579503774643,\n", 349 | " -1.2515078783035278,\n", 350 | " 1.1291191577911377,\n", 351 | " 0.5229134559631348,\n", 352 | " -0.23188558220863342,\n", 353 | " -1.422921895980835,\n", 354 | " -1.042598843574524,\n", 355 | " -0.8147723078727722,\n", 356 | " 0.4027683138847351,\n", 357 | " -0.27900266647338867,\n", 358 | " -0.5367242097854614,\n", 359 | " 1.2570911645889282,\n", 360 | " 0.06552226096391678,\n", 361 | " 0.4726077616214752,\n", 362 | " 1.4140957593917847,\n", 363 | " -0.1505214422941208,\n", 364 | " -0.9271345138549805,\n", 365 | " -0.5079649090766907,\n", 366 | " -0.884830892086029,\n", 367 | " -2.2932214736938477,\n", 368 | " -1.9722329378128052,\n", 369 | " -0.8722268342971802,\n", 370 | " -0.28844600915908813,\n", 371 | " 19.44192886352539,\n", 372 | " -0.2979297339916229,\n", 373 | " -1.4080429077148438,\n", 374 | " -0.4696235656738281,\n", 375 | " -1.2489571571350098,\n", 376 | " -0.385774701833725,\n", 377 | " 1.364355444908142,\n", 378 | " 0.6763606667518616,\n", 379 | " 0.812376856803894,\n", 380 | " -0.7547740340232849,\n", 381 | " 0.42347756028175354,\n", 382 | " 0.6370386481285095,\n", 383 | " -0.03511738404631615,\n", 384 | " 1.4015851020812988,\n", 385 | " -0.5457579493522644,\n", 386 | " 0.7681674957275391,\n", 387 | " 1.2023279666900635,\n", 388 | " 0.14452749490737915,\n", 389 | " -0.08237066864967346,\n", 390 | " 0.05412037670612335,\n", 391 | " -0.12128080427646637,\n", 392 | " 1.4684292078018188,\n", 393 | " 1.2556300163269043,\n", 394 | " 0.17084968090057373,\n", 395 | " 0.7415304780006409,\n", 396 | " -0.02867943048477173,\n", 397 | " 1.0878149271011353,\n", 398 | " -1.1083157062530518,\n", 399 | " -0.7220864295959473,\n", 400 | " -0.7588027715682983,\n", 401 | " -0.15550057590007782,\n", 402 | " 0.4311741888523102,\n", 403 | " -1.6504896879196167,\n", 404 | " -3.373288869857788,\n", 405 | " 0.1542992740869522,\n", 406 | " 0.4982507824897766,\n", 407 | " -0.8904581069946289,\n", 408 | " 0.46063926815986633,\n", 409 | " 1.3910088539123535,\n", 410 | " 0.7285654544830322,\n", 411 | " -1.78311288356781,\n", 412 | " -0.8139880299568176,\n", 413 | " 2.1947290897369385,\n", 414 | " -0.979268491268158,\n", 415 | " -0.6111469268798828,\n", 416 | " -1.387622594833374,\n", 417 | " -0.03979291021823883,\n", 418 | " -2.355494260787964,\n", 419 | " -0.468722403049469,\n", 420 | " 0.5448663830757141,\n", 421 | " -1.9492884874343872,\n", 422 | " 0.005195395089685917,\n", 423 | " -0.2794204652309418,\n", 424 | " 1.713936686515808,\n", 425 | " 0.1690380722284317,\n", 426 | " 0.3502027094364166,\n", 427 | " -1.395937442779541,\n", 428 | " 0.9421184062957764,\n", 429 | " -0.7821661233901978,\n", 430 | " -0.479741632938385,\n", 431 | " 0.9420724511146545,\n", 432 | " -0.46737176179885864,\n", 433 | " 1.4206880331039429,\n", 434 | " 4.267658233642578,\n", 435 | " -1.486731767654419,\n", 436 | " -3.0443544387817383,\n", 437 | " -1.1120132207870483,\n", 438 | " 0.3889847993850708,\n", 439 | " 1.6526991128921509,\n", 440 | " -0.0006598964100703597,\n", 441 | " -1.4008636474609375,\n", 442 | " 0.17762388288974762,\n", 443 | " -0.9604708552360535,\n", 444 | " -0.41563090682029724,\n", 445 | " 1.7263293266296387,\n", 446 | " 1.4118766784667969,\n", 447 | " -0.47097310423851013,\n", 448 | " -1.5511562824249268,\n", 449 | " -5.656863212585449,\n", 450 | " 4.09107780456543,\n", 451 | " -2.2299749851226807,\n", 452 | " 0.3161380887031555,\n", 453 | " -0.3256723880767822,\n", 454 | " -0.4791492223739624,\n", 455 | " 0.9797413349151611,\n", 456 | " -0.11180461943149567,\n", 457 | " 0.8744916319847107,\n", 458 | " 0.6572480201721191,\n", 459 | " 0.5286825895309448,\n", 460 | " 0.641042947769165,\n", 461 | " 0.20650732517242432,\n", 462 | " -0.6093241572380066,\n", 463 | " -2.0734405517578125,\n", 464 | " 2.463494062423706,\n", 465 | " -0.8053969144821167,\n", 466 | " -0.5522754788398743,\n", 467 | " 0.7993236780166626,\n", 468 | " 0.8004868030548096,\n", 469 | " -0.013346873223781586,\n", 470 | " -0.8602145314216614,\n", 471 | " 0.8571605086326599,\n", 472 | " 0.6898683309555054,\n", 473 | " -2.6586501598358154,\n", 474 | " 1.783110499382019,\n", 475 | " -1.4821349382400513,\n", 476 | " -0.40850886702537537,\n", 477 | " 2.104304075241089,\n", 478 | " -0.45357194542884827,\n", 479 | " -2.4929566383361816,\n", 480 | " -0.13436894118785858,\n", 481 | " -2.257506847381592,\n", 482 | " -1.316947102546692,\n", 483 | " 1.8044846057891846,\n", 484 | " -1.5079271793365479,\n", 485 | " 1.854217529296875,\n", 486 | " -0.1164916604757309,\n", 487 | " -0.39025646448135376,\n", 488 | " -0.5744288563728333,\n", 489 | " -3.8258605003356934,\n", 490 | " 1.034224510192871,\n", 491 | " -1.3129602670669556,\n", 492 | " -0.5030744075775146,\n", 493 | " -1.2598416805267334,\n", 494 | " -0.2649790644645691,\n", 495 | " 0.16811060905456543,\n", 496 | " 1.8345565795898438,\n", 497 | " -1.0809379816055298,\n", 498 | " -0.3366050124168396,\n", 499 | " -0.6299411654472351,\n", 500 | " 1.0682854652404785,\n", 501 | " 1.8202605247497559,\n", 502 | " -1.815799593925476,\n", 503 | " -2.5597596168518066,\n", 504 | " 1.7217237949371338,\n", 505 | " -1.0095784664154053,\n", 506 | " 1.387959599494934,\n", 507 | " -1.9617128372192383,\n", 508 | " -1.3759138584136963,\n", 509 | " 1.9039185047149658,\n", 510 | " 0.8513885736465454,\n", 511 | " -1.100890040397644,\n", 512 | " -3.1687982082366943,\n", 513 | " 0.1918392926454544,\n", 514 | " -0.6067649722099304,\n", 515 | " 1.8482260704040527,\n", 516 | " 0.31783491373062134,\n", 517 | " -0.31759577989578247,\n", 518 | " 9.779356956481934,\n", 519 | " -0.7242318987846375,\n", 520 | " 0.48665717244148254,\n", 521 | " 4.297808647155762,\n", 522 | " -1.859512209892273,\n", 523 | " -0.5760313272476196,\n", 524 | " 0.5553678870201111,\n", 525 | " 0.2449655681848526,\n", 526 | " -0.7756179571151733,\n", 527 | " 1.3920371532440186,\n", 528 | " -2.4698729515075684,\n", 529 | " 1.0189017057418823,\n", 530 | " -0.024191593751311302,\n", 531 | " 2.1267974376678467,\n", 532 | " 0.44989046454429626,\n", 533 | " -0.12835125625133514,\n", 534 | " -1.1614384651184082,\n", 535 | " -2.043290615081787,\n", 536 | " 0.11611947417259216,\n", 537 | " 0.1310700923204422,\n", 538 | " 0.6317808628082275,\n", 539 | " 1.0812718868255615,\n", 540 | " 0.010132497176527977,\n", 541 | " 2.044386148452759,\n", 542 | " 1.668485164642334,\n", 543 | " 1.5151863098144531,\n", 544 | " -0.1434469223022461,\n", 545 | " 0.8199250102043152,\n", 546 | " -0.6581781506538391,\n", 547 | " -1.0501008033752441,\n", 548 | " 0.8108392953872681,\n", 549 | " 0.9611724019050598,\n", 550 | " -0.0009476720006205142,\n", 551 | " 3.753171682357788,\n", 552 | " -1.0710341930389404,\n", 553 | " -2.3731613159179688,\n", 554 | " -0.7271124720573425,\n", 555 | " -0.2884036600589752,\n", 556 | " -1.5460714101791382,\n", 557 | " -2.3570919036865234,\n", 558 | " -1.0793849229812622,\n", 559 | " -0.04784757271409035,\n", 560 | " 0.06171015277504921,\n", 561 | " 2.0230603218078613,\n", 562 | " 0.06697095185518265,\n", 563 | " -0.29462119936943054,\n", 564 | " 0.13630881905555725,\n", 565 | " 1.3137528896331787,\n", 566 | " -0.2474130094051361,\n", 567 | " 0.18215641379356384,\n", 568 | " -1.2853749990463257,\n", 569 | " 0.7435331344604492,\n", 570 | " -1.2657232284545898,\n", 571 | " 0.8826099634170532,\n", 572 | " 1.7742369174957275,\n", 573 | " 0.2578572928905487,\n", 574 | " 0.5502281785011292,\n", 575 | " 1.5491106510162354,\n", 576 | " 1.158115029335022,\n", 577 | " 0.2668176293373108,\n", 578 | " -1.8956308364868164,\n", 579 | " 0.600658118724823,\n", 580 | " 1.8138222694396973,\n", 581 | " -1.1547822952270508,\n", 582 | " 0.33588844537734985,\n", 583 | " 0.9129354953765869,\n", 584 | " 1.2585686445236206,\n", 585 | " 4.161393642425537,\n", 586 | " -0.7917596101760864,\n", 587 | " -3.2268755435943604,\n", 588 | " 1.547519564628601,\n", 589 | " 0.48217466473579407,\n", 590 | " 2.9936249256134033,\n", 591 | " 1.0413527488708496,\n", 592 | " 1.2831875085830688,\n", 593 | " 0.05984799563884735,\n", 594 | " 0.450615257024765,\n", 595 | " 1.9789469242095947,\n", 596 | " -0.9319844841957092,\n", 597 | " 0.29188477993011475,\n", 598 | " -0.43371298909187317,\n", 599 | " 0.7070187330245972,\n", 600 | " 2.129189968109131,\n", 601 | " -0.321640282869339,\n", 602 | " 1.7049508094787598,\n", 603 | " -0.27351194620132446,\n", 604 | " -0.03686108812689781,\n", 605 | " 0.38895633816719055,\n", 606 | " 0.6737495064735413,\n", 607 | " 2.5110392570495605,\n", 608 | " 1.6604855060577393,\n", 609 | " -0.45883625745773315,\n", 610 | " 0.45195451378822327,\n", 611 | " -0.9845672249794006,\n", 612 | " -1.4073069095611572,\n", 613 | " -0.25203344225883484,\n", 614 | " 0.745300829410553,\n", 615 | " -1.7878806591033936,\n", 616 | " -0.6721293330192566,\n", 617 | " -2.0127289295196533,\n", 618 | " 0.5658420324325562,\n", 619 | " -0.01071428693830967,\n", 620 | " -0.257308691740036,\n", 621 | " -0.6314165592193604,\n", 622 | " -0.3284357190132141,\n", 623 | " -0.13496601581573486,\n", 624 | " 0.04340853542089462,\n", 625 | " 1.1568217277526855,\n", 626 | " 0.5966781377792358,\n", 627 | " -1.2513731718063354,\n", 628 | " -1.3053216934204102,\n", 629 | " 1.7840818166732788,\n", 630 | " -2.150883197784424,\n", 631 | " 0.5218984484672546,\n", 632 | " -0.043337952345609665,\n", 633 | " 1.4133884906768799,\n", 634 | " 1.9152798652648926,\n", 635 | " -1.82464599609375,\n", 636 | " -0.18893951177597046,\n", 637 | " -0.03729415312409401,\n", 638 | " -0.9047521948814392,\n", 639 | " -0.616465151309967,\n", 640 | " 0.13310472667217255,\n", 641 | " -0.2720763087272644,\n", 642 | " 1.6180739402770996,\n", 643 | " -1.5894711017608643,\n", 644 | " -0.21225710213184357,\n", 645 | " 1.5556342601776123,\n", 646 | " 1.454729437828064,\n", 647 | " -0.9138553142547607,\n", 648 | " -0.3133024573326111,\n", 649 | " -2.0188910961151123,\n", 650 | " -0.5204169750213623,\n", 651 | " -3.681997060775757,\n", 652 | " -0.7906506657600403,\n", 653 | " 0.5959123373031616,\n", 654 | " -1.490124225616455,\n", 655 | " -1.7411621809005737,\n", 656 | " -0.2220078855752945,\n", 657 | " -0.48412230610847473,\n", 658 | " 0.5434921979904175,\n", 659 | " 1.4064984321594238,\n", 660 | " 0.46755120158195496,\n", 661 | " -1.705332636833191,\n", 662 | " 1.0520844459533691,\n", 663 | " 1.2998913526535034,\n", 664 | " -1.6876649856567383,\n", 665 | " -0.4900060296058655,\n", 666 | " -0.8227477073669434,\n", 667 | " 0.44300326704978943,\n", 668 | " -0.16241732239723206,\n", 669 | " -3.339862823486328,\n", 670 | " -0.6667983531951904,\n", 671 | " 0.632196307182312,\n", 672 | " -0.6274320483207703,\n", 673 | " -1.3928942680358887,\n", 674 | " 1.533515453338623,\n", 675 | " -0.13574548065662384,\n", 676 | " 0.8551362156867981,\n", 677 | " -1.3283482789993286,\n", 678 | " -1.2982256412506104,\n", 679 | " 0.27223560214042664,\n", 680 | " -2.8511362075805664,\n", 681 | " 0.7754475474357605,\n", 682 | " 2.2711400985717773,\n", 683 | " 0.4747863709926605,\n", 684 | " -1.4456276893615723,\n", 685 | " -2.112881898880005,\n", 686 | " -0.9632813930511475,\n", 687 | " 0.0278870090842247,\n", 688 | " 2.497314691543579,\n", 689 | " -2.3531808853149414,\n", 690 | " -0.6952527165412903,\n", 691 | " -0.3744198977947235,\n", 692 | " -2.411081314086914,\n", 693 | " -0.630007266998291,\n", 694 | " 0.14274953305721283,\n", 695 | " 0.33328884840011597,\n", 696 | " -1.205763339996338,\n", 697 | " -0.8301210403442383,\n", 698 | " -0.5593848824501038,\n", 699 | " 1.6403967142105103,\n", 700 | " -0.13854840397834778,\n", 701 | " -0.7952913045883179,\n", 702 | " 1.4994001388549805,\n", 703 | " 0.2246534377336502,\n", 704 | " -0.404498815536499,\n", 705 | " -1.1759082078933716,\n", 706 | " -0.017210496589541435,\n", 707 | " -1.2505768537521362,\n", 708 | " -1.0854521989822388,\n", 709 | " 0.6534472107887268,\n", 710 | " 3.972792387008667,\n", 711 | " 0.355577677488327,\n", 712 | " 0.20129622519016266,\n", 713 | " 0.03306291252374649,\n", 714 | " 0.6362214088439941,\n", 715 | " 0.5310419201850891,\n", 716 | " -0.38921380043029785,\n", 717 | " -0.49607789516448975,\n", 718 | " -0.20421843230724335,\n", 719 | " 1.8575541973114014,\n", 720 | " 2.053288459777832,\n", 721 | " -1.5407559871673584,\n", 722 | " -0.6533126831054688,\n", 723 | " 1.014625906944275,\n", 724 | " -2.1206068992614746,\n", 725 | " -0.12133312225341797,\n", 726 | " 0.18042689561843872,\n", 727 | " 5.401031494140625,\n", 728 | " 1.1001695394515991,\n", 729 | " 2.276118278503418,\n", 730 | " 0.13903433084487915,\n", 731 | " -1.8242998123168945,\n", 732 | " 1.0605734586715698,\n", 733 | " -0.3903619647026062,\n", 734 | " -0.06489279866218567,\n", 735 | " -1.2463812828063965,\n", 736 | " 1.2634238004684448,\n", 737 | " 0.7913013100624084,\n", 738 | " -1.7207236289978027,\n", 739 | " 1.0430115461349487,\n", 740 | " -0.17337414622306824,\n", 741 | " -0.5212171673774719,\n", 742 | " 0.3239036202430725,\n", 743 | " 2.2381794452667236,\n", 744 | " -1.1267319917678833,\n", 745 | " -4.690943717956543,\n", 746 | " 0.7563027143478394,\n", 747 | " 1.348197340965271,\n", 748 | " -0.3414371907711029,\n", 749 | " 1.1979689598083496,\n", 750 | " 2.180375814437866,\n", 751 | " 2.0437746047973633,\n", 752 | " 1.347714900970459,\n", 753 | " 0.5598114728927612,\n", 754 | " -0.2958389222621918,\n", 755 | " 1.0651189088821411,\n", 756 | " 0.7418068647384644,\n", 757 | " 2.1141881942749023,\n", 758 | " -0.0662030503153801,\n", 759 | " -1.9632459878921509,\n", 760 | " -1.5146234035491943,\n", 761 | " -3.3296732902526855,\n", 762 | " 2.4043805599212646,\n", 763 | " -0.8049283027648926,\n", 764 | " 0.23791731894016266,\n", 765 | " -0.8301637172698975,\n", 766 | " -0.02914416231215,\n", 767 | " 2.929199695587158,\n", 768 | " 3.0227973461151123,\n", 769 | " -1.0176151990890503,\n", 770 | " -0.6077916622161865,\n", 771 | " -0.27609843015670776,\n", 772 | " -1.734735369682312,\n", 773 | " 0.9142254590988159,\n", 774 | " 0.9077465534210205,\n", 775 | " -2.4213831424713135,\n", 776 | " -1.3981807231903076,\n", 777 | " 1.900290846824646,\n", 778 | " 0.33978399634361267,\n", 779 | " -1.7175335884094238,\n", 780 | " 32.41661071777344,\n", 781 | " 0.41115042567253113,\n", 782 | " 0.23293937742710114,\n", 783 | " -1.9812171459197998,\n", 784 | " -2.8458027839660645,\n", 785 | " 0.9738672971725464,\n", 786 | " -0.9701225757598877,\n", 787 | " 1.5288501977920532,\n", 788 | " -4.0334086418151855,\n", 789 | " -0.260423481464386,\n", 790 | " 1.6177010536193848,\n", 791 | " 0.5211339592933655,\n", 792 | " -0.8723446130752563,\n", 793 | " 0.5734013319015503,\n", 794 | " -0.7391897439956665,\n", 795 | " 0.49581849575042725,\n", 796 | " 0.6872029900550842,\n", 797 | " 0.6622218489646912,\n", 798 | " -0.7794556617736816,\n", 799 | " 1.134706735610962,\n", 800 | " 1.1726144552230835,\n", 801 | " -0.3269646465778351,\n", 802 | " -0.9531785249710083,\n", 803 | " 0.02031192183494568,\n", 804 | " 1.4282060861587524,\n", 805 | " -2.4558706283569336,\n", 806 | " -0.6481875777244568,\n", 807 | " 0.04382152482867241,\n", 808 | " 0.14982542395591736,\n", 809 | " -0.3840688467025757,\n", 810 | " 0.31624332070350647,\n", 811 | " 0.9063214659690857,\n", 812 | " -1.532709002494812,\n", 813 | " 1.3933777809143066,\n", 814 | " -0.7410053014755249,\n", 815 | " 1.4550265073776245,\n", 816 | " -0.9482161998748779,\n", 817 | " -0.518763542175293,\n", 818 | " -1.5742175579071045,\n", 819 | " -0.8558131456375122,\n", 820 | " 1.3236960172653198,\n", 821 | " 1.937267780303955,\n", 822 | " -0.6216604709625244,\n", 823 | " 0.9598169922828674,\n", 824 | " 0.5801859498023987,\n", 825 | " 2.786884069442749,\n", 826 | " -0.7338442802429199,\n", 827 | " -0.0366881899535656,\n", 828 | " 0.2843918204307556,\n", 829 | " -0.3620307743549347,\n", 830 | " 0.41800209879875183,\n", 831 | " -1.7022851705551147,\n", 832 | " -1.8655633926391602,\n", 833 | " 1.296433925628662,\n", 834 | " 1.2047148942947388,\n", 835 | " -0.30587923526763916,\n", 836 | " -0.9569429755210876,\n", 837 | " -0.6246398687362671,\n", 838 | " -0.1702265590429306,\n", 839 | " -0.19278568029403687,\n", 840 | " -2.033525228500366,\n", 841 | " 0.9834752678871155,\n", 842 | " -0.6254092454910278,\n", 843 | " 1.098067283630371,\n", 844 | " -0.6513136029243469,\n", 845 | " 0.23423711955547333,\n", 846 | " 0.495281845331192,\n", 847 | " -2.193453311920166,\n", 848 | " 1.8826056718826294,\n", 849 | " -1.284339189529419,\n", 850 | " -0.5686086416244507,\n", 851 | " 1.1019529104232788,\n", 852 | " 0.07770360261201859,\n", 853 | " -1.62596595287323,\n", 854 | " -0.9089177846908569,\n", 855 | " 0.10362625867128372,\n", 856 | " -0.22994253039360046,\n", 857 | " -0.8198869824409485,\n", 858 | " 1.4154542684555054,\n", 859 | " -0.4864971339702606,\n", 860 | " -1.8748151063919067,\n", 861 | " -1.1929281949996948,\n", 862 | " -0.06205283850431442,\n", 863 | " -1.4342405796051025,\n", 864 | " 1.6122491359710693,\n", 865 | " 1.8562037944793701,\n", 866 | " -0.1211060881614685,\n", 867 | " -0.574827253818512,\n", 868 | " -0.8028783202171326,\n", 869 | " -1.2929939031600952,\n", 870 | " 1.025201439857483,\n", 871 | " -0.9960240125656128,\n", 872 | " -0.15139217674732208,\n", 873 | " 1.0522536039352417,\n", 874 | " 0.31600841879844666,\n", 875 | " -1.3732680082321167,\n", 876 | " -27.182415008544922,\n", 877 | " 0.154983252286911,\n", 878 | " 0.6092917323112488,\n", 879 | " 0.7948514223098755,\n", 880 | " 1.0533103942871094,\n", 881 | " 0.0868465006351471,\n", 882 | " 1.1397076845169067,\n", 883 | " 0.13280442357063293,\n", 884 | " -1.3385761976242065,\n", 885 | " 1.5303831100463867,\n", 886 | " -0.5146344304084778,\n", 887 | " 0.6761888861656189,\n", 888 | " -0.41874516010284424,\n", 889 | " -0.9255934953689575,\n", 890 | " -1.2653870582580566,\n", 891 | " 1.4069006443023682,\n", 892 | " -1.6065043210983276,\n", 893 | " -1.7832735776901245,\n", 894 | " -0.2925128638744354,\n", 895 | " 1.473049521446228,\n", 896 | " 3.364495277404785,\n", 897 | " 1.0322723388671875,\n", 898 | " 3.009256601333618,\n", 899 | " 0.2865718603134155,\n", 900 | " -0.35082191228866577,\n", 901 | " 0.5058472752571106,\n", 902 | " 0.2264745533466339,\n", 903 | " 0.7411601543426514,\n", 904 | " 0.012891922146081924,\n", 905 | " 0.48189839720726013,\n", 906 | " -2.7857465744018555,\n", 907 | " 0.026944171637296677,\n", 908 | " -0.07940882444381714,\n", 909 | " 0.5382645726203918,\n", 910 | " 0.8608627319335938,\n", 911 | " -0.39351755380630493,\n", 912 | " -0.7622421383857727,\n", 913 | " 0.2533319294452667,\n", 914 | " -0.7403301000595093,\n", 915 | " 0.84891676902771,\n", 916 | " 2.2536492347717285,\n", 917 | " 1.2785656452178955,\n", 918 | " 0.43964463472366333,\n", 919 | " 1.3284823894500732,\n", 920 | " -0.3753799796104431,\n", 921 | " 1.5096975564956665,\n", 922 | " -5.795699119567871,\n", 923 | " 0.12428686767816544,\n", 924 | " -1.62626051902771,\n", 925 | " 0.13739725947380066,\n", 926 | " 1.9450013637542725,\n", 927 | " 0.10212218016386032,\n", 928 | " 0.7112809419631958,\n", 929 | " 1.5153837203979492,\n", 930 | " -0.46861499547958374,\n", 931 | " -1.4096287488937378,\n", 932 | " -1.0446267127990723,\n", 933 | " -0.45547395944595337,\n", 934 | " -2.1259732246398926,\n", 935 | " -0.6853254437446594,\n", 936 | " 0.2989114820957184,\n", 937 | " -1.0353790521621704,\n", 938 | " 2.100987672805786,\n", 939 | " -0.4579892158508301,\n", 940 | " -0.9679483771324158,\n", 941 | " -0.31305360794067383,\n", 942 | " -1.6998623609542847,\n", 943 | " -0.09843070060014725,\n", 944 | " -0.4785277247428894,\n", 945 | " 0.33112916350364685,\n", 946 | " -1.0006706714630127,\n", 947 | " 1.1701407432556152,\n", 948 | " 0.31212136149406433,\n", 949 | " 0.6311988234519958,\n", 950 | " 0.5271905064582825,\n", 951 | " 0.36075568199157715,\n", 952 | " 1.4530924558639526,\n", 953 | " 0.8412582278251648,\n", 954 | " 1.4684303998947144,\n", 955 | " 2.8496220111846924,\n", 956 | " -0.04135359823703766,\n", 957 | " -0.2275446504354477,\n", 958 | " -0.9956041574478149,\n", 959 | " -1.1148769855499268,\n", 960 | " -0.5835208892822266,\n", 961 | " 0.12220026552677155,\n", 962 | " 1.1671851873397827,\n", 963 | " 3.2550930976867676,\n", 964 | " 1.10433030128479,\n", 965 | " 9.33289623260498,\n", 966 | " -0.6712905764579773,\n", 967 | " 0.5393915176391602,\n", 968 | " -1.0416017770767212,\n", 969 | " -1.0868760347366333,\n", 970 | " -1.141477346420288,\n", 971 | " 0.8875858783721924,\n", 972 | " 0.15064752101898193,\n", 973 | " -0.41381868720054626,\n", 974 | " 0.498884379863739,\n", 975 | " 1.3068515062332153,\n", 976 | " -0.719569742679596,\n", 977 | " 0.9179916381835938,\n", 978 | " -0.31509411334991455,\n", 979 | " -1.669404149055481,\n", 980 | " 0.4562411904335022,\n", 981 | " 0.270628422498703,\n", 982 | " -0.5290043354034424,\n", 983 | " 1.3742294311523438,\n", 984 | " 0.5085194706916809,\n", 985 | " 1.2223339080810547,\n", 986 | " -0.1174226924777031,\n", 987 | " 1.5968425273895264,\n", 988 | " -0.598842203617096,\n", 989 | " -0.6910765171051025,\n", 990 | " -1.1462469100952148,\n", 991 | " -0.4406338036060333,\n", 992 | " -2.133370876312256,\n", 993 | " 1.2033441066741943,\n", 994 | " -0.15982556343078613,\n", 995 | " -0.7691492438316345,\n", 996 | " 0.8236265182495117,\n", 997 | " -1.6174235343933105,\n", 998 | " 2.456486940383911,\n", 999 | " -0.9011369943618774,\n", 1000 | " 2.728081703186035,\n", 1001 | " -1.5632096529006958,\n", 1002 | " 3.0761871337890625,\n", 1003 | " 57.3606071472168,\n", 1004 | " 0.7768049240112305,\n", 1005 | " -2.1693403720855713,\n", 1006 | " 2.3408870697021484,\n", 1007 | " 0.7840091586112976,\n", 1008 | " -0.7604485154151917,\n", 1009 | " -0.9924947023391724,\n", 1010 | " 1.0269184112548828,\n", 1011 | " 0.19607983529567719,\n", 1012 | " 1.6285783052444458,\n", 1013 | " -0.7480160593986511,\n", 1014 | " 2.099355936050415,\n", 1015 | " 0.4106958210468292,\n", 1016 | " 0.9712944626808167,\n", 1017 | " 0.5889525413513184,\n", 1018 | " -1.925346851348877,\n", 1019 | " -1.567502498626709,\n", 1020 | " 34.92219543457031,\n", 1021 | " 1.3544727563858032,\n", 1022 | " -1.3182264566421509,\n", 1023 | " 0.042217839509248734,\n", 1024 | " -0.628876268863678,\n", 1025 | " 2.2722525596618652,\n", 1026 | " -0.5237693786621094,\n", 1027 | " -0.9268882870674133,\n", 1028 | " -0.492673397064209,\n", 1029 | " 4.460019111633301,\n", 1030 | " -1.4476773738861084,\n", 1031 | " 0.13552206754684448,\n", 1032 | " 1.0895203351974487,\n", 1033 | " -0.8717625737190247,\n", 1034 | " -1.7290581464767456,\n", 1035 | " -0.7999343872070312,\n", 1036 | " 1.0853841304779053,\n", 1037 | " 0.9652037620544434,\n", 1038 | " -0.6274214386940002,\n", 1039 | " -2.6512768268585205,\n", 1040 | " -0.9348969459533691,\n", 1041 | " -1.0724680423736572,\n", 1042 | " 0.43641582131385803,\n", 1043 | " 0.008920666761696339,\n", 1044 | " -0.8348070383071899,\n", 1045 | " 0.2412656545639038,\n", 1046 | " -0.33064180612564087,\n", 1047 | " 0.8394573926925659,\n", 1048 | " -0.6390005946159363,\n", 1049 | " 0.5502236485481262,\n", 1050 | " -0.9613075852394104,\n", 1051 | " -1.49109947681427,\n", 1052 | " -0.44935205578804016,\n", 1053 | " -0.5659302473068237,\n", 1054 | " 0.9239310622215271,\n", 1055 | " -1.768730640411377,\n", 1056 | " 1.5863547325134277,\n", 1057 | " -3.1260554790496826,\n", 1058 | " 1.0818079710006714,\n", 1059 | " -1.1050350666046143,\n", 1060 | " -1.5495226383209229,\n", 1061 | " -1.082871675491333,\n", 1062 | " 1.883342981338501,\n", 1063 | " 0.6110782623291016,\n", 1064 | " 0.26857247948646545,\n", 1065 | " 2.8556275367736816,\n", 1066 | " -0.2333056479692459,\n", 1067 | " -1.3012393712997437,\n", 1068 | " -0.3937743008136749,\n", 1069 | " 0.6736701726913452,\n", 1070 | " -2.196873903274536,\n", 1071 | " 0.9187985062599182,\n", 1072 | " -1.45941162109375,\n", 1073 | " -1.273436188697815,\n", 1074 | " 1.674828052520752,\n", 1075 | " 2.722978115081787,\n", 1076 | " -2.512270212173462,\n", 1077 | " 0.8692014813423157,\n", 1078 | " 0.1126626506447792,\n", 1079 | " 0.7396382689476013,\n", 1080 | " 1.006971001625061,\n", 1081 | " 0.7232924699783325,\n", 1082 | " -0.009056083858013153,\n", 1083 | " -0.6243733167648315,\n", 1084 | " 0.3752109706401825,\n", 1085 | " 0.014827475883066654,\n", 1086 | " -2.5610055923461914,\n", 1087 | " -0.19200186431407928,\n", 1088 | " -0.9594016075134277,\n", 1089 | " 2.603450298309326,\n", 1090 | " 0.40766817331314087,\n", 1091 | " 0.8929102420806885,\n", 1092 | " 2.4351093769073486,\n", 1093 | " 0.7876821756362915,\n", 1094 | " -1.0059723854064941,\n", 1095 | " 0.14938947558403015,\n", 1096 | " -2.179485559463501,\n", 1097 | " 1.7925609350204468,\n", 1098 | " 1.3205926418304443,\n", 1099 | " 1.025534749031067,\n", 1100 | " 3.9275193214416504,\n", 1101 | " 1.4562275409698486,\n", 1102 | " 1.1396267414093018,\n", 1103 | " 0.2919597923755646,\n", 1104 | " 1.5426069498062134,\n", 1105 | " -15.719386100769043,\n", 1106 | " 0.09793777763843536,\n", 1107 | " 2.2827651500701904,\n", 1108 | " -2.3987295627593994,\n", 1109 | " -1.8260499238967896,\n", 1110 | " -2.0293259620666504,\n", 1111 | " -0.6957385540008545,\n", 1112 | " -0.2188287228345871,\n", 1113 | " -1.33565092086792,\n", 1114 | " -0.3563850224018097,\n", 1115 | " 0.4016251862049103,\n", 1116 | " -1.400220274925232,\n", 1117 | " -1.6705321073532104,\n", 1118 | " -0.24890395998954773,\n", 1119 | " 0.009524766355752945,\n", 1120 | " 1.6684049367904663,\n", 1121 | " 1.5606313943862915,\n", 1122 | " 1.0772886276245117,\n", 1123 | " -2.7861669063568115,\n", 1124 | " 0.18575261533260345,\n", 1125 | " 0.5765672326087952,\n", 1126 | " 1.4266549348831177,\n", 1127 | " 2.376008987426758,\n", 1128 | " -2.694223403930664,\n", 1129 | " 0.34737876057624817,\n", 1130 | " 0.9531977772712708,\n", 1131 | " 1.3848310708999634,\n", 1132 | " 2.5496339797973633,\n", 1133 | " -0.149235799908638,\n", 1134 | " -0.5126417875289917,\n", 1135 | " 0.20302452147006989,\n", 1136 | " -1.5953525304794312,\n", 1137 | " -0.11273770779371262,\n", 1138 | " -0.16092927753925323,\n", 1139 | " -0.4749731421470642,\n", 1140 | " 1.1015260219573975,\n", 1141 | " 0.4913117587566376,\n", 1142 | " 0.9037510752677917,\n", 1143 | " -0.8177967071533203,\n", 1144 | " -1.160469651222229,\n", 1145 | " 1.0617918968200684,\n", 1146 | " 1.5581883192062378,\n", 1147 | " -2.0320003032684326,\n", 1148 | " -0.9873416423797607,\n", 1149 | " -2.269489049911499,\n", 1150 | " 0.53379887342453,\n", 1151 | " -2.8140106201171875,\n", 1152 | " -0.027145445346832275,\n", 1153 | " -0.6571286916732788,\n", 1154 | " 0.04527514800429344,\n", 1155 | " -2.2338309288024902,\n", 1156 | " 0.7639337182044983,\n", 1157 | " 0.227696493268013,\n", 1158 | " 0.9214191436767578,\n", 1159 | " -1.623695731163025,\n", 1160 | " 0.4885410666465759,\n", 1161 | " -2.0437586307525635,\n", 1162 | " 1.064626932144165,\n", 1163 | " -13.280803680419922,\n", 1164 | " 1.3395452499389648,\n", 1165 | " -2.1115128993988037,\n", 1166 | " -1.4313369989395142,\n", 1167 | " 0.19674326479434967,\n", 1168 | " -0.043386004865169525,\n", 1169 | " 0.1957242637872696,\n", 1170 | " -0.7178359627723694,\n", 1171 | " -0.08310665190219879,\n", 1172 | " -0.9032981991767883,\n", 1173 | " 0.9152768850326538,\n", 1174 | " 2.4644291400909424,\n", 1175 | " -0.38881921768188477,\n", 1176 | " -0.6886571645736694,\n", 1177 | " -1.5615381002426147,\n", 1178 | " 1.37763512134552,\n", 1179 | " -0.34001263976097107,\n", 1180 | " -0.6143917441368103,\n", 1181 | " 2.5646674633026123,\n", 1182 | " -0.20668303966522217,\n", 1183 | " -0.4762762188911438,\n", 1184 | " 0.1925143599510193,\n", 1185 | " 2.094196319580078,\n", 1186 | " -0.1659897416830063,\n", 1187 | " ...]" 1188 | ] 1189 | }, 1190 | "execution_count": 12, 1191 | "metadata": {}, 1192 | "output_type": "execute_result" 1193 | } 1194 | ], 1195 | "source": [ 1196 | "embedding_vector = embeddings.embed_query(query)\n", 1197 | "embedding_vector" 1198 | ] 1199 | }, 1200 | { 1201 | "cell_type": "code", 1202 | "execution_count": 14, 1203 | "metadata": {}, 1204 | "outputs": [], 1205 | "source": [ 1206 | "docs_score = database.similarity_search_with_score(embedding_vector)" 1207 | ] 1208 | }, 1209 | { 1210 | "cell_type": "code", 1211 | "execution_count": 15, 1212 | "metadata": {}, 1213 | "outputs": [ 1214 | { 1215 | "name": "stdout", 1216 | "output_type": "stream", 1217 | "text": [ 1218 | "[(Document(id='2a18d4c6-7942-4f8f-8002-49cee08ab4ac', metadata={'source': './data/speech.txt'}, page_content='I am a Data Scientist, Open-source Contributor, and Frontend Engineer with years of experience in the tech industry. Throughout my career, I’ve been privileged to build innovative products, solve real-world problems, and empower over 5,000 individuals to grow into better versions of themselves. My work and insights have also connected me with an audience of over 10,000 followers across social media platforms.'), 14313.063), (Document(id='4ec1d08b-e438-41c0-9cbf-7a07324044b4', metadata={'source': './data/speech.txt'}, page_content='Looking ahead, my vision is to create groundbreaking projects and support millions—if not billions—of people in achieving their goals and thriving in the tech industry.'), 15009.113), (Document(id='d497400c-27fa-46e6-9bce-d25782a8009a', metadata={'source': './data/speech.txt'}, page_content='In this program, I am here to guide you every step of the way, ensuring you have an unforgettable learning experience.'), 15084.582), (Document(id='2c8aea5c-a577-457b-9bb0-1ab57afaacfc', metadata={'source': './data/speech.txt'}, page_content='Hello!'), 15978.035)]\n" 1219 | ] 1220 | } 1221 | ], 1222 | "source": [ 1223 | "print(docs_score)" 1224 | ] 1225 | }, 1226 | { 1227 | "cell_type": "code", 1228 | "execution_count": 16, 1229 | "metadata": {}, 1230 | "outputs": [], 1231 | "source": [ 1232 | "# saving and loading the database\n", 1233 | "database.save_local(\"faiss_db\")" 1234 | ] 1235 | }, 1236 | { 1237 | "cell_type": "code", 1238 | "execution_count": 17, 1239 | "metadata": {}, 1240 | "outputs": [], 1241 | "source": [ 1242 | "new_database = FAISS.load_local(\"faiss_db\", embeddings, allow_dangerous_deserialization=True)" 1243 | ] 1244 | }, 1245 | { 1246 | "cell_type": "code", 1247 | "execution_count": 18, 1248 | "metadata": {}, 1249 | "outputs": [], 1250 | "source": [ 1251 | "docs = new_database.similarity_search(query)" 1252 | ] 1253 | }, 1254 | { 1255 | "cell_type": "code", 1256 | "execution_count": 20, 1257 | "metadata": {}, 1258 | "outputs": [ 1259 | { 1260 | "data": { 1261 | "text/plain": [ 1262 | "'Looking ahead, my vision is to create groundbreaking projects and support millions—if not billions—of people in achieving their goals and thriving in the tech industry.'" 1263 | ] 1264 | }, 1265 | "execution_count": 20, 1266 | "metadata": {}, 1267 | "output_type": "execute_result" 1268 | } 1269 | ], 1270 | "source": [ 1271 | "docs[0].page_content" 1272 | ] 1273 | } 1274 | ], 1275 | "metadata": { 1276 | "kernelspec": { 1277 | "display_name": "Python 3", 1278 | "language": "python", 1279 | "name": "python3" 1280 | }, 1281 | "language_info": { 1282 | "codemirror_mode": { 1283 | "name": "ipython", 1284 | "version": 3 1285 | }, 1286 | "file_extension": ".py", 1287 | "mimetype": "text/x-python", 1288 | "name": "python", 1289 | "nbconvert_exporter": "python", 1290 | "pygments_lexer": "ipython3", 1291 | "version": "3.10.0" 1292 | } 1293 | }, 1294 | "nbformat": 4, 1295 | "nbformat_minor": 2 1296 | } 1297 | -------------------------------------------------------------------------------- /4.0-vector_store_db/4.2-chromadb.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "#### Chroma\n", 8 | "Chroma is a AI-native open-source vector database focused on developer productivity and happiness. Chroma is licensed under Apache 2.0.\n", 9 | "\n", 10 | "https://python.langchain.com/v0.2/docs/integrations/vectorstores/" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 2, 16 | "metadata": {}, 17 | "outputs": [], 18 | "source": [ 19 | "from langchain_chroma import Chroma\n", 20 | "from langchain_community.document_loaders import TextLoader\n", 21 | "from langchain_community.embeddings import OllamaEmbeddings\n", 22 | "from langchain_text_splitters import RecursiveCharacterTextSplitter" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 3, 28 | "metadata": {}, 29 | "outputs": [ 30 | { 31 | "data": { 32 | "text/plain": [ 33 | "[Document(metadata={'source': './data/speech.txt'}, page_content='Hello!\\nMy name is DAVEWORLD, and I am excited to be your tutor for this program.\\n\\nI am a Data Scientist, Open-source Contributor, and Frontend Engineer with years of experience in the tech industry. Throughout my career, I’ve been privileged to build innovative products, solve real-world problems, and empower over 5,000 individuals to grow into better versions of themselves. My work and insights have also connected me with an audience of over 10,000 followers across social media platforms.\\nLooking ahead, my vision is to create groundbreaking projects and support millions—if not billions—of people in achieving their goals and thriving in the tech industry.\\n\\nIn this program, I am here to guide you every step of the way, ensuring you have an unforgettable learning experience.\\nLet’s embark on this transformative journey together!')]" 34 | ] 35 | }, 36 | "execution_count": 3, 37 | "metadata": {}, 38 | "output_type": "execute_result" 39 | } 40 | ], 41 | "source": [ 42 | "loader = TextLoader(\"./data/speech.txt\")\n", 43 | "data = loader.load()\n", 44 | "data" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 4, 50 | "metadata": {}, 51 | "outputs": [], 52 | "source": [ 53 | "text_splitter = RecursiveCharacterTextSplitter(chunk_size=200, chunk_overlap=10)\n", 54 | "splits = text_splitter.split_documents(data)" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 5, 60 | "metadata": {}, 61 | "outputs": [ 62 | { 63 | "data": { 64 | "text/plain": [ 65 | "[Document(metadata={'source': './data/speech.txt'}, page_content='Hello!\\nMy name is DAVEWORLD, and I am excited to be your tutor for this program.'),\n", 66 | " Document(metadata={'source': './data/speech.txt'}, page_content='I am a Data Scientist, Open-source Contributor, and Frontend Engineer with years of experience in the tech industry. Throughout my career, I’ve been privileged to build innovative products, solve'),\n", 67 | " Document(metadata={'source': './data/speech.txt'}, page_content='solve real-world problems, and empower over 5,000 individuals to grow into better versions of themselves. My work and insights have also connected me with an audience of over 10,000 followers across'),\n", 68 | " Document(metadata={'source': './data/speech.txt'}, page_content='across social media platforms.'),\n", 69 | " Document(metadata={'source': './data/speech.txt'}, page_content='Looking ahead, my vision is to create groundbreaking projects and support millions—if not billions—of people in achieving their goals and thriving in the tech industry.'),\n", 70 | " Document(metadata={'source': './data/speech.txt'}, page_content='In this program, I am here to guide you every step of the way, ensuring you have an unforgettable learning experience.\\nLet’s embark on this transformative journey together!')]" 71 | ] 72 | }, 73 | "execution_count": 5, 74 | "metadata": {}, 75 | "output_type": "execute_result" 76 | } 77 | ], 78 | "source": [ 79 | "splits" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": 7, 85 | "metadata": {}, 86 | "outputs": [ 87 | { 88 | "data": { 89 | "text/plain": [ 90 | "" 91 | ] 92 | }, 93 | "execution_count": 7, 94 | "metadata": {}, 95 | "output_type": "execute_result" 96 | } 97 | ], 98 | "source": [ 99 | "embeddings = OllamaEmbeddings(model=\"gemma2:2b\")\n", 100 | "vectordb = Chroma.from_documents(documents=splits, embedding=embeddings)\n", 101 | "vectordb" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": 8, 107 | "metadata": {}, 108 | "outputs": [ 109 | { 110 | "data": { 111 | "text/plain": [ 112 | "'Looking ahead, my vision is to create groundbreaking projects and support millions—if not billions—of people in achieving their goals and thriving in the tech industry.'" 113 | ] 114 | }, 115 | "execution_count": 8, 116 | "metadata": {}, 117 | "output_type": "execute_result" 118 | } 119 | ], 120 | "source": [ 121 | "query = \"WHat's the speaker future ambition?\"\n", 122 | "docs = vectordb.similarity_search(query)\n", 123 | "docs[0].page_content\n" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": 9, 129 | "metadata": {}, 130 | "outputs": [], 131 | "source": [ 132 | "vectordb = Chroma.from_documents(documents=splits, embedding=embeddings, persist_directory=\"./chroma_db\")" 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": 10, 138 | "metadata": {}, 139 | "outputs": [ 140 | { 141 | "name": "stdout", 142 | "output_type": "stream", 143 | "text": [ 144 | "Looking ahead, my vision is to create groundbreaking projects and support millions—if not billions—of people in achieving their goals and thriving in the tech industry.\n" 145 | ] 146 | } 147 | ], 148 | "source": [ 149 | "# load the db\n", 150 | "database = Chroma(persist_directory=\"./chroma_db\", embedding_function=embeddings)\n", 151 | "docs = database.similarity_search(query)\n", 152 | "print(docs[0].page_content)" 153 | ] 154 | }, 155 | { 156 | "cell_type": "code", 157 | "execution_count": 12, 158 | "metadata": {}, 159 | "outputs": [ 160 | { 161 | "data": { 162 | "text/plain": [ 163 | "[(Document(id='d1fd407a-be40-4a65-b079-78364e7572b5', metadata={'source': './data/speech.txt'}, page_content='Looking ahead, my vision is to create groundbreaking projects and support millions—if not billions—of people in achieving their goals and thriving in the tech industry.'),\n", 164 | " 4627.60037707944),\n", 165 | " (Document(id='23f384af-28f2-48d2-ac1e-59da2db340ee', metadata={'source': './data/speech.txt'}, page_content='across social media platforms.'),\n", 166 | " 4930.159339123306),\n", 167 | " (Document(id='dc6a9259-2a72-4d98-b861-3e05fb935453', metadata={'source': './data/speech.txt'}, page_content='In this program, I am here to guide you every step of the way, ensuring you have an unforgettable learning experience.\\nLet’s embark on this transformative journey together!'),\n", 168 | " 5306.030743067734),\n", 169 | " (Document(id='e2f2da87-54fa-4b9a-9a9a-1a6de5363e2e', metadata={'source': './data/speech.txt'}, page_content='Hello!\\nMy name is DAVEWORLD, and I am excited to be your tutor for this program.'),\n", 170 | " 5682.998784135277)]" 171 | ] 172 | }, 173 | "execution_count": 12, 174 | "metadata": {}, 175 | "output_type": "execute_result" 176 | } 177 | ], 178 | "source": [ 179 | "# similiarity search with score\n", 180 | "docs = vectordb.similarity_search_with_score(query)\n", 181 | "docs" 182 | ] 183 | }, 184 | { 185 | "cell_type": "code", 186 | "execution_count": 14, 187 | "metadata": {}, 188 | "outputs": [], 189 | "source": [ 190 | "# Retrieval\n", 191 | "retriever = vectordb.as_retriever()\n", 192 | "retriever_result = retriever.invoke(query)" 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": 15, 198 | "metadata": {}, 199 | "outputs": [ 200 | { 201 | "data": { 202 | "text/plain": [ 203 | "'Looking ahead, my vision is to create groundbreaking projects and support millions—if not billions—of people in achieving their goals and thriving in the tech industry.'" 204 | ] 205 | }, 206 | "execution_count": 15, 207 | "metadata": {}, 208 | "output_type": "execute_result" 209 | } 210 | ], 211 | "source": [ 212 | "retriever_result[0].page_content" 213 | ] 214 | } 215 | ], 216 | "metadata": { 217 | "kernelspec": { 218 | "display_name": "Python 3", 219 | "language": "python", 220 | "name": "python3" 221 | }, 222 | "language_info": { 223 | "codemirror_mode": { 224 | "name": "ipython", 225 | "version": 3 226 | }, 227 | "file_extension": ".py", 228 | "mimetype": "text/x-python", 229 | "name": "python", 230 | "nbconvert_exporter": "python", 231 | "pygments_lexer": "ipython3", 232 | "version": "3.10.0" 233 | } 234 | }, 235 | "nbformat": 4, 236 | "nbformat_minor": 2 237 | } 238 | -------------------------------------------------------------------------------- /4.0-vector_store_db/chroma_db/bfca9c1c-3c72-4c8f-9f79-0acc72382eab/header.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EniolaAdemola/langchain-basics/d0c47b35f41ac7810b4ed21015bca04e8012baa8/4.0-vector_store_db/chroma_db/bfca9c1c-3c72-4c8f-9f79-0acc72382eab/header.bin -------------------------------------------------------------------------------- /4.0-vector_store_db/chroma_db/bfca9c1c-3c72-4c8f-9f79-0acc72382eab/length.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EniolaAdemola/langchain-basics/d0c47b35f41ac7810b4ed21015bca04e8012baa8/4.0-vector_store_db/chroma_db/bfca9c1c-3c72-4c8f-9f79-0acc72382eab/length.bin -------------------------------------------------------------------------------- /4.0-vector_store_db/chroma_db/bfca9c1c-3c72-4c8f-9f79-0acc72382eab/link_lists.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EniolaAdemola/langchain-basics/d0c47b35f41ac7810b4ed21015bca04e8012baa8/4.0-vector_store_db/chroma_db/bfca9c1c-3c72-4c8f-9f79-0acc72382eab/link_lists.bin -------------------------------------------------------------------------------- /4.0-vector_store_db/chroma_db/chroma.sqlite3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EniolaAdemola/langchain-basics/d0c47b35f41ac7810b4ed21015bca04e8012baa8/4.0-vector_store_db/chroma_db/chroma.sqlite3 -------------------------------------------------------------------------------- /4.0-vector_store_db/data/speech.txt: -------------------------------------------------------------------------------- 1 | Hello! 2 | My name is DAVEWORLD, and I am excited to be your tutor for this program. 3 | 4 | I am a Data Scientist, Open-source Contributor, and Frontend Engineer with years of experience in the tech industry. Throughout my career, I’ve been privileged to build innovative products, solve real-world problems, and empower over 5,000 individuals to grow into better versions of themselves. My work and insights have also connected me with an audience of over 10,000 followers across social media platforms. 5 | Looking ahead, my vision is to create groundbreaking projects and support millions—if not billions—of people in achieving their goals and thriving in the tech industry. 6 | 7 | In this program, I am here to guide you every step of the way, ensuring you have an unforgettable learning experience. 8 | Let’s embark on this transformative journey together! -------------------------------------------------------------------------------- /4.0-vector_store_db/faiss_db/index.faiss: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EniolaAdemola/langchain-basics/d0c47b35f41ac7810b4ed21015bca04e8012baa8/4.0-vector_store_db/faiss_db/index.faiss -------------------------------------------------------------------------------- /4.0-vector_store_db/faiss_db/index.pkl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EniolaAdemola/langchain-basics/d0c47b35f41ac7810b4ed21015bca04e8012baa8/4.0-vector_store_db/faiss_db/index.pkl -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LangChain Basics 🦜️🔗 2 | 3 | A practical introduction to LangChain framework for developing applications powered by language models. 4 | 5 | ![LangChain Logo](https://langchain.com/assets/logo-dark.svg) 6 | 7 | ## Overview 8 | 9 | This repository contains Jupyter notebooks and code samples demonstrating fundamental LangChain concepts. Perfect for developers starting their journey in LLM-powered application development. 10 | 11 | ## What is LangChain? 12 | 13 | LangChain is a framework that simplifies: 14 | 15 | - Building context-aware applications 16 | - Connecting language models to data sources 17 | - Creating powerful pipelines (chains) for text processing 18 | 19 | ## Repository Contents 20 | 21 | ### 🧠 Foundational Concepts 22 | 23 | **0.0 - OpenAI Basics** 24 | 25 | - Prompt template fundamentals 26 | - Chain input/output parsing 27 | - Basic LLM interaction patterns 28 | 29 | **0.0 - LangChain Chatbot using Ollama** 30 | 31 | - Build mini-chatbot using Gemma + Ollama 32 | - Local LLM integration 33 | - Conversation memory basics 34 | 35 | ### 📊 Data Pipeline Implementation 36 | 37 | **1. Data Ingestion Techniques** 38 | 39 | - Document loaders (PDF, HTML, Markdown) 40 | - Web scraping integration 41 | - Structured/unstructured data loading 42 | 43 | **2. Data Transformation** 44 | 45 | - Text splitting strategies 46 | - Chunk size optimization 47 | - Overlap techniques for context preservation 48 | 49 | **3. Embeddings & Vectorization** 50 | 51 | - OpenAI embeddings implementation 52 | - Hugging Face sentence transformers 53 | - Ollama local embedding models 54 | - Embedding comparison/analysis 55 | 56 | **4. Vector Databases** 57 | 58 | - ChromaDB integration 59 | - FAISS similarity search 60 | - Vector store persistence 61 | - Retrieval-augmented generation (RAG) setup 62 | 63 | ## How to Use This Repository 64 | 65 | 1. Clone the repository: 66 | 67 | ```bash 68 | git clone https://github.com/EniolaAdemola/langchain-basics.git 69 | 70 | ``` 71 | 72 | 2. Navigate to the project directory: 73 | 74 | ```bash 75 | cd langchain-basics 76 | ``` 77 | 78 | 3. Install the required dependencies: 79 | 80 | (**Recommended**: make sure to create your virtual environment, activate it and run the command below) 81 | 82 | ```bash 83 | pip install -r requirements.txt 84 | ``` 85 | 86 | 🎥 Watch the tutorial here: [Step by step video on how to create virtual environment using conda and python](https://youtu.be/QQlaX9SNcbY?si=bFBew44ABO5qIrXh) 87 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | langchain 2 | openai 3 | ipykernel 4 | langchain-community 5 | python-dotenv 6 | pypdf 7 | bs4 8 | arxiv 9 | pymupdf 10 | wikipedia 11 | langchain-text-splitters 12 | lxml 13 | langchain-openai 14 | chromadb 15 | sentence-transformers 16 | langchain_huggingface 17 | faiss-cpu 18 | langchain_chroma 19 | streamlit --------------------------------------------------------------------------------