{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "683953b3",
   "metadata": {},
   "source": [
    "# LLMRails\n",
    "\n",
    ">[LLMRails](https://www.llmrails.com/) is a API platform for building GenAI applications. It provides an easy-to-use API for document indexing and querying that is managed by LLMRails and is optimized for performance and accuracy. \n",
    "See the [LLMRails API documentation ](https://docs.llmrails.com/) for more information on how to use the API.\n",
    "\n",
    "This notebook shows how to use functionality related to the `LLMRails`'s integration with langchain.\n",
    "Note that unlike many other integrations in this category, LLMRails provides an end-to-end managed service for retrieval agumented generation, which includes:\n",
    "1. A way to extract text from document files and chunk them into sentences.\n",
    "2. Its own embeddings model and vector store - each text segment is encoded into a vector embedding and stored in the LLMRails internal vector store\n",
    "3. A query service that automatically encodes the query into embedding, and retrieves the most relevant text segments (including support for [Hybrid Search](https://docs.llmrails.com/datastores/search))\n",
    "\n",
    "All of these are supported in this LangChain integration."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dc0f4344",
   "metadata": {},
   "source": [
    "# Setup\n",
    "\n",
    "You will need a LLMRails account to use LLMRails with LangChain. To get started, use the following steps:\n",
    "1. [Sign up](https://console.llmrails.com/signup) for a LLMRails account if you don't already have one.\n",
    "2. Next you'll need to create API keys to access the API. Click on the **\"API Keys\"** tab in the corpus view and then the **\"Create API Key\"** button. Give your key a name. Click \"Create key\" and you now have an active API key. Keep this key confidential. \n",
    "\n",
    "To use LangChain with LLMRails, you'll need to have this value: api_key.\n",
    "You can provide those to LangChain in two ways:\n",
    "\n",
    "1. Include in your environment these two variables: `LLM_RAILS_API_KEY`, `LLM_RAILS_DATASTORE_ID`.\n",
    "\n",
    "> For example, you can set these variables using os.environ and getpass as follows:\n",
    "\n",
    "```python\n",
    "import os\n",
    "import getpass\n",
    "\n",
    "os.environ[\"LLM_RAILS_API_KEY\"] = getpass.getpass(\"LLMRails API Key:\")\n",
    "os.environ[\"LLM_RAILS_DATASTORE_ID\"] = getpass.getpass(\"LLMRails Datastore Id:\")\n",
    "```\n",
    "\n",
    "1. Provide them as arguments when creating the LLMRails vectorstore object:\n",
    "\n",
    "```python\n",
    "vectorstore = LLMRails(\n",
    "    api_key=llm_rails_api_key,\n",
    "    datastore_id=datastore_id\n",
    ")\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d93c4fcd",
   "metadata": {},
   "source": [
    "## Adding text\n",
    "\n",
    "For adding text to your datastore first you have to go to [Datastores](https://console.llmrails.com/datastores) page and create one. Click Create Datastore button and choose a name and embedding model for your datastore. Then get your datastore id from newly created  datatore settings.\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "920f4644",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.vectorstores import LLMRails\n",
    "import os\n",
    "\n",
    "os.environ['LLM_RAILS_DATASTORE_ID'] = 'Your datastore id '\n",
    "os.environ['LLM_RAILS_API_KEY'] = 'Your API Key'\n",
    "\n",
    "llm_rails = LLMRails.from_texts(['Your text here'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1f9215c8",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-04-04T09:27:29.920258Z",
     "start_time": "2023-04-04T09:27:29.913714Z"
    }
   },
   "source": [
    "## Similarity search\n",
    "\n",
    "The simplest scenario for using LLMRails is to perform a similarity search. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a8c513ab",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-04-04T10:51:25.204469Z",
     "start_time": "2023-04-04T10:51:24.855618Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "query = \"What do you plan to do about national security?\"\n",
    "found_docs = llm_rails.similarity_search(\n",
    "    query, k=5\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "fc516993",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-04-04T10:51:25.220984Z",
     "start_time": "2023-04-04T10:51:25.213943Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Others may not be democratic but nevertheless depend upon a rules-based international system.\n",
      "\n",
      "Yet what we share in common, and the prospect of a freer and more open world, makes such a broad coalition necessary and worthwhile.\n",
      "\n",
      "We will listen to and consider ideas that our partners suggest about how to do this.\n",
      "\n",
      "Building this inclusive coalition requires reinforcing the multilateral system to uphold the founding principles of the United Nations, including respect for international law.\n",
      "\n",
      "141 countries expressed support at the United Nations General Assembly for a resolution condemning Russia’s unprovoked aggression against Ukraine.\n",
      "\n",
      "We continue to demonstrate this approach by engaging all regions across all issues, not in terms of what we are against but what we are for.\n",
      "\n",
      "This year, we partnered with ASEAN to advance clean energy infrastructure and maritime security in the region.\n",
      "\n",
      "We kickstarted the Prosper Africa Build Together Campaign to fuel economic growth across the continent and bolster trade and investment in the clean energy, health, and digital technology sectors.\n",
      "\n",
      "We are working to develop a partnership with countries on the Atlantic Ocean to establish and carry out a shared approach to advancing our joint development, economic, environmental, scientific, and maritime governance goals.\n",
      "\n",
      "We galvanized regional action to address the core challenges facing the Western Hemisphere by spearheading the Americas Partnership for Economic Prosperity to drive economic recovery and by mobilizing the region behind a bold and unprecedented approach to migration through the Los Angeles Declaration on Migration and Protection.\n",
      "\n",
      "In the Middle East, we have worked to enhance deterrence toward Iran, de-escalate regional conflicts, deepen integration among a diverse set of partners in the region, and bolster energy stability.\n",
      "\n",
      "A prime example of an inclusive coalition is IPEF, which we launched alongside a dozen regional partners that represent 40 percent of the world’s GDP.\n"
     ]
    }
   ],
   "source": [
    "print(found_docs[0].page_content)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1bda9bf5",
   "metadata": {},
   "source": [
    "## Similarity search with score\n",
    "\n",
    "Sometimes we might want to perform the search, but also obtain a relevancy score to know how good is a particular result."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "8804a21d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-04-04T10:51:25.631585Z",
     "start_time": "2023-04-04T10:51:25.227384Z"
    }
   },
   "outputs": [],
   "source": [
    "query = \"What is your approach to national defense\"\n",
    "found_docs = llm_rails.similarity_search_with_score(\n",
    "    query, k=5,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "756a6887",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-04-04T10:51:25.642282Z",
     "start_time": "2023-04-04T10:51:25.635947Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "But we will do so as the last resort and only when the objectives and mission are clear and achievable, consistent with our values and laws, alongside non-military tools, and the mission is undertaken with the informed consent of the American people.\n",
      "\n",
      "Our approach to national defense is described in detail in the 2022 National Defense Strategy.\n",
      "\n",
      "Our starting premise is that a powerful U.S. military helps advance and safeguard vital U.S. national interests by backstopping diplomacy, confronting aggression, deterring conflict, projecting strength, and protecting the American people and their economic interests.\n",
      "\n",
      "Amid intensifying competition, the military’s role is to maintain and gain warfighting advantages while limiting those of our competitors.\n",
      "\n",
      "The military will act urgently to sustain and strengthen deterrence, with the PRC as its pacing challenge.\n",
      "\n",
      "We will make disciplined choices regarding our national defense and focus our attention on the military’s primary responsibilities: to defend the homeland, and deter attacks and aggression against the United States, our allies and partners, while being prepared to fight and win the Nation’s wars should diplomacy and deterrence fail.\n",
      "\n",
      "To do so, we will combine our strengths to achieve maximum effect in deterring acts of aggression—an approach we refer to as integrated deterrence (see text box on page 22).\n",
      "\n",
      "We will operate our military using a campaigning mindset—sequencing logically linked military activities to advance strategy-aligned priorities.\n",
      "\n",
      "And, we will build a resilient force and defense ecosystem to ensure we can perform these functions for decades to come.\n",
      "\n",
      "We ended America’s longest war in Afghanistan, and with it an era of major military operations to remake other societies, even as we have maintained the capacity to address terrorist threats to the American people as they emerge.\n",
      "\n",
      "20  NATIONAL SECURITY STRATEGY Page 21 \n",
      "\n",
      "A combat-credible military is the foundation of deterrence and America’s ability to prevail in conflict.\n",
      "\n",
      "Score: 0.5040982687179959\n"
     ]
    }
   ],
   "source": [
    "document, score = found_docs[0]\n",
    "print(document.page_content)\n",
    "print(f\"\\nScore: {score}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "691a82d6",
   "metadata": {},
   "source": [
    "## LLMRails as a Retriever\n",
    "\n",
    "LLMRails, as all the other LangChain vectorstores, is most often used as a LangChain Retriever:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "9427195f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-04-04T10:51:26.031451Z",
     "start_time": "2023-04-04T10:51:26.018763Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LLMRailsRetriever(tags=None, metadata=None, vectorstore=<langchain.vectorstores.llm_rails.LLMRails object at 0x107b9c040>, search_type='similarity', search_kwargs={'k': 5})"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "retriever = llm_rails.as_retriever()\n",
    "retriever"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "f3c70c31",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-04-04T10:51:26.495652Z",
     "start_time": "2023-04-04T10:51:26.046407Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Document(page_content='But we will do so as the last resort and only when the objectives and mission are clear and achievable, consistent with our values and laws, alongside non-military tools, and the mission is undertaken with the informed consent of the American people.\\n\\nOur approach to national defense is described in detail in the 2022 National Defense Strategy.\\n\\nOur starting premise is that a powerful U.S. military helps advance and safeguard vital U.S. national interests by backstopping diplomacy, confronting aggression, deterring conflict, projecting strength, and protecting the American people and their economic interests.\\n\\nAmid intensifying competition, the military’s role is to maintain and gain warfighting advantages while limiting those of our competitors.\\n\\nThe military will act urgently to sustain and strengthen deterrence, with the PRC as its pacing challenge.\\n\\nWe will make disciplined choices regarding our national defense and focus our attention on the military’s primary responsibilities: to defend the homeland, and deter attacks and aggression against the United States, our allies and partners, while being prepared to fight and win the Nation’s wars should diplomacy and deterrence fail.\\n\\nTo do so, we will combine our strengths to achieve maximum effect in deterring acts of aggression—an approach we refer to as integrated deterrence (see text box on page 22).\\n\\nWe will operate our military using a campaigning mindset—sequencing logically linked military activities to advance strategy-aligned priorities.\\n\\nAnd, we will build a resilient force and defense ecosystem to ensure we can perform these functions for decades to come.\\n\\nWe ended America’s longest war in Afghanistan, and with it an era of major military operations to remake other societies, even as we have maintained the capacity to address terrorist threats to the American people as they emerge.\\n\\n20  NATIONAL SECURITY STRATEGY Page 21 \\x90\\x90\\x90\\x90\\x90\\x90\\n\\nA combat-credible military is the foundation of deterrence and America’s ability to prevail in conflict.', metadata={'type': 'file', 'url': 'https://cdn.llmrails.com/dst_d94b490c-4638-4247-ad5e-9aa0e7ef53c1/c2d63a2ea3cd406cb522f8312bc1535d', 'name': 'Biden-Harris-Administrations-National-Security-Strategy-10.2022.pdf'})"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "query = \"What is your approach to national defense\"\n",
    "retriever.get_relevant_documents(query)[0]"
   ]
  }
 ],
 "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.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
