diff --git a/Solar-Fullstack-LLM-101/09_1_Smart_RAG.ipynb b/Solar-Fullstack-LLM-101/09_1_Smart_RAG.ipynb index 048ab7d..11f3481 100644 --- a/Solar-Fullstack-LLM-101/09_1_Smart_RAG.ipynb +++ b/Solar-Fullstack-LLM-101/09_1_Smart_RAG.ipynb @@ -1,400 +1,477 @@ { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "\n", - "\"Open\n", - "" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# 09. Smart RAG\n", - "\n", - "## Overview \n", - "In this exercise, we will explore the concept of Smart Retrieval-Augmented Generation (Smart RAG) using the Solar framework. Smart RAG enhances the standard RAG approach by incorporating intelligent retrieval mechanisms to select the most relevant and high-quality information from external sources. This notebook will guide you through implementing Smart RAG and demonstrate its effectiveness in generating more accurate and contextually appropriate responses.\n", - " \n", - "## Purpose of the Exercise\n", - "The purpose of this exercise is to demonstrate the advanced application of Smart Retrieval-Augmented Generation within the Solar framework. By the end of this tutorial, users will understand how to leverage intelligent retrieval techniques to improve the relevance and accuracy of the language model’s outputs, thereby enhancing its overall performance and reliability.\n", - "\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Explanation of the Code: Smart Retrieval Augmented Generation (RAG)\n", - "![smartRAG](./figures/a_in.png)\n", - "\n", - "### High-Level Overview\n", - "\n", - "The code demonstrates a smart Retrieval Augmented Generation (RAG) system that combines local retrieval with external search capabilities. The main goal is to provide relevant context for answering user questions by first searching a local vector database and then falling back to an external search service if the local context is insufficient.\n", - "\n", - "\n", - "The code defines two main functions:\n", - "\n", - "\n", - " 1. is_in: Determines whether the answer to a given question can be found within the provided context.\n", - "smart_rag: Retrieves relevant context for a given question, either from the local vector database or an external search service, and generates an answer using the retrieved context.\n", - "\n", - " 1. The code uses the LangChain library for generating prompts and invoking language models, as well as the Tavily API for external search capabilities.\n", - "\n", - "\n", - "### Detailed Explanation \n", - "\n", - "1. The code starts by defining the is_in function, which takes a question and context as input and determines whether the answer to the question can be found within the context.\n", - " * It defines a prompt template called is_in_conetxt that asks the language model to check if the answer is in the context and return \"yes\" or \"no\".\n", - " * The prompt template is used to create a ChatPromptTemplate object.\n", - " * A chain of operations is constructed using the | operator:\n", - " * The ChatPromptTemplate is passed to the ChatUpstage model.\n", - " * The model's output is parsed using the StrOutputParser.\n", - " * The chain is invoked with the question and context, and the response is stored in the response variable.\n", - " * The function returns True if the response starts with \"yes\" (case-insensitive), indicating that the answer is in the context.\n", - "\n", - "1. The code then demonstrates the usage of the is_in function with two example questions and their corresponding contexts retrieved from a retriever.\n", - "\n", - "1. Next, the code defines the smart_rag function, which takes a question as input and generates an answer using the retrieved context.\n", - " * It first retrieves the context for the question using the retriever.invoke method.\n", - " * If the is_in function determines that the answer is not in the retrieved context, it falls back to searching for additional context using the Tavily API.\n", - " * The retrieved context (either from the local retriever or Tavily) is stored in the context variable.\n", - " * A chain of operations is constructed using the | operator:\n", - " * The rag_with_history_prompt (not shown in the code snippet) is used as the prompt template.\n", - " * The prompt is passed to the llm language model.\n", - " * The model's output is parsed using the StrOutputParser.\n", - " * The chain is invoked with the conversation history, retrieved context, and the question, and the generated answer is returned.\n", - "\n", - "1. Finally, the code demonstrates the usage of the smart_rag function with two example questions:\n", - " * \"What is DUS?\": The answer is expected to be found in the local context.\n", - " * \"What's the population of San Francisco?\": The answer is not expected to be found in the local context, so it falls back to searching with Tavily.\n", - "\n", - "This code showcases how LangChain can be used to build a smart RAG system that combines local retrieval with external search capabilities. By first searching a local vector database and falling back to an external search service if needed, the system aims to provide relevant context for generating accurate answers to user questions." - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [], - "source": [ - "! pip3 install -qU markdownify langchain-upstage rank_bm25 python-dotenv" - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [], - "source": [ - "# @title set API key\n", - "import os\n", - "import getpass\n", - "from pprint import pprint\n", - "import warnings\n", - "\n", - "warnings.filterwarnings(\"ignore\")\n", - "\n", - "from IPython import get_ipython\n", - "\n", - "if \"google.colab\" in str(get_ipython()):\n", - " # Running in Google Colab. Please set the UPSTAGE_API_KEY in the Colab Secrets\n", - " from google.colab import userdata\n", - " os.environ[\"UPSTAGE_API_KEY\"] = userdata.get(\"UPSTAGE_API_KEY\")\n", - "else:\n", - " # Running locally. Please set the UPSTAGE_API_KEY in the .env file\n", - " from dotenv import load_dotenv\n", - "\n", - " load_dotenv()\n", - "\n", - "if \"UPSTAGE_API_KEY\" not in os.environ:\n", - " os.environ[\"UPSTAGE_API_KEY\"] = getpass.getpass(\"Enter your Upstage API key: \")\n" -] - - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": {}, - "outputs": [], - "source": [ - "solar_summary = \"\"\"\n", - "SOLAR 10.7B: Scaling Large Language Models with Simple yet Effective Depth Up-Scaling\n", - "\n", - "We introduce SOLAR 10.7B, a large language model (LLM) with 10.7 billion parameters, \n", - "demonstrating superior performance in various natural language processing (NLP) tasks. \n", - "Inspired by recent efforts to efficiently up-scale LLMs, \n", - "we present a method for scaling LLMs called depth up-scaling (DUS), \n", - "which encompasses depthwise scaling and continued pretraining.\n", - "In contrast to other LLM up-scaling methods that use mixture-of-experts, \n", - "DUS does not require complex changes to train and inference efficiently. \n", - "We show experimentally that DUS is simple yet effective \n", - "in scaling up high-performance LLMs from small ones. \n", - "Building on the DUS model, we additionally present SOLAR 10.7B-Instruct, \n", - "a variant fine-tuned for instruction-following capabilities, \n", - "surpassing Mixtral-8x7B-Instruct. \n", - "SOLAR 10.7B is publicly available under the Apache 2.0 license, \n", - "promoting broad access and application in the LLM field.\n", - "\"\"\"" - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": {}, - "outputs": [], - "source": [ - "from langchain_core.prompts import PromptTemplate\n", - "from langchain_core.output_parsers import StrOutputParser\n", - "from langchain_upstage import ChatUpstage\n", - "\n", - "llm = ChatUpstage()\n", - "\n", - "\n", - "prompt_template = PromptTemplate.from_template(\n", - " \"\"\"\n", - " Please provide answer from the following context. \n", - " If the answer is not present in the context, please write \"The information is not present in the context.\"\n", - "\n", - " ---\n", - " Question: {question}\n", - " ---\n", - " Context: {context}\n", - " \"\"\"\n", - ")\n", - "chain = prompt_template | llm | StrOutputParser()" - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [ + "cells": [ { - "data": { - "text/plain": [ - "'The answer is: DUS stands for Depth Up-Scaling.'" + "cell_type": "markdown", + "metadata": { + "id": "NtR872vkubFf" + }, + "source": [ + "\n", + "\"Open\n", + "" ] - }, - "execution_count": 21, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "chain.invoke({\"question\": \"What is DUS?\", \"context\": solar_summary})" - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "metadata": {}, - "outputs": [ + }, { - "data": { - "text/plain": [ - "'The information is not present in the context.'" + "cell_type": "markdown", + "metadata": { + "id": "ZM1FJk2CubFg" + }, + "source": [ + "# 09. Smart RAG\n", + "\n", + "## Overview \n", + "In this exercise, we will explore the concept of Smart Retrieval-Augmented Generation (Smart RAG) using the Solar framework. Smart RAG enhances the standard RAG approach by incorporating intelligent retrieval mechanisms to select the most relevant and high-quality information from external sources. This notebook will guide you through implementing Smart RAG and demonstrate its effectiveness in generating more accurate and contextually appropriate responses.\n", + "\n", + "## Purpose of the Exercise\n", + "The purpose of this exercise is to demonstrate the advanced application of Smart Retrieval-Augmented Generation within the Solar framework. By the end of this tutorial, users will understand how to leverage intelligent retrieval techniques to improve the relevance and accuracy of the language model’s outputs, thereby enhancing its overall performance and reliability.\n", + "\n" ] - }, - "execution_count": 22, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "chain.invoke({\"question\": \"How to get to Seoul from SF\", \"context\": solar_summary})" - ] - }, - { - "cell_type": "code", - "execution_count": 23, - "metadata": {}, - "outputs": [], - "source": [ - "# RAG or Search?\n", - "def is_in(question, context):\n", - " is_in_conetxt = \"\"\"As a helpful assistant, \n", - "please use your best judgment to determine if the answer to the question is within the given context. \n", - "If the answer is present in the context, please respond with \"yes\". \n", - "If not, please respond with \"no\". \n", - "Only provide \"yes\" or \"no\" and avoid including any additional information. \n", - "Please do your best. Here is the question and the context:\n", - "---\n", - "CONTEXT: {context}\n", - "---\n", - "QUESTION: {question}\n", - "---\n", - "OUTPUT (yes or no):\"\"\"\n", - "\n", - " is_in_prompt = PromptTemplate.from_template(is_in_conetxt)\n", - " chain = is_in_prompt | ChatUpstage() | StrOutputParser()\n", - "\n", - " response = chain.invoke({\"context\": context, \"question\": question})\n", - " print(response)\n", - " return response.lower().startswith(\"yes\")" - ] - }, - { - "cell_type": "code", - "execution_count": 24, - "metadata": {}, - "outputs": [ + }, { - "name": "stdout", - "output_type": "stream", - "text": [ - "no\n" - ] + "cell_type": "markdown", + "metadata": { + "id": "hb4nFl6bubFh" + }, + "source": [ + "## Explanation of the Code: Smart Retrieval Augmented Generation (RAG)\n", + "![smartRAG](https://github.com/UpstageAI/cookbook/blob/main/Solar-Fullstack-LLM-101/figures/a_in.png?raw=1)\n", + "\n", + "### High-Level Overview\n", + "\n", + "The code demonstrates a smart Retrieval Augmented Generation (RAG) system that combines local retrieval with external search capabilities. The main goal is to provide relevant context for answering user questions by first searching a local vector database and then falling back to an external search service if the local context is insufficient.\n", + "\n", + "\n", + "The code defines two main functions:\n", + "\n", + "\n", + " 1. is_in: Determines whether the answer to a given question can be found within the provided context.\n", + "smart_rag: Retrieves relevant context for a given question, either from the local vector database or an external search service, and generates an answer using the retrieved context.\n", + "\n", + " 1. The code uses the LangChain library for generating prompts and invoking language models, as well as the Tavily API for external search capabilities.\n", + "\n", + "\n", + "### Detailed Explanation\n", + "\n", + "1. The code starts by defining the is_in function, which takes a question and context as input and determines whether the answer to the question can be found within the context.\n", + " * It defines a prompt template called is_in_conetxt that asks the language model to check if the answer is in the context and return \"yes\" or \"no\".\n", + " * The prompt template is used to create a ChatPromptTemplate object.\n", + " * A chain of operations is constructed using the | operator:\n", + " * The ChatPromptTemplate is passed to the ChatUpstage model.\n", + " * The model's output is parsed using the StrOutputParser.\n", + " * The chain is invoked with the question and context, and the response is stored in the response variable.\n", + " * The function returns True if the response starts with \"yes\" (case-insensitive), indicating that the answer is in the context.\n", + "\n", + "1. The code then demonstrates the usage of the is_in function with two example questions and their corresponding contexts retrieved from a retriever.\n", + "\n", + "1. Next, the code defines the smart_rag function, which takes a question as input and generates an answer using the retrieved context.\n", + " * It first retrieves the context for the question using the retriever.invoke method.\n", + " * If the is_in function determines that the answer is not in the retrieved context, it falls back to searching for additional context using the Tavily API.\n", + " * The retrieved context (either from the local retriever or Tavily) is stored in the context variable.\n", + " * A chain of operations is constructed using the | operator:\n", + " * The rag_with_history_prompt (not shown in the code snippet) is used as the prompt template.\n", + " * The prompt is passed to the llm language model.\n", + " * The model's output is parsed using the StrOutputParser.\n", + " * The chain is invoked with the conversation history, retrieved context, and the question, and the generated answer is returned.\n", + "\n", + "1. Finally, the code demonstrates the usage of the smart_rag function with two example questions:\n", + " * \"What is DUS?\": The answer is expected to be found in the local context.\n", + " * \"What's the population of San Francisco?\": The answer is not expected to be found in the local context, so it falls back to searching with Tavily.\n", + "\n", + "This code showcases how LangChain can be used to build a smart RAG system that combines local retrieval with external search capabilities. By first searching a local vector database and falling back to an external search service if needed, the system aims to provide relevant context for generating accurate answers to user questions." + ] }, { - "data": { - "text/plain": [ - "False" + "cell_type": "code", + "execution_count": 1, + "metadata": { + "id": "MfS1RCpmubFh", + "outputId": "20a07176-42bb-4faf-ca69-c0fdeb1cf086", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "outputs": [], + "source": [ + "! pip3 install -qU markdownify langchain-upstage rank_bm25 python-dotenv tavily-python" ] - }, - "execution_count": 24, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "is_in(\"How to get to Seoul from SF\", solar_summary)" - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "metadata": {}, - "outputs": [ + }, { - "name": "stdout", - "output_type": "stream", - "text": [ - "yes\n" + "cell_type": "code", + "execution_count": 2, + "metadata": { + "id": "5qzqkpe-ubFh" + }, + "outputs": [], + "source": [ + "# @title set API key\n", + "from pprint import pprint\n", + "import os\n", + "\n", + "import warnings\n", + "\n", + "warnings.filterwarnings(\"ignore\")\n", + "\n", + "if \"google.colab\" in str(get_ipython()):\n", + " # Running in Google Colab. Please set the UPSTAGE_API_KEY in the Colab Secrets\n", + " from google.colab import userdata\n", + "\n", + " os.environ[\"UPSTAGE_API_KEY\"] = userdata.get(\"UPSTAGE_API_KEY\")\n", + "else:\n", + " # Running locally. Please set the UPSTAGE_API_KEY in the .env file\n", + " from dotenv import load_dotenv\n", + "\n", + " load_dotenv()\n", + "\n", + "assert (\n", + " \"UPSTAGE_API_KEY\" in os.environ\n", + "), \"Please set the UPSTAGE_API_KEY environment variable\"" ] }, { - "data": { - "text/plain": [ - "True" + "cell_type": "code", + "execution_count": 3, + "metadata": { + "id": "okqxn2OCubFi" + }, + "outputs": [], + "source": [ + "solar_summary = \"\"\"\n", + "SOLAR 10.7B: Scaling Large Language Models with Simple yet Effective Depth Up-Scaling\n", + "\n", + "We introduce SOLAR 10.7B, a large language model (LLM) with 10.7 billion parameters,\n", + "demonstrating superior performance in various natural language processing (NLP) tasks.\n", + "Inspired by recent efforts to efficiently up-scale LLMs,\n", + "we present a method for scaling LLMs called depth up-scaling (DUS),\n", + "which encompasses depthwise scaling and continued pretraining.\n", + "In contrast to other LLM up-scaling methods that use mixture-of-experts,\n", + "DUS does not require complex changes to train and inference efficiently.\n", + "We show experimentally that DUS is simple yet effective\n", + "in scaling up high-performance LLMs from small ones.\n", + "Building on the DUS model, we additionally present SOLAR 10.7B-Instruct,\n", + "a variant fine-tuned for instruction-following capabilities,\n", + "surpassing Mixtral-8x7B-Instruct.\n", + "SOLAR 10.7B is publicly available under the Apache 2.0 license,\n", + "promoting broad access and application in the LLM field.\n", + "\"\"\"" ] - }, - "execution_count": 25, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "is_in(\"What is DUS?\", solar_summary)" - ] - }, - { - "cell_type": "code", - "execution_count": 26, - "metadata": {}, - "outputs": [], - "source": [ - "# Smart RAG, Self-Improving RAG\n", - "import os\n", - "from tavily import TavilyClient\n", - "\n", - "\n", - "def smart_rag(question, context):\n", - " if not is_in(question, context):\n", - " print(\"Searching in tavily\")\n", - " tavily = TavilyClient(api_key=os.environ[\"TAVILY_API_KEY\"])\n", - " context = tavily.search(query=question)\n", - "\n", - " chain = prompt_template | llm | StrOutputParser()\n", - " return chain.invoke({\"context\": context, \"question\": question})" - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": {}, - "outputs": [ + }, { - "name": "stdout", - "output_type": "stream", - "text": [ - "yes\n" - ] + "cell_type": "code", + "execution_count": 4, + "metadata": { + "id": "oJkHpGXBubFi" + }, + "outputs": [], + "source": [ + "from langchain_core.prompts import PromptTemplate\n", + "from langchain_core.output_parsers import StrOutputParser\n", + "from langchain_upstage import ChatUpstage\n", + "\n", + "llm = ChatUpstage(model=\"solar-pro\")\n", + "\n", + "\n", + "prompt_template = PromptTemplate.from_template(\n", + " \"\"\"\n", + " Please provide answer from the following context.\n", + " If the answer is not present in the context, please write \"The information is not present in the context.\"\n", + "\n", + " ---\n", + " Question: {question}\n", + " ---\n", + " Context: {context}\n", + " \"\"\"\n", + ")\n", + "chain = prompt_template | llm | StrOutputParser()" + ] }, { - "data": { - "text/plain": [ - "'The answer to the question \"What is DUS?\" is:\\n\\nDepth Up-Scaling (DUS)'" + "cell_type": "code", + "execution_count": 13, + "metadata": { + "id": "j3ST0qEMubFi", + "outputId": "9335b5b7-ce86-42fc-849c-8b7b54b8f0f7", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "'DUS stands for Depth Up-Scaling.'" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "string" + } + }, + "metadata": {}, + "execution_count": 13 + } + ], + "source": [ + "chain.invoke({\"question\": \"What is DUS?\", \"context\": solar_summary})" ] - }, - "execution_count": 27, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "smart_rag(\"What is DUS?\", solar_summary)" - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": {}, - "outputs": [ + }, { - "name": "stdout", - "output_type": "stream", - "text": [ - "no.\n", - "Searching in tavily\n" - ] + "cell_type": "code", + "execution_count": 12, + "metadata": { + "id": "FQ2bMDVrubFi", + "outputId": "78c27973-f5bb-40eb-84b3-ce49674a35aa", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "'The information is not present in the context.'" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "string" + } + }, + "metadata": {}, + "execution_count": 12 + } + ], + "source": [ + "chain.invoke({\"question\": \"How to get to Seoul from SF\", \"context\": solar_summary})" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": { + "id": "rKEF8H0mubFi" + }, + "outputs": [], + "source": [ + "# RAG or Search?\n", + "def is_in(question, context):\n", + " is_in_conetxt = \"\"\"As a helpful assistant,\n", + "please use your best judgment to determine if the answer to the question is within the given context.\n", + "If the answer is present in the context, please respond with \"yes\".\n", + "If not, please respond with \"no\".\n", + "Only provide \"yes\" or \"no\" and avoid including any additional information.\n", + "Please do your best. Here is the question and the context:\n", + "---\n", + "CONTEXT: {context}\n", + "---\n", + "QUESTION: {question}\n", + "---\n", + "OUTPUT (yes or no):\"\"\"\n", + "\n", + " is_in_prompt = PromptTemplate.from_template(is_in_conetxt)\n", + " chain = is_in_prompt | ChatUpstage(model=\"solar-pro\") | StrOutputParser()\n", + "\n", + " response = chain.invoke({\"context\": context, \"question\": question})\n", + " print(response)\n", + " return response.lower().startswith(\"yes\")" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": { + "id": "n-q3swv-ubFi", + "outputId": "ed9c4582-a70f-4118-81ee-36280e6623ec", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "no\n" + ] + }, + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "False" + ] + }, + "metadata": {}, + "execution_count": 37 + } + ], + "source": [ + "is_in(\"How to get to Seoul from SF\", solar_summary)" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": { + "id": "6azt_UKvubFi", + "outputId": "a73c7bfb-fd4c-4e0a-8bfa-cb1c320dee36", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "yes\n" + ] + }, + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "True" + ] + }, + "metadata": {}, + "execution_count": 38 + } + ], + "source": [ + "is_in(\"What is DUS?\", solar_summary)" + ] }, { - "data": { - "text/plain": [ - "'The answer to \"How to get to Seoul from SF?\" is:\\n\\n1. Fly from San Francisco (SFO) to Seoul (ICN) with airlines such as ANA, Japan Airlines, Asiana Airlines, Korean Air, and United Airlines.\\n2. Take a train from Incheon Int\\'l Airport T1 to Seoul Station.\\n3. Take the BART from Civic Center / UN Plaza to Milpitas and then fly from San Jose (SJC) to Incheon (ICN).\\n\\nPlease note that the cheapest flights from San Francisco to Seoul start at $453 with AIR PREMIA.'" + "cell_type": "code", + "execution_count": 43, + "metadata": { + "id": "K7fXaLaOubFj" + }, + "outputs": [], + "source": [ + "# Smart RAG, Self-Improving RAG\n", + "import os\n", + "from tavily import TavilyClient\n", + "\n", + "def smart_rag(question, context):\n", + " if not is_in(question, context):\n", + " print(\"Searching in tavily\")\n", + " tavily = TavilyClient(api_key=os.environ[\"TAVILY_API_KEY\"])\n", + " context = tavily.search(query=question)\n", + "\n", + " chain = prompt_template | llm | StrOutputParser()\n", + " return chain.invoke({\"context\": context, \"question\": question})" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": { + "id": "Kw-bAamJubFj", + "outputId": "9c86aee2-9dbb-499e-e168-93b7203f2542", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 52 + } + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "yes\n" + ] + }, + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "'DUS stands for Depth Up-Scaling.'" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "string" + } + }, + "metadata": {}, + "execution_count": 44 + } + ], + "source": [ + "smart_rag(\"What is DUS?\", solar_summary)" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": { + "id": "QZnfjPRNubFj", + "outputId": "d26763f1-27bc-4818-a330-04c573a73dc4", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 122 + } + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "no\n", + "Searching in tavily\n" + ] + }, + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "'There are several ways to get from San Francisco to Seoul, including flying, taking a bus, using the subway, or riding a train. The most common method is to fly, with airlines such as AIR PREMIA, Asiana Airlines, Korean Air, and United Airlines offering nonstop flights from San Francisco to Seoul. The cheapest flight can be found with AIR PREMIA for $698 one-way, while the best prices for this route can be found on AIR PREMIA. The average price for a round trip flight from San Francisco to Seoul is around $682.'" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "string" + } + }, + "metadata": {}, + "execution_count": 45 + } + ], + "source": [ + "smart_rag(\"How to get to Seoul from SF?\", solar_summary)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ATC6wkv8ubFj" + }, + "source": [ + "# Excercise\n", + "\n", + "The `is_in` function sometimes works, but other times it does not. You can significantly improve it by providing a more detailed description and adding two or three examples in the prompt to see how it performs better." ] - }, - "execution_count": 28, - "metadata": {}, - "output_type": "execute_result" } - ], - "source": [ - "smart_rag(\"How to get to Seoul from SF?\", solar_summary)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Excercise\n", - "\n", - "The `is_in` function sometimes works, but other times it does not. You can significantly improve it by providing a more detailed description and adding two or three examples in the prompt to see how it performs better." - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "language": "python", - "name": "python3" + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.10" + }, + "colab": { + "provenance": [] + } }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.10.10" - } - }, - "nbformat": 4, - "nbformat_minor": 4 + "nbformat": 4, + "nbformat_minor": 0 }