Skip to content

Commit

Permalink
Refine feedback UI and improve type handling (Chainlit#1325)
Browse files Browse the repository at this point in the history
* Enhance type checking for output logging in callbacks
* Display feedback buttons at end of run regardless of last step type
* Prevent feedback UI when no data layer is configured
* Update imports to include useConfig from chainlit/react-client
  • Loading branch information
willydouhard authored Sep 13, 2024
1 parent 0f7aad5 commit 7de6081
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
5 changes: 3 additions & 2 deletions backend/chainlit/langchain/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,12 +587,13 @@ def _on_run_update(self, run: Run) -> None:
outputs = run.outputs or {}
output_keys = list(outputs.keys())
output = outputs

if output_keys:
output = outputs.get(output_keys[0], outputs)

if current_step:
current_step.output = (
output[0] if isinstance(output, Sequence) and len(output) else output
output[0] if isinstance(output, Sequence) and not isinstance(output, str) and len(output) else output
)
current_step.end = utc_now()
self._run_sync(current_step.update())
Expand Down
9 changes: 6 additions & 3 deletions frontend/src/components/molecules/messages/Message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,12 @@ const Message = memo(
{actions?.length ? (
<MessageActions message={message} actions={actions} />
) : null}
{scorableRun && isScorable ? (
<MessageButtons message={message} run={scorableRun} />
) : null}
<MessageButtons
message={message}
run={
scorableRun && isScorable ? scorableRun : undefined
}
/>
</Stack>
)}
</Stack>
Expand Down
10 changes: 9 additions & 1 deletion frontend/src/components/molecules/messages/Messages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,19 @@ const Messages = memo(
// Score the current run
const _scorableRun = m.type === 'run' ? m : scorableRun;
// The message is scorable if it is the last assistant message of the run
const isScorable =

const isRunLastAssistantMessage =
m ===
_scorableRun?.steps?.findLast(
(_m) => _m.type === 'assistant_message'
);

const isLastAssistantMessage =
messages.findLast((_m) => _m.type === 'assistant_message') === m;

const isScorable =
isRunLastAssistantMessage || isLastAssistantMessage;

return (
<Message
message={m}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import IconButton from '@mui/material/IconButton';
import Stack from '@mui/material/Stack';
import Tooltip from '@mui/material/Tooltip';

import { firstUserInteraction, useChatSession } from '@chainlit/react-client';
import {
firstUserInteraction,
useChatSession,
useConfig
} from '@chainlit/react-client';

import Dialog from 'components/atoms/Dialog';
import { AccentButton } from 'components/atoms/buttons/AccentButton';
Expand All @@ -30,6 +34,7 @@ interface Props {
}

const FeedbackButtons = ({ message }: Props) => {
const config = useConfig();
const { onFeedbackUpdated, onFeedbackDeleted } = useContext(MessageContext);
const [showFeedbackDialog, setShowFeedbackDialog] = useState<number>();
const [commentInput, setCommentInput] = useState<string>();
Expand All @@ -39,6 +44,10 @@ const FeedbackButtons = ({ message }: Props) => {
const [feedback, setFeedback] = useState(message.feedback?.value);
const [comment, setComment] = useState(message.feedback?.comment);

if (!config.config?.dataPersistence) {
return null;
}

const DownIcon = feedback === 0 ? ThumbDownFilledIcon : ThumbDownIcon;
const UpIcon = feedback === 1 ? ThumbUpFilledIcon : ThumbUpIcon;

Expand Down

0 comments on commit 7de6081

Please sign in to comment.