mirror of
https://github.com/hwchase17/langchain.git
synced 2025-04-28 11:55:21 +00:00
docs: Add Milvus Standalone to documentation (#29650)
- [x] **PR title**: - [x] **PR message**: - Added a new section for how to set up and use Milvus with Docker, and added an example of how to instantiate Milvus for hybrid retrieval - Fixed the documentation setup to run `make lint` and `make format` - [x] **Add tests and docs**: If you're adding a new integration, please include N/A - [x] **Lint and test**: Run `make format`, `make lint` and `make test` from the root of the package(s) you've modified. See contribution guidelines for more: https://python.langchain.com/docs/contributing/ Additional guidelines: - Make sure optional dependencies are imported within a function. - Please do not add dependencies to pyproject.toml files (even optional ones) unless they are required for unit tests. - Most PRs should not touch more than one package. - Changes should be backwards compatible. - If you are adding something to community, do not re-import it in langchain. If no one reviews your PR within a few days, please @-mention one of baskaryan, efriis, eyurtsev, ccurme, vbarda, hwchase17. --------- Co-authored-by: Mark Perfect <mark.anthony.perfect1@gmail.com> Co-authored-by: Chester Curme <chester.curme@gmail.com>
This commit is contained in:
parent
e0e9e560b3
commit
289b3422dc
@ -50,11 +50,6 @@ locally to ensure that it looks good and is free of errors.
|
||||
If you're unable to build it locally that's okay as well, as you will be able to
|
||||
see a preview of the documentation on the pull request page.
|
||||
|
||||
From the **monorepo root**, run the following command to install the dependencies:
|
||||
|
||||
```bash
|
||||
poetry install --with lint,docs --no-root
|
||||
````
|
||||
|
||||
### Building
|
||||
|
||||
@ -158,14 +153,6 @@ the working directory to the `langchain-community` directory:
|
||||
cd [root]/libs/langchain-community
|
||||
```
|
||||
|
||||
Set up a virtual environment for the package if you haven't done so already.
|
||||
|
||||
Install the dependencies for the package.
|
||||
|
||||
```bash
|
||||
poetry install --with lint
|
||||
```
|
||||
|
||||
Then you can run the following commands to lint and format the in-code documentation:
|
||||
|
||||
```bash
|
||||
|
@ -20,17 +20,35 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 1,
|
||||
"id": "a62cff8a-bcf7-4e33-bbbc-76999c2e3e20",
|
||||
"metadata": {
|
||||
"id": "a62cff8a-bcf7-4e33-bbbc-76999c2e3e20",
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Note: you may need to restart the kernel to use updated packages.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"%pip install -qU langchain_milvus"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "dfd17253",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Credentials\n",
|
||||
"\n",
|
||||
"No credentials are needed to use the `Milvus` vector store."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "633addc3",
|
||||
@ -38,12 +56,6 @@
|
||||
"id": "633addc3"
|
||||
},
|
||||
"source": [
|
||||
"The latest version of `pymilvus` comes with a local vector database called Milvus Lite, which is good for prototyping. If you have a large amount of data (e.g., more than a million vectors), we recommend setting up a more performant Milvus server on [Docker](https://milvus.io/docs/install_standalone-docker.md#Start-Milvus) or [Kubernetes](https://milvus.io/docs/install_cluster-milvusoperator.md).\n",
|
||||
"\n",
|
||||
"### Credentials\n",
|
||||
"\n",
|
||||
"No credentials are needed to use the `Milvus` vector store.\n",
|
||||
"\n",
|
||||
"## Initialization\n",
|
||||
"\n",
|
||||
"import EmbeddingTabs from \"@theme/EmbeddingTabs\";\n",
|
||||
@ -53,7 +65,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 2,
|
||||
"id": "a7dd253f",
|
||||
"metadata": {
|
||||
"id": "a7dd253f"
|
||||
@ -67,9 +79,19 @@
|
||||
"embeddings = OpenAIEmbeddings(model=\"text-embedding-3-large\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "50e55d42",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Milvus Lite\n",
|
||||
"\n",
|
||||
"The easiest way to prototype is to use Milvus Lite, where everything is stored in a local vector database file. Only the Flat index can be used."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 3,
|
||||
"id": "dcf88bdf",
|
||||
"metadata": {
|
||||
"id": "dcf88bdf",
|
||||
@ -79,18 +101,145 @@
|
||||
"source": [
|
||||
"from langchain_milvus import Milvus\n",
|
||||
"\n",
|
||||
"# The easiest way is to use Milvus Lite where everything is stored in a local file.\n",
|
||||
"# If you have a Milvus server you can use the server URI such as \"http://localhost:19530\".\n",
|
||||
"URI = \"./milvus_example.db\"\n",
|
||||
"\n",
|
||||
"vector_store = Milvus(\n",
|
||||
" embedding_function=embeddings,\n",
|
||||
" connection_args={\"uri\": URI},\n",
|
||||
" # Set index_params if needed\n",
|
||||
" index_params={\"index_type\": \"FLAT\", \"metric_type\": \"L2\"},\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "df34e8f4",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Milvus Standalone\n",
|
||||
"\n",
|
||||
"If you have a large amount of data (e.g., more than a million vectors), we recommend setting up a more performant Milvus server on [Docker](https://milvus.io/docs/install_standalone-docker.md#Start-Milvus) or [Kubernetes](https://milvus.io/docs/install_cluster-milvusoperator.md).\n",
|
||||
"\n",
|
||||
"Milvus Standalone also supports different [indexes](https://milvus.io/docs/index.md?tab=floating), if you want to improve retrieval functionality.\n",
|
||||
"\n",
|
||||
"To launch the Docker container, run:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "0fddc9a6",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Password:"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"!curl -sfL https://raw.githubusercontent.com/milvus-io/milvus/master/scripts/standalone_embed.sh -o standalone_embed.sh\n",
|
||||
"\n",
|
||||
"!bash standalone_embed.sh start"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "9045ef4a",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Here we create a Milvus database:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"id": "fcff1834",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Database 'milvus_demo' does not exist.\n",
|
||||
"Database 'milvus_demo' created successfully.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from pymilvus import Collection, MilvusException, connections, db, utility\n",
|
||||
"\n",
|
||||
"conn = connections.connect(host=\"127.0.0.1\", port=19530)\n",
|
||||
"\n",
|
||||
"# Check if the database exists\n",
|
||||
"db_name = \"milvus_demo\"\n",
|
||||
"try:\n",
|
||||
" existing_databases = db.list_database()\n",
|
||||
" if db_name in existing_databases:\n",
|
||||
" print(f\"Database '{db_name}' already exists.\")\n",
|
||||
"\n",
|
||||
" # Use the database context\n",
|
||||
" db.using_database(db_name)\n",
|
||||
"\n",
|
||||
" # Drop all collections in the database\n",
|
||||
" collections = utility.list_collections()\n",
|
||||
" for collection_name in collections:\n",
|
||||
" collection = Collection(name=collection_name)\n",
|
||||
" collection.drop()\n",
|
||||
" print(f\"Collection '{collection_name}' has been dropped.\")\n",
|
||||
"\n",
|
||||
" db.drop_database(db_name)\n",
|
||||
" print(f\"Database '{db_name}' has been deleted.\")\n",
|
||||
" else:\n",
|
||||
" print(f\"Database '{db_name}' does not exist.\")\n",
|
||||
" database = db.create_database(db_name)\n",
|
||||
" print(f\"Database '{db_name}' created successfully.\")\n",
|
||||
"except MilvusException as e:\n",
|
||||
" print(f\"An error occurred: {e}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "b50e3ff7",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Note the change in the URI below. Once the instance is initialized, navigate to http://127.0.0.1:9091/webui to view the local web UI.\n",
|
||||
"\n",
|
||||
"Here is an example of how you would use a dense embedding + the Milvus BM25 built-in function to assemble a hybrid retrieval vector store instance:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"id": "07460732",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_milvus import BM25BuiltInFunction, Milvus\n",
|
||||
"\n",
|
||||
"dense_index_param = {\n",
|
||||
" \"metric_type\": \"COSINE\",\n",
|
||||
" \"index_type\": \"HNSW\",\n",
|
||||
"}\n",
|
||||
"sparse_index_param = {\n",
|
||||
" \"metric_type\": \"BM25\",\n",
|
||||
" \"index_type\": \"AUTOINDEX\",\n",
|
||||
"}\n",
|
||||
"\n",
|
||||
"URI = \"http://localhost:19530\"\n",
|
||||
"\n",
|
||||
"vectorstore = Milvus(\n",
|
||||
" embedding_function=embeddings,\n",
|
||||
" builtin_function=BM25BuiltInFunction(output_field_names=\"sparse\"),\n",
|
||||
" index_params=[dense_index_param, sparse_index_param],\n",
|
||||
" vector_field=[\"dense\", \"sparse\"],\n",
|
||||
" connection_args={\"uri\": URI, \"token\": \"root:Milvus\", \"db_name\": \"milvus_demo\"},\n",
|
||||
" consistency_level=\"Strong\",\n",
|
||||
" drop_old=False, # set to True if seeking to drop the collection with that name if it exists\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "cae1a7d5",
|
||||
@ -100,16 +249,8 @@
|
||||
"source": [
|
||||
"### Compartmentalize the data with Milvus Collections\n",
|
||||
"\n",
|
||||
"You can store unrelated documents in different collections within the same Milvus instance."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "c07cd24b",
|
||||
"metadata": {
|
||||
"id": "c07cd24b"
|
||||
},
|
||||
"source": [
|
||||
"You can store unrelated documents in different collections within the same Milvus instance.\n",
|
||||
"\n",
|
||||
"Here's how you can create a new collection:"
|
||||
]
|
||||
},
|
||||
@ -182,34 +323,14 @@
|
||||
"id": "3ced24f6",
|
||||
"outputId": "9c57a6bb-86eb-456c-f007-6cabd6865299"
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"['b0248595-2a41-4f6b-9c25-3a24c1278bb3',\n",
|
||||
" 'fa642726-5329-4495-a072-187e948dd71f',\n",
|
||||
" '9905001c-a4a3-455e-ab94-72d0ed11b476',\n",
|
||||
" 'eacc7256-d7fa-4036-b1f7-83d7a4bee0c5',\n",
|
||||
" '7508f7ff-c0c9-49ea-8189-634f8a0244d8',\n",
|
||||
" '2e179609-3ff7-4c6a-9e05-08978903fe26',\n",
|
||||
" 'fab1f2ac-43e1-45f9-b81b-fc5d334c6508',\n",
|
||||
" '1206d237-ee3a-484f-baf2-b5ac38eeb314',\n",
|
||||
" 'd43cbf9a-a772-4c40-993b-9439065fec01',\n",
|
||||
" '25e667bb-6f09-4574-a368-661069301906']"
|
||||
]
|
||||
},
|
||||
"execution_count": 31,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from uuid import uuid4\n",
|
||||
"\n",
|
||||
"from langchain_core.documents import Document\n",
|
||||
"\n",
|
||||
"document_1 = Document(\n",
|
||||
" page_content=\"I had chocalate chip pancakes and scrambled eggs for breakfast this morning.\",\n",
|
||||
" page_content=\"I had chocolate chip pancakes and scrambled eggs for breakfast this morning.\",\n",
|
||||
" metadata={\"source\": \"tweet\"},\n",
|
||||
")\n",
|
||||
"\n",
|
||||
@ -462,7 +583,7 @@
|
||||
"When building a retrieval app, you often have to build it with multiple users in mind. This means that you may be storing data not just for one user, but for many different users, and they should not be able to see each other’s data.\n",
|
||||
"\n",
|
||||
"Milvus recommends using [partition_key](https://milvus.io/docs/multi_tenancy.md#Partition-key-based-multi-tenancy) to implement multi-tenancy. Here is an example:\n",
|
||||
"> The feature of Partition key is now not available in Milvus Lite, if you want to use it, you need to start Milvus server, as mentioned above."
|
||||
"> The Partition key feature is not available in Milvus Lite, if you want to use it, you need to start Milvus server, as mentioned above."
|
||||
]
|
||||
},
|
||||
{
|
||||
@ -591,7 +712,7 @@
|
||||
"provenance": []
|
||||
},
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"display_name": ".venv",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user