将 Cohere 与 Elasticsearch 结合使用

本教程中的说明向你展示了如何使用推理 API 使用 Cohere 计算嵌入并将其存储起来,以便在 Elasticsearch 中进行高效的向量或混合搜索。本教程将使用 Python Elasticsearch 客户端执行操作。

你将学习如何:

  • 使用 Cohere 服务为文本嵌入创建推理端点,
  • 为 Elasticsearch 索引创建必要的索引映射,
  • 构建推理管道以将文档与嵌入一起提取到索引中,
  • 对数据执行混合搜索,
  • 使用 Cohere 的重新排名模型对搜索结果进行重新排名,
  • 使用 Cohere 的 Chat API 设计 RAG 系统。

本教程使用 SciFact 数据集。

请参阅 Cohere 的教程,了解使用不同数据集的示例。

要求

  • 一个 Cohere 帐户。你可以在地址申请一个 API key
  • 一个本地安装的集群。安装指令如下
  • Python 3.7 或更高版本

安装

Elasticsearch 及 Kibana

 如果你还没有安装好自己的 Elasticsearch 及 Kibana,请参考如下的链接来进行安装:

  • 如何在 Linux,MacOS 及 Windows 上进行安装 Elasticsearch
  • Kibana:如何在 Linux,MacOS 及 Windows上安装 Elastic 栈中的 Kibana

在安装的时候,我们选择 Elastic Stack 8.x 来进行安装。特别值得指出的是:ES|QL 只在 Elastic Stack 8.11 及以后得版本中才有。你需要下载 Elastic Stack 8.11 及以后得版本来进行安装。

在首次启动 Elasticsearch 的时候,我们可以看到如下的输出:

在上面,我们可以看到 elastic 超级用户的密码。我们记下它,并将在下面的代码中进行使用。

我们还可以在安装 Elasticsearch 目录中找到 Elasticsearch 的访问证书:

$ pwd
/Users/liuxg/elastic/elasticsearch-8.14.1/config/certs
$ ls
http.p12      http_ca.crt   transport.p12

在上面,http_ca.crt 是我们需要用来访问 Elasticsearch 的证书。

 我们首先克隆已经写好的代码:

git clone https://github.com/liu-xiao-guo/elasticsearch-labs

我们然后进入到该项目的根目录下:

$ pwd
/Users/liuxg/python/elasticsearch-labs/notebooks/cohere
$ ls
cohere-elasticsearch.ipynb inference-cohere.ipynb 

如上所示,cohere-elasticsearch.ipynb 就是我们今天想要工作的 notebook。

我们通过如下的命令来拷贝所需要的证书:

$ pwd
/Users/liuxg/python/elasticsearch-labs/notebooks/cohere
$ ls
cohere-elasticsearch.ipynb inference-cohere.ipynb
$ cp ~/elastic/elasticsearch-8.14.1/config/certs/http_ca.crt .
$ ls http_ca.crt 
http_ca.crt

安装所需要的 python 依赖包

pip3 install elasticsearch python-dotenv cohere

我们通过如下的命令来查看 Elasticsearch 客户端的版本:

$ pip3 list | grep cohere
cohere                      5.5.8
$ pip3 list | grep elasticsearch
elasticsearch               8.14.0

启动白金试用

在下面,我们需要使用 ELSER。这是一个白金试用的功能。我们按照如下的步骤来启动白金试用:

这样我们就完成了白金试用功能。

创建环境变量

为了能够使得下面的应用顺利执行,在项目当前的目录下运行如下的命令:

export ES_ENDPOINT="localhost"
export ES_USER="elastic"
export ES_PASSWORD="uK+7WbkeXMzwk9YvP-H3"
export COHERE_API_KEY="YourCohereAPIkey"

然后,我们在运行上面命令的 terminal 中打入如下的命令:

$ pwd
/Users/liuxg/python/elasticsearch-labs/notebooks/cohere
$ ls
cohere-elasticsearch.ipynb http_ca.crt                inference-cohere.ipynb
$ jupyter notebook cohere-elasticsearch.ipynb

准备数据

我们通过如下的命令来下载数据集:

wget https://huggingface.co/datasets/mteb/scifact/raw/main/corpus.jsonl
$ wget https://huggingface.co/datasets/mteb/scifact/raw/main/corpus.jsonl
--2024-06-24 09:50:46--  https://huggingface.co/datasets/mteb/scifact/raw/main/corpus.jsonl
Resolving huggingface.co (huggingface.co)... 3.163.189.90
Connecting to huggingface.co (huggingface.co)|3.163.189.90|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 8023638 (7.7M) [text/plain]
Saving to: ‘corpus.jsonl’

corpus.jsonl            100%[=============================>]   7.65M  5.48MB/s    in 1.4s    

2024-06-24 09:50:48 (5.48 MB/s) - ‘corpus.jsonl’ saved [8023638/8023638]

$ ls
cohere-elasticsearch.ipynb http_ca.crt
corpus.jsonl               inference-cohere.ipynb

上面的 corpus.jsonl 就是我们想要工作的数据集。它的格式如下:

展示

读入变量并连接到 Elasticsearch

from elasticsearch import Elasticsearch, helpers
import cohere
import json
import requests
from dotenv import load_dotenv
import os
load_dotenv()
 
ES_USER = os.getenv("ES_USER")
ES_PASSWORD = os.getenv("ES_PASSWORD")
ES_ENDPOINT = os.getenv("ES_ENDPOINT")
COHERE_API_KEY = os.getenv("COHERE_API_KEY")
 
url = f"https://{ES_USER}:{ES_PASSWORD}@{ES_ENDPOINT}:9200"
print(url)
 
client = Elasticsearch(url, ca_certs = "./http_ca.crt", verify_certs = True)
print(client.info())

创建推理端点

首先创建推理端点。在此示例中,推理端点使用 Cohere 的 embed-english-v3.0 模型,并将 embedding_type 设置为 byte。

from elasticsearch import BadRequestError

client.inference.delete_model(inference_id="cohere_embeddings")

try: 
    client.inference.put_model(
        task_type="text_embedding",
        inference_id="cohere_embeddings",
        body={
            "service": "cohere",
            "service_settings": {
                "api_key": COHERE_API_KEY,
                "model_id": "embed-english-v3.0",
                "embedding_type": "byte",
            },
        },
    )
except BadRequestError as e:
    print(e)

我们可以在 Kibana 中进行查看:

GET /_inference/_all

或者:

GET /_inference/cohere_embeddings

创建索引映射

为包含嵌入的索引创建索引映射。

client.indices.create(
    index="cohere-embeddings",
    settings={"index": {"default_pipeline": "cohere_embeddings"}},
    mappings={
        "properties": {
            "text_embedding": {
                "type": "dense_vector",
                "dims": 1024,
                "element_type": "byte",
            },
            "text": {"type": "text"},
            "id": {"type": "integer"},
            "title": {"type": "text"},
        }
    },
)

在运行完上面的代码后,我们可以在 Kibana 中进行查看:

GET cohere-embeddings/_mapping

创建摄入管道

现在,你已拥有一个推理端点和一个可用于存储嵌入的索引。下一步是创建一个摄取管道,该管道使用推理端点创建嵌入并将其存储在索引中。

client.ingest.put_pipeline(
    id="cohere_embeddings",
    description="Ingest pipeline for Cohere inference.",
    processors=[
        {
            "inference": {
                "model_id": "cohere_embeddings",
                "input_output": {
                    "input_field": "text",
                    "output_field": "text_embedding",
                },
            }
        }
    ],
)

在运行完上面的命令后,我们可以在 Kibana 中进行查看:

准备数据并写入数据

此示例使用你可以在 HuggingFace 上找到的 SciFact 数据集。

#url = "https://huggingface.co/datasets/mteb/scifact/raw/main/corpus.jsonl"

# Fetch the JSONL data from the URL
#response = requests.get(url)
#response.raise_for_status()  # Ensure we notice bad responses

import json

with open('./corpus.jsonl', 'r') as file:
    content = file.read()
 
# Split the content by new lines and parse each line as JSON
data = [json.loads(line) for line in content.strip().split("\n") if line]

data = data[:10]
print(f"Successfully loaded {len(data)} documents")

# Change `_id` key to `id` as `_id` is a reserved key in Elasticsearch.
for item in data:
    if "_id" in item:
        item["id"] = item.pop("_id")

# Prepare the documents to be indexed
documents = []
for line in data:
    data_dict = line
    documents.append(
        {
            "_index": "cohere-embeddings",
            "_source": data_dict,
        }
    )

print(documents)

# Use the bulk endpoint to index
helpers.bulk(client, documents)

print("Data ingestion completed, text embeddings generated!")

在我们的练习中,由于我们使用的是 trial 的 Cohere API key。它的使用是有一定的限制的。在上面,我们只取了前面的 20 个文档来进行向量化。

我们可以在 Kibana 中进行查看:

从上面,我们可以看出来有 20 个文档被写入到 Elasticsearch 中。

混合搜索

让我们开始查询索引吧!

下面的代码执行混合搜索。kNN 查询使用 text_embedding 字段根据向量相似度计算搜索结果的相关性。词汇搜索查询使用 BM25 检索来计算 title 和 text 字段的关键字相似度。

query = "What is biosimilarity?"

response = client.search(
    index="cohere-embeddings",
    size=100,
    knn={
        "field": "text_embedding",
        "query_vector_builder": {
            "text_embedding": {
                "model_id": "cohere_embeddings",
                "model_text": query,
            }
        },
        "k": 10,
        "num_candidates": 50,
    },
    query={"multi_match": {"query": query, "fields": ["text", "title"]}},
)

raw_documents = response["hits"]["hits"]

# Display the first 10 results
for document in raw_documents[0:10]:
    print(
        f'Title: {document["_source"]["title"]}\nText: {document["_source"]["text"]}\n'
    )

# Format the documents for ranking
documents = []
for hit in response["hits"]["hits"]:
    documents.append(hit["_source"]["text"])

重新排序搜索结果

为了更有效地组合结果,请通过 inference API 使用 Cohere 的 Rerank v3 模型,以提供更精确的结果语义重新排序。

使用你的 Cohere API 密钥和使用的模型名称作为 model_id(本例中为 rerank-english-v3.0)创建推理端点。

client.inference.delete_model(inference_id="cohere_embeddings")

try:
    client.inference.put_model(
        task_type="rerank",
        inference_id="cohere_rerank",
        body={
            "service": "cohere",
            "service_settings": {
                "api_key": COHERE_API_KEY,
                "model_id": "rerank-english-v3.0",
            },
            "task_settings": {
                "top_n": 10,
            },
        },
    )
except BadRequestError as e:
    print(e)

使用新的推理端点对结果重新排序。

response = client.inference.inference(
    inference_id="cohere_rerank",
    body={
        "query": query,
        "input": documents,
        "task_settings": {"return_documents": False},
    },
)

# Reconstruct the input documents based on the index provided in the rereank response
ranked_documents = []
for document in response.body["rerank"]:
    ranked_documents.append(
        {
            "title": raw_documents[int(document["index"])]["_source"]["title"],
            "text": raw_documents[int(document["index"])]["_source"]["text"],
        }
    )

# Print the top 10 results
for document in ranked_documents[0:10]:
    print(f"Title: {document['title']}\nText: {document['text']}\n")

使用 Cohere 和 Elasticsearch 进行检索增强生成 (RAG)

RAG 是一种使用从外部数据源获取的附加信息生成文本的方法。借助排名结果,你可以在使用 Cohere 的 Chat API 创建的内容的基础上构建 RAG 系统。

传入检索到的文档和查询,以使用 Cohere 最新的生成模型 Command R+ 接收有根据的响应。

然后将查询和文档传入 Chat API,并打印出响应。

response = co.chat(message=query, documents=ranked_documents, model="command-r-plus")

#source_documents = []
#for citation in response.citations:
#    for document_id in citation.document_ids:
#        if document_id not in source_documents:
#            source_documents.append(document_id)

print(f"Query: {query}")
print(f"Response: {response.text}")
#print("Sources:")
#for document in response.documents:
#    if document["id"] in source_documents:
#        print(f"{document['title']}: {document['text']}")

由于我们的数据量是很有限的,我们没有得到相应的回答。如果我们把所有的数据都写入,那么你可能会得到一个比较满意的结果,比如:

Query: What is biosimilarity?
Response: Biosimilarity is based on the comparability concept, which has been used successfully for several decades to ensure close similarity of a biological product before and after a manufacturing change. Over the last 10 years, experience with biosimilars has shown that even complex biotechnology-derived proteins can be copied successfully.
Sources:
Interchangeability of Biosimilars: A European Perspective: Many of the best-selling ‘blockbuster’ biological medicinal products are, or will soon be, facing competition from similar biological medicinal products (biosimilars) in the EU. Biosimilarity is based on the comparability concept, which has been used successfully for several decades to ensure close similarity of a biological product before and after a manufacturing change. Over the last 10 years, experience with biosimilars has shown that even complex biotechnology-derived proteins can be copied successfully. Most best-selling biologicals are used for chronic treatment. This has triggered intensive discussion on the interchangeability of a biosimilar with its reference product, with the main concern being immunogenicity. We explore the theoretical basis of the presumed risks of switching between a biosimilar and its reference product and the available data on switches. Our conclusion is that a switch between comparable versions of the same active substance approved in accordance with EU legislation is not expected to trigger or enhance immunogenicity. On the basis of current knowledge, it is unlikely and very difficult to substantiate that two products, comparable on a population level, would have different safety or efficacy in individual patients upon a switch. Our conclusion is that biosimilars licensed in the EU are interchangeable.

最终的代码在地址可以下载:elasticsearch-labs/notebooks/cohere/cohere-elasticsearch.ipynb at main · liu-xiao-guo/elasticsearch-labs · GitHub

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/739168.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

swiper 幻灯片

index.html <!DOCTYPE html> <html lang"en"> <head> <meta charset"utf-8"> <title>swiper全屏响应式幻灯片代码</title> <meta name"viewport" content"widthdevice-width, initial-scale1, min…

reflutter工具实践之--xx一番赏app

此文章已经录制b站视频&#xff1a; flutter逆向案例-某某一番赏_哔哩哔哩_bilibili 一、工具介绍--reFlutter 这个框架帮助 Flutter 应用逆向工程&#xff0c;使用 Flutter 库的补丁版本&#xff0c;该版本已经编译并准备好重新打包应用。此库修改了快照反序列化过程&#…

Nature推荐的三种ChatGPT论文写作指令

1. 润色学术论文 ChatGPT学术润色指令&#xff1a; “I’m writing a paper on [topic]for a leading [discipline] academic journal. WhatItried to say in the following section is [specific point]. Please rephrase itfor clarity, coherence and conciseness, ensuri…

【源码】最新源支付系统源码 V7版全开源 免授权 附搭建教程

最新源支付系统源码_V7版全开源_免授权_附详细搭建教程_站长亲测 YPay是专为个人站长打造的聚合免签系统&#xff0c;拥有卓越的性能和丰富的功能。它采用全新轻量化的界面UI&#xff0c;让您能更方便快捷地解决知识付费和运营赞助的难题。同时&#xff0c;它基于高性能的thin…

【数据结构与算法】拓扑排序,关键活动,关键路径 详解

拓扑排序算法 bool topologicalSort() {stack<int> stk;int id[N];int cnt 0;for (int i 1; i < n; i) {if (!inDeg[i]) {stk.push(i);}id[i] inDeg[i];}while (stk.size()) {int t stk.top();stk.pop();cout << t << " ";cnt;for (auto i…

Java智慧工地源码 5G智慧工地系统源码 使用SAAS部署 三维可视化管理,与一线生产过程相融合,集成数据后台,统一前端入口,呈现多方项目信息;

Java智慧工地源码 5G智慧工地系统源码 使用SAAS部署 三维可视化管理&#xff0c;与一线生产过程相融合&#xff0c;集成数据后台&#xff0c;统一前端入口&#xff0c;呈现多方项目信息; 智慧工地是指运用信息化手段&#xff0c;通过三维设计平台对工程项目进行精确设计和施工…

【计划】软件项目总体计划书(项目必备资料合集原件)

项目开发计划包括项目描述、项目组织、成本预算、人力资源估算、设备资源计划、沟通计划、采购计划、风险计划、项目过程定义及项目的进度安排和里程碑、质量计划、数据管理计划、度量和分析计划、监控计划和培训计划等。 软件全套精华资料包清单部分文件列表&#xff1a; 工作…

智慧环保一体化平台登录

据悉&#xff0c;在当今这个数字化、智能化的时代&#xff0c;环境保护工作也需要与时俱进&#xff0c;不断创新。朗观视觉智慧环保一体化平台应运而生&#xff0c;它利用先进的信息技术手段&#xff0c;为环保工作提供了更加便捷、高效的管理方式&#xff0c;成为推动绿色发展…

移动端 UI 风格,诠释精致

移动端 UI 风格&#xff0c;诠释精致

【C++】————类和对象(上)

作者主页&#xff1a; 作者主页 本篇博客专栏&#xff1a;C 创作时间 &#xff1a;2024年6月21日 一、类与对象的初步认识 1、类其实就是对对象的抽象&#xff0c;而对象就是对类的具体实例 类不占用内存&#xff0c;而对象占用内存。 2、面向对象与面向过程 C语言是面…

大语言模型(LLMs)能够进行推理和规划吗?

大语言模型&#xff08;LLMs&#xff09;&#xff0c;基本上是经过强化训练的 n-gram 模型&#xff0c;它们在网络规模的语言语料库&#xff08;实际上&#xff0c;可以说是我们文明的知识库&#xff09;上进行了训练&#xff0c;展现出了一种超乎预期的语言行为&#xff0c;引…

GMP合规下的纯蒸汽检查要点:三项值检测及冷凝水取样

制药企业进行纯蒸汽质量验证主要目的&#xff1a; 其一&#xff0c;为了确保药品生产过程的合规性&#xff0c;遵循GMP及国内外法规标准&#xff0c;验证纯蒸汽质量是关键环节。 其二&#xff0c;纯蒸汽质量直接影响药品的纯净度和安全性&#xff0c;验证工作能保障药品的质量…

双指针算法——滑动窗口

前言&#xff1a; 滑动窗口本质上也是利用双指针来解决特定情况下的问题。滑动窗口算法思想是通过俩个指针&#xff0c;定义在左边和右边&#xff0c;俩指针同向运动&#xff0c;保持着一个像“窗口”一样的双指针来不停的压缩或者扩展来移动“窗口”&#xff0c;从而找到特定…

基于Java医院药品交易系统详细设计和实现(源码+LW+调试文档+讲解等)

&#x1f497;博主介绍&#xff1a;✌全网粉丝10W,CSDN作者、博客专家、全栈领域优质创作者&#xff0c;博客之星、平台优质作者、专注于Java、小程序技术领域和毕业项目实战✌&#x1f497; &#x1f31f;文末获取源码数据库&#x1f31f; 感兴趣的可以先收藏起来&#xff0c;…

LeetCode11. 盛最多水的容器题解

LeetCode11. 盛最多水的容器题解 题目链接&#xff1a; https://leetcode.cn/problems/container-with-most-water 示例 思路 暴力解法 定住一个柱子不动&#xff0c;然后用其他柱子与其围住面积&#xff0c;取最大值。 代码如下&#xff1a; public int maxArea1(int[]…

vue3 运用高德地图 自定义弹框 为信息窗体 添加 new AMaps.value.InfoWindow 添加事件

效果图 划过散点的时候出现每个三点位置的数据提示 点击具体散点获取展示信息弹框&#xff0c;并为其添加点击事件 注意点&#xff1a; 1 即使是用的vue&#xff0c;也不能使用click为窗体添加点击事件&#xff0c;需要使用onclick&#xff0c; &#xff08;原因&#xff1a…

PPO代码理解

目录 # Finding the ratio (pi_theta / pi_theta__old): ratios torch.exp(logprobs - old_logprobs.detach()) advantages rewards - state_values.detach() surr1 ratios * advantages surr2 torch.clamp(ratios, 1-self.eps_clip, 1self.eps_clip) * advantages l…

农业四情监测设备——提高农业生产的效率和质量

TH-Q1农业四情监测设备是用于实时监测农业领域的四大关键监测内容的设备&#xff0c;这些内容包括土壤墒情、苗情、病虫情和灾情。以下是关于农业四情监测设备的详细介绍&#xff1a; 主要用于实时测量农田土壤的水分状况。包含土壤湿度传感器、土壤温度传感器等&#xff0c;安…

获取打包后jar包内resource文件路径

Exception:java.lang.IllegalArgumentException: URI is not hierarchical 出现这个异常有很多原因&#xff0c;这里只描述一下我所遇到的 这是源代码&#xff0c;这段代码在本地运行是没有问题的&#xff0c;但是打成jar包&#xff0c;拿到linux上运行之后&#xff0c;就会出…

羊大师:拒绝心灵内耗:走向高效与平和

在繁忙的生活中&#xff0c;我们时常感到疲惫不堪&#xff0c;仿佛心灵被无形的枷锁束缚&#xff0c;这就是精神内耗。它让我们在思考、决策和行动中犹豫不决&#xff0c;消耗着我们的精力和时间&#xff0c;让我们无法专注于真正重要的事情。然而&#xff0c;我们有能力打破这…