|
9 | 9 | "# Use Amazon Bedrock\n",
|
10 | 10 | "[](https://colab.research.google.com/github/elastic/elasticsearch-labs/blob/main/notebooks/integrations/amazon-bedrock/langchain-qa-example.ipynb)\n",
|
11 | 11 | "\n",
|
12 |
| - "This workbook demonstrates how to work with Langchain [Amazon Bedrock](https://aws.amazon.com/bedrock/). Amazon Bedrock is a managed service that makes foundation models from leading AI startup and Amazon's own Titan models available through APIs.\n", |
13 |
| - "\n", |
14 |
| - "\n", |
15 |
| - "\n" |
| 12 | + "This workbook demonstrates how to work with Langchain [Amazon Bedrock](https://aws.amazon.com/bedrock/). Amazon Bedrock is a managed service that makes foundation models from leading AI startup and Amazon's own Titan models available through APIs." |
16 | 13 | ]
|
17 | 14 | },
|
18 | 15 | {
|
|
33 | 30 | "outputs": [],
|
34 | 31 | "source": [
|
35 | 32 | "# install packages\n",
|
36 |
| - "!python3 -m pip install -qU langchain langchain-elasticsearch boto3\n", |
| 33 | + "!python3 -m pip install -qU langchain langchain-elasticsearch langchain_community boto3 tiktoken\n", |
37 | 34 | "\n",
|
38 | 35 | "# import modules\n",
|
39 | 36 | "from getpass import getpass\n",
|
40 | 37 | "from urllib.request import urlopen\n",
|
41 | 38 | "from langchain_elasticsearch import ElasticsearchStore\n",
|
42 |
| - "from langchain.embeddings.bedrock import BedrockEmbeddings\n", |
| 39 | + "from langchain_community.embeddings.bedrock import BedrockEmbeddings\n", |
43 | 40 | "from langchain.llms import Bedrock\n",
|
44 | 41 | "from langchain.chains import RetrievalQA\n",
|
45 | 42 | "import boto3\n",
|
|
102 | 99 | "We'll use the **Cloud ID** to identify our deployment, because we are using Elastic Cloud deployment. To find the Cloud ID for your deployment, go to https://cloud.elastic.co/deployments and select your deployment.\n",
|
103 | 100 | "\n",
|
104 | 101 | "\n",
|
105 |
| - "We will use [ElasticsearchStore](https://api.python.langchain.com/en/latest/vectorstores/langchain.vectorstores.elasticsearch.ElasticsearchStore.html) to connect to our elastic cloud deployment. This would help create and index data easily. In the ElasticsearchStore instance, will set embedding to [BedrockEmbeddings](https://api.python.langchain.com/en/latest/embeddings/langchain.embeddings.bedrock.BedrockEmbeddings.html) to embed the texts and elasticsearch index name that will be used in this example. In the instance, we will set `strategy` to [ElasticsearchStore.SparseVectorRetrievalStrategy()](https://api.python.langchain.com/en/latest/vectorstores/langchain.vectorstores.elasticsearch.SparseRetrievalStrategy.html#langchain.vectorstores.elasticsearch.SparseRetrievalStrategy) as we use this strategy to split documents.\n", |
106 |
| - "\n", |
107 |
| - "As we're using [ELSER](https://www.elastic.co/guide/en/machine-learning/current/ml-nlp-elser.html) we use [SparseVectorRetrievalStrategy](https://python.langchain.com/docs/integrations/vectorstores/elasticsearch#sparsevectorretrievalstrategy-elser) strategy. This strategy uses Elasticsearch's sparse vector retrieval to retrieve the top-k results. There is more other [strategies](https://python.langchain.com/docs/integrations/vectorstores/elasticsearch#approxretrievalstrategy) in langchain that might be used base on your needs." |
| 102 | + "We will use [ElasticsearchStore](https://api.python.langchain.com/en/latest/vectorstores/langchain.vectorstores.elasticsearch.ElasticsearchStore.html) to connect to our elastic cloud deployment. This would help create and index data easily. In the ElasticsearchStore instance, will set embedding to [BedrockEmbeddings](https://api.python.langchain.com/en/latest/embeddings/langchain.embeddings.bedrock.BedrockEmbeddings.html) to embed the texts and elasticsearch index name that will be used in this example." |
108 | 103 | ]
|
109 | 104 | },
|
110 | 105 | {
|
|
121 | 116 | "# https://www.elastic.co/search-labs/tutorials/install-elasticsearch/elastic-cloud#creating-an-api-key\n",
|
122 | 117 | "ELASTIC_API_KEY = getpass(\"Elastic Api Key: \")\n",
|
123 | 118 | "\n",
|
124 |
| - "embeddings = BedrockEmbeddings(client=bedrock_client)\n", |
| 119 | + "bedrock_embedding = BedrockEmbeddings(client=bedrock_client)\n", |
125 | 120 | "\n",
|
126 | 121 | "vector_store = ElasticsearchStore(\n",
|
127 | 122 | " es_cloud_id=ELASTIC_CLOUD_ID,\n",
|
128 | 123 | " es_api_key=ELASTIC_API_KEY,\n",
|
129 | 124 | " index_name=\"workplace_index\",\n",
|
130 |
| - " embedding=embeddings,\n", |
131 |
| - " strategy=ElasticsearchStore.SparseVectorRetrievalStrategy(),\n", |
| 125 | + " embedding=bedrock_embedding,\n", |
132 | 126 | ")"
|
133 | 127 | ]
|
134 | 128 | },
|
|
210 | 204 | "source": [
|
211 | 205 | "## Index data into elasticsearch\n",
|
212 | 206 | "\n",
|
213 |
| - "Next, we will index data to elasticsearch using [ElasticsearchStore.from_documents](https://api.python.langchain.com/en/latest/vectorstores/langchain.vectorstores.elasticsearch.ElasticsearchStore.html#langchain.vectorstores.elasticsearch.ElasticsearchStore.from_documents). We will use Cloud ID, Password and Index name values set in the `Create cloud deployment` step.\n", |
214 |
| - "\n", |
215 |
| - "In the instance, we will set `strategy` to [ElasticsearchStore.SparseVectorRetrievalStrategy()](https://api.python.langchain.com/en/latest/vectorstores/langchain.vectorstores.elasticsearch.SparseRetrievalStrategy.html#langchain.vectorstores.elasticsearch.SparseRetrievalStrategy)\n", |
216 |
| - "\n", |
217 |
| - "Note: Before we begin indexing, ensure you have [downloaded and deployed ELSER model](https://www.elastic.co/guide/en/machine-learning/current/ml-nlp-elser.html#download-deploy-elser) in your deployment and is running in ml node.\n" |
| 207 | + "Next, we will index data to elasticsearch using [ElasticsearchStore.from_documents](https://api.python.langchain.com/en/latest/vectorstores/langchain.vectorstores.elasticsearch.ElasticsearchStore.html#langchain.vectorstores.elasticsearch.ElasticsearchStore.from_documents). We will use Cloud ID, Password and Index name values set in the `Create cloud deployment` step." |
218 | 208 | ]
|
219 | 209 | },
|
220 | 210 | {
|
|
230 | 220 | " es_cloud_id=ELASTIC_CLOUD_ID,\n",
|
231 | 221 | " es_api_key=ELASTIC_API_KEY,\n",
|
232 | 222 | " index_name=\"workplace_index\",\n",
|
233 |
| - " strategy=ElasticsearchStore.SparseVectorRetrievalStrategy(),\n", |
| 223 | + " embedding=bedrock_embedding,\n", |
234 | 224 | ")"
|
235 | 225 | ]
|
236 | 226 | },
|
|
265 | 255 | },
|
266 | 256 | "source": [
|
267 | 257 | "## Asking a question\n",
|
268 |
| - "Now that we have the passages stored in Elasticsearch and llm is initialized, we can now ask a question to get the relevant passages.\n" |
| 258 | + "Now that we have the passages stored in Elasticsearch and llm is initialized, we can now ask a question to get the relevant passages." |
269 | 259 | ]
|
270 | 260 | },
|
271 | 261 | {
|
|
333 | 323 | "\n",
|
334 | 324 | "Employees working from home are responsible for creating a comfortable and safe workspace that is conducive to productivity. This includes ensuring that their home office is ergonomically designed, well-lit, and free from distractions.\n",
|
335 | 325 | "Communication\n",
|
336 |
| - "-------\n", |
337 |
| - "\n" |
| 326 | + "-------" |
338 | 327 | ]
|
339 | 328 | }
|
340 | 329 | ],
|
|
351 | 340 | " \"How does compensation work?\",\n",
|
352 | 341 | "]\n",
|
353 | 342 | "question = questions[1]\n",
|
354 |
| - "print(f\"Question: {question}\\n\")\n", |
| 343 | + "print(f\"Question: {question}\")\n", |
355 | 344 | "\n",
|
356 | 345 | "ans = qa({\"query\": question})\n",
|
357 | 346 | "\n",
|
|
361 | 350 | "for doc in ans[\"source_documents\"]:\n",
|
362 | 351 | " print(\"Name: \" + doc.metadata[\"name\"])\n",
|
363 | 352 | " print(\"Content: \" + doc.page_content)\n",
|
364 |
| - " print(\"-------\\n\")" |
| 353 | + " print(\"-------\")" |
365 | 354 | ]
|
366 | 355 | }
|
367 | 356 | ],
|
|
0 commit comments