mirror of
https://github.com/hwchase17/langchain.git
synced 2025-05-19 14:01:50 +00:00
Adding example usage for elasticsearch knn embeddings [per](https://github.com/hwchase17/langchain/pull/3401#issuecomment-1548518389) https://github.com/hwchase17/langchain/blob/master/langchain/embeddings/elasticsearch.py
124 lines
2.9 KiB
Plaintext
124 lines
2.9 KiB
Plaintext
{
|
|
"nbformat": 4,
|
|
"nbformat_minor": 0,
|
|
"metadata": {
|
|
"colab": {
|
|
"provenance": []
|
|
},
|
|
"kernelspec": {
|
|
"name": "python3",
|
|
"display_name": "Python 3"
|
|
},
|
|
"language_info": {
|
|
"name": "python"
|
|
}
|
|
},
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"source": [
|
|
"!pip -q install elasticsearch langchain"
|
|
],
|
|
"metadata": {
|
|
"id": "6dJxqebov4eU"
|
|
},
|
|
"execution_count": null,
|
|
"outputs": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": [
|
|
"import elasticsearch\n",
|
|
"from langchain.embeddings.elasticsearch import ElasticsearchEmbeddings"
|
|
],
|
|
"metadata": {
|
|
"id": "RV7C3DUmv4aq"
|
|
},
|
|
"execution_count": null,
|
|
"outputs": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": [
|
|
"# Define the model ID\n",
|
|
"model_id = 'your_model_id'"
|
|
],
|
|
"metadata": {
|
|
"id": "MrT3jplJvp09"
|
|
},
|
|
"execution_count": null,
|
|
"outputs": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": [
|
|
"# 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": "svtdnC-dvpxR"
|
|
},
|
|
"execution_count": null,
|
|
"outputs": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": [
|
|
"# 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.embed_documents(documents)\n"
|
|
],
|
|
"metadata": {
|
|
"id": "7DXZAK7Kvpth"
|
|
},
|
|
"execution_count": null,
|
|
"outputs": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": [
|
|
"# Print document embeddings\n",
|
|
"for i, embedding in enumerate(document_embeddings):\n",
|
|
" print(f\"Embedding for document {i+1}: {embedding}\")\n"
|
|
],
|
|
"metadata": {
|
|
"id": "K8ra75W_vpqy"
|
|
},
|
|
"execution_count": null,
|
|
"outputs": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": [
|
|
"# Create an embedding for a single query\n",
|
|
"query = 'This is a single query.'\n",
|
|
"query_embedding = embeddings.embed_query(query)\n"
|
|
],
|
|
"metadata": {
|
|
"id": "V4Q5kQo9vpna"
|
|
},
|
|
"execution_count": null,
|
|
"outputs": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"source": [
|
|
"# Print query embedding\n",
|
|
"print(f\"Embedding for query: {query_embedding}\")\n"
|
|
],
|
|
"metadata": {
|
|
"id": "O0oQDzGKvpkz"
|
|
},
|
|
"execution_count": null,
|
|
"outputs": []
|
|
}
|
|
]
|
|
} |