{ "cells": [ { "cell_type": "markdown", "id": "683953b3", "metadata": {}, "source": [ "# Milvus\n", "\n", ">[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", "This notebook shows how to use functionality related to the Milvus vector database.\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": 1, "id": "8b6ed9cd-81b9-46e5-9c20-5aafca2844d0", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "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": 2, "id": "aac9563e", "metadata": { "tags": [] }, "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", "execution_count": 3, "id": "a3c3999a", "metadata": { "tags": [] }, "outputs": [], "source": [ "from langchain.document_loaders import TextLoader\n", "\n", "loader = TextLoader(\"../../../state_of_the_union.txt\")\n", "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": 4, "id": "dcf88bdf", "metadata": { "tags": [] }, "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": 5, "id": "a8c513ab", "metadata": {}, "outputs": [], "source": [ "query = \"What did the president say about Ketanji Brown Jackson\"\n", "docs = vector_db.similarity_search(query)" ] }, { "cell_type": "code", "execution_count": 6, "id": "fc516993", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Tonight. I call on the Senate to: Pass the Freedom to Vote Act. Pass the John Lewis Voting Rights Act. And while you’re at it, pass the Disclose Act so Americans can know who is funding our elections. \\n\\nTonight, I’d like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service. \\n\\nOne of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court. \\n\\nAnd I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence.'" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "docs[0].page_content" ] }, { "cell_type": "code", "execution_count": null, "id": "e40d558b", "metadata": {}, "outputs": [], "source": [] } ], "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", "version": "3.9.12" } }, "nbformat": 4, "nbformat_minor": 5 }