Skip to content

Commit

Permalink
fix: LLM tool type adjustment
Browse files Browse the repository at this point in the history
  • Loading branch information
lixinghua123 committed Jul 3, 2023
1 parent 486a68f commit 3d578aa
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ import { i18n } from '@labelbee/lb-utils';

interface IProps {
hoverKey?: number;
question: any;
question: string;
answerList: IAnswerList[];
lang?: string;
}
const LLMViewCls = `${prefix}-LLMView`;
const QuestionView: React.FC<IProps> = (props) => {
const { hoverKey, question, answerList, lang } = props;

const { t } = useTranslation();

useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@ import { LLMContext } from '@/store/ctx';
import { classnames } from '@/utils';
import { InfoCircleOutlined } from '@ant-design/icons';
import { expandIconFuc } from '@/views/MainView/sidebar/TagSidebar';
import {
IndicatorScore,
IndicatorDetermine,
ILLMToolConfig,
IAnswerList,
} from '@/components/LLMToolView/types';
import { isBoolean } from 'lodash';

interface IProps {
list?: any;
list?: IAnswerList[];
checkMode?: boolean;
LLMConfig?: any;
LLMConfig?: ILLMToolConfig;
setHoverKey: (value: number) => void;
updateValue: ({
order,
Expand All @@ -39,13 +46,14 @@ enum ETagType {
const { Panel } = Collapse;
const LLMSidebarCls = `${prefix}-LLMSidebar`;
const AnswerList = (props: IProps) => {
const { list, LLMConfig, updateValue, checkMode } = props;
const { list = [], LLMConfig = {}, updateValue, checkMode } = props;

const { hoverKey, setHoverKey } = useContext(LLMContext);
const { t } = useTranslation();
const isDisableAll = checkMode;

const getFinishStatus = (i: any) => {
const { indicatorScore, indicatorDetermine, score } = LLMConfig;
const getFinishStatus = (i: IAnswerList) => {
const { indicatorScore = [], indicatorDetermine = [], score } = LLMConfig;

let finishStatus = ETagType.Default;
if (score) {
Expand All @@ -56,17 +64,19 @@ const AnswerList = (props: IProps) => {
finishStatus = ETagType.Finish;
}
if (indicatorScore?.length > 0) {
const scoreUnFinish = indicatorScore.some((item: any) => !i?.indicatorScore?.[item.value]);
const scoreUnFinish = indicatorScore.some(
(item: IndicatorScore) => !i?.indicatorScore?.[item.value],
);
if (scoreUnFinish) {
finishStatus = ETagType.UnFinish;
return finishStatus;
}
finishStatus = ETagType.Finish;
}
if (indicatorDetermine?.length > 0) {
const determineUnFinish = indicatorDetermine.some((item: any) => {
const determineUnFinish = indicatorDetermine.some((item: IndicatorDetermine) => {
const determineResult = i?.indicatorDetermine?.[item.value];
return ![true, false].includes(determineResult);
return !isBoolean(determineResult);
});
if (determineUnFinish) {
finishStatus = ETagType.UnFinish;
Expand All @@ -78,7 +88,7 @@ const AnswerList = (props: IProps) => {
return finishStatus;
};

const getTagStyle = (item: any) => {
const getTagStyle = (item: IAnswerList) => {
const tagStatus = getFinishStatus(item);

let tagText = item.order;
Expand Down Expand Up @@ -113,18 +123,20 @@ const AnswerList = (props: IProps) => {
bordered={false}
expandIcon={expandIconFuc}
expandIconPosition='end'
defaultActiveKey={list.length > 0 && list.map((i: any, index: number) => index)}
defaultActiveKey={
list.length > 0 ? list.map((i: IAnswerList, index: number) => index) : undefined
}
style={{ margin: '16px 0px' }}
>
{list.map((i: any, index: number) => {
const { score, indicatorScore, indicatorDetermine } = LLMConfig;
{list.map((i: IAnswerList, index: number) => {
const { score, indicatorScore = [], indicatorDetermine = [] } = LLMConfig;
const { backgroundColor, fontColor, tagText, tagStatus } = getTagStyle(i);

const noIndicatorScore =
indicatorScore?.filter((i: any) => i.label && i.value && i.score)?.length > 0;
indicatorScore?.filter((i: IndicatorScore) => i.label && i.value && i.score)?.length > 0;

const noIndicatorDetermine =
indicatorDetermine?.filter((i: any) => i.label && i.value)?.length > 0;
indicatorDetermine?.filter((i: IndicatorDetermine) => i.label && i.value)?.length > 0;

const noConfig = !(score || noIndicatorScore || noIndicatorDetermine);
const header = (
Expand Down Expand Up @@ -175,7 +187,7 @@ const AnswerList = (props: IProps) => {
)}
{/* 指标评分 */}
{indicatorScore?.length > 0 &&
indicatorScore.map((item: any, index: number) => {
indicatorScore.map((item: IndicatorScore, index: number) => {
const { label, text, value, score } = item;
const renderTitle = (
<span>
Expand Down Expand Up @@ -206,7 +218,7 @@ const AnswerList = (props: IProps) => {
})}
{/* 指标判断 */}
{indicatorDetermine?.length > 0 &&
indicatorDetermine.map((item: any, index: number) => {
indicatorDetermine.map((item: IndicatorDetermine, index: number) => {
const { label, value } = item;

return label ? (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import { Tag } from 'antd';
import { useTranslation } from 'react-i18next';
import { LeftOutlined } from '@ant-design/icons';
import { classnames } from '@/utils';
import { cloneDeep } from 'lodash';
import { cloneDeep, isArray } from 'lodash';
import { IAnswerSort, IWaitAnswerSort } from '@/components/LLMToolView/types';
import { MathUtils } from '@labelbee/lb-annotation';

interface IProps {
setSortList: (value: any) => void;
setSortList: (value: IAnswerSort[][]) => void;
sortList: IAnswerSort[][];
waitSortList: IWaitAnswerSort[];
checkMode?: boolean;
Expand Down Expand Up @@ -71,7 +71,15 @@ const AnswerSort = (props: IProps) => {
formatSortList();
}, [JSON.stringify(sortList)]);

const singleAnswerItem = ({ item, operation, id }: any) => {
const singleAnswerItem = ({
item,
operation,
id,
}: {
item: IWaitAnswerSort;
id: string;
operation: any;
}) => {
const borderStyle = { [`border${activateDirection}`]: '2px solid #8C9AFF' };

return (
Expand Down Expand Up @@ -141,7 +149,7 @@ const AnswerSort = (props: IProps) => {
const sortBox = document.getElementById('sortBox');

if (sortBox?.childNodes) {
let newSortList: any[] = [];
let newSortList: IAnswerSort[][] = [];
sortBox.childNodes.forEach((item: any, nodeIndex: number) => {
let itemBox = item;
if (item?.childNodes?.length > 1) {
Expand All @@ -157,17 +165,17 @@ const AnswerSort = (props: IProps) => {
br: { x: right, y: bottom },
bl: { x: left, y: bottom },
};
const newList = sortList[nodeIndex].reduce((list: IAnswerSort[], key: any) => {

const newList = sortList[nodeIndex].reduce((list: IAnswerSort[], key: IAnswerSort) => {
let tagColumn = key;
if (key.length > 1) {
if (isArray(key) && key.length > 1) {
tagColumn = key[0];
}
tagColumn = { ...tagColumn, tagCenterPoint, tagVertexPoint };
return [...list, tagColumn];
}, []);
newSortList.push(newList);
});

setSortList(newSortList);
}
};
Expand Down Expand Up @@ -208,7 +216,9 @@ const AnswerSort = (props: IProps) => {
return;
}
setTargetTagKey(tagNearest[0]?.id);
compareDistance(sourceTagCenterPoint, tagNearest[0]?.tagVertexPoint);
if (tagNearest[0]?.tagVertexPoint && sourceTagCenterPoint) {
compareDistance(sourceTagCenterPoint, tagNearest[0]?.tagVertexPoint);
}
}
};

Expand All @@ -217,7 +227,7 @@ const AnswerSort = (props: IProps) => {
let key = -1;
let oldIndex = -1;
let tagIndex = -1;
let newList: any = [];
let newList: IAnswerSort[] = [];
let formatList = cloneDeep(sortList);

tagIndex = formatList.findIndex((i: IAnswerSort[]) => i[0].id === Number(targetTagKey));
Expand All @@ -234,13 +244,16 @@ const AnswerSort = (props: IProps) => {
}
key = getAttributeIndex(target.id);
oldIndex = formatList.findIndex((i: IAnswerSort[]) => i[0].id === key);
newList = formatList.find((i: IAnswerSort[]) => i[0].id === key);
const newAnswerValue = formatList.find((i: IAnswerSort[]) => i[0].id === key);
if (newAnswerValue) {
newList = newAnswerValue;
}
formatList.splice(oldIndex, 1);
tagIndex = formatList.findIndex((i: IAnswerSort[]) => i[0].id === ~~targetTagKey);
}
if (target.parentNode.id === 'waitBox') {
key = getAttributeIndex(target.id);
oldIndex = answers.findIndex((i: any) => i.id === key);
oldIndex = answers.findIndex((i: IWaitAnswerSort) => i.id === key);
newList = [answers[oldIndex]];
answers.splice(oldIndex, 1);
}
Expand Down Expand Up @@ -285,7 +298,7 @@ const AnswerSort = (props: IProps) => {
<span style={{ marginRight: '16px' }}>{t('ToBeSorted')}</span>
<div id='waitBox' className={`${contentBoxCls}__answerBox`}>
{answers.length > 0 &&
answers.map((i: any) =>
answers.map((i: IWaitAnswerSort) =>
singleAnswerItem({
item: i,
id: `waitBoxItem-${i?.id}`,
Expand All @@ -299,11 +312,11 @@ const AnswerSort = (props: IProps) => {
</div>
<Navigation />
<div id='sortBox' className={`${contentBoxCls}__answerBox`}>
{sortList.map((i: any, index: number) => {
{sortList.map((i: IAnswerSort[], index: number) => {
if (i.length > 1) {
return (
<div key={`item-${index}`} id={`sortBox-${index}`}>
{i.map((item: any) =>
{i.map((item: IAnswerSort) =>
singleAnswerItem({
item,
id: `sortBoxItem-${item?.id}`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,25 @@ import { useTranslation } from 'react-i18next';

interface IProps {
title: string;
selectValue?: number;
selectValue?: boolean;
isDisableAll?: boolean;
updateValue: (changeValue: boolean) => void;
}

export enum EDetermineGroup {
Yes,
No,
}

const DetermineGroup = (props: IProps) => {
const { title, selectValue, isDisableAll, updateValue } = props;
let selectDetermine: EDetermineGroup | undefined;
if (selectValue === true) {
selectDetermine = EDetermineGroup.Yes;
} else if (selectValue === false) {
selectDetermine = EDetermineGroup.No;
}

const { t } = useTranslation();
return (
<div className={`${prefix}-LLMSidebar-contentBox`}>
Expand All @@ -26,10 +38,10 @@ const DetermineGroup = (props: IProps) => {
onChange={(e) => updateValue(e.target.value)}
disabled={isDisableAll}
>
<Radio value={true} key={1}>
<Radio value={true} key={EDetermineGroup.Yes}>
{t('Yes')}
</Radio>
<Radio value={false} key={2}>
<Radio value={false} key={EDetermineGroup.No}>
{t('No')}
</Radio>
</Radio.Group>
Expand Down
38 changes: 23 additions & 15 deletions packages/lb-components/src/components/LLMToolView/sidebar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ import { getStepConfig } from '@/store/annotation/reducer';
import { useCustomToolInstance } from '@/hooks/annotation';
import { PageForward } from '@/store/annotation/actionCreators';
import { EToolName } from '@labelbee/lb-annotation';
import { IWaitAnswerSort, IAnswerSort, ILLMBoxResult } from '@/components/LLMToolView/types';
import {
IWaitAnswerSort,
IAnswerSort,
ILLMBoxResult,
ILLMToolConfig,
IAnswerList,
} from '@/components/LLMToolView/types';
import { useTranslation } from 'react-i18next';
import { formatSort, getCurrentResultFromResultList } from '../utils/data';

Expand Down Expand Up @@ -40,11 +46,11 @@ const Sidebar: React.FC<IProps> = (props) => {
const currentData = imgList[imgIndex] ?? {};
const basicInfo = jsonParser(currentData?.result);
const { toolInstanceRef } = useCustomToolInstance({ basicInfo });
const [sortList, setSortList] = useState<IAnswerSort[][]>([]);
const [LLMConfig, setLLMConfig] = useState<any>({});
const [answerList, setAnswerList] = useState<IWaitAnswerSort[]>([]);
const [LLMConfig, setLLMConfig] = useState<ILLMToolConfig>();
const [answerList, setAnswerList] = useState<IAnswerList[]>([]);
const [text, setText] = useState<string | undefined>(undefined);
const [waitSortList, setWaitSortList] = useState<IAnswerSort[]>([]);
const [sortList, setSortList] = useState<IAnswerSort[][]>([]);
const [waitSortList, setWaitSortList] = useState<IWaitAnswerSort[]>([]);

useEffect(() => {
if (stepList && step) {
Expand Down Expand Up @@ -86,29 +92,29 @@ const Sidebar: React.FC<IProps> = (props) => {
toolInstanceRef.current.currentPageResult = result;
}, [answerList, sortList, text]);

const getWaitSortList = (answerList: any) => {
const getWaitSortList = (answerList: IAnswerList[]) => {
setSortList([]);
if (answerList?.length > 0) {
let waitSorts: any = [];
let newSort: any = [];
let waitSorts: IWaitAnswerSort[] = [];
let newSort: IAnswerSort[][] = [];
// 将[[1],[2,3]]格式转成[[{ title: 1, id: 1 }],[{...},{...}]]
const result = getCurrentResultFromResultList(currentData?.result);
const currentResult = result?.length > 0 ? result[0] : result;
if (currentResult?.sort?.length > 0) {
newSort = currentResult.sort.reduce((i: any, key: any) => {
newSort = currentResult.sort.reduce((i: IWaitAnswerSort[][], key: number[]) => {
let tagColumn = [{ title: key[0], id: key[0] }];
if (key.length > 1) {
tagColumn = key.map((item: any) => ({ title: item, id: item }));
tagColumn = key.map((item: number) => ({ title: item, id: item }));
}
return [...i, tagColumn];
}, []);
setSortList(newSort);
}
// 待排序容器需要过滤已排序容器存在的答案
answerList.forEach((i: any) => {
const existed = newSort.some((sortItem: any) => {
answerList.forEach((i: IAnswerList) => {
const existed = newSort.some((sortItem: IAnswerSort[]) => {
if (sortItem.length > 1) {
return sortItem.some((v: any) => v.id === i.order);
return sortItem.some((v: IAnswerSort) => v.id === i.order);
}
return sortItem[0].id === i.order;
});
Expand All @@ -122,14 +128,16 @@ const Sidebar: React.FC<IProps> = (props) => {
};

const updateValue = ({ order, value, key }: IConfigUpdate) => {
const newList = answerList?.map((i: any, listIndex) => {
const newList = answerList?.map((i: IAnswerList) => {
if (i?.order === order) {
if (isNumber(value)) {
return { ...i, score: value };
}
if (isObject(value) && key) {
const obj = { [value?.key]: value.value };
return { ...i, [key]: { ...i[key], ...obj } };
// @ts-ignore
const originData = i[key] ?? {};
return { ...i, [key]: { ...originData, ...obj } };
}
}
return i;
Expand Down
Loading

0 comments on commit 3d578aa

Please sign in to comment.