|
|
|
@ -17,10 +17,10 @@
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
|
|
|
|
"source": [
|
|
|
|
|
"!pip install elasticsearch langchain"
|
|
|
|
|
"!pip -q install elasticsearch langchain"
|
|
|
|
|
],
|
|
|
|
|
"metadata": {
|
|
|
|
|
"id": "OOiBBjc0Kd-6"
|
|
|
|
|
"id": "6dJxqebov4eU"
|
|
|
|
|
},
|
|
|
|
|
"execution_count": null,
|
|
|
|
|
"outputs": []
|
|
|
|
@ -28,16 +28,11 @@
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
|
|
|
|
"source": [
|
|
|
|
|
"%env ES_CLOUDID=<cloud id from cloud.elastic.co>\n",
|
|
|
|
|
"%env ES_USER=<user>\n",
|
|
|
|
|
"%env ES_PASS=<password>\n",
|
|
|
|
|
"\n",
|
|
|
|
|
"es_cloudid = os.environ.get(\"ES_CLOUDID\")\n",
|
|
|
|
|
"es_user = os.environ.get(\"ES_USER\")\n",
|
|
|
|
|
"es_pass = os.environ.get(\"ES_PASS\")"
|
|
|
|
|
"import elasticsearch\n",
|
|
|
|
|
"from langchain.embeddings.elasticsearch import ElasticsearchEmbeddings"
|
|
|
|
|
],
|
|
|
|
|
"metadata": {
|
|
|
|
|
"id": "Wr8unljAKdCh"
|
|
|
|
|
"id": "RV7C3DUmv4aq"
|
|
|
|
|
},
|
|
|
|
|
"execution_count": null,
|
|
|
|
|
"outputs": []
|
|
|
|
@ -45,11 +40,11 @@
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
|
|
|
|
"source": [
|
|
|
|
|
"# Connect to Elasticsearch\n",
|
|
|
|
|
"es_connection = Elasticsearch(cloud_id=es_cloudid, basic_auth=(es_user, es_pass))"
|
|
|
|
|
"# Define the model ID\n",
|
|
|
|
|
"model_id = 'your_model_id'"
|
|
|
|
|
],
|
|
|
|
|
"metadata": {
|
|
|
|
|
"id": "YIDsrBqTKs85"
|
|
|
|
|
"id": "MrT3jplJvp09"
|
|
|
|
|
},
|
|
|
|
|
"execution_count": null,
|
|
|
|
|
"outputs": []
|
|
|
|
@ -57,12 +52,16 @@
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
|
|
|
|
"source": [
|
|
|
|
|
"# Define the model ID and input field name (if different from default)\n",
|
|
|
|
|
"model_id = \"your_model_id\"\n",
|
|
|
|
|
"input_field = \"your_input_field\" # Optional, only if different from 'text_field'"
|
|
|
|
|
"# Instantiate ElasticsearchEmbeddings using credentials\n",
|
|
|
|
|
"embeddings = ElasticsearchEmbeddings.from_credentials(\n",
|
|
|
|
|
" model_id,\n",
|
|
|
|
|
" es_cloud_id='your_cloud_id', \n",
|
|
|
|
|
" es_user='your_user', \n",
|
|
|
|
|
" es_password='your_password'\n",
|
|
|
|
|
")\n"
|
|
|
|
|
],
|
|
|
|
|
"metadata": {
|
|
|
|
|
"id": "sfFhnFHOKvbM"
|
|
|
|
|
"id": "svtdnC-dvpxR"
|
|
|
|
|
},
|
|
|
|
|
"execution_count": null,
|
|
|
|
|
"outputs": []
|
|
|
|
@ -70,27 +69,15 @@
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
|
|
|
|
"source": [
|
|
|
|
|
"# Initialize the ElasticsearchEmbeddings instance\n",
|
|
|
|
|
"embeddings_generator = ElasticsearchEmbeddings(es_connection, model_id, input_field)"
|
|
|
|
|
],
|
|
|
|
|
"metadata": {
|
|
|
|
|
"id": "V-pCgqLCKvYs"
|
|
|
|
|
},
|
|
|
|
|
"execution_count": null,
|
|
|
|
|
"outputs": []
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
|
|
|
|
"source": [
|
|
|
|
|
"# Generate embeddings for a list of documents\n",
|
|
|
|
|
"# Create embeddings for multiple documents\n",
|
|
|
|
|
"documents = [\n",
|
|
|
|
|
" \"This is an example document.\",\n",
|
|
|
|
|
" \"Another example document to generate embeddings for.\",\n",
|
|
|
|
|
" ]\n",
|
|
|
|
|
"document_embeddings = embeddings_generator.embed_documents(documents)"
|
|
|
|
|
" 'This is an example document.', \n",
|
|
|
|
|
" 'Another example document to generate embeddings for.'\n",
|
|
|
|
|
"]\n",
|
|
|
|
|
"document_embeddings = embeddings.embed_documents(documents)\n"
|
|
|
|
|
],
|
|
|
|
|
"metadata": {
|
|
|
|
|
"id": "lJg2iRDWKvV_"
|
|
|
|
|
"id": "7DXZAK7Kvpth"
|
|
|
|
|
},
|
|
|
|
|
"execution_count": null,
|
|
|
|
|
"outputs": []
|
|
|
|
@ -98,12 +85,12 @@
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
|
|
|
|
"source": [
|
|
|
|
|
"# Print the generated document embeddings\n",
|
|
|
|
|
"for i, doc_embedding in enumerate(document_embeddings):\n",
|
|
|
|
|
" print(f\"Embedding for document {i + 1}: {doc_embedding}\")"
|
|
|
|
|
"# Print document embeddings\n",
|
|
|
|
|
"for i, embedding in enumerate(document_embeddings):\n",
|
|
|
|
|
" print(f\"Embedding for document {i+1}: {embedding}\")\n"
|
|
|
|
|
],
|
|
|
|
|
"metadata": {
|
|
|
|
|
"id": "R3sYQlh3KvTQ"
|
|
|
|
|
"id": "K8ra75W_vpqy"
|
|
|
|
|
},
|
|
|
|
|
"execution_count": null,
|
|
|
|
|
"outputs": []
|
|
|
|
@ -111,12 +98,12 @@
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
|
|
|
|
"source": [
|
|
|
|
|
"# Generate an embedding for a single query text\n",
|
|
|
|
|
"query_text = \"What is the meaning of life?\"\n",
|
|
|
|
|
"query_embedding = embeddings_generator.embed_query(query_text)"
|
|
|
|
|
"# Create an embedding for a single query\n",
|
|
|
|
|
"query = 'This is a single query.'\n",
|
|
|
|
|
"query_embedding = embeddings.embed_query(query)\n"
|
|
|
|
|
],
|
|
|
|
|
"metadata": {
|
|
|
|
|
"id": "n0un5Vc0KvQd"
|
|
|
|
|
"id": "V4Q5kQo9vpna"
|
|
|
|
|
},
|
|
|
|
|
"execution_count": null,
|
|
|
|
|
"outputs": []
|
|
|
|
@ -124,11 +111,11 @@
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
|
|
|
|
"source": [
|
|
|
|
|
"# Print the generated query embedding\n",
|
|
|
|
|
"print(f\"Embedding for query: {query_embedding}\")"
|
|
|
|
|
"# Print query embedding\n",
|
|
|
|
|
"print(f\"Embedding for query: {query_embedding}\")\n"
|
|
|
|
|
],
|
|
|
|
|
"metadata": {
|
|
|
|
|
"id": "PANph6pmKvLD"
|
|
|
|
|
"id": "O0oQDzGKvpkz"
|
|
|
|
|
},
|
|
|
|
|
"execution_count": null,
|
|
|
|
|
"outputs": []
|
|
|
|
|