Skip to content

Commit bb87e8f

Browse files
authored
Fix bedrock integration and remove incorrect part regarding ELSER (#249)
1 parent 5f505a1 commit bb87e8f

File tree

1 file changed

+12
-23
lines changed

1 file changed

+12
-23
lines changed

notebooks/integrations/amazon-bedrock/langchain-qa-example.ipynb

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@
99
"# Use Amazon Bedrock\n",
1010
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/elastic/elasticsearch-labs/blob/main/notebooks/integrations/amazon-bedrock/langchain-qa-example.ipynb)\n",
1111
"\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."
1613
]
1714
},
1815
{
@@ -33,13 +30,13 @@
3330
"outputs": [],
3431
"source": [
3532
"# 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",
3734
"\n",
3835
"# import modules\n",
3936
"from getpass import getpass\n",
4037
"from urllib.request import urlopen\n",
4138
"from langchain_elasticsearch import ElasticsearchStore\n",
42-
"from langchain.embeddings.bedrock import BedrockEmbeddings\n",
39+
"from langchain_community.embeddings.bedrock import BedrockEmbeddings\n",
4340
"from langchain.llms import Bedrock\n",
4441
"from langchain.chains import RetrievalQA\n",
4542
"import boto3\n",
@@ -102,9 +99,7 @@
10299
"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",
103100
"\n",
104101
"\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."
108103
]
109104
},
110105
{
@@ -121,14 +116,13 @@
121116
"# https://www.elastic.co/search-labs/tutorials/install-elasticsearch/elastic-cloud#creating-an-api-key\n",
122117
"ELASTIC_API_KEY = getpass(\"Elastic Api Key: \")\n",
123118
"\n",
124-
"embeddings = BedrockEmbeddings(client=bedrock_client)\n",
119+
"bedrock_embedding = BedrockEmbeddings(client=bedrock_client)\n",
125120
"\n",
126121
"vector_store = ElasticsearchStore(\n",
127122
" es_cloud_id=ELASTIC_CLOUD_ID,\n",
128123
" es_api_key=ELASTIC_API_KEY,\n",
129124
" index_name=\"workplace_index\",\n",
130-
" embedding=embeddings,\n",
131-
" strategy=ElasticsearchStore.SparseVectorRetrievalStrategy(),\n",
125+
" embedding=bedrock_embedding,\n",
132126
")"
133127
]
134128
},
@@ -210,11 +204,7 @@
210204
"source": [
211205
"## Index data into elasticsearch\n",
212206
"\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."
218208
]
219209
},
220210
{
@@ -230,7 +220,7 @@
230220
" es_cloud_id=ELASTIC_CLOUD_ID,\n",
231221
" es_api_key=ELASTIC_API_KEY,\n",
232222
" index_name=\"workplace_index\",\n",
233-
" strategy=ElasticsearchStore.SparseVectorRetrievalStrategy(),\n",
223+
" embedding=bedrock_embedding,\n",
234224
")"
235225
]
236226
},
@@ -265,7 +255,7 @@
265255
},
266256
"source": [
267257
"## 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."
269259
]
270260
},
271261
{
@@ -333,8 +323,7 @@
333323
"\n",
334324
"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",
335325
"Communication\n",
336-
"-------\n",
337-
"\n"
326+
"-------"
338327
]
339328
}
340329
],
@@ -351,7 +340,7 @@
351340
" \"How does compensation work?\",\n",
352341
"]\n",
353342
"question = questions[1]\n",
354-
"print(f\"Question: {question}\\n\")\n",
343+
"print(f\"Question: {question}\")\n",
355344
"\n",
356345
"ans = qa({\"query\": question})\n",
357346
"\n",
@@ -361,7 +350,7 @@
361350
"for doc in ans[\"source_documents\"]:\n",
362351
" print(\"Name: \" + doc.metadata[\"name\"])\n",
363352
" print(\"Content: \" + doc.page_content)\n",
364-
" print(\"-------\\n\")"
353+
" print(\"-------\")"
365354
]
366355
}
367356
],

0 commit comments

Comments
 (0)