Skip to content

Commit

Permalink
adjust UI to new schema
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeldking committed Dec 20, 2024
1 parent 30ce59a commit 120a23f
Show file tree
Hide file tree
Showing 7 changed files with 325 additions and 109 deletions.
1 change: 1 addition & 0 deletions app/src/hooks/useChatMessageStyles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export function useChatMessageStyles(
role: string
): Pick<ViewStyleProps, "backgroundColor" | "borderColor"> {
return useMemo<ViewStyleProps>(() => {
role = role.toLowerCase();

Check failure on line 9 in app/src/hooks/useChatMessageStyles.ts

View workflow job for this annotation

GitHub Actions / CI Typescript

Mutating component props or hook arguments is not allowed. Consider using a local variable instead

Check failure on line 9 in app/src/hooks/useChatMessageStyles.ts

View workflow job for this annotation

GitHub Actions / CI Typescript

Assignments to the 'role' variable from inside React Hook useMemo will be lost after each render. To preserve the value over time, store it in a useRef Hook and keep the mutable value in the '.current' property. Otherwise, you can move this variable directly inside useMemo
if (role === "user" || role === "human") {
return {
backgroundColor: "grey-100",
Expand Down
67 changes: 43 additions & 24 deletions app/src/pages/prompt/PromptChatMessages.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import React, { useMemo } from "react";
import { useFragment } from "react-relay";
import { graphql } from "relay-runtime";
import React from "react";
import { graphql, useFragment } from "react-relay";

import { Flex, Text } from "@arizeai/components";

import { TemplateLanguages } from "@phoenix/components/templateEditor/constants";
import { TemplateLanguage } from "@phoenix/components/templateEditor/types";

import {
PromptChatMessages__main$data,
PromptChatMessages__main$key,
PromptTemplateFormat,
} from "./__generated__/PromptChatMessages__main.graphql";
import { ChatTemplateMessage } from "./ChatTemplateMessage";
import { PromptChatTemplate, PromptChatTemplateSchema } from "./schemas";

const convertTemplateFormat = (
templateFormat: PromptTemplateFormat
Expand All @@ -30,49 +29,69 @@ export function PromptChatMessages({
}: {
promptVersion: PromptChatMessages__main$key;
}) {
const { template, templateType, templateFormat } = useFragment(
const { template, templateFormat } = useFragment(
graphql`
fragment PromptChatMessages__main on PromptVersion {
template
template {
__typename
... on PromptChatTemplate {
messages {
... on JSONPromptMessage {
role
jsonContent: content
}
... on TextPromptMessage {
role
content
}
}
}
... on PromptStringTemplate {
template
}
}
templateType
templateFormat
}
`,
promptVersion
);

if (templateType === "STRING") {
return <Text>{template}</Text>;
if (template.__typename === "PromptStringTemplate") {
return <Text>{template.template}</Text>;
}
if (template.__typename === "PromptChatTemplate") {
return (
<ChatMessages
template={template}
templateFormat={convertTemplateFormat(templateFormat)}
/>
);
}
if (template.__typename === "%other") {
throw new Error("Unknown template type" + template.__typename);
}

return (
<ChatMessages
template={template}
templateFormat={convertTemplateFormat(templateFormat)}
/>
);
}

function ChatMessages({
template,
templateFormat,
}: {
template: PromptChatTemplate | unknown;
template: Extract<
PromptChatMessages__main$data["template"],
{ __typename: "PromptChatTemplate" }
>;
templateFormat: TemplateLanguage;
}) {
const messages = useMemo(() => {
const parsedTemplate = PromptChatTemplateSchema.safeParse(template);
if (!parsedTemplate.success) {
return [];
}
return parsedTemplate.data.messages;
}, [template]);
const { messages } = template;
return (
<Flex direction="column" gap="size-200">
{messages.map((message, i) => (
// TODO: Handle JSON content for things like tool calls
<ChatTemplateMessage
key={i}
{...message}
role={message.role as string}
content={message.content || JSON.stringify(message.jsonContent)}
templateFormat={templateFormat}
/>
))}
Expand Down
107 changes: 102 additions & 5 deletions app/src/pages/prompt/__generated__/PromptChatMessages__main.graphql.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

87 changes: 81 additions & 6 deletions app/src/pages/prompt/__generated__/promptLoaderQuery.graphql.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 120a23f

Please sign in to comment.