Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(playground): Implement message role picker callback #4909

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions app/src/pages/playground/MessageRolePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { css } from "@emotion/react";

import { Item, Picker } from "@arizeai/components";

import { isChatMessageRole } from "@phoenix/pages/playground/playgroundUtils";
cephalization marked this conversation as resolved.
Show resolved Hide resolved
import { ChatMessageRole } from "@phoenix/store";

const hiddenLabelCSS = css`
Expand All @@ -22,22 +23,30 @@ type MessageRolePickerProps = {
* @default true
*/
includeLabel?: boolean;
/**
* Callback for when the message role changes
*/
onChange: (role: ChatMessageRole) => void;
};

export function MessageRolePicker({
role,
includeLabel = true,
onChange,
}: MessageRolePickerProps) {
return (
<Picker
selectedKey={role}
css={!includeLabel ? hiddenLabelCSS : undefined}
label="Role"
data-testid="inferences-time-range"
aria-label={`Time range for the primary inferences`}
data-testid="messages-role-picker"
cephalization marked this conversation as resolved.
Show resolved Hide resolved
aria-label={`Role for the chat message`}
size="compact"
onSelectionChange={() => {
// TODO: fill out
onSelectionChange={(e) => {
if (!isChatMessageRole(e)) {
throw new Error(`Invalid chat message role: ${e}`);
}
onChange(e);
}}
>
<Item key="system">System</Item>
Expand Down
18 changes: 17 additions & 1 deletion app/src/pages/playground/PlaygroundChatTemplate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,23 @@ export function PlaygroundChatTemplate(props: PlaygroundChatTemplateProps) {
<li key={index}>
<Card
title={
<MessageRolePicker includeLabel={false} role={message.role} />
<MessageRolePicker
includeLabel={false}
role={message.role}
onChange={(role) => {
updateInstance({
instanceId: id,
patch: {
template: {
__type: "chat",
messages: template.messages.map((message, i) =>
i === index ? { ...message, role } : message
cephalization marked this conversation as resolved.
Show resolved Hide resolved
),
},
},
});
}}
/>
}
variant="compact"
backgroundColor="light"
Expand Down
11 changes: 11 additions & 0 deletions app/src/pages/playground/playgroundUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {
ChatMessageRole,
chatMessageRoles,
} from "@phoenix/store/playgroundStore";

/**
* Checks if a string is a valid chat message role
*/
export function isChatMessageRole(role: unknown): role is ChatMessageRole {
return chatMessageRoles.includes(role as ChatMessageRole);
}
cephalization marked this conversation as resolved.
Show resolved Hide resolved
7 changes: 6 additions & 1 deletion app/src/store/playgroundStore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@ export type PlaygroundTemplate =
| PlaygroundChatTemplate
| PlaygroundTextCompletionTemplate;

/**
* Array of roles for a chat message with a LLM
*/
export const chatMessageRoles = ["user", "ai", "system", "tool"] as const;

/**
* The role of a chat message with a LLM
*/
export type ChatMessageRole = "user" | "ai" | "system" | "tool";
export type ChatMessageRole = (typeof chatMessageRoles)[number];

/**
* A chat message with a role and content
Expand Down
Loading