Skip to content

Commit b566d40

Browse files
authored
fix: normalize gemini roles (#7404)
1 parent 0cf871e commit b566d40

File tree

4 files changed

+20
-15
lines changed

4 files changed

+20
-15
lines changed

app/src/constants/generativeConstants.ts

+15
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,18 @@ export const AZURE_OPENAI_API_VERSIONS = [
4545
"2023-05-15",
4646
"2023-03-15-preview",
4747
];
48+
49+
/**
50+
* Map of {@link ChatMessageRole} to potential role values.
51+
* Used to map roles to a canonical role.
52+
*/
53+
export const ChatRoleMap: Record<ChatMessageRole, string[]> = {
54+
user: ["user", "human"],
55+
// Assistant used by OpenAI
56+
// Model used by Gemini
57+
ai: ["assistant", "bot", "ai", "model"],
58+
// Developer is a newer tier of role from OpenAI
59+
// While not 1 to 1 with system, it's a close match and so we treat it as such
60+
system: ["system", "developer"],
61+
tool: ["tool"],
62+
};

app/src/hooks/useChatMessageStyles.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
import { useMemo } from "react";
22

33
import { ViewStyleProps } from "@phoenix/components/types";
4+
import { ChatRoleMap } from "@phoenix/constants/generativeConstants";
45

56
export function useChatMessageStyles(
67
role: string
78
): Pick<ViewStyleProps, "backgroundColor" | "borderColor"> {
89
return useMemo<ViewStyleProps>(() => {
910
const normalizedRole = role.toLowerCase();
10-
if (normalizedRole === "user" || normalizedRole === "human") {
11+
if (ChatRoleMap.user.includes(normalizedRole)) {
1112
return {
1213
backgroundColor: "grey-200",
1314
borderColor: "grey-500",
1415
};
15-
} else if (normalizedRole === "assistant" || normalizedRole === "ai") {
16+
} else if (ChatRoleMap.ai.includes(normalizedRole)) {
1617
return {
1718
backgroundColor: "blue-100",
1819
borderColor: "blue-700",
1920
};
20-
} else if (normalizedRole === "system" || normalizedRole === "developer") {
21+
} else if (ChatRoleMap.system.includes(normalizedRole)) {
2122
return {
2223
backgroundColor: "indigo-100",
2324
borderColor: "indigo-700",

app/src/pages/playground/constants.tsx

-11
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,6 @@ import { CanonicalParameterName } from "./__generated__/ModelSupportedParamsFetc
22

33
export const NUM_MAX_PLAYGROUND_INSTANCES = 4;
44

5-
/**
6-
* Map of {@link ChatMessageRole} to potential role values.
7-
* Used to map roles to a canonical role.
8-
*/
9-
export const ChatRoleMap: Record<ChatMessageRole, string[]> = {
10-
user: ["user", "human"],
11-
ai: ["assistant", "bot", "ai"],
12-
system: ["system"],
13-
tool: ["tool"],
14-
};
15-
165
/**
176
* Parsing errors for parsing a span to a playground instance
187
*/

app/src/pages/playground/playgroundUtils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { TemplateFormats } from "@phoenix/components/templateEditor/constants";
66
import { getTemplateFormatUtils } from "@phoenix/components/templateEditor/templateEditorUtils";
77
import { TemplateFormat } from "@phoenix/components/templateEditor/types";
88
import {
9+
ChatRoleMap,
910
DEFAULT_CHAT_ROLE,
1011
DEFAULT_MODEL_PROVIDER,
1112
} from "@phoenix/constants/generativeConstants";
@@ -51,7 +52,6 @@ import {
5152
InvocationParameterInput,
5253
} from "./__generated__/PlaygroundOutputSubscription.graphql";
5354
import {
54-
ChatRoleMap,
5555
INPUT_MESSAGES_PARSING_ERROR,
5656
MODEL_CONFIG_PARSING_ERROR,
5757
MODEL_CONFIG_WITH_INVOCATION_PARAMETERS_PARSING_ERROR,

0 commit comments

Comments
 (0)