在本文中,我们将介绍如何将阿里云 AI 功能与 Elasticsearch 集成,以提高语义搜索的相关性。
阿里云人工智能搜索是一种将高级人工智能功能与 Elasticsearch 工具相结合的解决方案,利用 Qwen LLM/DeepSeek-R1 系列提供高级推理和分类模型(链接:https://x.sm.cn/1GQOMFb)。在本文中,我们将使用同一作者撰写的小说和戏剧的描述来测试阿里巴巴重新排名和稀疏嵌入端点。
-
创建 Elasticsearch 映射
-
将数据索引到 Elasticsearch 中
-
查询数据
-
奖励:完成回答问题
01
阿里云 AI 重新排名和嵌入
阿里云 AI 重新排名和嵌入
获取阿里云 API Key
获取阿里云 API Key
-
从 服务广场 部分访问阿里云门户。 -
转到左侧菜单 API Keys ,如下所示。 -
生成一个新的 API 密钥。
配置阿里巴巴端点
配置阿里巴巴端点
PUT _inference/sparse_embedding/alibabacloud_ai_search_sparse
{
"service": "alibabacloud-ai-search",
"service_settings": {
"api_key": "
", "service_id": "ops-text-sparse-embedding-001",
"host": "default-j01.platform-cn-shanghai.opensearch.aliyuncs.com",
"workspace": "default"
}
}
PUT _inference/rerank/alibabacloud_ai_search_rerank
{
"service": "alibabacloud-ai-search",
"service_settings": {
"api_key": "
", "service_id": "ops-bge-reranker-larger",
"host": "default-j01.platform-cn-shanghai.opensearch.aliyuncs.com",
"workspace": "default"
}
}
02
-
semantic_description:存储模型生成的嵌入并运行语义搜索。 -
description:我们将使用 “text” 类型来存储小说(novels)和戏剧(plays)的描述,并使用它们进行全文搜索。
PUT arts
{
"mappings": {
"properties": {
"semantic_description": {
"type": "semantic_text",
"inference_id": "alibabacloud_ai_search_sparse"
},
"description": {
"type": "text",
"copy_to": "semantic_description"
}
}
}
}
03
POST arts/_bulk
{ "index": {} }
{ "description": " Black Coffee is a play by the British crime-fiction author Agatha Christie. In the play, a scientist discovers that someone in his household has stolen the formula for an explosive." }
{ "index": {} }
{ "description": "The Mousetrap is a murder mystery play by Agatha Christie. The play opened in London's West End in 1952 and ran continuously until 16 March 2020." }
{ "index": {} }
{ "description": "The Body in the Murder is a Miss Marple mystery novel published by Agatha Christie in 1942. The case involves the murder of two teenage girls who are similar in appearance." }
{ "index": {} }
{ "description": " Agatha Christie's last published novel before she passed, Curtain: Poirot's Last Case is also her indelible detective's last appearance. Poirot and Hastings return to the very same house from The Mysterious Affairs at Styles over 30 years later." }
{ "index": {} }
{ "description": " Death on the Nile is Agatha Christie's most daring travel mystery novel. The tranquillity of a cruise along the Nile is shattered by the discovery that Linnet Ridgeway has been shot through the head." }
{ "index": {} }
{ "description": " The Murder of Roger Ackroyd was Agatha Christie’s first book to be published by William Collins in the spring of 1926. William Collins became part of HarperCollins and are still Christie’s publishers today." }
04
查询数据
语义搜索
语义搜索
GET /arts/_search
{
"_source": {
"includes": [
"description"
]
},
"query": {
"semantic": {
"field": "semantic_description",
"query": "Which novel was written by Agatha Christie?"
}
}
}
{
"took": 1246,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 6,
"relation": "eq"
},
"max_score": 0.1759066,
"hits": [
{
"_index": "arts",
"_id": "rdJ4-ZMB36zj9EVTnMgJ",
"_score": 0.1759066,
"_source": {
"description": " Death on the Nile is Agatha Christie's most daring travel mystery novel. The tranquillity of a cruise along the Nile is shattered by the discovery that Linnet Ridgeway has been shot through the head."
}
},
{
"_index": "arts",
"_id": "rNJ4-ZMB36zj9EVTnMgJ",
"_score": 0.17499167,
"_source": {
"description": " Agatha Christie's last published novel before she passed, Curtain: Poirot's Last Case is also her indelible detective's last appearance. Poirot and Hastings return to the very same house from The Mysterious Affairs at Styles over 30 years later."
}
},
{
"_index": "arts",
"_id": "q9J4-ZMB36zj9EVTnMgJ",
"_score": 0.16319725,
"_source": {
"description": "The Body in the Murder is a Miss Marple mystery novel published by Agatha Christie in 1942. The case involves the murder of two teenage girls who are similar in appearance."
}
},
{
"_index": "arts",
"_id": "qtJ4-ZMB36zj9EVTnMgJ",
"_score": 0.15506727,
"_source": {
"description": "The Mousetrap is a murder mystery play by Agatha Christie. The play opened in London's West End in 1952 and ran continuously until 16 March 2020."
}
},
{
"_index": "arts",
"_id": "qdJ4-ZMB36zj9EVTnMgJ",
"_score": 0.14572844,
"_source": {
"description": " Black Coffee is a play by the British crime-fiction author Agatha Christie. In the play, a scientist discovers that someone in his household has stolen the formula for an explosive."
}
},
{
"_index": "arts",
"_id": "rtJ4-ZMB36zj9EVTnMgJ",
"_score": 0.13951442,
"_source": {
"description": " The Murder of Roger Ackroyd was Agatha Christie’s first book to be published by William Collins in the spring of 1926. William Collins became part of HarperCollins and are still Christie’s publishers today."
}
}
]
}
}
通过重新排序优化结果
通过重新排序优化结果
POST _inference/rerank/alibabacloud_ai_search_rerank
{
"query": "Which novel was written by Agatha Christie?",
"input": [
"Black Coffee is a play by the British crime-fiction author Agatha Christie. In the play, a scientist discovers that someone in his household has stolen the formula for an explosive.",
"The Mousetrap is a murder mystery play by Agatha Christie. The play opened in London's West End in 1952 and ran continuously until 16 March 2020.",
" The Body in the Murder is a Miss Marple mystery novel published by Agatha Christie in 1942. The case involves the murder of two teenage girls who are similar in appearance.",
" Agatha Christie's last published novel before she passed, Curtain: Poirot's Last Case is also her indelible detective's last appearance. Poirot and Hastings return to the very same house from The Mysterious Affairs at Styles over 30 years later.",
" Death on the Nile is Agatha Christie's most daring travel mystery novel. The tranquillity of a cruise along the Nile is shattered by the discovery that Linnet Ridgeway has been shot through the head.",
" The Murder of Roger Ackroyd was Agatha Christie’s first book to be published by William Collins in the spring of 1926. William Collins became part of HarperCollins and are still Christie’s publishers today."
]
}
{
"rerank": [
{
"index": 3,
"relevance_score": 0.91086304
},
{
"index": 4,
"relevance_score": 0.8409133
},
{
"index": 2,
"relevance_score": 0.76838577
},
{
"index": 5,
"relevance_score": 0.2295352
},
{
"index": 0,
"relevance_score": 0.13846178
},
{
"index": 1,
"relevance_score": 0.06620602
}
]
}
语义搜索和重新排名端点相结合
语义搜索和重新排名端点相结合
POST /arts/_search
{
"_source": {
"includes": ["description"]
},
"retriever": {
"text_similarity_reranker": {
"retriever": {
"standard": {
"query": {
"semantic": {
"field": "semantic_description",
"query": "Which novel was written by Agatha Christie?"
}
}
}
},
"field": "description",
"rank_window_size": 10,
"inference_id": "alibabacloud_ai_search_rerank",
"inference_text": "Which novel was written by Agatha Christie?"
}
}
}
"took": 1568,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 6,
"relation": "eq"
},
"max_score": 0.91086304,
"hits": [
{
"_index": "arts",
"_id": "rNJ4-ZMB36zj9EVTnMgJ",
"_score": 0.91086304,
"_source": {
"description": " Agatha Christie's last published novel before she passed, Curtain: Poirot's Last Case is also her indelible detective's last appearance. Poirot and Hastings return to the very same house from The Mysterious Affairs at Styles over 30 years later."
}
},
{
"_index": "arts",
"_id": "rdJ4-ZMB36zj9EVTnMgJ",
"_score": 0.8409133,
"_source": {
"description": " Death on the Nile is Agatha Christie's most daring travel mystery novel. The tranquillity of a cruise along the Nile is shattered by the discovery that Linnet Ridgeway has been shot through the head."
}
},
{
"_index": "arts",
"_id": "q9J4-ZMB36zj9EVTnMgJ",
"_score": 0.76838577,
"_source": {
"description": "The Body in the Murder is a Miss Marple mystery novel published by Agatha Christie in 1942. The case involves the murder of two teenage girls who are similar in appearance."
}
},
{
"_index": "arts",
"_id": "rtJ4-ZMB36zj9EVTnMgJ",
"_score": 0.2295352,
"_source": {
"description": " The Murder of Roger Ackroyd was Agatha Christie’s first book to be published by William Collins in the spring of 1926. William Collins became part of HarperCollins and are still Christie’s publishers today."
}
},
{
"_index": "arts",
"_id": "qdJ4-ZMB36zj9EVTnMgJ",
"_score": 0.13846178,
"_source": {
"description": " Black Coffee is a play by the British crime-fiction author Agatha Christie. In the play, a scientist discovers that someone in his household has stolen the formula for an explosive."
}
},
{
"_index": "arts",
"_id": "qtJ4-ZMB36zj9EVTnMgJ",
"_score": 0.06620602,
"_source": {
"description": "The Mousetrap is a murder mystery play by Agatha Christie. The play opened in London's West End in 1952 and ran continuously until 16 March 2020."
}
}
]
}
}
奖励:使用 completion 来完成回答问题
奖励:使用 completion 来完成回答问题
1. 让我们创建端点
PUT _inference/completion/alibabacloud_ai_search_completion
{
"service": "alibabacloud-ai-search",
"service_settings": {
"host" : "default-j01.platform-cn-shanghai.opensearch.aliyuncs.com",
"api_key": "
" ,"service_id": "ops-qwen-turbo",
"workspace" : "default"
}
}
我们也可以使用 DeepSeek-R1 来创建:
PUT _inference/completion/alibabacloud_ai_search_completion_deepseek_r1
{
"service": "alibabacloud-ai-search",
"service_settings": {
"host" : "default-j01.platform-cn-shanghai.opensearch.aliyuncs.com",
"api_key": "{{API_KEY}}",
"service_id": "deepseek-r1",
"workspace" : "default"
}
}
2. 使用阿里 Qwen 来进行查询
POST _inference/completion/alibabacloud_ai_search_completion
{
""" :
Answer the following question using the context provided:
QUESTION: Which novel was written by Agatha Christie?
CONTEXT:
DOCUMENT1
Black Coffee is a play by the British crime-fiction author Agatha Christie. In the play, a scientist discovers that someone in his household has stolen the formula for an explosive.
DOCUMENT2
The Mousetrap is a murder mystery play by Agatha Christie. The play opened in London's West End in 1952 and ran continuously until 16 March 2020.
DOCUMENT3
The Body in the Murder is a Miss Marple mystery novel published by Agatha Christie in 1942. The case involves the murder of two teenage girls who are similar in appearance.
DOCUMENT4
Agatha Christie's last published novel before she passed, Curtain: Poirot's Last Case is also her indelible detective's last appearance. Poirot and Hastings return to the very same house from The Mysterious Affairs at Styles over 30 years later.
DOCUMENT5
Death on the Nile is Agatha Christie's most daring travel mystery novel. The tranquillity of a cruise along the Nile is shattered by the discovery that Linnet Ridgeway has been shot through the head."
DOCUMENT6
The Murder of Roger Ackroyd was Agatha Christie’s first book to be published by William Collins in the spring of 1926. William Collins became part of HarperCollins and are still Christie’s publishers today.
ANSWER:
"""
}
响应是:
{
"completion": [
{
"result": "Agatha Christie wrote several novels, including \"The Body in the Murder,\" \"Curtain: Poirot's Last Case,\" \"Death on the Nile,\" and \"The Murder of Roger Ackroyd.\""
}
]
}
3. 使用阿里 DeepSeek-R1 来进行查询
POST _inference/completion/alibabacloud_ai_search_completion_deepseek_r1?timeout=180s
{
" :
你是一个机器人助手.
CONTEXT:
Black Coffee is a play by the British crime-fiction author Agatha Christie. In the play, a scientist discovers that someone in his household has stolen the formula for an explosive;
The Mousetrap is a murder mystery play by Agatha Christie. The play opened in London's West End in 1952 and ran continuously until 16 March 2020;
The Body in the Murder is a Miss Marple mystery novel published by Agatha Christie in 1942. The case involves the murder of two teenage girls who are similar in appearance;
Agatha Christie's last published novel before she passed, Curtain: Poirot's Last Case is also her indelible detective's last appearance. Poirot and Hastings return to the very same house from The Mysterious Affairs at Styles over 30 years later;
Death on the Nile is Agatha Christie's most daring travel mystery novel. The tranquillity of a cruise along the Nile is shattered by the discovery that Linnet Ridgeway has been shot through the head;
The Murder of Roger Ackroyd was Agatha Christie’s first book to be published by William Collins in the spring of 1926. William Collins became part of HarperCollins and are still Christie’s publishers today;
QUESTION:
Which novela were written by Agatha Christie?
"
}
{
"completion": [
{
"result": """
Okay, let's see. The user is asking which novels were written by Agatha Christie based on the given context. First, I need to go through each item in the context and determine if it's a novel. The user mentioned "novela," which I think is Spanish for "novel," so they're asking about novels, not plays or other works.
Looking at the context entries one by one:
1. **Black Coffee** is described as a play by Christie. So that's a play, not a novel. Exclude.
2. **The Mousetrap** is a murder mystery play, opened in London's West End. Definitely a play, not a novel. Exclude.
3. **The Body in the Murder** is listed as a Miss Marple mystery novel published in 1942. Wait, the title here might be a bit off. Agatha Christie wrote a novel called "The Body in the Library," which is a Miss Marple story from 1942. Maybe the user made a typo. Assuming it's "The Body in the Library," then yes, that's a novel. But the title given is "The Body in the Murder," which I don't recall. Need to check if that's a real title or a mistake. However, since the context says it's a Miss Marple novel published in 1942, I'll proceed with that, even if the title is slightly wrong. So include as a novel.
4. **Curtain: Poirot's Last Case** is mentioned as her last published novel before she passed. So that's a novel. Include.
5. **Death on the Nile** is described as a travel mystery novel. That's a novel. Include.
6. **The Murder of Roger Ackroyd** was her first book published by William Collins. That's a novel. Include.
So the novels listed here are: The Body in the Murder (assuming typo), Curtain, Death on the Nile, and The Murder of Roger Ackroyd. However, "The Body in the Murder" might actually be "The Body in the Library," which is the correct title. But since the user provided that exact title, I should list it as given, even if there's an error. Alternatively, note the possible typo.
Also, check if there are other works mentioned. The other entries are plays. So the answer should list the four novels mentioned in the context, being careful with the title accuracy.
The novels written by Agatha Christie mentioned in the context are:
1. **The Body in the Murder** (likely a typo for *The Body in the Library*, a Miss Marple novel published in 1942).
2. **Curtain: Poirot's Last Case** (her final published novel featuring Hercule Poirot).
3. **Death on the Nile** (a travel mystery novel set on a Nile cruise).
4. **The Murder of Roger Ackroyd** (her breakthrough novel published in 1926).
*Note*:
- *Black Coffee* and *The Mousetrap* are plays, not novels.
- If "The Body in the Murder" is intended to refer to *The Body in the Library*, the latter is the correct title of Christie's 1942 Miss Marple novel."""
}
]
}
05
/ END /
更多推荐
↙ 点击 阅读原文 ,查看原文