Langchain 集成 Milvus
- 1. 安装 Docker
- 2. 部署 Milvus
- 3.
- 4. Langchain 集成 Milvus
1. 安装 Docker
refer: https://docs.docker.com/engine/install/centos/
Milvus 会以容器方式启动,所以先安装 Docker。(本示例使用的是 Alma Linux 9.2)
卸载旧版本,
sudo yum remove docker \docker-client \docker-client-latest \docker-common \docker-latest \docker-latest-logrotate \docker-logrotate \docker-engine
设置存储库,
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
安装 Docker 引擎,
sudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
启动 Docker,
sudo systemctl start docker
通过运行 hello-world 映像来验证 Docker 引擎安装是否成功,
sudo docker run hello-world
2. 部署 Milvus
refer: https://milvus.io/docs/install_standalone-docker.md
使用 Docker Compose 安装 Milvus 单机版。
下载 YAML 文件,
wget https://github.com/milvus-io/milvus/releases/download/v2.2.12/milvus-standalone-docker-compose.yml -O docker-compose.yml
启动 Milvus,
sudo docker compose up -d
现在检查容器是否已启动并正在运行,
sudo docker compose ps
Milvus Standalone 启动后,将会运行三个 docker 容器,包括 Milvus Standalone 服务及其两个依赖项。
Name Command State Ports
--------------------------------------------------------------------------------------------------------------------
milvus-etcd etcd -advertise-client-url ... Up 2379/tcp, 2380/tcp
milvus-minio /usr/bin/docker-entrypoint ... Up (healthy) 9000/tcp
milvus-standalone /tini -- milvus run standalone Up 0.0.0.0:19530->19530/tcp, 0.0.0.0:9091->9091/tcp
连接到 Milvus,
docker port milvus-standalone 19530/tcp
您可以使用该命令返回的本地 IP 地址和端口号连接到 Milvus 集群。
(可选)停止 Milvus,
sudo docker compose down
(可选)要在停止 Milvus 后删除数据,请运行,
sudo rm -rf volumes
3.
修改 docker-compose.yml 文件,并将以下内容添加到服务块中,
attu:container_name: attuimage: zilliz/attu:v2.2.6environment:MILVUS_URL: milvus-standalone:19530ports:- "8000:3000"depends_on:- "standalone"
运行以下命令启动 Milvus 和 Attu,
sudo docker compose up -d
在浏览器中访问 http://{ your machine IP }:8000 ,然后单击“连接”进入Attu服务。我们还支持 TLS 连接、用户名和密码。
4. Langchain 集成 Milvus
Milvus 是一个数据库,用于存储、索引和管理由深度神经网络和其他机器学习 (ML) 模型生成的大量嵌入向量。
本笔记本展示了如何使用 Milvus 矢量数据库相关功能。
要运行,您应该启动并运行一个 Milvus 实例。
示例代码,
# !pip install pymilvus
import os
import getpass# os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:")
os.environ["COHERE_API_KEY"] = getpass.getpass("Cohere API Key:")
# from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.embeddings.cohere import CohereEmbeddings
from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores import Milvus
from langchain.document_loaders import TextLoader
from langchain.document_loaders import TextLoaderloader = TextLoader("./state_of_the_union_en.txt", encoding="utf-8")
documents = loader.load()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
docs = text_splitter.split_documents(documents)# embeddings = OpenAIEmbeddings()
embeddings = CohereEmbeddings()
vector_db = Milvus.from_documents(docs,embeddings,# connection_args={"host": "127.0.0.1", "port": "19530"},connection_args={"host": "192.168.31.92", "port": "19530"},
)
query = "What did the president say about Ketanji Brown Jackson"
docs = vector_db.similarity_search(query)
docs[0].page_content
输出结果,
'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.'
完结!