File size: 7,466 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "683953b3",
   "metadata": {},
   "source": [
    "# ElasticSearch\n",
    "\n",
    "[Elasticsearch](https://www.elastic.co/elasticsearch/) is a distributed, RESTful search and analytics engine. It provides a distributed, multitenant-capable full-text search engine with an HTTP web interface and schema-free JSON documents.\n",
    "\n",
    "This notebook shows how to use functionality related to the `Elasticsearch` database."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b66c12b2-2a07-4136-ac77-ce1c9fa7a409",
   "metadata": {
    "tags": []
   },
   "source": [
    "## Installation"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "81f43794-f002-477c-9b68-4975df30e718",
   "metadata": {},
   "source": [
    "Check out [Elasticsearch installation instructions](https://www.elastic.co/guide/en/elasticsearch/reference/current/install-elasticsearch.html).\n",
    "\n",
    "To connect to an Elasticsearch instance that does not require\n",
    "login credentials, pass the Elasticsearch URL and index name along with the\n",
    "embedding object to the constructor.\n",
    "\n",
    "Example:\n",
    "```python\n",
    "        from langchain import ElasticVectorSearch\n",
    "        from langchain.embeddings import OpenAIEmbeddings\n",
    "\n",
    "        embedding = OpenAIEmbeddings()\n",
    "        elastic_vector_search = ElasticVectorSearch(\n",
    "            elasticsearch_url=\"http://localhost:9200\",\n",
    "            index_name=\"test_index\",\n",
    "            embedding=embedding\n",
    "        )\n",
    "```\n",
    "\n",
    "To connect to an Elasticsearch instance that requires login credentials,\n",
    "including Elastic Cloud, use the Elasticsearch URL format\n",
    "https://username:password@es_host:9243. For example, to connect to Elastic\n",
    "Cloud, create the Elasticsearch URL with the required authentication details and\n",
    "pass it to the ElasticVectorSearch constructor as the named parameter\n",
    "elasticsearch_url.\n",
    "\n",
    "You can obtain your Elastic Cloud URL and login credentials by logging in to the\n",
    "Elastic Cloud console at https://cloud.elastic.co, selecting your deployment, and\n",
    "navigating to the \"Deployments\" page.\n",
    "\n",
    "To obtain your Elastic Cloud password for the default \"elastic\" user:\n",
    "1. Log in to the Elastic Cloud console at https://cloud.elastic.co\n",
    "2. Go to \"Security\" > \"Users\"\n",
    "3. Locate the \"elastic\" user and click \"Edit\"\n",
    "4. Click \"Reset password\"\n",
    "5. Follow the prompts to reset the password\n",
    "\n",
    "Format for Elastic Cloud URLs is\n",
    "https://username:password@cluster_id.region_id.gcp.cloud.es.io:9243.\n",
    "\n",
    "Example:\n",
    "```python\n",
    "        from langchain import ElasticVectorSearch\n",
    "        from langchain.embeddings import OpenAIEmbeddings\n",
    "\n",
    "        embedding = OpenAIEmbeddings()\n",
    "\n",
    "        elastic_host = \"cluster_id.region_id.gcp.cloud.es.io\"\n",
    "        elasticsearch_url = f\"https://username:password@{elastic_host}:9243\"\n",
    "        elastic_vector_search = ElasticVectorSearch(\n",
    "            elasticsearch_url=elasticsearch_url,\n",
    "            index_name=\"test_index\",\n",
    "            embedding=embedding\n",
    "        )\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d6197931-cbe5-460c-a5e6-b5eedb83887c",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "!pip install elasticsearch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "67ab8afa-f7c6-4fbf-b596-cb512da949da",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "OpenAI API Key: 路路路路路路路路\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import getpass\n",
    "\n",
    "os.environ['OPENAI_API_KEY'] = getpass.getpass('OpenAI API Key:')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f6030187-0bd7-4798-8372-a265036af5e0",
   "metadata": {
    "tags": []
   },
   "source": [
    "## Example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "aac9563e",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from langchain.embeddings.openai import OpenAIEmbeddings\n",
    "from langchain.text_splitter import CharacterTextSplitter\n",
    "from langchain.vectorstores import ElasticVectorSearch\n",
    "from langchain.document_loaders import TextLoader"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a3c3999a",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from langchain.document_loaders import TextLoader\n",
    "loader = TextLoader('../../../state_of_the_union.txt')\n",
    "documents = loader.load()\n",
    "text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n",
    "docs = text_splitter.split_documents(documents)\n",
    "\n",
    "embeddings = OpenAIEmbeddings()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12eb86d8",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "db = ElasticVectorSearch.from_documents(docs, embeddings, elasticsearch_url=\"http://localhost:9200\")\n",
    "\n",
    "query = \"What did the president say about Ketanji Brown Jackson\"\n",
    "docs = db.similarity_search(query)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4b172de8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "In state after state, new laws have been passed, not only to suppress the vote, but to subvert entire elections. \n",
      "\n",
      "We cannot let this happen. \n",
      "\n",
      "Tonight. I call on the Senate to: Pass the Freedom to Vote Act. Pass the John Lewis Voting Rights Act. And while you鈥檙e at it, pass the Disclose Act so Americans can know who is funding our elections. \n",
      "\n",
      "Tonight, I鈥檇 like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer鈥攁n Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service. \n",
      "\n",
      "One of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court. \n",
      "\n",
      "And I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation鈥檚 top legal minds, who will continue Justice Breyer鈥檚 legacy of excellence.\n"
     ]
    }
   ],
   "source": [
    "print(docs[0].page_content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a359ed74",
   "metadata": {},
   "outputs": [],
   "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
}