本文由 简悦 SimpRead 转码, 原文地址 blog.csdn.net
这是继上一篇文章 "Elasticsearch:使用 Open AI 和 Langchain 的 RAG - Retrieval Augmented Generation (一)" 的续篇。在这篇文章中,我主要来讲述 [](integrations.langchain.com/vectorstore... 的使用。
我们的设置和之前的那篇文章是一样的,只不过,在这里我们使用 ElasticVectorSearch 而不是 ElasticKnnSearch。 \]([integrations.langchain.com/vectorstore...](https://link.juejin.cn?target=https%3A%2F%2Fintegrations.langchain.com%2Fvectorstores%3Fintegration_%253EElasticVectorSearch "https://integrations.langchain.com/vectorstores?integration_%3EElasticVectorSearch") 的使用。 \[\]([integrations.langchain.com/vectorstore...](https://link.juejin.cn?target=https%3A%2F%2Fintegrations.langchain.com%2Fvectorstores%3Fintegration_%253EElasticVectorSearch "https://integrations.langchain.com/vectorstores?integration_%3EElasticVectorSearch") 的使用。 ### 安装包 ```bash #!pip3 install langchain ``` ### 导入包 ```javascript 1. from dotenv import load_dotenv 2. from langchain.embeddings import OpenAIEmbeddings 3. from langchain.vectorstores import ElasticKnnSearch 4. from langchain.text_splitter import CharacterTextSplitter 5. from urllib.request import urlopen 6. import os, json 8. load_dotenv() 10. openai_api_key=os.getenv('OPENAI_API_KEY') 11. elastic_user=os.getenv('ES_USER') 12. elastic_password=os.getenv('ES_PASSWORD') 13. elastic_endpoint=os.getenv("ES_ENDPOINT") 14. elastic_index_name='elastic-vector-search' ``` ### 将文档分成段落 ```python 1. import json 4. # Load data into a JSON object 5. with open('workplace-docs.json') as f: 6. workplace_docs = json.load(f) 8. print(f"Successfully loaded {len(workplace_docs)} documents") 10. metadata = [] 11. content = [] 13. for doc in workplace_docs: 14. content.append(doc["content"]) 15. metadata.append({ 16. "name": doc["name"], 17. "summary": doc["summary"], 18. "rolePermissions":doc["rolePermissions"] 19. }) 21. text_splitter = CharacterTextSplitter(chunk_size=100, chunk_overlap=0) 22. docs = text_splitter.create_documents(content, metadatas=metadata) ```  ### 把数据写入到 Elasticsearch ```ini 1. embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key) 3. url = f"https://{elastic_user}:{elastic_password}@{elastic_endpoint}:9200" 5. ssl_verify = { 6. "verify_certs": True, 7. "basic_auth": (elastic_user, elastic_password), 8. "ca_certs": "./http_ca.crt" 9. } 11. es = ElasticVectorSearch.from_documents( 12. docs, 13. embedding = embeddings, 14. elasticsearch_url = url, 15. index_name = elastic_index_name, 16. ssl_verify = ssl_verify) ```  如上所示,ElasticVectorSearch 在未来的发布中将被移除。 运行完上面的代码后,我们可以到 Kibana 中进行查看:  ### 展示结果 ```lua 1. def showResults(output): 2. print("Total results: ", len(output)) 3. for index in range(len(output)): 4. print(output[index]) ``` ### Similarity / Vector Search (KNN Search) ```ini 1. query = "work from home policy" 2. result = es.similarity_search(query=query) 4. showResults(result) ```  我们上面实现的代码可以在地址 [github.com/liu-xiao-gu...](https://link.juejin.cn?target=https%3A%2F%2Fgithub.com%2Fliu-xiao-guo%2Fsemantic_search_es%2Fblob%2Fmain%2FElasticVectorSearch.ipynb "https://github.com/liu-xiao-guo/semantic_search_es/blob/main/ElasticVectorSearch.ipynb") 进行下载。