fix: show embedding token usage
This commit is contained in:
@@ -5,14 +5,7 @@ import { CloseOutlined } from '@ant-design/icons';
|
|||||||
import { useIntl } from '@umijs/max';
|
import { useIntl } from '@umijs/max';
|
||||||
import { Button, Drawer } from 'antd';
|
import { Button, Drawer } from 'antd';
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import React, {
|
import React, { useEffect, useMemo, useRef, useState } from 'react';
|
||||||
memo,
|
|
||||||
useCallback,
|
|
||||||
useEffect,
|
|
||||||
useMemo,
|
|
||||||
useRef,
|
|
||||||
useState
|
|
||||||
} from 'react';
|
|
||||||
import styled from 'styled-components';
|
import styled from 'styled-components';
|
||||||
import { queryCatalogItemSpec } from '../apis';
|
import { queryCatalogItemSpec } from '../apis';
|
||||||
import {
|
import {
|
||||||
@@ -126,14 +119,22 @@ const AddModal: React.FC<AddModalProps> = (props) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// use for size change and quantization change
|
// use for size change and quantization change
|
||||||
const pickSomeFieldsValue = () => {
|
const pickSomeFieldsValue = (defaultSpec: CatalogSpec) => {
|
||||||
const formData = form.current?.getFieldsValue();
|
const formData = form.current?.getFieldsValue();
|
||||||
return _.pick(formData, [
|
const currentData = _.pick(formData, [
|
||||||
'worker_selector',
|
'worker_selector',
|
||||||
'gpu_selector',
|
'gpu_selector',
|
||||||
'env',
|
'env',
|
||||||
'backend_version'
|
'backend_version',
|
||||||
|
'backend_parameters'
|
||||||
]);
|
]);
|
||||||
|
return {
|
||||||
|
...currentData,
|
||||||
|
backend_parameters:
|
||||||
|
currentData.backend_parameters?.length > 0
|
||||||
|
? currentData.backend_parameters
|
||||||
|
: defaultSpec.backend_parameters || []
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
const generateSubmitData = (formData: FormData) => {
|
const generateSubmitData = (formData: FormData) => {
|
||||||
@@ -431,7 +432,7 @@ const AddModal: React.FC<AddModalProps> = (props) => {
|
|||||||
});
|
});
|
||||||
form.current.setFieldsValue({
|
form.current.setFieldsValue({
|
||||||
...data,
|
...data,
|
||||||
...pickSomeFieldsValue()
|
...pickSomeFieldsValue(data)
|
||||||
});
|
});
|
||||||
handleCheckFormData();
|
handleCheckFormData();
|
||||||
};
|
};
|
||||||
@@ -453,7 +454,7 @@ const AddModal: React.FC<AddModalProps> = (props) => {
|
|||||||
// set form data
|
// set form data
|
||||||
form.current.setFieldsValue({
|
form.current.setFieldsValue({
|
||||||
...data,
|
...data,
|
||||||
...pickSomeFieldsValue()
|
...pickSomeFieldsValue(data)
|
||||||
});
|
});
|
||||||
handleCheckFormData();
|
handleCheckFormData();
|
||||||
};
|
};
|
||||||
@@ -466,10 +467,10 @@ const AddModal: React.FC<AddModalProps> = (props) => {
|
|||||||
onOk(data);
|
onOk(data);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleCancel = useCallback(() => {
|
const handleCancel = () => {
|
||||||
onCancel?.();
|
onCancel?.();
|
||||||
axiosToken.current?.cancel?.();
|
axiosToken.current?.cancel?.();
|
||||||
}, [onCancel]);
|
};
|
||||||
|
|
||||||
const showExtraButton = useMemo(() => {
|
const showExtraButton = useMemo(() => {
|
||||||
return warningStatus.show && warningStatus.type !== 'success';
|
return warningStatus.show && warningStatus.type !== 'success';
|
||||||
@@ -614,4 +615,4 @@ const AddModal: React.FC<AddModalProps> = (props) => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default memo(AddModal);
|
export default AddModal;
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ import { generateEmbeddingCode } from '../view-code/embedding';
|
|||||||
import DynamicParams from './dynamic-params';
|
import DynamicParams from './dynamic-params';
|
||||||
import FileList from './file-list';
|
import FileList from './file-list';
|
||||||
import InputList from './input-list';
|
import InputList from './input-list';
|
||||||
|
import TokenUsage from './token-usage';
|
||||||
import ViewCommonCode from './view-common-code';
|
import ViewCommonCode from './view-common-code';
|
||||||
|
|
||||||
interface MessageProps {
|
interface MessageProps {
|
||||||
@@ -525,21 +526,29 @@ const GroundEmbedding: React.FC<MessageProps> = forwardRef((props, ref) => {
|
|||||||
onSelect={handleonSelect}
|
onSelect={handleonSelect}
|
||||||
onPaste={handleOnPaste}
|
onPaste={handleOnPaste}
|
||||||
></InputList>
|
></InputList>
|
||||||
<div style={{ marginTop: 8 }}>
|
{fileList.length > 0 && (
|
||||||
<FileList
|
<div style={{ marginTop: 8 }}>
|
||||||
fileList={fileList}
|
<FileList
|
||||||
textListCount={textList.length || 0}
|
fileList={fileList}
|
||||||
onDelete={handleDeleteFile}
|
textListCount={textList.length || 0}
|
||||||
></FileList>
|
onDelete={handleDeleteFile}
|
||||||
</div>
|
></FileList>
|
||||||
{lessTwoInput && (
|
</div>
|
||||||
<AlertInfo
|
|
||||||
type="danger"
|
|
||||||
message={intl.formatMessage({
|
|
||||||
id: 'playground.documents.verify.embedding'
|
|
||||||
})}
|
|
||||||
></AlertInfo>
|
|
||||||
)}
|
)}
|
||||||
|
{lessTwoInput && (
|
||||||
|
<div className="m-t-16">
|
||||||
|
<AlertInfo
|
||||||
|
type="danger"
|
||||||
|
message={intl.formatMessage({
|
||||||
|
id: 'playground.documents.verify.embedding'
|
||||||
|
})}
|
||||||
|
></AlertInfo>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
<TokenUsage
|
||||||
|
tokenResult={tokenResult}
|
||||||
|
className="m-t-16"
|
||||||
|
></TokenUsage>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ import '../style/system-message-wrap.less';
|
|||||||
import { generateRerankCode } from '../view-code/rerank';
|
import { generateRerankCode } from '../view-code/rerank';
|
||||||
import DynamicParams from './dynamic-params';
|
import DynamicParams from './dynamic-params';
|
||||||
import InputList from './input-list';
|
import InputList from './input-list';
|
||||||
|
import TokenUsage from './token-usage';
|
||||||
import ViewCommonCode from './view-common-code';
|
import ViewCommonCode from './view-common-code';
|
||||||
|
|
||||||
interface MessageProps {
|
interface MessageProps {
|
||||||
@@ -179,7 +180,7 @@ const GroundReranker: React.FC<MessageProps> = forwardRef((props, ref) => {
|
|||||||
return res * 100;
|
return res * 100;
|
||||||
};
|
};
|
||||||
|
|
||||||
const renderPercent = useCallback((data: any) => {
|
const renderPercent = (data: any) => {
|
||||||
if (!data.showExtra || !data.percent) {
|
if (!data.showExtra || !data.percent) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -208,7 +209,7 @@ const GroundReranker: React.FC<MessageProps> = forwardRef((props, ref) => {
|
|||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}, []);
|
};
|
||||||
|
|
||||||
const setMessageId = () => {
|
const setMessageId = () => {
|
||||||
messageId.current = messageId.current + 1;
|
messageId.current = messageId.current + 1;
|
||||||
@@ -320,59 +321,52 @@ const GroundReranker: React.FC<MessageProps> = forwardRef((props, ref) => {
|
|||||||
inputListRef.current?.handleAdd();
|
inputListRef.current?.handleAdd();
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleTextListChange = useCallback(
|
const handleTextListChange = (
|
||||||
(list: { text: string; uid: number | string; name: string }[]) => {
|
list: { text: string; uid: number | string; name: string }[]
|
||||||
const newList = list?.map((item: any) => {
|
) => {
|
||||||
item.percent = undefined;
|
const newList = list?.map((item: any) => {
|
||||||
item.score = undefined;
|
item.percent = undefined;
|
||||||
item.rank = undefined;
|
item.score = undefined;
|
||||||
return item;
|
item.rank = undefined;
|
||||||
|
return item;
|
||||||
|
});
|
||||||
|
setTextList(newList);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleonSelect = (data: {
|
||||||
|
start: number;
|
||||||
|
end: number;
|
||||||
|
beforeText: string;
|
||||||
|
afterText: string;
|
||||||
|
index: number;
|
||||||
|
}) => {
|
||||||
|
selectionTextRef.current = data;
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleOnPaste = (e: any, index: number) => {
|
||||||
|
if (!multiplePasteEnable.current) return;
|
||||||
|
const text = e.clipboardData.getData('text');
|
||||||
|
if (text) {
|
||||||
|
const dataLlist = text.split('\n').map((item: string) => {
|
||||||
|
return {
|
||||||
|
text: item?.trim(),
|
||||||
|
name: '',
|
||||||
|
uid: setMessageId(),
|
||||||
|
percent: undefined,
|
||||||
|
score: undefined,
|
||||||
|
rank: undefined
|
||||||
|
};
|
||||||
});
|
});
|
||||||
setTextList(newList);
|
dataLlist[0].text = `${selectionTextRef.current?.beforeText || ''}${dataLlist[0].text}${selectionTextRef.current?.afterText || ''}`;
|
||||||
},
|
const result = [
|
||||||
[]
|
...textList.slice(0, index),
|
||||||
);
|
...dataLlist,
|
||||||
|
...textList.slice(index + 1)
|
||||||
|
].filter((item) => item.text);
|
||||||
|
|
||||||
const handleonSelect = useCallback(
|
setTextList(result);
|
||||||
(data: {
|
}
|
||||||
start: number;
|
};
|
||||||
end: number;
|
|
||||||
beforeText: string;
|
|
||||||
afterText: string;
|
|
||||||
index: number;
|
|
||||||
}) => {
|
|
||||||
selectionTextRef.current = data;
|
|
||||||
},
|
|
||||||
[]
|
|
||||||
);
|
|
||||||
|
|
||||||
const handleOnPaste = useCallback(
|
|
||||||
(e: any, index: number) => {
|
|
||||||
if (!multiplePasteEnable.current) return;
|
|
||||||
const text = e.clipboardData.getData('text');
|
|
||||||
if (text) {
|
|
||||||
const dataLlist = text.split('\n').map((item: string) => {
|
|
||||||
return {
|
|
||||||
text: item?.trim(),
|
|
||||||
name: '',
|
|
||||||
uid: setMessageId(),
|
|
||||||
percent: undefined,
|
|
||||||
score: undefined,
|
|
||||||
rank: undefined
|
|
||||||
};
|
|
||||||
});
|
|
||||||
dataLlist[0].text = `${selectionTextRef.current?.beforeText || ''}${dataLlist[0].text}${selectionTextRef.current?.afterText || ''}`;
|
|
||||||
const result = [
|
|
||||||
...textList.slice(0, index),
|
|
||||||
...dataLlist,
|
|
||||||
...textList.slice(index + 1)
|
|
||||||
].filter((item) => item.text);
|
|
||||||
|
|
||||||
setTextList(result);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
[textList]
|
|
||||||
);
|
|
||||||
|
|
||||||
const renderExtra = useMemo(() => {
|
const renderExtra = useMemo(() => {
|
||||||
if (modelMeta?.n_ctx && modelMeta?.n_slot) {
|
if (modelMeta?.n_ctx && modelMeta?.n_slot) {
|
||||||
@@ -469,15 +463,7 @@ const GroundReranker: React.FC<MessageProps> = forwardRef((props, ref) => {
|
|||||||
{intl.formatMessage({ id: 'playground.embedding.documents' })}
|
{intl.formatMessage({ id: 'playground.embedding.documents' })}
|
||||||
</span>
|
</span>
|
||||||
</h3>
|
</h3>
|
||||||
<span className="m-l-10 font-size-12">
|
|
||||||
{' '}
|
|
||||||
{tokenResult?.total_tokens && (
|
|
||||||
<span style={{ color: 'var(--ant-orange)' }}>
|
|
||||||
{intl.formatMessage({ id: 'playground.tokenusage' })}:{' '}
|
|
||||||
{tokenResult?.total_tokens}
|
|
||||||
</span>
|
|
||||||
)}
|
|
||||||
</span>
|
|
||||||
<div className="flex-center gap-10">
|
<div className="flex-center gap-10">
|
||||||
<Tooltip
|
<Tooltip
|
||||||
title={intl.formatMessage({
|
title={intl.formatMessage({
|
||||||
@@ -532,6 +518,10 @@ const GroundReranker: React.FC<MessageProps> = forwardRef((props, ref) => {
|
|||||||
></AlertInfo>
|
></AlertInfo>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
<TokenUsage
|
||||||
|
tokenResult={tokenResult}
|
||||||
|
className="m-t-16"
|
||||||
|
></TokenUsage>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div></div>
|
<div></div>
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
import { useIntl } from '@umijs/max';
|
||||||
|
import React from 'react';
|
||||||
|
import styled from 'styled-components';
|
||||||
|
|
||||||
|
const TokenUsageWrapper = styled.div`
|
||||||
|
font-size: var(--font-size-small);
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
padding: 2px 5px;
|
||||||
|
.text {
|
||||||
|
color: var(--ant-orange);
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
||||||
|
const TokenUsage: React.FC<{
|
||||||
|
tokenResult?: any;
|
||||||
|
[key: string]: any;
|
||||||
|
}> = ({ tokenResult, ...rest }) => {
|
||||||
|
const intl = useIntl();
|
||||||
|
if (!tokenResult) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
<TokenUsageWrapper {...rest}>
|
||||||
|
{tokenResult?.total_tokens && (
|
||||||
|
<span className="text">
|
||||||
|
{intl.formatMessage({ id: 'playground.tokenusage' })}:{' '}
|
||||||
|
{tokenResult?.total_tokens}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</TokenUsageWrapper>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default TokenUsage;
|
||||||
Reference in New Issue
Block a user