Skip to content

Commit

Permalink
example usage (langchain-ai#5182)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffvestal authored and Undertone0809 committed Jun 19, 2023
1 parent 0a17dd6 commit 8dc2b9c
Showing 1 changed file with 33 additions and 46 deletions.
79 changes: 33 additions & 46 deletions docs/modules/models/text_embedding/examples/elasticsearch.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -17,118 +17,105 @@
{
"cell_type": "code",
"source": [
"!pip install elasticsearch langchain"
"!pip -q install elasticsearch langchain"
],
"metadata": {
"id": "OOiBBjc0Kd-6"
"id": "6dJxqebov4eU"
},
"execution_count": null,
"outputs": []
},
{
"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": []
},
{
"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": []
},
{
"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": []
},
{
"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": []
},
{
"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": []
},
{
"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": []
},
{
"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": []
Expand Down

0 comments on commit 8dc2b9c

Please sign in to comment.