当前位置:   article > 正文

Langchain 集成 Milvus_langchain milvus

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

设置存储库,

sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
  • 1
  • 2

安装 Docker 引擎,

sudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
  • 1

启动 Docker,

sudo systemctl start docker
  • 1

通过运行 hello-world 映像来验证 Docker 引擎安装是否成功,

sudo docker run hello-world
  • 1

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.3.1/milvus-standalone-docker-compose.yml -O docker-compose.yml
  • 1

启动 Milvus,

sudo docker compose up -d
  • 1

现在检查容器是否已启动并正在运行,

sudo docker compose ps
  • 1

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
  • 1
  • 2
  • 3
  • 4
  • 5

连接到 Milvus,

docker port milvus-standalone 19530/tcp
  • 1

您可以使用该命令返回的本地 IP 地址和端口号连接到 Milvus 集群。

(可选)停止 Milvus,

sudo docker compose down
  • 1

(可选)要在停止 Milvus 后删除数据,请运行,

sudo rm -rf  volumes
  • 1

3. 安装 Attu

修改 docker-compose.yml 文件,并将以下内容添加到服务块中,

  attu:
    container_name: attu
    image: zilliz/attu:v2.3.2
    environment:
      MILVUS_URL: milvus-standalone:19530
    ports:
      - "8008:3000"
    depends_on:
      - "standalone"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

运行以下命令启动 Milvus 和 Attu,

sudo docker compose up -d
  • 1

在浏览器中访问 http://{ your machine IP }:8008 ,然后单击“连接”进入Attu服务。我们还支持 TLS 连接、用户名和密码。

在这里插入图片描述

4. Langchain 集成 Milvus

Milvus 是一个数据库,用于存储、索引和管理由深度神经网络和其他机器学习 (ML) 模型生成的大量嵌入向量。

本笔记本展示了如何使用 Milvus 矢量数据库相关功能。

要运行,您应该启动并运行一个 Milvus 实例。

示例代码,

# !pip install pymilvus
  • 1
import os
import getpass

# os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:")
os.environ["COHERE_API_KEY"] = getpass.getpass("Cohere API Key:")
  • 1
  • 2
  • 3
  • 4
  • 5
# 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
  • 1
  • 2
  • 3
  • 4
  • 5
from langchain.document_loaders import TextLoader

loader = 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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
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"},
)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
query = "What did the president say about Ketanji Brown Jackson"
docs = vector_db.similarity_search(query)
  • 1
  • 2
docs[0].page_content
  • 1

输出结果,

'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.'
  • 1

完结!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/255630
推荐阅读
相关标签
  

闽ICP备14008679号