前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用 Elastic 和 Mistral 构建多语言 RAG 系统

使用 Elastic 和 Mistral 构建多语言 RAG 系统

原创
作者头像
点火三周
发布2024-08-05 10:09:58
1310
发布2024-08-05 10:09:58
举报
文章被收录于专栏:Elastic Stack专栏

Mixtral 8x22B 是目前性能最强的开源模型之一,其最强大的功能之一是其在多种语言上的流利度,包括英语、西班牙语、法语、意大利语和德语。

想象一下,一个跨国公司拥有用不同语言编写的支持票据和解决方案,希望在各部门之间利用这些知识。目前,知识仅限于代理所掌握的语言。让我们来解决这个问题吧!

在本文中,我将向您展示如何测试 Mixtral 的语言能力,创建一个多语言 RAG 系统。

图示
图示

您可以按照本笔记本再现本文的示例。

步骤

  1. 创建嵌入端点
  2. 创建映射
  3. 索引数据
  4. 提出问题

创建嵌入端点

在这个例子中,我们的支持票据将包含英语、西班牙语和德语。虽然 Mistral 嵌入模型不是多语言的,但我们可以使用 e5 模型生成多语言嵌入,这样我们可以将不同语言的文本索引并作为单一来源进行管理,从而获得更丰富的上下文。

您可以使用 Kibana 创建 e5 多语言嵌入:

创建多语言端点
创建多语言端点

或者使用 _inference API:

代码语言:javascript
复制
PUT _inference/text_embedding/multilingual-embeddings 
{
    "service": "elasticsearch",
    "service_settings": {
        "model_id": ".multilingual-e5-small",
        "num_allocations": 1,
        "num_threads": 1
    }
}

创建映射

对于映射,我们将使用 semantic_text 映射类型,这是我最喜欢的功能之一。它会为您处理数据分块、生成嵌入以及查询嵌入的过程!

代码语言:javascript
复制
PUT multilingual-mistral
{
    "mappings": {
        "properties": {
            "super_body": {
                "type": "semantic_text",
                "inference_id": "multilingual-embeddings"
            }
        }
    }
}

我们将文本字段命名为 super_body,因为通过单一映射类型,它将处理分块和嵌入。

索引数据

我们将索引包含问题和解决方案的两个语言的支持票据,然后在第三种语言中提出一个关于多个文档中问题的问题。

以下文档将被添加到索引中:

  1. 英语支持票据:日历同步问题
  2. 德语支持票据:文件上传问题
  3. 市场营销活动创意(噪音)
  4. 月度员工(噪音)

这是一个文档在 Elasticsearch 中的样子:

代码语言:javascript
复制
{
    "took": 9,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 0.9155389,
        "hits": [
            {
                "_index": "multilingual-mistral",
                "_id": "1",
                "_score": 0.9155389,
                "_source": {
                    "super_body": {
                        "text": "\n        _Support Ticket #EN1234_\n        **Subject**: Calendar sync not working with Google Calendar\n\n        **Description**:\n        I'm having trouble syncing my project deadlines with Google Calendar. Whenever I try to sync, I get an error message saying \"Unable to connect to external calendar service.\"\n\n        **Resolution**:\n        The issue was resolved by following these steps:\n        1. Go to Settings > Integrations\n        2. Disconnect the Google Calendar integration\n        3. Clear browser cache and cookies\n        4. Reconnect the Google Calendar integration\n        5. Authorize the app again in Google's security settings\n\n        The sync should now work correctly. If problems persist, ensure that third-party cookies are enabled in your browser settings.\n    ",
                        "inference": {
                            "inference_id": "multilingual-embeddings",
                            "model_settings": {
                                "task_type": "text_embedding",
                                "dimensions": 384,
                                "similarity": "cosine",
                                "element_type": "float"
                            },
                            "chunks": [
                                {
                                    "text": "passage: \n        _Support Ticket #EN1234_\n        **Subject**: Calendar sync not working with Google Calendar\n\n        **Description**:\n        I'm having trouble syncing my project deadlines with Google Calendar. Whenever I try to sync, I get an error message saying \"Unable to connect to external calendar service.\"\n\n        **Resolution**:\n        The issue was resolved by following these steps:\n        1. Go to Settings > Integrations\n        2. Disconnect the Google Calendar integration\n        3. Clear browser cache and cookies\n        4. Reconnect the Google Calendar integration\n        5. Authorize the app again in Google's security settings\n\n        The sync should now work correctly. If problems persist, ensure that third-party cookies are enabled in your browser settings.",
                                    "embeddings": [
                                        0.0059651174,
                                        0.0016363655,
                                        -0.064753555,
                                        0.0093298275,
                                        0.05689768,
                                        -0.049640983,
                                        0.02504726,
                                        0.0048340675,
                                        0.08093895,
                                        ...
                                    ]
                                }
                            ]
                        }
                    }
                }
            }
        ]
    }
}

提出问题

现在,我们将用西班牙语提出一个问题:

Hola, estoy teniendo problemas para ocupar su aplicación, estoy teniendo problemas para sincronizar mi calendario, y encima al intentar subir un archivo me da error.

预期的结果是检索到文档 #1 和 #2,然后将它们作为附加上下文发送给 LLM,最终得到一个西班牙语的答案。

检索文档

要检索相关文档,我们可以使用这个简洁的查询,它将在嵌入上运行搜索,并返回与问题最相关的支持票据。

代码语言:javascript
复制
GET multilingual-mistral/_search
{
    "size": 2,
    "_source": {
        "excludes": ["*embeddings", "*chunks"]
    },
    "query": {
        "semantic": {
            "field": "super_body",
            "query": "Hola, estoy teniendo problemas para ocupar su aplicación, estoy teniendo problemas para sincronizar mi calendario, y encima al intentar subir un archivo me da error."
        }
    }
}

关于参数设置的说明: size: 2 因为我们知道我们需要前两个文档。excludes 为了响应的清晰度。文档很短,所以每个文档将是一个块长。

回答问题

现在我们可以使用 Python 库调用 Mistral 完成 API 来回答问题。

代码语言:javascript
复制
from mistralai.client import MistralClient
from mistralai.models.chat_completion import ChatMessage

api_key = os.environ["MISTRAL_API_KEY"]
model = "open-mixtral-8x22b"

client = MistralClient(api_key=api_key)

system_message = "You are a helpful multilingual agent that helps users with their problems. You have access to a knowledge base of different languages and you must answer in the same language the question was asked."

user_message = """
## Question:

Hola, estoy teniendo problemas para ocupar su aplicación, estoy teniendo problemas para sincronizar mi calendario, y encima al intentar subir un archivo me da error. 

## Related knowledge:

Support Ticket #EN1234 Subject: Calendar sync not working with Google Calendar...
(the rest of the content of the document)

\n

Support-Ticket #DE5678 Betreff: Datei-Upload funktioniert nicht... 
(the rest of the content of the document)

ANSWER:

"""

messages = [
    ChatMessage(role="system", content=system_message),
    ChatMessage(role="user", content=user_message)
]

chat_response = client.chat(
    model=model,
    messages=messages,
)

print(chat_response.choices[0].message.content) 

答案是完美的西班牙语,并且切中要害!

结论

Mixtral 8x22B 是一个强大的模型,使我们能够利用不同语言的数据源,能够回答、理解和翻译多种语言。这种能力与多语言嵌入相结合,使您在数据检索和答案生成阶段都能实现多语言支持,彻底消除语言障碍。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 步骤
  • 创建嵌入端点
  • 创建映射
  • 索引数据
  • 提出问题
    • 检索文档
      • 回答问题
      • 结论
      相关产品与服务
      Elasticsearch Service
      腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档