Elasticsearch:使用 Open AI 和 Langchain 的 RAG - Retrieval Augmented Generation (二)

本文由 简悦 SimpRead 转码, 原文地址 blog.csdn.net

这是继上一篇文章 "Elasticsearch:使用 Open AI 和 Langchain 的 RAG - Retrieval Augmented Generation (一)" 的续篇。在这篇文章中,我主要来讲述 \[\](integrations.langchain.com/vectorstore... 的使用。

[

我们的设置和之前的那篇文章是一样的,只不过,在这里我们使用 ElasticVectorSearch 而不是 ElasticKnnSearch。

](integrations.langchain.com/vectorstore... 的使用。

\[\](integrations.langchain.com/vectorstore... 的使用。

安装包

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])
ini 复制代码
1.  query = "work from home policy"
2.  result = es.similarity_search(query=query)

4.  showResults(result)

我们上面实现的代码可以在地址 github.com/liu-xiao-gu... 进行下载。

相关推荐
Elastic 中国社区官方博客10 小时前
Elasticsearch Python DSL 客户端开发
大数据·数据库·python·elasticsearch·搜索引擎·全文检索
阿里云大数据AI技术11 小时前
Al Search x ES Agent Builder:让数据活起来,从搜索走向行动
人工智能·elasticsearch·agent
Elasticsearch13 小时前
Elastic Agent Builder 中能够引用证据的 AI 根因分析
elasticsearch
考虑考虑15 小时前
elasticSearch中的element_type
运维·后端·elasticsearch
CHANCE V19 小时前
Elasticsearch 进阶
大数据·elasticsearch·搜索引擎
Elasticsearch21 小时前
Elasticsearch 向量数据库:几分钟内完成部署,以经济高效的方式扩展至数千亿规模
elasticsearch
vx-Biye_Design1 天前
SSM伴侣动物伴护星小程序06330-计算机课程设计、毕业设计
spring boot·后端·elasticsearch·小程序·架构·课程设计·idea
云泽8081 天前
Git 版本控制系统(下):从 .git 目录结构到冲突解决机制详解
大数据·git·elasticsearch
Elastic 中国社区官方博客1 天前
教程:使用 ES|QL 进行威胁狩猎
大数据·运维·数据库·elasticsearch·搜索引擎·全文检索·安全威胁分析
Elasticsearch1 天前
jina-reranker-v3.5:通过混合注意力与自蒸馏实现更快的列表式重排序
elasticsearch