DeepSeek-R1 think 标签 markdown 渲染

DeepSeek-R1 思考部分返回:<think>xxx</think>

通过 rehype-raw 自定义渲染 think 标签为 blockquote

yarn add rehype-raw

但 think 中返回的数据可能并不是标准的 blockquote 格式,因此需要对其中的内容做转换。

const processedValue = useMemo(() => {
	return value.replace(/<think>([\s\S]*?)<\/think>/g, (_, content) => {
	    const lines = content.trim().split('\n');
	    const quotedLines = lines.map((line: string) => `> ${line}`).join('\n');
	    return quotedLines;
	  });
}, [value]);

完整代码:

import ReactMarkdown from 'react-markdown';
import remarkGfm from 'remark-gfm';
import './index.less';
import { useMemo } from 'react';
import rehypeRaw from 'rehype-raw';

export interface MarkDwonProps {
  value: string;
}

function MarkDwon(props: MarkDwonProps) {
  let { value } = props;

  const components = useMemo<any>(
    () => ({
      think: 'blockquote',
    }),
    [],
  );

  const processedValue = useMemo(() => {
    return value.replace(/<think>([\s\S]*?)<\/think>/g, (_, content) => {
      const lines = content.trim().split('\n');
      const quotedLines = lines.map((line: string) => `> ${line}`).join('\n');
      return quotedLines;
    });
  }, [value]);

  return (
    <div className="markdown-wrapper">
      <ReactMarkdown
        remarkPlugins={[remarkGfm]}
        rehypePlugins={[rehypeRaw]}
        linkTarget={'_blank'}
        components={components}
      >
        {processedValue}
      </ReactMarkdown>
    </div>
  );
}

export default MarkDwon;
### DeepSeek-R1 中配置思考部分返回格式 为了使 `DeepSeek-R1` 的 `<think>` 标签能够被正确处理并渲染,可以采用自定义解析器来实现特定的Markdown渲染效果。具体来说,在项目环境中安装 `rehype-raw` 插件可以帮助完成这一目标。 #### 安装依赖包 首先需要引入必要的工具库以便支持对特殊标签如 `<think>` 进行定制化处理: ```bash yarn add rehype-raw ``` 此操作会将所需的软件包加入到项目的依赖列表中[^3]。 #### 修改服务启动参数 当运行基于 `vllm` 的推理服务器时,可以通过指定额外选项让模型理解如何解释带有逻辑推导性质的内容片段。这涉及到调整用于提供API接口的服务端设置,确保其具备开启推理功能以及指明相应的解析方式的能力: ```bash vllm serve /path/to/model \ --gpu-memory-utilization 0.95 \ --max-model-len 40000 \ --served-model-name "DeepSeek-R1-14B" \ --kv-cache-dtype="fp8_e4m3" \ --calculate-kv-scales \ --port 30001 \ --enable-reasoning \ --reasoning-parser deepseek_r1 ``` 上述命令中的 `--enable-reasoning` 和 `--reasoning-parser deepseek_r1` 参数就是用来激活和支持这种特性的开关[^2]。 #### 实现自定义渲染逻辑 为了让前端展示层面上能更好地呈现由后台传来的包含 `<think>` 标记的数据,可以在构建React组件或其他视图框架的过程中集成 `rehype-raw` 来转换这些特殊的HTML结构为更友好的样式,比如将其表现为区块引用(blockquote)。这样不仅提升了用户体验,也使得机器产生的思维过程更加直观易读。 ```javascript import React from 'react'; import remarkGfm from 'remark-gfm'; import rehypeRaw from 'rehype-raw'; import { unified } from 'unified'; import parse from 'html-react-parser'; const renderThinkTag = (children) => ( <blockquote className="custom-think-tag"> {parse(children)} </blockquote> ); // 使用统一处理器处理输入文本,并应用插件 function processContent(contentString) { return unified() .use(remarkGfm) .use(rehypeRaw, { handlers: { think: renderThinkTag } }) .processSync(contentString).result; } ``` 这段代码展示了怎样利用 `unified` 处理链结合 `rehype-raw` 及其他相关模块来自定义处理流程,从而达到预期的效果——即把所有的 `<think>` 元素都转化为具有独特样式的区块引用形式显示出来。 通过以上步骤,就可以有效地管理和优化 `DeepSeek-R1` 输出内容中涉及思考环节的表现形式了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值