2023-04-02 20:38:20 +00:00
{
"cells": [
{
"cell_type": "markdown",
"id": "683953b3",
"metadata": {},
"source": [
"# Zilliz\n",
"\n",
2023-04-29 02:26:50 +00:00
">[Zilliz Cloud](https://zilliz.com/doc/quick_start) is a fully managed service on cloud for `LF AI Milvus®`,\n",
"\n",
2023-04-02 20:38:20 +00:00
"This notebook shows how to use functionality related to the Zilliz Cloud managed vector database.\n",
"\n",
2023-04-29 02:26:50 +00:00
"To run, you should have a `Zilliz Cloud` instance up and running. Here are the [installation instructions](https://zilliz.com/cloud)"
2023-04-02 20:38:20 +00:00
]
},
{
"cell_type": "code",
"execution_count": null,
2023-04-29 02:26:50 +00:00
"id": "c0c50102-e6ac-4475-a930-49c94ed0bd99",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"!pip install pymilvus"
]
},
{
"cell_type": "markdown",
"id": "4b25e246-ffe7-4822-a6bf-85d1a120df00",
"metadata": {},
"source": [
"We want to use `OpenAIEmbeddings` so we have to get the OpenAI API Key."
]
},
{
"cell_type": "code",
2023-05-15 23:16:57 +00:00
"execution_count": 1,
2023-04-29 02:26:50 +00:00
"id": "d6691489-1ebc-40fa-bc09-b0916903a24d",
2023-04-02 20:38:20 +00:00
"metadata": {},
2023-05-15 23:16:57 +00:00
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"OpenAI API Key:········\n"
]
}
],
2023-04-02 20:38:20 +00:00
"source": [
2023-04-29 02:26:50 +00:00
"import os\n",
"import getpass\n",
"\n",
2023-06-16 18:52:56 +00:00
"os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"OpenAI API Key:\")"
2023-04-02 20:38:20 +00:00
]
},
{
"cell_type": "code",
2023-05-15 23:16:57 +00:00
"execution_count": 2,
2023-04-02 20:38:20 +00:00
"id": "19a71422",
"metadata": {},
"outputs": [],
"source": [
2023-06-16 18:52:56 +00:00
"# replace\n",
"ZILLIZ_CLOUD_URI = \"\" # example: \"https://in01-17f69c292d4a5sa.aws-us-west-2.vectordb.zillizcloud.com:19536\"\n",
2023-04-29 05:10:13 +00:00
"ZILLIZ_CLOUD_USERNAME = \"\" # example: \"username\"\n",
2023-07-18 16:31:39 +00:00
"ZILLIZ_CLOUD_PASSWORD = \"\" # example: \"*********\"\n",
"ZILLIZ_CLOUD_API_KEY = \"\" # example: \"*********\" (for serverless clusters which can be used as replacements for user and password)"
2023-04-02 20:38:20 +00:00
]
},
2023-04-29 02:26:50 +00:00
{
"cell_type": "code",
2023-05-15 23:16:57 +00:00
"execution_count": 3,
2023-04-29 02:26:50 +00:00
"id": "aac9563e",
"metadata": {},
"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"
]
},
2023-04-02 20:38:20 +00:00
{
"cell_type": "code",
2023-05-15 23:16:57 +00:00
"execution_count": 4,
2023-04-02 20:38:20 +00:00
"id": "a3c3999a",
"metadata": {},
"outputs": [],
"source": [
"from langchain.document_loaders import TextLoader\n",
2023-06-16 18:52:56 +00:00
"\n",
"loader = TextLoader(\"../../../state_of_the_union.txt\")\n",
2023-04-02 20:38:20 +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",
2023-05-15 23:16:57 +00:00
"execution_count": 5,
2023-04-02 20:38:20 +00:00
"id": "dcf88bdf",
"metadata": {},
"outputs": [],
"source": [
"vector_db = Milvus.from_documents(\n",
" docs,\n",
" embeddings,\n",
2023-04-29 05:10:13 +00:00
" connection_args={\n",
" \"uri\": ZILLIZ_CLOUD_URI,\n",
2023-05-15 23:16:57 +00:00
" \"user\": ZILLIZ_CLOUD_USERNAME,\n",
2023-04-29 05:10:13 +00:00
" \"password\": ZILLIZ_CLOUD_PASSWORD,\n",
2023-07-18 16:31:39 +00:00
" # \"token\": ZILLIZ_CLOUD_API_KEY, # API key, for serverless clusters which can be used as replacements for user and password\n",
2023-06-16 18:52:56 +00:00
" \"secure\": True,\n",
" },\n",
2023-04-02 20:38:20 +00:00
")"
]
},
{
"cell_type": "code",
2023-05-15 23:16:57 +00:00
"execution_count": 6,
2023-04-02 20:38:20 +00:00
"id": "a8c513ab",
"metadata": {},
"outputs": [],
"source": [
2023-05-15 23:16:57 +00:00
"query = \"What did the president say about Ketanji Brown Jackson\"\n",
2023-04-02 20:38:20 +00:00
"docs = vector_db.similarity_search(query)"
]
},
{
"cell_type": "code",
2023-05-15 23:16:57 +00:00
"execution_count": 7,
2023-04-02 20:38:20 +00:00
"id": "fc516993",
"metadata": {},
2023-05-15 23:16:57 +00:00
"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": 7,
"metadata": {},
"output_type": "execute_result"
}
],
2023-04-02 20:38:20 +00:00
"source": [
2023-05-15 23:16:57 +00:00
"docs[0].page_content"
2023-04-02 20:38:20 +00:00
]
2023-05-15 23:16:57 +00:00
},
{
"cell_type": "code",
"execution_count": null,
"id": "dc85398b",
"metadata": {},
"outputs": [],
"source": []
2023-04-02 20:38:20 +00:00
}
],
"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-07-18 16:31:39 +00:00
"version": "3.11.3"
2023-04-02 20:38:20 +00:00
}
},
"nbformat": 4,
"nbformat_minor": 5
}