mirror of
https://github.com/langgenius/dify.git
synced 2026-04-12 09:00:19 -04:00
refactor(web): store of evaluation
This commit is contained in:
@@ -130,7 +130,7 @@ describe('Evaluation', () => {
|
||||
])
|
||||
store.addCondition(resourceType, resourceId)
|
||||
|
||||
const condition = useEvaluationStore.getState().resources['apps:app-2'].conditions.conditions[0]
|
||||
const condition = useEvaluationStore.getState().resources['apps:app-2'].judgmentConfig.conditions[0]
|
||||
conditionId = condition.id
|
||||
store.updateConditionOperator(resourceType, resourceId, conditionId, '=')
|
||||
})
|
||||
|
||||
@@ -107,7 +107,7 @@ describe('evaluation store', () => {
|
||||
{ node_id: 'node-2', title: 'Retriever Node', type: 'retriever' },
|
||||
])
|
||||
expect(state.metrics.filter(item => item.optionId === metricId)).toHaveLength(1)
|
||||
expect(state.conditions.conditions).toHaveLength(0)
|
||||
expect(state.judgmentConfig.conditions).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('should build numeric conditions from selected metrics', () => {
|
||||
@@ -124,9 +124,9 @@ describe('evaluation store', () => {
|
||||
store.addCondition(resourceType, resourceId)
|
||||
|
||||
const state = useEvaluationStore.getState().resources['apps:app-conditions']
|
||||
const condition = state.conditions.conditions[0]
|
||||
const condition = state.judgmentConfig.conditions[0]
|
||||
|
||||
expect(state.conditions.logicalOperator).toBe('or')
|
||||
expect(state.judgmentConfig.logicalOperator).toBe('or')
|
||||
expect(condition.variableSelector).toEqual(['node-answer', 'answer-correctness'])
|
||||
expect(condition.comparisonOperator).toBe('=')
|
||||
expect(getAllowedOperators(state.metrics, condition.variableSelector)).toEqual(['=', '≠', '>', '<', '≥', '≤', 'is null', 'is not null'])
|
||||
@@ -153,13 +153,13 @@ describe('evaluation store', () => {
|
||||
}])
|
||||
store.addCondition(resourceType, resourceId)
|
||||
|
||||
const condition = useEvaluationStore.getState().resources['apps:app-3'].conditions.conditions[0]
|
||||
const condition = useEvaluationStore.getState().resources['apps:app-3'].judgmentConfig.conditions[0]
|
||||
|
||||
store.updateConditionMetric(resourceType, resourceId, condition.id, [config.workflowOptions[0].id, 'reason'])
|
||||
store.updateConditionValue(resourceType, resourceId, condition.id, 'needs follow-up')
|
||||
store.updateConditionOperator(resourceType, resourceId, condition.id, 'empty')
|
||||
|
||||
const updatedCondition = useEvaluationStore.getState().resources['apps:app-3'].conditions.conditions[0]
|
||||
const updatedCondition = useEvaluationStore.getState().resources['apps:app-3'].judgmentConfig.conditions[0]
|
||||
|
||||
expect(requiresConditionValue('empty')).toBe(false)
|
||||
expect(updatedCondition.value).toBeNull()
|
||||
@@ -232,8 +232,8 @@ describe('evaluation store', () => {
|
||||
expect(hydratedState.metrics[1].customConfig?.mappings[0].inputVariableId).toBe('query')
|
||||
expect(hydratedState.metrics[1].customConfig?.mappings[0].outputVariableId).toBe('answer')
|
||||
expect(hydratedState.metrics[1].customConfig?.outputs).toEqual([{ id: 'reason', valueType: 'string' }])
|
||||
expect(hydratedState.conditions.logicalOperator).toBe('or')
|
||||
expect(hydratedState.conditions.conditions[0]).toMatchObject({
|
||||
expect(hydratedState.judgmentConfig.logicalOperator).toBe('or')
|
||||
expect(hydratedState.judgmentConfig.conditions[0]).toMatchObject({
|
||||
variableSelector: ['node-1', 'faithfulness'],
|
||||
comparisonOperator: '≥',
|
||||
value: '0.9',
|
||||
|
||||
@@ -231,7 +231,7 @@ const ConditionGroup = ({
|
||||
type="button"
|
||||
className={cn(
|
||||
'rounded-md px-3 py-1.5 system-xs-medium-uppercase',
|
||||
resource.conditions.logicalOperator === operator
|
||||
resource.judgmentConfig.logicalOperator === operator
|
||||
? 'bg-components-card-bg text-text-primary shadow-xs'
|
||||
: 'text-text-tertiary',
|
||||
)}
|
||||
@@ -245,7 +245,7 @@ const ConditionGroup = ({
|
||||
</div>
|
||||
|
||||
<div className="space-y-3">
|
||||
{resource.conditions.conditions.map((condition) => {
|
||||
{resource.judgmentConfig.conditions.map((condition) => {
|
||||
const metric = metricOptions.find(option => isSelectorEqual(option.variableSelector, condition.variableSelector))
|
||||
const allowedOperators = getAllowedOperators(resource.metrics, condition.variableSelector)
|
||||
const showValue = !!metric && requiresConditionValue(condition.comparisonOperator)
|
||||
|
||||
@@ -26,12 +26,12 @@ const ConditionsSection = ({
|
||||
tooltip={t('conditions.description')}
|
||||
/>
|
||||
<div className="mt-2 space-y-4">
|
||||
{resource.conditions.conditions.length === 0 && (
|
||||
{resource.judgmentConfig.conditions.length === 0 && (
|
||||
<div className="rounded-xl bg-background-section px-3 py-3 system-xs-regular text-text-tertiary">
|
||||
{t('conditions.emptyDescription')}
|
||||
</div>
|
||||
)}
|
||||
{resource.conditions.conditions.length > 0 && (
|
||||
{resource.judgmentConfig.conditions.length > 0 && (
|
||||
<ConditionGroup
|
||||
resourceType={resourceType}
|
||||
resourceId={resourceId}
|
||||
|
||||
@@ -180,21 +180,6 @@ const getNormalizedConditionValue = (
|
||||
return typeof previousValue === 'string' ? previousValue : null
|
||||
}
|
||||
|
||||
const getRawJudgmentConfig = (config: EvaluationConfig): EvaluationJudgmentConfig | null | undefined => {
|
||||
if (config.judgment_config)
|
||||
return config.judgment_config
|
||||
|
||||
if (
|
||||
config.judgement_conditions
|
||||
&& !Array.isArray(config.judgement_conditions)
|
||||
&& 'conditions' in config.judgement_conditions
|
||||
) {
|
||||
return config.judgement_conditions as EvaluationJudgmentConfig
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
const normalizeConditionItem = (
|
||||
value: EvaluationJudgmentCondition,
|
||||
metrics: EvaluationMetric[],
|
||||
@@ -234,7 +219,7 @@ const normalizeJudgmentConfig = (
|
||||
config: EvaluationConfig,
|
||||
metrics: EvaluationMetric[],
|
||||
): JudgmentConfig => {
|
||||
const rawJudgmentConfig = getRawJudgmentConfig(config)
|
||||
const rawJudgmentConfig: EvaluationJudgmentConfig | null | undefined = config.judgment_config
|
||||
|
||||
if (!rawJudgmentConfig)
|
||||
return createEmptyJudgmentConfig()
|
||||
@@ -393,7 +378,7 @@ export const buildInitialState = (_resourceType: EvaluationResourceType): Evalua
|
||||
return {
|
||||
judgeModelId: null,
|
||||
metrics: [],
|
||||
conditions: createEmptyJudgmentConfig(),
|
||||
judgmentConfig: createEmptyJudgmentConfig(),
|
||||
activeBatchTab: 'input-fields',
|
||||
uploadedFileName: null,
|
||||
batchRecords: [],
|
||||
@@ -414,7 +399,7 @@ export const buildStateFromEvaluationConfig = (
|
||||
? encodeModelSelection(config.evaluation_model_provider, config.evaluation_model)
|
||||
: null,
|
||||
metrics,
|
||||
conditions: normalizeJudgmentConfig(config, metrics),
|
||||
judgmentConfig: normalizeJudgmentConfig(config, metrics),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -133,7 +133,7 @@ export const useEvaluationStore = create<EvaluationStore>((set, get) => ({
|
||||
return {
|
||||
...currentResource,
|
||||
metrics,
|
||||
conditions: syncJudgmentConfigWithMetrics(currentResource.conditions, metrics),
|
||||
judgmentConfig: syncJudgmentConfigWithMetrics(currentResource.judgmentConfig, metrics),
|
||||
}
|
||||
}),
|
||||
}
|
||||
@@ -160,7 +160,7 @@ export const useEvaluationStore = create<EvaluationStore>((set, get) => ({
|
||||
return {
|
||||
...resource,
|
||||
metrics,
|
||||
conditions: syncJudgmentConfigWithMetrics(resource.conditions, metrics),
|
||||
judgmentConfig: syncJudgmentConfigWithMetrics(resource.judgmentConfig, metrics),
|
||||
}
|
||||
}),
|
||||
}))
|
||||
@@ -173,7 +173,7 @@ export const useEvaluationStore = create<EvaluationStore>((set, get) => ({
|
||||
return {
|
||||
...resource,
|
||||
metrics,
|
||||
conditions: syncJudgmentConfigWithMetrics(resource.conditions, metrics),
|
||||
judgmentConfig: syncJudgmentConfigWithMetrics(resource.judgmentConfig, metrics),
|
||||
}
|
||||
}),
|
||||
}))
|
||||
@@ -201,7 +201,7 @@ export const useEvaluationStore = create<EvaluationStore>((set, get) => ({
|
||||
return {
|
||||
...resource,
|
||||
metrics,
|
||||
conditions: syncJudgmentConfigWithMetrics(resource.conditions, metrics),
|
||||
judgmentConfig: syncJudgmentConfigWithMetrics(resource.judgmentConfig, metrics),
|
||||
}
|
||||
}),
|
||||
}))
|
||||
@@ -238,7 +238,7 @@ export const useEvaluationStore = create<EvaluationStore>((set, get) => ({
|
||||
return {
|
||||
...resource,
|
||||
metrics,
|
||||
conditions: syncJudgmentConfigWithMetrics(resource.conditions, metrics),
|
||||
judgmentConfig: syncJudgmentConfigWithMetrics(resource.judgmentConfig, metrics),
|
||||
}
|
||||
}),
|
||||
}))
|
||||
@@ -263,8 +263,8 @@ export const useEvaluationStore = create<EvaluationStore>((set, get) => ({
|
||||
set(state => ({
|
||||
resources: updateResourceState(state.resources, resourceType, resourceId, resource => ({
|
||||
...resource,
|
||||
conditions: {
|
||||
...resource.conditions,
|
||||
judgmentConfig: {
|
||||
...resource.judgmentConfig,
|
||||
logicalOperator,
|
||||
},
|
||||
})),
|
||||
@@ -274,9 +274,9 @@ export const useEvaluationStore = create<EvaluationStore>((set, get) => ({
|
||||
set(state => ({
|
||||
resources: updateResourceState(state.resources, resourceType, resourceId, resource => ({
|
||||
...resource,
|
||||
conditions: {
|
||||
...resource.conditions,
|
||||
conditions: [...resource.conditions.conditions, buildConditionItem(resource.metrics)],
|
||||
judgmentConfig: {
|
||||
...resource.judgmentConfig,
|
||||
conditions: [...resource.judgmentConfig.conditions, buildConditionItem(resource.metrics)],
|
||||
},
|
||||
})),
|
||||
}))
|
||||
@@ -285,9 +285,9 @@ export const useEvaluationStore = create<EvaluationStore>((set, get) => ({
|
||||
set(state => ({
|
||||
resources: updateResourceState(state.resources, resourceType, resourceId, resource => ({
|
||||
...resource,
|
||||
conditions: {
|
||||
...resource.conditions,
|
||||
conditions: resource.conditions.conditions.filter(condition => condition.id !== conditionId),
|
||||
judgmentConfig: {
|
||||
...resource.judgmentConfig,
|
||||
conditions: resource.judgmentConfig.conditions.filter(condition => condition.id !== conditionId),
|
||||
},
|
||||
})),
|
||||
}))
|
||||
@@ -303,9 +303,9 @@ export const useEvaluationStore = create<EvaluationStore>((set, get) => ({
|
||||
|
||||
return {
|
||||
...resource,
|
||||
conditions: {
|
||||
...resource.conditions,
|
||||
conditions: resource.conditions.conditions.map(condition => condition.id === conditionId
|
||||
judgmentConfig: {
|
||||
...resource.judgmentConfig,
|
||||
conditions: resource.judgmentConfig.conditions.map(condition => condition.id === conditionId
|
||||
? {
|
||||
...condition,
|
||||
variableSelector,
|
||||
@@ -323,9 +323,9 @@ export const useEvaluationStore = create<EvaluationStore>((set, get) => ({
|
||||
return {
|
||||
resources: updateResourceState(state.resources, resourceType, resourceId, currentResource => ({
|
||||
...currentResource,
|
||||
conditions: {
|
||||
...currentResource.conditions,
|
||||
conditions: currentResource.conditions.conditions.map((condition) => {
|
||||
judgmentConfig: {
|
||||
...currentResource.judgmentConfig,
|
||||
conditions: currentResource.judgmentConfig.conditions.map((condition) => {
|
||||
if (condition.id !== conditionId)
|
||||
return condition
|
||||
|
||||
@@ -350,9 +350,9 @@ export const useEvaluationStore = create<EvaluationStore>((set, get) => ({
|
||||
set(state => ({
|
||||
resources: updateResourceState(state.resources, resourceType, resourceId, resource => ({
|
||||
...resource,
|
||||
conditions: {
|
||||
...resource.conditions,
|
||||
conditions: resource.conditions.conditions.map(condition => condition.id === conditionId ? { ...condition, value } : condition),
|
||||
judgmentConfig: {
|
||||
...resource.judgmentConfig,
|
||||
conditions: resource.judgmentConfig.conditions.map(condition => condition.id === conditionId ? { ...condition, value } : condition),
|
||||
},
|
||||
})),
|
||||
}))
|
||||
|
||||
@@ -130,7 +130,7 @@ export type BatchTestRecord = {
|
||||
export type EvaluationResourceState = {
|
||||
judgeModelId: string | null
|
||||
metrics: EvaluationMetric[]
|
||||
conditions: JudgmentConfig
|
||||
judgmentConfig: JudgmentConfig
|
||||
activeBatchTab: BatchTestTab
|
||||
uploadedFileName: string | null
|
||||
batchRecords: BatchTestRecord[]
|
||||
|
||||
@@ -13,36 +13,12 @@ export type EvaluationJudgmentConfig = {
|
||||
conditions?: EvaluationJudgmentCondition[]
|
||||
}
|
||||
|
||||
export type EvaluationConditionValue = string | number | boolean | null
|
||||
|
||||
export type EvaluationJudgementConditionItem = {
|
||||
id?: string
|
||||
fieldId?: string
|
||||
field_id?: string
|
||||
operator?: string
|
||||
value?: EvaluationConditionValue
|
||||
}
|
||||
|
||||
export type EvaluationJudgementConditionGroup = {
|
||||
id?: string
|
||||
logicalOperator?: 'and' | 'or'
|
||||
logical_operator?: 'and' | 'or'
|
||||
items?: EvaluationJudgementConditionItem[]
|
||||
}
|
||||
|
||||
export type EvaluationJudgementConditions
|
||||
= | EvaluationJudgementConditionGroup[]
|
||||
| {
|
||||
groups?: EvaluationJudgementConditionGroup[]
|
||||
}
|
||||
|
||||
export type EvaluationConfig = {
|
||||
evaluation_model: string | null
|
||||
evaluation_model_provider: string | null
|
||||
default_metrics?: EvaluationDefaultMetric[] | null
|
||||
customized_metrics?: EvaluationCustomizedMetric | null
|
||||
judgment_config?: EvaluationJudgmentConfig | null
|
||||
judgement_conditions?: EvaluationJudgementConditions | null
|
||||
}
|
||||
|
||||
export type NodeInfo = {
|
||||
|
||||
Reference in New Issue
Block a user