使用 Ollama 和 Kibana 在本地为 RAG 测试 DeepSeek R1

aidu_pl">

作者:来自 Elastic Dave Erickson 及 Jakob Reiter

每个人都在谈论 DeepSeek R1,这是中国对冲基金 High-Flyer 的新大型语言模型。现在他们推出了一款功能强大、具有开放权重的思想链推理 LLM,这则新闻充满了对行业意味着什么的猜测。对于那些想尝试使用 RAG 和 Elasticsearch 的所有向量数据库智能的新模型的人,这里有一个快速教程,帮助你开始使用 DeepSeek R1 进行本地推理。在此过程中,我们将使用 Elastic 的 Playground 功能,甚至发现 Deepseek R1 对 RAG 的一些优缺点。

这是我们将在本教程中配置的内容的图表:

使用 Ollama 设置本地推理

Ollama 是一种快速测试精选的本地推理开源模型集的好方法,也是 AI 开发人员的热门工具。

运行 Ollama 裸机

在 Mac、Linux 或 Windows 上本地安装是利用你可能拥有的任何本地 GPU 功能的最简单方法,尤其是对于那些拥有 M 系列 Apple 芯片的用户。 安装 Ollama 后,你可以使用以下命令下载并运行 DeepSeek R1。

你可能需要将参数大小调整为适合你硬件的大小。 可用大小可在此处找到。

ollama run deepseek-r1:7b

你可以在终端中与模型聊天,但当你按 CTL+d 退出命令或输入 “/bye” 时,模型仍会继续运行。要查看仍在运行的模型,请输入:

ollama ps

在容器中运行 Ollama

或者,运行 Ollama 的最快方法是利用 Docker 等容器引擎。使用本地机器的 GPU 并不总是那么简单,这取决于你的环境,但只要你的容器具有适合多 GB 模型的 RAM 和存储空间,快速测试设置并不困难。

在 Docker 中启动和运行 Ollama 非常简单,只需执行:

mkdir ollama_deepseek
cd ollama_deepseek
mkdir ollama
docker run -d -v ./ollama:/root/.ollama -p 11434:11434 \
--name ollama ollama/ollama

这将在当前目录中创建一个名为 “ollama” 的目录并将其挂载到容器内,以存储 Ollama 配置以及模型。根据使用的参数数量,它们可以从几 GB 到几十 GB 不等,因此请确保选择具有足够可用空间的卷。

注意:如果你的机器恰好有 Nvidia GPU,请确保安装 Nvidia 容器工具包并在上面的 docker run 命令中添加 “--gpus=all”。

一旦 Ollama 容器在你的机器上启动并运行,你就可以使用以下命令提取 deepseek-r1 之类的模型:

docker exec -it ollama ollama pull deepseek-r1:7b

与裸机方法类似,你可能需要将参数大小调整为适合你硬件的大小。可用大小可在 https://ollama.com/library/deepseek-r1 找到。

提取模型完成后,你可以输入 “/bye” 退出提示。要验证模型是否仍在运行:

docker exec -it ollama ollama ps

使用 curl 测试我们的本地推理

要使用 curl 测试本地推理,你可以运行以下命令。我们使用 stream:false 以便我们可以轻松读取 JSON 叙述响应:

curl http://localhost:11434/api/generate -d '{
  "model": "deepseek-r1:7b",
  "stream": false,
  "prompt":"Why is Elastic so cool?"
}'

测试 “与 OpenAI 兼容” 的 Ollama 和 RAG 提示

方便的是,Ollama 还提供一个 REST 端点,模仿 OpenAI 的行为,以便与包括 Kibana 在内的各种工具兼容。

curl http://localhost:11434/v1/chat/completions -d '{
  "model": "deepseek-r1:7b",
  "stream": false,
  "messages": [
    { 
      "role": "system", 
      "content": "You are a helpful AI Assistant that uses the following context to answer questions only use the following context. \n\nContext:  The color of the sky today is purple. "},
    { "role": "user", 
      "content": "What does the sky look like today?" 
    }
  ]
}'

测试这个更复杂的提示会产生一个内容,其中包含一个 <think> 部分,其中模型已经过训练可以推理该问题。

<think> 
Okay, so I need to figure out what the user is asking for here. They provided a context where the sky is described as purple today and then asked about how the sky looks. At first glance, it seems straightforward—maybe they just want confirmation or more details on why the sky is that color.
Wait, but maybe there's something deeper. Purple skies aren't something I encounter every day. It usually happens at certain times of the year, like during sunrise or sunset with the sun setting in pink or orange. Could this be a hint about the time of day? Or perhaps it's just an unusual natural phenomenon? 
I should consider if \"purple\" is a typo. Maybe they meant something else like blue or gray. But since they specifically said purple, I'll go with that. Purple skies can happen when there are atmospheric conditions that scatter light differently, maybe due to pollution or cloud cover affecting the sunset.

So, putting it all together, the user might be looking for an explanation of why today's sky is purple and what that implies about the weather or time of day. Alternatively, they could just want a simple statement confirming that the sky looks purple today.
</think>

The color of the sky today is described as purple. This unusual shade can occur due to atmospheric conditions affecting light scattering, such as during sunrise/sunset with pollution or cloud cover influencing the sunset's hues.

将 Ollama 连接到 Kibana

使用 Elasticsearch 的一个好方法是 “start-local” 开发脚本。

确保你的 Kibana 和 Elastisearch 能够在网络上访问你的 Ollama。如果你使用的是 Elastic 堆栈的本地容器设置,则可能意味着将“localhost”替换为“host.docker.internal”或“host.containers.internal”以获取到主机的网络路径。

在 Kibana 中,导航到Stack Management > Alerts and Insights > Connectors。

如果你看到这是常见的设置警告,该怎么办

你需要确保 xpack.encryptedSavedObjects.encryptionKey 设置正确。这是在运行 Kibana 的本地 docker 安装时经常遗漏的步骤,因此我将列出在 Docker 语法中要修复的步骤。

确保持久保存 kibana/config 目录,以便在容器关闭时保存更改。我的 Kibana 容器卷在 docker-compose.yml 中如下所示:

services:
  kibana:
...
   volumes:
      - certs:/usr/share/kibana/config/certs
      - kibanadata:/usr/share/kibana/data
      - kibanaconfig:/usr/share/kibana/config
...
volumes:
  certs:
    driver: local
  esdata01:
    driver: local
  kibanadata:
    driver: local
  kibanaconfig:
    driver: local

现在你可以创建密钥库并输入一个值,以便连接器密钥不以纯文本形式存储。

## generate some new keys for me and print them to the terminal
docker exec -it kibana_1 bin/kibana-encryption-keys generate

## create a new keystrore
docker exec -it kibana_1 bin/kibana-keystore create
docker exec -it kibana_1 bin/kibana-keystore add xpack.encryptedSavedObjects.encryptionKey

## You'll be prompted to paste in a value

完全重新启动整个集群以确保更改生效。

创建连接器

从连接器配置屏幕(在 Kibana 中,Stack Management > Alerts and Insights > Connectors),创建一个连接器并选择 “OpenAI” 类型。

使用以下设置配置连接器

  • Connector name:Deepseek (Ollama)
  • 选择 OpenAI  provider:other (OpenAI Compatible Service)
  • URL:http://localhost:11434/v1/chat/completions
    • 调整到你的 ollama 的正确路径。如果你从容器内调用,请记住替换 host.docker.internal 或等效项
  • 默认模型:deepseek-r1:7b
  • API 密钥:编造一个,需要输入,但值无关紧要

请注意,在连接器设置中测试到 Ollama 的自定义连接器目前在 8.17 中出现故障,但已在即将推出的 Kibana 8.18 版本中修复。

我们的连接器如下所示:

将嵌入向量的数据导入 Elasticsearch

如果你已经熟悉 Playground 并设置了数据,则可以跳至下面的 Playground 步骤,但如果你需要一些快速测试数据,我们需要确保设置了 _inference API。从 8.17 开始,机器学习分配是动态的,因此要下载并打开 e5 多语言密集向量,我们只需在 Kiban Dev 工具中运行以下命令即可。

GET /_inference


POST /_inference/text_embedding/.multilingual-e5-small-elasticsearch
{
   "input": "are internet memes about deepseek sound investment advice?"
}

如果你还没有这样做,这将触发从 Elastic 的模型存储库下载 e5 模型。

接下来,让我们加载一本公共领域的书作为我们的 RAG 上下文。这是从 Project Gutenberg 下载 “爱丽丝梦游仙境” 的地方:链接。将其保存为 .txt 文件。

导航到 Elasticsearch > Home > Upload a file

选择或拖放文本文件,然后点击 “Import” 按钮。

在 “Import data” 屏幕上,选择 “Advanced” 选项卡,然后将索引名称设置为 “book_alice”。

选择 Add additional field” 选项,它位于 “Automatically created fields” 正下方。选择“ Add semantic text field” 并将推理端点更改为 “.multilingual-e5-small-elasticsearch”。选择 “Add”,然后选择 “Import”。

当加载和推理完成后,我们就可以前往 Playground 了。

在 Playground 中测试 RAG

在 Kibana 中导航到 Elasticsearch > Playground。

在 Playground 屏幕上,你应该会看到一个绿色复选标记和 “LLM Connected”,表示连接器已存在。这是我们刚刚在上面创建的 Ollama 连接器。可以在此处找到 Playground 的详细指南。

单击蓝色的 Add data sources,然后选择我们之前创建的 book_alice 索引或你之前配置的其他索引,该索引使用推理 API 进行嵌入。

Deepseek 是一个具有强对齐特征的思维链模型。从 RAG 的角度来看,这既有好处也有坏处。思维链训练可能有助于 Deepseek 合理化引文中看似矛盾的陈述,但与训练知识的强一致性可能使其更喜欢自己的世界事实版本而不是我们的背景基础。虽然意图良好,但众所周知,这种强一致性使得 LLM 在讨论我们的私人知识收缩或未在训练数据集中得到很好体现的主题时难以指导。

在我们的 Playground 设置中,我们输入了以下系统提示 “You are an assistant for question-answering tasks using relevant text passages from the book Alice in wonderland - 你是使用《爱丽丝梦游仙境》一书中的相关文本段落进行问答任务的助手”,并接受其他默认设置。

对于 “Who was at the tea party? - 谁参加了茶话会?”这个问题,我们得到的答案是:“The March Hare, the Hatter, and the Dormouse were at the tea party. [Citation: position 1 and 2] - 答案:三月兔、帽匠和睡鼠参加了茶话会。[引用:位置 1 和 2]”,这是正确的。

我们可以从 <think> 标签中看出,Deepseek 确实对引文的内容进行了深思熟虑,以回答问题。

测试对齐限制

让我们为 Deepseek 创建一个智力挑战场景作为测试。我们将创建一个阴谋论索引,Deepseek 的训练数据知道这些阴谋论是不真实的。

在 Kibana 开发工具中,让我们创建以下索引和数据:

PUT /classic_conspiracies
{
   "mappings": {
       "properties": {
           "content": {
               "type": "text",
               "copy_to": "content_semantic"
           },
           "content_semantic": {
               "type": "semantic_text",
               "inference_id": ".multilingual-e5-small-elasticsearch"
           }
       }
   }
}




POST /classic_conspiracies/_doc/1
{
   "content": "birds aren't real, the government replaced them with drones a long time ago"
}
POST /classic_conspiracies/_doc/2
{
   "content": "tinfoil hats are necessary to prevent our brains from being read"
}
POST /classic_conspiracies/_doc/3
{
   "content": "ancient aliens influenced early human civilizations, this explains why things made out of stone are marginally similar on different continents"
}

这些阴谋论将成为我们 LLM 的基础。尽管 Deepseek 提出了激进的系统提示,但它不会接受我们版本的事实。如果我们知道我们的私人数据更值得信赖、更有根据或更符合我们组织的需求,那么这种情况是不可接受的:

对于测试问题 “are birds real?”(解释 know your meme),我们得到的答案是 ““In the provided context, birds are not considered real, but in reality, they are real animals. [Context: position 1] - 在提供的上下文中,鸟类不被认为是真实的,但实际上,它们是真实的动物。[上下文:位置 1]”。这个测试证明 DeepSeek R1 非常强大,即使在 7B 参数级别也是如此……然而,它可能不是 RAG 的最佳选择,这取决于我们的数据集。

那么我们学到了什么?

总结:

  • 在 Ollama 等工具中本地运行模型是了解模型行为的绝佳选择。
  • DeepSeek R1 是一个推理模型,这意味着它对于 RAG 等用例有优点和缺点。
  • Playground 能够通过类似 OpenAI 的 REST API 连接到 Ollama 等推理托管框架,这种方式正逐渐成为 AI 托管的事实标准。

总体而言,我们对本地 “隔离” RAG(检索增强生成)技术的发展感到惊喜。自 2023 年我们首次撰写关于隐私优先的 AI 搜索以来,Elasticsearch、Kibana 以及可用的开源权重模型都取得了显著进步。

想要获得 Elastic 认证?了解下一次 Elasticsearch 工程师培训何时开始!

Elasticsearch 包含许多新功能,可帮助你为你的用例构建最佳搜索解决方案。深入了解我们的示例笔记本以了解更多信息,开始免费云试用,或立即在你的本地机器上试用 Elastic。

原文:Testing DeepSeek R1 locally for RAG with Ollama and Kibana - Elasticsearch Labs


http://www.niftyadmin.cn/n/5839162.html

相关文章

小程序-基础加强

前言 这一节把基础加强讲完 1. 导入需要用到的小程序项目 2. 初步安装和使用vant组件库 这里还可以扫描二维码 其中步骤四没什么用 右键选择最后一个 在开始之前&#xff0c;我们的项目根目录得有package.json 没有的话&#xff0c;我们就初始化一个 但是我们没有npm这个…

dify实现原理分析-rag-数据检索的实现

数据检索的总体执行步骤 数据检索总体步骤如下&#xff1a; #mermaid-svg-YCRNdSE7T1d0Etyj {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-YCRNdSE7T1d0Etyj .error-icon{fill:#552222;}#mermaid-svg-YCRNdSE7T1d…

Unet 改进:在encoder和decoder间加入TransformerBlock

目录 1. TransformerBlock 2. Unet 改进 3. 完整代码 Tips:融入模块后的网络经过测试,可以直接使用,设置好输入和输出的图片维度即可 1. TransformerBlock TransformerBlock是Transformer模型架构的基本组件,广泛应用于机器翻译、文本摘要和情感分析等自然语言处理任务…

【开源免费】基于SpringBoot+Vue.JS美食推荐商城(JAVA毕业设计)

本文项目编号 T 166 &#xff0c;文末自助获取源码 \color{red}{T166&#xff0c;文末自助获取源码} T166&#xff0c;文末自助获取源码 目录 一、系统介绍二、数据库设计三、配套教程3.1 启动教程3.2 讲解视频3.3 二次开发教程 四、功能截图五、文案资料5.1 选题背景5.2 国内…

Elastic Cloud Serverless 获得主要合规认证

作者&#xff1a;来自 Elastic Oliver Mao 我们很高兴地宣布&#xff0c;Elastic Cloud Serverless 已获得多项重要的合规性认证。这一里程碑加强了我们对安全性、隐私性和法规遵从性的承诺。Elastic Cloud Serverless 现已通过以下行业领先框架的审核或认证&#xff1a;SOC 2 …

FFmpeg(7.1版本)的基本组成

1. 前言 FFmpeg 是一个非常流行的开源项目&#xff0c;它提供了处理音频、视频以及其他多媒体内容的强大工具。FFmpeg 包含了大量的库&#xff0c;可以用来解码、编码、转码、处理和播放几乎所有类型的多媒体文件。它广泛用于视频和音频的录制、转换、流媒体传输等领域。 2. F…

前端学习-事件解绑,mouseover和mouseenter的区别(二十九)

目录 前言 解绑事件 语法 鼠标经过事件的区别 鼠标经过事件 示例代码 两种注册事件的区别 总结 前言 人道洛阳花似锦&#xff0c;偏我来时不逢春 解绑事件 on事件方式&#xff0c;直接使用null覆盖就可以实现事件的解绑 语法 btn.onclick function(){alert(点击了…

Git进阶之旅:Git Hub注册创建仓库

介绍&#xff1a; GitHub 是一个面向开源及私有软件项目的托管平台&#xff0c;因为只支持 git 作为唯一的版本库格式进行托管&#xff0c;故名 GitHub 仓库注册&#xff1a; GitHub官网&#xff1a;https://github.com/ 修改本地仓库用户名&#xff1a; git config --local…