notebooks/langchain/_nbtest.setup.langchain-vector-store-using-elser.ipynb (121 lines of code) (raw):
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "62ab8486-e088-424b-80d4-1e9a6a181051",
"metadata": {},
"outputs": [],
"source": [
"!pip install -qU \"elasticsearch<9\""
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "088845c2-ec80-4a66-995d-6f8092fe5058",
"metadata": {},
"outputs": [],
"source": [
"# get the Elasticsearch client\n",
"from elasticsearch import Elasticsearch, exceptions\n",
"from getpass import getpass\n",
"import time\n",
"\n",
"ELASTIC_CLOUD_ID = getpass(\"Elastic Cloud ID: \")\n",
"ELASTIC_API_KEY = getpass(\"Elastic Api Key: \")\n",
"\n",
"client = Elasticsearch(\n",
" cloud_id=ELASTIC_CLOUD_ID,\n",
" api_key=ELASTIC_API_KEY,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "26d7a2ad-4ace-4122-8d07-7b300014dca8",
"metadata": {},
"outputs": [],
"source": [
"# delete model if already downloaded and deployed\n",
"try:\n",
" client.ml.delete_trained_model(model_id=\".elser_model_2\", force=True)\n",
" print(\"Model deleted successfully, We will proceed with creating one\")\n",
"except exceptions.NotFoundError:\n",
" print(\"Model doesn't exist, but We will proceed with creating one\")\n",
"\n",
"# Creates the ELSER model configuration. Automatically downloads the model if it doesn't exist.\n",
"client.ml.put_trained_model(\n",
" model_id=\".elser_model_2\", input={\"field_names\": [\"text_field\"]}\n",
")\n",
"\n",
"while True:\n",
" status = client.ml.get_trained_models(\n",
" model_id=\".elser_model_2\", include=\"definition_status\"\n",
" )\n",
"\n",
" if status[\"trained_model_configs\"][0][\"fully_defined\"]:\n",
" break\n",
" time.sleep(5)\n",
"\n",
"# Start trained model deployment if not already deployed\n",
"client.ml.start_trained_model_deployment(\n",
" model_id=\".elser_model_2\", number_of_allocations=1, wait_for=\"starting\"\n",
")\n",
"\n",
"while True:\n",
" status = client.ml.get_trained_models_stats(\n",
" model_id=\".elser_model_2\",\n",
" )\n",
" if status[\"trained_model_stats\"][0][\"deployment_stats\"][\"state\"] == \"started\":\n",
" print(\"ELSER Model has been successfully deployed.\")\n",
" break\n",
" else:\n",
" print(\"ELSER Model is currently being deployed.\")\n",
" time.sleep(5)\n",
"\n",
"time.sleep(5)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "75fb9815-56d2-45c7-8467-e92b2f8aee7c",
"metadata": {},
"outputs": [],
"source": [
"client.indices.delete(index=\"workplace_index\", ignore_unavailable=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5a8141b0-5ae5-44d4-aa84-32368a55d276",
"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.11.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}