{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1edb9e6b",
   "metadata": {},
   "source": [
    "# Azure Cognitive Search\n",
    "\n",
    ">[Azure Cognitive Search](https://learn.microsoft.com/en-us/azure/search/search-what-is-azure-search) (formerly known as `Azure Search`) is a cloud search service that gives developers infrastructure, APIs, and tools for building a rich search experience over private, heterogeneous content in web, mobile, and enterprise applications.\n",
    "\n",
    ">Search is foundational to any app that surfaces text to users, where common scenarios include catalog or document search, online retail apps, or data exploration over proprietary content. When you create a search service, you'll work with the following capabilities:\n",
    ">- A search engine for full text search over a search index containing user-owned content\n",
    ">- Rich indexing, with lexical analysis and optional AI enrichment for content extraction and transformation\n",
    ">- Rich query syntax for text search, fuzzy search, autocomplete, geo-search and more\n",
    ">- Programmability through REST APIs and client libraries in Azure SDKs\n",
    ">- Azure integration at the data layer, machine learning layer, and AI (Cognitive Services)\n",
    "\n",
    "This notebook shows how to use Azure Cognitive Search (ACS) within LangChain."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "074b0004",
   "metadata": {},
   "source": [
    "## Set up Azure Cognitive Search\n",
    "\n",
    "To set up ACS, please follow the instructions [here](https://learn.microsoft.com/en-us/azure/search/search-create-service-portal).\n",
    "\n",
    "Please note\n",
    "1. the name of your ACS service, \n",
    "2. the name of your ACS index,\n",
    "3. your API key.\n",
    "\n",
    "Your API key can be either Admin or Query key, but as we only read data it is recommended to use a Query key."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0474661d",
   "metadata": {},
   "source": [
    "## Using the Azure Cognitive Search Retriever"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "39d6074e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "from langchain.retrievers import AzureCognitiveSearchRetriever"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b7243e6d",
   "metadata": {},
   "source": [
    "Set Service Name, Index Name and API key as environment variables (alternatively, you can pass them as arguments to `AzureCognitiveSearchRetriever`)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "33fd23d1",
   "metadata": {},
   "outputs": [],
   "source": [
    "os.environ[\"AZURE_COGNITIVE_SEARCH_SERVICE_NAME\"] = \"<YOUR_ACS_SERVICE_NAME>\"\n",
    "os.environ[\"AZURE_COGNITIVE_SEARCH_INDEX_NAME\"] = \"<YOUR_ACS_INDEX_NAME>\"\n",
    "os.environ[\"AZURE_COGNITIVE_SEARCH_API_KEY\"] = \"<YOUR_API_KEY>\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "057deaad",
   "metadata": {},
   "source": [
    "Create the Retriever"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c18d0c4c",
   "metadata": {},
   "outputs": [],
   "source": [
    "retriever = AzureCognitiveSearchRetriever(content_key=\"content\", top_k=10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e94ea104",
   "metadata": {},
   "source": [
    "Now you can use retrieve documents from Azure Cognitive Search"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c8b5794b",
   "metadata": {},
   "outputs": [],
   "source": [
    "retriever.get_relevant_documents(\"what is langchain\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "72eca08e",
   "metadata": {},
   "source": [
    "You can change the number of results returned with the `top_k` parameter. The default value is `None`, which returns all results. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "097146c5",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6d9963f5",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "dc120696",
   "metadata": {},
   "source": []
  }
 ],
 "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.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
