Skip to content

Commit

Permalink
one more langchain notebook added to nbtest
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Jan 25, 2024
1 parent 9190da9 commit 6dad915
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 36 deletions.
3 changes: 2 additions & 1 deletion notebooks/langchain/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
NBTEST = ../../bin/nbtest
NOTEBOOKS = \
langchain-using-own-model.ipynb
langchain-using-own-model.ipynb \
langchain-vector-store-using-elser.ipynb

.PHONY: all $(NOTEBOOKS)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "62ab8486-e088-424b-80d4-1e9a6a181051",
"metadata": {},
"outputs": [],
"source": [
"!pip install -qU elasticsearch"
]
},
{
"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(cloud_id=ELASTIC_CLOUD_ID, api_key=ELASTIC_API_KEY,)"
]
},
{
"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\",\n",
" input={\n",
" \"field_names\": [\"text_field\"]\n",
" }\n",
" )\n",
"\n",
"while True:\n",
" status = client.ml.get_trained_models(\n",
" model_id=\".elser_model_2\",\n",
" 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\",\n",
" number_of_allocations=1,\n",
" 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
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "8bed2c83-87a7-40ca-bc5e-b9088c7e38a0",
"metadata": {},
"outputs": [],
"source": [
"from elasticsearch import Elasticsearch\n",
"from getpass import getpass\n",
"\n",
"ELASTIC_CLOUD_ID = getpass(\"Elastic Cloud ID: \")\n",
"ELASTIC_API_KEY = getpass(\"Elastic Api Key: \")\n",
"\n",
"client = Elasticsearch(cloud_id=ELASTIC_CLOUD_ID, api_key=ELASTIC_API_KEY,)\n",
"\n",
"# delete the notebook's index\n",
"client.indices.delete(index=\"workplace_index\", ignore_unavailable=True)\n",
"\n",
"# delete the pipeline\n",
"try:\n",
" client.ingest.delete_pipeline(id=\".elser_model_2_sparse_embedding\")\n",
"except:\n",
" pass\n",
"\n",
"# delete the model\n",
"try:\n",
" client.ml.delete_trained_model(model_id=\".elser_model_2\", force=True)\n",
"except:\n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6dbce06c-fea9-457b-87c4-0a0e5cfcc1b7",
"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
}
Loading

0 comments on commit 6dad915

Please sign in to comment.