(feature)(chat-sdk) add export log (#1482)

This commit is contained in:
williamhliu
2024-07-30 15:20:02 +08:00
committed by GitHub
parent 9a1fac5d4c
commit e571ca1f55
6 changed files with 157 additions and 45 deletions

View File

@@ -3,10 +3,11 @@ import { format } from 'sql-formatter';
import { CopyToClipboard } from 'react-copy-to-clipboard';
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
import { solarizedlight } from 'react-syntax-highlighter/dist/esm/styles/prism';
import { message } from 'antd';
import { Button, message } from 'antd';
import { PREFIX_CLS } from '../../common/constants';
import { CheckCircleFilled, UpOutlined } from '@ant-design/icons';
import { CheckCircleFilled, DownloadOutlined, UpOutlined } from '@ant-design/icons';
import { SqlInfoType } from '../../common/type';
import { exportTextFile } from '../../utils/utils';
type Props = {
llmReq?: any;
@@ -46,6 +47,99 @@ const SqlItem: React.FC<Props> = ({
const fewShots = (Object.values(llmResp?.sqlRespMap || {})[0] as any)?.fewShots || [];
const getSchemaMapText = () => {
return `
Schema映射
${schema?.fieldNameList?.length > 0 ? `名称:${schema.fieldNameList.join('、')}` : ''}${
linking?.length > 0
? `
取值:${linking
.map((item: any) => {
return `${item.fieldName}: ${item.fieldValue}`;
})
.join('、')}`
: ''
}${
priorExts
? `
附加:${priorExts}`
: ''
}${
schema?.terms?.length > 0
? `
术语:${schema.terms
.map((item: any) => {
return `${item.name}${item.alias?.length > 0 ? `(${item.alias.join(',')})` : ''}: ${
item.description
}`;
})
.join('、')}`
: ''
}
`;
};
const getFewShotText = () => {
return `
Few-shot示例${fewShots
.map((item: any, index: number) => {
return `
示例${index + 1}
问题:${item.question}
SQL
${format(item.sql)}
`;
})
.join('')}
`;
};
const getParsedS2SQLText = () => {
return `
${queryMode === 'LLM_S2SQL' || queryMode === 'PLAIN_TEXT' ? 'LLM' : 'Rule'}解析S2SQL
${format(sqlInfo.parsedS2SQL)}
`;
};
const getCorrectedS2SQLText = () => {
return `
修正S2SQL
${format(sqlInfo.correctedS2SQL)}
`;
};
const getQuerySQLText = () => {
return `
最终执行SQL
${format(sqlInfo.querySQL)}
`;
};
const onExportLog = () => {
let text = '';
if (llmReq) {
text += getSchemaMapText();
}
if (fewShots.length > 0) {
text += getFewShotText();
}
if (sqlInfo.parsedS2SQL) {
text += getParsedS2SQLText();
}
if (sqlInfo.correctedS2SQL) {
text += getCorrectedS2SQLText();
}
if (sqlInfo.querySQL) {
text += getQuerySQLText();
}
exportTextFile(text, 'file.txt');
};
return (
<div className={`${tipPrefixCls}-parse-tip`}>
<div className={`${tipPrefixCls}-title-bar`}>
@@ -123,6 +217,10 @@ const SqlItem: React.FC<Props> = ({
SQL
</div>
)}
<Button className={`${prefixCls}-export-log`} size="small" onClick={onExportLog}>
<DownloadOutlined />
</Button>
</div>
</div>
<div