File size: 11,031 Bytes
e23b66d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3939dda
e23b66d
 
 
 
 
3939dda
e23b66d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3939dda
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e23b66d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3939dda
 
 
 
 
 
 
 
 
 
 
 
e23b66d
 
 
 
 
 
 
 
 
 
3939dda
 
 
 
 
 
 
 
 
 
 
 
e23b66d
 
 
 
 
 
 
 
 
 
 
 
 
3939dda
e23b66d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3939dda
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e23b66d
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/**
 *
 * Copyright 2023-2025 InspectorRAGet Team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 **/

'use client';

import { isEmpty } from 'lodash';
import DOMPurify from 'dompurify';
import parse from 'html-react-parser';
import { useMemo, useState } from 'react';
import {
  Tabs,
  TabList,
  Tab,
  TabPanels,
  TabPanel,
  ContainedList,
  ContainedListItem,
} from '@carbon/react';

import { Model, TaskEvaluation, Task, Metric, MessageStep } from '@/src/types';
import { useDataStore } from '@/src/store';
import { truncate } from '@/src/utilities/strings';

import AnnotationsTable from '@/src/views/annotations-table/AnnotationsTable';
import ChatLine from '@/src/components/chatline/ChatLine';
import ChatTaskCopierModal from '@/src/components/task-copier/ChatTaskCopier';

import classes from './ChatTask.module.scss';

// ===================================================================================
//                                TYPES
// ===================================================================================
interface Props {
  task: Task;
  models: Map<string, Model>;
  metrics: Metric[];
  taskCopierModalOpen: boolean;
  setTaskCopierModalOpen: Function;
  updateCommentProvenance: Function;
}

// ===================================================================================
//                               RENDER FUNCTIONS
// ===================================================================================

function Evaluation({
  evaluation,
  hMetrics,
  aMetrics,
}: {
  evaluation: TaskEvaluation;
  hMetrics: Map<string, Metric>;
  aMetrics: Map<string, Metric>;
}) {
  return (
    <div className={classes.evaluationContainer}>
      {evaluation.annotations && hMetrics.size ? (
        <>
          <h5>Human Evaluations:</h5>
          <AnnotationsTable
            annotations={evaluation.annotations}
            metrics={[...hMetrics.values()]}
          ></AnnotationsTable>
        </>
      ) : null}
      {evaluation.annotations && aMetrics.size ? (
        <>
          <h5>Algorithmic Evaluations:</h5>
          <AnnotationsTable
            annotations={evaluation.annotations}
            metrics={[...aMetrics.values()]}
          ></AnnotationsTable>
        </>
      ) : null}
    </div>
  );
}

function Steps({ steps }: { steps?: MessageStep[] }) {
  return (
    <div className={classes.stepsContainer}>
      {steps && !isEmpty(steps) ? (
        <></>
      ) : (
        <h4>No steps information is available.</h4>
      )}
    </div>
  );
}

// ===================================================================================
//                               MAIN FUNCTION
// ===================================================================================
export default function ChatTask({
  task,
  models,
  metrics,
  taskCopierModalOpen,
  setTaskCopierModalOpen,
  updateCommentProvenance,
}: Props) {
  // Step 1: Initialize state and necessary variables
  const [selectedEvaluationIndex, setSelectedEvaluationIndex] =
    useState<number>(0);

  // Step 2: Run effects
  // Step 2.a: Fetch data from data store
  const { item: data } = useDataStore();

  // Step 2.b: Fetch documents and evaluations
  const evaluations = useMemo(() => {
    // Step 2.b.i: Fetch evaluations
    let taskEvaluations: TaskEvaluation[] | undefined = undefined;
    if (data) {
      taskEvaluations = data.evaluations.filter(
        (evaluation) => evaluation.taskId === task.taskId,
      );
    }

    return taskEvaluations;
  }, [task.taskId, data]);

  // Step 2.c: Build human & algorithmic metric maps
  const [hMetrics, aMetrics] = useMemo(() => {
    const humanMetrics = new Map(
      metrics
        ?.filter((metric) => metric.author === 'human')
        .map((metric) => [metric.name, metric]),
    );
    const algorithmicMetrics = new Map(
      metrics
        ?.filter((metric) => metric.author === 'algorithm')
        .map((metric) => [metric.name, metric]),
    );

    return [humanMetrics, algorithmicMetrics];
  }, [metrics]);

  // Step 3: Render
  return (
    <>
      {models && metrics && task && evaluations && (
        <ChatTaskCopierModal
          open={taskCopierModalOpen}
          models={Array.from(models.values())}
          metrics={metrics}
          task={task}
          evaluations={evaluations}
          onClose={() => {
            setTaskCopierModalOpen(false);
          }}
        ></ChatTaskCopierModal>
      )}
      {task && models && evaluations && (
        <>
          <div className={classes.inputContainer}>
            {Array.isArray(task.input)
              ? task.input.map((message, messageIdx) => (
                  <ChatLine
                    key={`data_point__message--${messageIdx}`}
                    messageId={`data_point__message--${messageIdx}`}
                    message={message}
                    onSelection={updateCommentProvenance}
                    focused={
                      Array.isArray(task.input) &&
                      messageIdx === task.input.length - 1
                        ? true
                        : false
                    }
                    latestResponse={
                      Array.isArray(task.input) &&
                      messageIdx === task.input.length - 1
                        ? true
                        : false
                    }
                  ></ChatLine>
                ))
              : null}
          </div>

          <div className={classes.separator} />

          <div className={classes.modelsContainer}>
            <Tabs
              onChange={(e) => {
                setSelectedEvaluationIndex(e.selectedIndex);
              }}
            >
              <TabList aria-label="Models tab" contained>
                {evaluations.map((evaluation) => (
                  <Tab key={'model-' + evaluation.modelId}>
                    {truncate(
                      models.get(evaluation.modelId)?.name ||
                        evaluation.modelId,
                      15,
                    )}
                  </Tab>
                ))}
              </TabList>
              <TabPanels>
                {evaluations.map((evaluation) => (
                  <TabPanel key={'model-' + evaluation.modelId + '-panel'}>
                    <div className={classes.tabContainer}>
                      <div className={classes.tabContentHeader}>
                        <h5>Model:</h5>
                        <span>
                          {models.get(evaluation.modelId)?.name ||
                            evaluation.modelId}
                        </span>
                      </div>
                      <ContainedList
                        size="sm"
                        label="Response"
                        kind="disclosed"
                      >
                        <ContainedListItem>
                          <div
                            className={classes.responseContainer}
                            onMouseDown={() => {
                              updateCommentProvenance(
                                `${evaluation.modelId}::evaluation::response`,
                              );
                            }}
                            onMouseUp={() =>
                              updateCommentProvenance(
                                `${evaluation.modelId}::evaluation::response`,
                              )
                            }
                          >
                            {parse(
                              DOMPurify.sanitize(evaluation.modelResponse),
                            )}
                          </div>
                        </ContainedListItem>
                      </ContainedList>

                      {task.targets && !isEmpty(task.targets) ? (
                        <ContainedList
                          label="Targets"
                          kind="disclosed"
                          size="sm"
                        >
                          {task.targets.length > 1 ? (
                            task.targets.map((target, targetIdx) =>
                              target.text ? (
                                <ContainedListItem key={`target--${targetIdx}`}>
                                  <span>
                                    Target {targetIdx + 1}: {target.text}
                                  </span>
                                </ContainedListItem>
                              ) : null,
                            )
                          ) : (
                            <ContainedListItem key={`target--0`}>
                              <span>{task.targets[0].text}</span>
                            </ContainedListItem>
                          )}
                        </ContainedList>
                      ) : null}

                      <Tabs>
                        <TabList
                          aria-label="Model performance tab"
                          contained
                          fullWidth
                        >
                          <Tab
                            key={'model-' + evaluation.modelId + '-evaluations'}
                          >
                            Evaluations
                          </Tab>
                          <Tab key={'model-' + evaluation.modelId + '-steps'}>
                            Steps
                          </Tab>
                        </TabList>
                        <TabPanels>
                          <TabPanel
                            key={
                              'model-' +
                              evaluation.modelId +
                              '-evaluations-panel'
                            }
                          >
                            <Evaluation
                              evaluation={evaluation}
                              hMetrics={hMetrics}
                              aMetrics={aMetrics}
                            />
                          </TabPanel>
                          <TabPanel
                            key={'model-' + evaluation.modelId + '-steps-panel'}
                          >
                            <Steps steps={evaluation.steps} />
                          </TabPanel>
                        </TabPanels>
                      </Tabs>
                    </div>
                  </TabPanel>
                ))}
              </TabPanels>
            </Tabs>
          </div>
        </>
      )}
    </>
  );
}