{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from langchain_chroma import Chroma\n", "import os\n", "from pyprojroot import here\n", "from langchain_huggingface import HuggingFaceEmbeddings\n", "from groq import Groq\n", "from dotenv import load_dotenv\n", "from pprint import pprint\n", "load_dotenv()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Load environment variables and configs**" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "os.environ['GROQ_API_KEY'] = os.getenv(\"GROQ_API_KEY\")\n", "\n", "EMBEDDING_MODEL = \"all-MiniLM-L6-v2\"\n", "VECTORDB_DIR = \"data/airline_policy_vectordb\"\n", "K=2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Load the vectorDB**" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "f:\\end_to_end_AI_Projects\\QueryMind _ AI_Powered_Natural_Language_Interface_for_SQL_&_Vector_Databases\\querymind\\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", " from .autonotebook import tqdm as notebook_tqdm\n", "Loading weights: 100%|██████████| 103/103 [00:00<00:00, 6866.51it/s]\n", "Failed to send telemetry event ClientStartEvent: capture() takes 1 positional argument but 3 were given\n", "Failed to send telemetry event ClientCreateCollectionEvent: capture() takes 1 positional argument but 3 were given\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Number of vectors in vectordb: 22 \n", "\n", "\n" ] } ], "source": [ "vectordb = Chroma(\n", " collection_name=\"rag-chroma\",\n", " persist_directory=str(here(VECTORDB_DIR)),\n", " embedding_function=HuggingFaceEmbeddings(model_name=EMBEDDING_MODEL)\n", ")\n", "print(\"Number of vectors in vectordb:\",\n", " vectordb._collection.count(), \"\\n\\n\")\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Sample Query**" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "message = \"What is the cancelation rule for a flight ticket at swiss airline policy?\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Perform the vector Search**" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Failed to send telemetry event CollectionQueryEvent: capture() takes 1 positional argument but 3 were given\n" ] } ], "source": [ "docs = vectordb.similarity_search(message, k=K)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[Document(metadata={'page': 8, 'source': 'F:\\\\end_to_end_AI_Projects\\\\QueryMind _ AI_Powered_Natural_Language_Interface_for_SQL_&_Vector_Databases\\\\data\\\\unstructured_docs\\\\swiss_airline_policy\\\\swiss_faq.pdf'}, page_content=\"How to Cancel a Swiss Air Flight: 877-\\n5O7-7341 Step-by-Step Guide\\nSwiss Air is a premium airline based in Switzerland that of fers a range of domestic and international flights to\\npassengers. However , sometimes situations arise where passengers may need to cancel their flights. In such cases, it is\\nimportant to understand the Swiss Air Cancellation Policy to avoid any confusion or additional charges.\\nSwiss International Airlines Cancellation Policy In this article, we will provide you with everything you need to know about\\nthe Swiss Air Cancellation Policy , including how to cancel a Swiss Air flight, the fees associated with cancelling a flight,\\nand the refund policy .\\nIf you have booked a flight with Swiss Airlines but need to cancel it, it's important to understand their cancellation policy\\nto avoid any unnecessary fees or charges. Swiss Airlines of fers dif ferent fare types, each with their own specific\\ncancellation terms and conditions. The most flexible fare types such as Flex and Business Flex allow you to cancel your\\nflight up to 24 hours before departure without any penalty . For other fare types, cancellation fees may apply . If you cancel\\nyour Swiss Airlines flight outside of the 24-hour window , cancellation fees will be charged depending on your fare type\\nand the time of cancellation. For example, if you cancel a non-flexible economy class ticket, a cancellation fee will be\\ncharged. The closer you cancel to the departure date, the higher the cancellation fee. In some cases, Swiss Airlines may\\nallow you to make changes to your flight instead of cancelling it outright. However , these changes may also come with\\nfees or penalties depending on your fare type and the type of change requested. If Swiss Airlines cancels your flight, you\\nmay be entitled to a full refund or rebooking on another flight. However , if the cancellation is due to extraordinary\\ncircumstances such as bad weather or political unrest, Swiss Airlines may not be obligated to of fer any compensation. In\\nsummary , Swiss Airlines' cancellation policy varies depending on your fare type and the time of cancellation. T o avoid any\\nunnecessary fees or charges, it's important to familiarise yourself with the terms and conditions of your ticket and to\\ncontact Swiss Airlines as soon as possible if you need to make changes or cancel your flight.\"),\n", " Document(metadata={'page': 9, 'source': 'F:\\\\end_to_end_AI_Projects\\\\QueryMind _ AI_Powered_Natural_Language_Interface_for_SQL_&_Vector_Databases\\\\data\\\\unstructured_docs\\\\swiss_airline_policy\\\\swiss_faq.pdf'}, page_content=\"for a refund or may only be able to receive a partial refund. If you booked your flight through a third-party website or\\ntravel agent, you may need to contact them directly to cancel your flight. Always check the terms and conditions of your\\nticket to make sure you understand the cancellation policy and any associated fees or penalties. If you're cancelling your\\nflight due to unforeseen circumstances such as a medical emergency or a natural disaster , Swiss Air may of fer you\\nspecial exemptions or accommodations. What is Swiss Airlines 24 Hour Cancellation Policy? Swiss Airlines has a 24\")]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "docs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Prepare the prompt for the Groq model**" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "question = \"# User new question:\\n\" + message\n", "retrieved_content = \"\"\n", "for doc in docs:\n", " retrieved_content += f\"{doc.page_content}\\n\\n\"\n", "prompt = f\"# Content:\\n{retrieved_content}\\n\\n{question}\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Prepared prompt" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('# Content:\\n'\n", " 'How to Cancel a Swiss Air Flight: 877-\\n'\n", " '5O7-7341 Step-by-Step Guide\\n'\n", " 'Swiss Air is a premium airline based in Switzerland that of fers a range of '\n", " 'domestic and international flights to\\n'\n", " 'passengers. However , sometimes situations arise where passengers may need '\n", " 'to cancel their flights. In such cases, it is\\n'\n", " 'important to understand the Swiss Air Cancellation Policy to avoid any '\n", " 'confusion or additional charges.\\n'\n", " 'Swiss International Airlines Cancellation Policy In this article, we will '\n", " 'provide you with everything you need to know about\\n'\n", " 'the Swiss Air Cancellation Policy , including how to cancel a Swiss Air '\n", " 'flight, the fees associated with cancelling a flight,\\n'\n", " 'and the refund policy .\\n'\n", " \"If you have booked a flight with Swiss Airlines but need to cancel it, it's \"\n", " 'important to understand their cancellation policy\\n'\n", " 'to avoid any unnecessary fees or charges. Swiss Airlines of fers dif ferent '\n", " 'fare types, each with their own specific\\n'\n", " 'cancellation terms and conditions. The most flexible fare types such as Flex '\n", " 'and Business Flex allow you to cancel your\\n'\n", " 'flight up to 24 hours before departure without any penalty . For other fare '\n", " 'types, cancellation fees may apply . If you cancel\\n'\n", " 'your Swiss Airlines flight outside of the 24-hour window , cancellation fees '\n", " 'will be charged depending on your fare type\\n'\n", " 'and the time of cancellation. For example, if you cancel a non-flexible '\n", " 'economy class ticket, a cancellation fee will be\\n'\n", " 'charged. The closer you cancel to the departure date, the higher the '\n", " 'cancellation fee. In some cases, Swiss Airlines may\\n'\n", " 'allow you to make changes to your flight instead of cancelling it outright. '\n", " 'However , these changes may also come with\\n'\n", " 'fees or penalties depending on your fare type and the type of change '\n", " 'requested. If Swiss Airlines cancels your flight, you\\n'\n", " 'may be entitled to a full refund or rebooking on another flight. However , '\n", " 'if the cancellation is due to extraordinary\\n'\n", " 'circumstances such as bad weather or political unrest, Swiss Airlines may '\n", " 'not be obligated to of fer any compensation. In\\n'\n", " \"summary , Swiss Airlines' cancellation policy varies depending on your fare \"\n", " 'type and the time of cancellation. T o avoid any\\n'\n", " \"unnecessary fees or charges, it's important to familiarise yourself with the \"\n", " 'terms and conditions of your ticket and to\\n'\n", " 'contact Swiss Airlines as soon as possible if you need to make changes or '\n", " 'cancel your flight.\\n'\n", " '\\n'\n", " 'for a refund or may only be able to receive a partial refund. If you booked '\n", " 'your flight through a third-party website or\\n'\n", " 'travel agent, you may need to contact them directly to cancel your flight. '\n", " 'Always check the terms and conditions of your\\n'\n", " 'ticket to make sure you understand the cancellation policy and any '\n", " \"associated fees or penalties. If you're cancelling your\\n\"\n", " 'flight due to unforeseen circumstances such as a medical emergency or a '\n", " 'natural disaster , Swiss Air may of fer you\\n'\n", " 'special exemptions or accommodations. What is Swiss Airlines 24 Hour '\n", " 'Cancellation Policy? Swiss Airlines has a 24\\n'\n", " '\\n'\n", " '\\n'\n", " '\\n'\n", " '# User new question:\\n'\n", " 'What is the cancelation rule for a flight ticket at swiss airline policy?')\n" ] } ], "source": [ "pprint(prompt)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Pass the prompt to the GPT model and get the response**" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "client = Groq()\n", "response = client.chat.completions.create(\n", " model=\"llama-3.3-70b-versatile\",\n", " messages=[\n", " {\"role\": \"system\", \"content\": \"You will receive a user's query and possible content where the answer might be. If the answer is found, provide it, if not, state that the answer does not exist.\"},\n", " {\"role\": \"user\", \"content\": prompt}\n", " ]\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Printing the response" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('Swiss Airlines allows passengers to cancel their flights based on the type '\n", " 'of ticket purchased. Here are the cancellation rules:\\n'\n", " '\\n'\n", " '1. **Flex Ticket**: You can cancel your flight without incurring any fees '\n", " 'and receive a full refund.\\n'\n", " '\\n'\n", " '2. **Standard Ticket**: \\n'\n", " ' - If you cancel within 24 hours of booking, you can receive a full '\n", " 'refund.\\n'\n", " ' - If you cancel after 24 hours, you may be charged a cancellation fee '\n", " '(ranging from 100 to 250 CHF) and receive a partial refund.\\n'\n", " '\\n'\n", " '3. **Economy Ticket**: \\n'\n", " ' - If you cancel within 24 hours of booking, you can receive a full '\n", " 'refund.\\n'\n", " ' - If you cancel after 24 hours, you may be charged a cancellation fee '\n", " '(ranging from 150 to 350 CHF) and receive a partial refund.\\n'\n", " '\\n'\n", " \"It's important to check the terms and conditions of your specific ticket, as \"\n", " 'fees and refund eligibility may vary. Additionally, if you cancel your '\n", " 'flight outside of the 24-hour window, cancellation fees apply.')\n" ] } ], "source": [ "pprint(response.choices[0].message.content)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**RAG Tool design using LangChain**" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "from langchain_core.tools import tool\n", "\n", "@tool\n", "def lookup_swiss_airline_policy(query: str)->str:\n", " \"\"\"Search within the Swiss Airline's company policies to check whether certain options are permitted. Input should be a search query.\"\"\"\n", " vectordb = Chroma(\n", " collection_name=\"rag-chroma\",\n", " persist_directory=str(here(VECTORDB_DIR)),\n", " embedding_function=HuggingFaceEmbeddings(model_name=EMBEDDING_MODEL)\n", " )\n", " docs = vectordb.similarity_search(query, k=K)\n", " return \"\\n\\n\".join([doc.page_content for doc in docs])" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "lookup_swiss_airline_policy\n", "{'query': {'title': 'Query', 'type': 'string'}}\n", "Search within the Swiss Airline's company policies to check whether certain options are permitted. Input should be a search query.\n" ] } ], "source": [ "print(lookup_swiss_airline_policy.name)\n", "print(lookup_swiss_airline_policy.args)\n", "print(lookup_swiss_airline_policy.description)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Loading weights: 100%|██████████| 103/103 [00:00<00:00, 2156.99it/s]\n", "Failed to send telemetry event ClientStartEvent: capture() takes 1 positional argument but 3 were given\n", "Failed to send telemetry event ClientCreateCollectionEvent: capture() takes 1 positional argument but 3 were given\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "('hour cancellation policy that allows passengers to cancel their flights '\n", " 'within 24 hours of booking at +1-877-507-7341\\n'\n", " 'without penalty . This policy applies to all fare types, including '\n", " 'non-refundable tickets. If you cancel your Swiss Airlines\\n'\n", " \"flight within 24 hours of booking, you'll receive a full refund of your \"\n", " 'ticket price.\\n'\n", " 'How to Cancel Swiss Airlines Flight within 24 Hours? If you need to cancel '\n", " 'your Swiss Airlines flight within 24 hours of\\n'\n", " 'booking, you can do so easily online. Here are the steps to follow:\\n'\n", " 'Go to Swiss Airlines\\' website and click on the \"Manage your bookings\" tab. '\n", " 'Enter your booking reference number and last\\n'\n", " 'name to access your booking. Select the flight you want to cancel and click '\n", " 'on \"Cancel flight.\" Confirm your cancellation\\n'\n", " \"and you'll receive a full refund of your ticket price. If you booked your \"\n", " \"Swiss Airlines flight through a travel agent, you'll\\n\"\n", " 'need to contact them directly to cancel your flight within 24 hours.\\n'\n", " 'Important Things to Keep in Mind for Swiss Airlines 24 Hour Cancellation '\n", " 'Here are some important things to keep in mind\\n'\n", " 'when cancelling your Swiss Airlines flight within 24 hours:\\n'\n", " \"Swiss Airlines' 24 hour cancellation policy only applies to flights booked \"\n", " 'directly through Swiss Airlines. If you booked\\n'\n", " \"your flight through a travel agent or third-party website, you'll need to \"\n", " 'check their cancellation policy . If you cancel your\\n'\n", " 'Swiss Airlines flight after the 24 hour window , you may be subject to '\n", " 'cancellation fees or penalties. If you have a non-\\n'\n", " \"refundable ticket and cancel your flight within 24 hours of booking, you'll \"\n", " 'receive a full refund of your ticket price.\\n'\n", " 'However , if you cancel your flight after the 24 hour window , you may not '\n", " \"be eligible for a refund. Swiss Airlines' 24 hour\\n\"\n", " 'cancellation policy allows passengers to cancel their flights within 24 '\n", " 'hours of booking without penalty . If you need to\\n'\n", " 'cancel your Swiss Airlines flight within 24 hours, you can do so easily '\n", " 'online. Just remember to check the terms and\\n'\n", " \"conditions of your ticket to make sure you're eligible for a refund.\\n\"\n", " 'Swiss Air Cancellation Fees The cancellation fees for Swiss Air flights may '\n", " 'vary depending on the type of ticket you have\\n'\n", " 'purchased. The airline of fers three dif ferent types of tickets, which '\n", " 'are:\\n'\n", " '\\n'\n", " 'for a refund or may only be able to receive a partial refund. If you booked '\n", " 'your flight through a third-party website or\\n'\n", " 'travel agent, you may need to contact them directly to cancel your flight. '\n", " 'Always check the terms and conditions of your\\n'\n", " 'ticket to make sure you understand the cancellation policy and any '\n", " \"associated fees or penalties. If you're cancelling your\\n\"\n", " 'flight due to unforeseen circumstances such as a medical emergency or a '\n", " 'natural disaster , Swiss Air may of fer you\\n'\n", " 'special exemptions or accommodations. What is Swiss Airlines 24 Hour '\n", " 'Cancellation Policy? Swiss Airlines has a 24')\n" ] } ], "source": [ "pprint(lookup_swiss_airline_policy.invoke(\"can I cancel my ticket?\"))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "querymind (3.12.10)", "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.12.10" } }, "nbformat": 4, "nbformat_minor": 2 }