File size: 2,974 Bytes
cfd3735
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1edb9e6b",
   "metadata": {},
   "source": [
    "# Azure Cognitive Search Retriever\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 instrcutions [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\")"
   ]
  },
  {
   "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\")"
   ]
  }
 ],
 "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.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}