Mixtral 8x22B 是目前性能最强的开源模型之一,其最强大的功能之一是其在多种语言上的流利度,包括英语、西班牙语、法语、意大利语和德语。
想象一下,一个跨国公司拥有用不同语言编写的支持票据和解决方案,希望在各部门之间利用这些知识。目前,知识仅限于代理所掌握的语言。让我们来解决这个问题吧!
在本文中,我将向您展示如何测试 Mixtral 的语言能力,创建一个多语言 RAG 系统。
您可以按照本笔记本再现本文的示例。
在这个例子中,我们的支持票据将包含英语、西班牙语和德语。虽然 Mistral 嵌入模型不是多语言的,但我们可以使用 e5 模型生成多语言嵌入,这样我们可以将不同语言的文本索引并作为单一来源进行管理,从而获得更丰富的上下文。
您可以使用 Kibana 创建 e5 多语言嵌入:
或者使用 _inference
API:
PUT _inference/text_embedding/multilingual-embeddings
{
"service": "elasticsearch",
"service_settings": {
"model_id": ".multilingual-e5-small",
"num_allocations": 1,
"num_threads": 1
}
}
对于映射,我们将使用 semantic_text 映射类型,这是我最喜欢的功能之一。它会为您处理数据分块、生成嵌入以及查询嵌入的过程!
PUT multilingual-mistral
{
"mappings": {
"properties": {
"super_body": {
"type": "semantic_text",
"inference_id": "multilingual-embeddings"
}
}
}
}
我们将文本字段命名为 super_body
,因为通过单一映射类型,它将处理分块和嵌入。
我们将索引包含问题和解决方案的两个语言的支持票据,然后在第三种语言中提出一个关于多个文档中问题的问题。
以下文档将被添加到索引中:
这是一个文档在 Elasticsearch 中的样子:
{
"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,最终得到一个西班牙语的答案。
要检索相关文档,我们可以使用这个简洁的查询,它将在嵌入上运行搜索,并返回与问题最相关的支持票据。
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 来回答问题。
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 删除。