Skip to content

Commit 0db0ff9

Browse files
authored
Merge pull request #647 from lihqi/lhq-alpha-0605
feat(lb-components): Enhanced Latex rendering
2 parents a9d68e4 + e2b61ee commit 0db0ff9

File tree

5 files changed

+42
-2
lines changed

5 files changed

+42
-2
lines changed

packages/lb-components/src/components/LLMToolView/modelAPIView/index.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { InfoCircleFilled, SyncOutlined } from '@ant-design/icons';
1111
import { LLMViewCls } from '../questionView';
1212
import { ILLMToolConfig, IModelAPIAnswer } from '../types';
1313
import { i18n } from '@labelbee/lb-utils';
14+
import { convertLatexFormat } from '@/utils/LLM';
1415

1516
interface IProps {
1617
dataFormatType: EDataFormatType;
@@ -32,6 +33,10 @@ const RenderContent = ({
3233
return <MarkdownView value={answer} />;
3334
}
3435

36+
if (dataFormatType === EDataFormatType.Latex) {
37+
return <MarkdownView value={convertLatexFormat(answer)} />;
38+
}
39+
3540
return <span>{answer}</span>;
3641
};
3742

packages/lb-components/src/components/LLMToolView/questionView/components/header/index.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ import { useTranslation } from 'react-i18next';
88
import { Resizable } from 're-resizable';
99
import { Radio, Image, Empty } from 'antd';
1010
import { EDataFormatType, prefix } from '@/constant';
11-
import { FileTextOutlined } from '@ant-design/icons';
11+
import { FileTextOutlined, FileMarkdownOutlined } from '@ant-design/icons';
1212
import MarkdownView from '@/components/markdownView';
1313
import { isObject, isString } from 'lodash';
1414
import { i18n } from '@labelbee/lb-utils';
1515
import ImgFailCn from '@/assets/annotation/LLMTool/imgFail_cn.svg';
1616
import ImgFailEn from '@/assets/annotation/LLMTool/imgFail_en.svg';
17+
import { convertLatexFormat } from '@/utils/LLM';
1718

1819
interface IProps {
1920
question:
@@ -74,7 +75,17 @@ export const RenderQuestion = ({
7475

7576
return (
7677
<div style={{ whiteSpace: 'pre-wrap' }}>
77-
{dataFormatType === EDataFormatType.Markdown ? <MarkdownView value={textValue} /> : textValue}
78+
{(() => {
79+
// 根据不同的数据格式类型渲染不同的视图
80+
switch (dataFormatType) {
81+
case EDataFormatType.Markdown:
82+
return <MarkdownView value={textValue} />;
83+
case EDataFormatType.Latex:
84+
return <MarkdownView value={convertLatexFormat(textValue)} />;
85+
default:
86+
return textValue;
87+
}
88+
})()}
7889
</div>
7990
);
8091
};
@@ -141,6 +152,9 @@ export const ToggleDataFormatType = (props: {
141152
>
142153
<FileTextOutlined />
143154
</Radio.Button>
155+
<Radio.Button value={EDataFormatType.Latex} style={{ textAlign: 'center', width: '52px' }}>
156+
<FileMarkdownOutlined />
157+
</Radio.Button>
144158
</Radio.Group>
145159
<span style={{ marginLeft: '8px', width: '4px', background: '#1890ff' }} />
146160
</span>

packages/lb-components/src/components/LLMToolView/questionView/index.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import Header from './components/header';
2323
import ImgView from './components/imgView';
2424
import { isString } from 'lodash';
2525
import AudioView from './components/audioView';
26+
import { convertLatexFormat } from '@/utils/LLM';
2627

2728
interface IProps {
2829
hoverKey?: number;
@@ -59,6 +60,11 @@ export const RenderAnswer = ({
5960
if (dataFormatType === EDataFormatType.Markdown) {
6061
return <MarkdownView value={i?.newAnswer ?? i?.answer} />;
6162
}
63+
64+
if (dataFormatType === EDataFormatType.Latex) {
65+
return <MarkdownView value={convertLatexFormat(i?.newAnswer ?? i?.answer)} />;
66+
}
67+
6268
if (isTextControl) {
6369
return (
6470
<div style={{ width: '100%', overflowWrap: 'break-word' }}>

packages/lb-components/src/constant/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export const componentCls = `${prefix}-component`;
2727
export enum EDataFormatType {
2828
Default = 'default', // 原文
2929
Markdown = 'markdown', // markdown
30+
Latex = 'latex', // latex
3031
}
3132

3233
// LLM工具的数据类型
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* 将 LaTeX 数学公式从 \(...\) 格式转换为 $...$ 格式
3+
* @param latexContent - 包含 LaTeX 数学公式的原始字符串
4+
* @returns 转换后的字符串,其中数学公式使用 $...$ 格式
5+
*/
6+
export const convertLatexFormat = (latexContent: string) => {
7+
try {
8+
// 匹配 \( ... \) 并替换为 $...$
9+
return latexContent.replace(/\\\((.*?)\\\)/g, (_, formula) => `$${formula.trim()}$`);
10+
} catch (error) {
11+
// 发生错误时返回原始字符串
12+
return latexContent;
13+
}
14+
};

0 commit comments

Comments
 (0)