Skip to content

Commit

Permalink
feat(playground): Implement message role picker callback (#4909)
Browse files Browse the repository at this point in the history
* feat(playground): Implement message role picker callback

* style(playground): Shorten import path for playgroundUtils
  • Loading branch information
cephalization authored and mikeldking committed Oct 11, 2024
1 parent ee1f85b commit b2a8bd0
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
18 changes: 14 additions & 4 deletions app/src/pages/playground/MessageRolePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { Item, Picker } from "@arizeai/components";

import { ChatMessageRole } from "@phoenix/store";

import { isChatMessageRole } from "./playgroundUtils";

const hiddenLabelCSS = css`
.ac-field-label {
display: none;
Expand All @@ -22,22 +24,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"
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
),
},
},
});
}}
/>
}
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);
}
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

0 comments on commit b2a8bd0

Please sign in to comment.