2023-02-20 07:14:50 +00:00
{
"cells": [
{
"cell_type": "markdown",
"id": "683953b3",
"metadata": {},
"source": [
"# Milvus\n",
"\n",
2023-04-29 02:26:50 +00:00
">[Milvus](https://milvus.io/docs/overview.md) is a database that stores, indexes, and manages massive embedding vectors generated by deep neural networks and other machine learning (ML) models.\n",
"\n",
2023-02-20 07:14:50 +00:00
"This notebook shows how to use functionality related to the Milvus vector database.\n",
"\n",
2023-04-29 02:26:50 +00:00
"To run, you should have a [Milvus instance up and running](https://milvus.io/docs/install_standalone-docker.md)."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a62cff8a-bcf7-4e33-bbbc-76999c2e3e20",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"!pip install pymilvus"
]
},
{
"cell_type": "markdown",
"id": "7a0f9e02-8eb0-4aef-b11f-8861360472ee",
"metadata": {},
"source": [
"We want to use OpenAIEmbeddings so we have to get the OpenAI API Key."
2023-02-20 07:14:50 +00:00
]
},
{
"cell_type": "code",
"execution_count": 1,
2023-04-29 02:26:50 +00:00
"id": "8b6ed9cd-81b9-46e5-9c20-5aafca2844d0",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"OpenAI API Key: ········\n"
]
}
],
"source": [
"import os\n",
"import getpass\n",
"\n",
"os.environ['OPENAI_API_KEY'] = getpass.getpass('OpenAI API Key:')"
]
},
{
"cell_type": "code",
"execution_count": 3,
2023-02-20 07:14:50 +00:00
"id": "aac9563e",
2023-04-29 02:26:50 +00:00
"metadata": {
"tags": []
},
2023-02-20 07:14:50 +00:00
"outputs": [],
"source": [
"from langchain.embeddings.openai import OpenAIEmbeddings\n",
"from langchain.text_splitter import CharacterTextSplitter\n",
"from langchain.vectorstores import Milvus\n",
"from langchain.document_loaders import TextLoader"
]
},
{
"cell_type": "code",
2023-04-29 02:26:50 +00:00
"execution_count": 4,
2023-02-20 07:14:50 +00:00
"id": "a3c3999a",
2023-04-29 02:26:50 +00:00
"metadata": {
"tags": []
},
2023-02-20 07:14:50 +00:00
"outputs": [],
"source": [
"from langchain.document_loaders import TextLoader\n",
2023-03-27 02:49:46 +00:00
"loader = TextLoader('../../../state_of_the_union.txt')\n",
2023-02-20 07:14:50 +00:00
"documents = loader.load()\n",
"text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n",
"docs = text_splitter.split_documents(documents)\n",
"\n",
"embeddings = OpenAIEmbeddings()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dcf88bdf",
2023-04-29 02:26:50 +00:00
"metadata": {
"tags": []
},
2023-02-20 07:14:50 +00:00
"outputs": [],
"source": [
"vector_db = Milvus.from_documents(\n",
" docs,\n",
" embeddings,\n",
" connection_args={\"host\": \"127.0.0.1\", \"port\": \"19530\"},\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a8c513ab",
"metadata": {},
"outputs": [],
"source": [
"docs = vector_db.similarity_search(query)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fc516993",
"metadata": {},
"outputs": [],
"source": [
"docs[0]"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
2023-04-29 02:26:50 +00:00
"version": "3.10.6"
2023-02-20 07:14:50 +00:00
}
},
"nbformat": 4,
"nbformat_minor": 5
}