Skip to content

Commit

Permalink
docs: Update elasticsearch README (langchain-ai#18497)
Browse files Browse the repository at this point in the history
Update Elasticsearch README with information on how to start a
deployment.

Also make some cosmetic changes to the [Elasticsearch
docs](https://python.langchain.com/docs/integrations/vectorstores/elasticsearch).

Follow up on langchain-ai#17467
  • Loading branch information
maxjakob authored and thebhulawat committed Mar 6, 2024
1 parent b9d306e commit 9106cef
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 65 deletions.
98 changes: 49 additions & 49 deletions docs/docs/integrations/vectorstores/elasticsearch.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"metadata": {},
"outputs": [],
"source": [
"%pip install --upgrade --quiet langchain-elasticsearch langchain-openai tiktoken langchain"
"%pip install --upgrade --quiet langchain-elasticsearch langchain-openai tiktoken langchain"
]
},
{
Expand Down Expand Up @@ -57,61 +57,61 @@
"Example: Run a single-node Elasticsearch instance with security disabled. This is not recommended for production use.\n",
"\n",
"```bash\n",
" docker run -p 9200:9200 -e \"discovery.type=single-node\" -e \"xpack.security.enabled=false\" -e \"xpack.security.http.ssl.enabled=false\" docker.elastic.co/elasticsearch/elasticsearch:8.9.0\n",
"docker run -p 9200:9200 -e \"discovery.type=single-node\" -e \"xpack.security.enabled=false\" -e \"xpack.security.http.ssl.enabled=false\" docker.elastic.co/elasticsearch/elasticsearch:8.12.1\n",
"```\n",
"\n",
"Once the Elasticsearch instance is running, you can connect to it using the Elasticsearch URL and index name along with the embedding object to the constructor.\n",
"\n",
"Example:\n",
"```python\n",
" from langchain_elasticsearch import ElasticsearchStore\n",
" from langchain_openai import OpenAIEmbeddings\n",
"\n",
" embedding = OpenAIEmbeddings()\n",
" elastic_vector_search = ElasticsearchStore(\n",
" es_url=\"http://localhost:9200\",\n",
" index_name=\"test_index\",\n",
" embedding=embedding\n",
" )\n",
"from langchain_elasticsearch import ElasticsearchStore\n",
"from langchain_openai import OpenAIEmbeddings\n",
"\n",
"embedding = OpenAIEmbeddings()\n",
"elastic_vector_search = ElasticsearchStore(\n",
" es_url=\"http://localhost:9200\",\n",
" index_name=\"test_index\",\n",
" embedding=embedding\n",
")\n",
"```\n",
"### Authentication\n",
"For production, we recommend you run with security enabled. To connect with login credentials, you can use the parameters `es_api_key` or `es_user` and `es_password`.\n",
"\n",
"Example:\n",
"```python\n",
" from langchain_elasticsearch import ElasticsearchStore\n",
" from langchain_openai import OpenAIEmbeddings\n",
"\n",
" embedding = OpenAIEmbeddings()\n",
" elastic_vector_search = ElasticsearchStore(\n",
" es_url=\"http://localhost:9200\",\n",
" index_name=\"test_index\",\n",
" embedding=embedding,\n",
" es_user=\"elastic\",\n",
" es_password=\"changeme\"\n",
" )\n",
"from langchain_elasticsearch import ElasticsearchStore\n",
"from langchain_openai import OpenAIEmbeddings\n",
"\n",
"embedding = OpenAIEmbeddings()\n",
"elastic_vector_search = ElasticsearchStore(\n",
" es_url=\"http://localhost:9200\",\n",
" index_name=\"test_index\",\n",
" embedding=embedding,\n",
" es_user=\"elastic\",\n",
" es_password=\"changeme\"\n",
")\n",
"```\n",
"\n",
"You can also use an `Elasticsearch` client object that gives you more flexibility, for example to configure the maximum number of retries.\n",
"\n",
"Example:\n",
"```python\n",
" import elasticsearch\n",
" from langchain_elasticsearch import ElasticsearchStore\n",
"\n",
" es_client= elasticsearch.Elasticsearch(\n",
" hosts=[\"http://localhost:9200\"],\n",
" es_user=\"elastic\",\n",
" es_password=\"changeme\"\n",
" max_retries=10,\n",
" )\n",
"\n",
" embedding = OpenAIEmbeddings()\n",
" elastic_vector_search = ElasticsearchStore(\n",
" index_name=\"test_index\",\n",
" es_connection=es_client,\n",
" embedding=embedding,\n",
" )\n",
"import elasticsearch\n",
"from langchain_elasticsearch import ElasticsearchStore\n",
"\n",
"es_client= elasticsearch.Elasticsearch(\n",
" hosts=[\"http://localhost:9200\"],\n",
" es_user=\"elastic\",\n",
" es_password=\"changeme\"\n",
" max_retries=10,\n",
")\n",
"\n",
"embedding = OpenAIEmbeddings()\n",
"elastic_vector_search = ElasticsearchStore(\n",
" index_name=\"test_index\",\n",
" es_connection=es_client,\n",
" embedding=embedding,\n",
")\n",
"```\n",
"\n",
"#### How to obtain a password for the default \"elastic\" user?\n",
Expand All @@ -137,17 +137,17 @@
"\n",
"Example:\n",
"```python\n",
" from langchain_elasticsearch import ElasticsearchStore\n",
" from langchain_openai import OpenAIEmbeddings\n",
"\n",
" embedding = OpenAIEmbeddings()\n",
" elastic_vector_search = ElasticsearchStore(\n",
" es_cloud_id=\"<cloud_id>\",\n",
" index_name=\"test_index\",\n",
" embedding=embedding,\n",
" es_user=\"elastic\",\n",
" es_password=\"changeme\"\n",
" )\n",
"from langchain_elasticsearch import ElasticsearchStore\n",
"from langchain_openai import OpenAIEmbeddings\n",
"\n",
"embedding = OpenAIEmbeddings()\n",
"elastic_vector_search = ElasticsearchStore(\n",
" es_cloud_id=\"<cloud_id>\",\n",
" index_name=\"test_index\",\n",
" embedding=embedding,\n",
" es_user=\"elastic\",\n",
" es_password=\"changeme\"\n",
")\n",
"```"
]
},
Expand Down
58 changes: 55 additions & 3 deletions libs/partners/elasticsearch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,38 @@ This package contains the LangChain integration with Elasticsearch.
pip install -U langchain-elasticsearch
```

TODO document how to get id and key
## Elasticsearch setup

### Elastic Cloud

You need a running Elasticsearch deployment. The easiest way to start one is through [Elastic Cloud](https://cloud.elastic.co/).
You can sign up for a [free trial](https://www.elastic.co/cloud/cloud-trial-overview).

1. [Create a deployment](https://www.elastic.co/guide/en/cloud/current/ec-create-deployment.html)
2. Get your Cloud ID:
1. In the [Elastic Cloud console](https://cloud.elastic.co), click "Manage" next to your deployment
2. Copy the Cloud ID and paste it into the `es_cloud_id` parameter below
3. Create an API key:
1. In the [Elastic Cloud console](https://cloud.elastic.co), click "Open" next to your deployment
2. In the left-hand side menu, go to "Stack Management", then to "API Keys"
3. Click "Create API key"
4. Enter a name for the API key and click "Create"
5. Copy the API key and paste it into the `es_api_key` parameter below

### Elastic Cloud

Alternatively, you can run Elasticsearch via Docker as described in the [docs](https://python.langchain.com/docs/integrations/vectorstores/elasticsearch).

## Usage

The `ElasticsearchStore` class exposes the connection to the Pinecone vector store.
### ElasticsearchStore

The `ElasticsearchStore` class exposes Elasticsearch as a vector store.

```python
from langchain_elasticsearch import ElasticsearchStore

embeddings = ... # use a LangChain Embeddings class
embeddings = ... # use a LangChain Embeddings class or ElasticsearchEmbeddings

vectorstore = ElasticsearchStore(
es_cloud_id="your-cloud-id",
Expand All @@ -27,3 +49,33 @@ vectorstore = ElasticsearchStore(
)
```

### ElasticsearchEmbeddings

The `ElasticsearchEmbeddings` class provides an interface to generate embeddings using a model
deployed in an Elasticsearch cluster.

```python
from langchain_elasticsearch import ElasticsearchEmbeddings

embeddings = ElasticsearchEmbeddings.from_credentials(
model_id="your-model-id",
input_field="your-input-field",
es_cloud_id="your-cloud-id",
es_api_key="your-api-key",
)
```

### ElasticsearchChatMessageHistory

The `ElasticsearchChatMessageHistory` class stores chat histories in Elasticsearch.

```python
from langchain_elasticsearch import ElasticsearchChatMessageHistory

chat_history = ElasticsearchChatMessageHistory(
index="your-index-name",
session_id="your-session-id",
es_cloud_id="your-cloud-id",
es_api_key="your-api-key",
)
```
45 changes: 32 additions & 13 deletions libs/partners/elasticsearch/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions libs/partners/elasticsearch/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pytest-asyncio = "^0.21.1"
langchain = { path = "../../langchain", develop = true }
langchain-community = { path = "../../community", develop = true }
langchain-core = { path = "../../core", develop = true }
langchain-text-splitters = {path = "../../text-splitters", develop = true}

[tool.poetry.group.codespell]
optional = true
Expand Down

0 comments on commit 9106cef

Please sign in to comment.