-
Notifications
You must be signed in to change notification settings - Fork 327
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(prompts): Render prompt messages on prompt detail view (#5786)
* feat(prompts): Render prompt messages on prompt detail view * Remove unused import * Implement copy and paste button on prompt chat messages * Render chat messages on prompt version details page * Redirect to latest version when clicking versions tab Additionally highlight active version in versions list * Update example data to match template language * Fix prompt details page width, prompt versions borders / scroll * Distinguish readOnly TemplateEditor styling from normal TemplateEditor * Refactor active prompt id into prop * Replace custom button with styled anchor * Rename node in promptVersion query
- Loading branch information
1 parent
8078906
commit 0f9a33e
Showing
20 changed files
with
592 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,44 @@ | ||
import React from "react"; | ||
|
||
import { Button, Card, Icon, Icons } from "@arizeai/components"; | ||
import { Card } from "@arizeai/components"; | ||
|
||
import { CopyToClipboardButton } from "@phoenix/components"; | ||
import { | ||
TemplateEditor, | ||
TemplateEditorWrap, | ||
} from "@phoenix/components/templateEditor"; | ||
import { TemplateLanguage } from "@phoenix/components/templateEditor/types"; | ||
import { useChatMessageStyles } from "@phoenix/hooks/useChatMessageStyles"; | ||
|
||
export type ChatTemplateMessageProps = { | ||
role: string; | ||
content: string; | ||
templateFormat: TemplateLanguage; | ||
}; | ||
|
||
/** | ||
* A Read-Only CodeMirror component for the chat template message | ||
* E.x. a system or user message template part | ||
*/ | ||
export function ChatTemplateMessage(props: ChatTemplateMessageProps) { | ||
const { role, content } = props; | ||
const { role, content, templateFormat } = props; | ||
const styles = useChatMessageStyles(role); | ||
return ( | ||
<Card | ||
title={role} | ||
variant="compact" | ||
{...styles} | ||
extra={ | ||
<Button | ||
variant="default" | ||
size="compact" | ||
icon={<Icon svg={<Icons.ClipboardCopy />} />} | ||
/> | ||
} | ||
bodyStyle={{ padding: 0 }} | ||
extra={<CopyToClipboardButton text={content} />} | ||
> | ||
{content} | ||
<TemplateEditorWrap readOnly> | ||
<TemplateEditor | ||
readOnly | ||
height="100%" | ||
value={content} | ||
templateLanguage={templateFormat} | ||
/> | ||
</TemplateEditorWrap> | ||
</Card> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import React, { useMemo } from "react"; | ||
import { useFragment } from "react-relay"; | ||
import { graphql } from "relay-runtime"; | ||
|
||
import { Flex, Text } from "@arizeai/components"; | ||
|
||
import { TemplateLanguages } from "@phoenix/components/templateEditor/constants"; | ||
import { TemplateLanguage } from "@phoenix/components/templateEditor/types"; | ||
|
||
import { | ||
PromptChatMessages__main$key, | ||
PromptTemplateFormat, | ||
} from "./__generated__/PromptChatMessages__main.graphql"; | ||
import { ChatTemplateMessage } from "./ChatTemplateMessage"; | ||
import { PromptChatTemplate, PromptChatTemplateSchema } from "./schemas"; | ||
|
||
const convertTemplateFormat = ( | ||
templateFormat: PromptTemplateFormat | ||
): TemplateLanguage => { | ||
if (templateFormat === "FSTRING") { | ||
return TemplateLanguages.FString; | ||
} else if (templateFormat === "MUSTACHE") { | ||
return TemplateLanguages.Mustache; | ||
} | ||
return TemplateLanguages.NONE; | ||
}; | ||
|
||
export function PromptChatMessages({ | ||
promptVersion, | ||
}: { | ||
promptVersion: PromptChatMessages__main$key; | ||
}) { | ||
const { template, templateType, templateFormat } = useFragment( | ||
graphql` | ||
fragment PromptChatMessages__main on PromptVersion { | ||
template | ||
templateType | ||
templateFormat | ||
} | ||
`, | ||
promptVersion | ||
); | ||
|
||
if (templateType === "STRING") { | ||
return <Text>{template}</Text>; | ||
} | ||
|
||
return ( | ||
<ChatMessages | ||
template={template} | ||
templateFormat={convertTemplateFormat(templateFormat)} | ||
/> | ||
); | ||
} | ||
|
||
function ChatMessages({ | ||
template, | ||
templateFormat, | ||
}: { | ||
template: PromptChatTemplate | unknown; | ||
templateFormat: TemplateLanguage; | ||
}) { | ||
const messages = useMemo(() => { | ||
const parsedTemplate = PromptChatTemplateSchema.safeParse(template); | ||
if (!parsedTemplate.success) { | ||
return []; | ||
} | ||
return parsedTemplate.data.messages; | ||
}, [template]); | ||
return ( | ||
<Flex direction="column" gap="size-200"> | ||
{messages.map((message, i) => ( | ||
<ChatTemplateMessage | ||
key={i} | ||
{...message} | ||
templateFormat={templateFormat} | ||
/> | ||
))} | ||
</Flex> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.