From eaf7d74b3e6c1a9386b36f06e14000610561af8a Mon Sep 17 00:00:00 2001 From: James Melvin Date: Sun, 8 Jan 2023 08:53:56 +0530 Subject: [PATCH 1/7] fix: new web route to update text_labels fix: minimum changes to textFlagLabels to keep it coherent with the backend --- website/src/components/FlaggableElement.tsx | 62 ++++++++++----------- website/src/pages/api/set_label.ts | 55 ++++++++++++++++++ 2 files changed, 86 insertions(+), 31 deletions(-) create mode 100644 website/src/pages/api/set_label.ts diff --git a/website/src/components/FlaggableElement.tsx b/website/src/components/FlaggableElement.tsx index a55d29c5..7f77a3b7 100644 --- a/website/src/components/FlaggableElement.tsx +++ b/website/src/components/FlaggableElement.tsx @@ -29,7 +29,7 @@ import { colors } from "styles/Theme/colors"; export const FlaggableElement = (props) => { const [isEditing, setIsEditing] = useBoolean(); - const { trigger } = useSWRMutation("/api/v1/text_labels", poster, { + const { trigger } = useSWRMutation("/api/set_label", poster, { onSuccess: () => { setIsEditing.off; }, @@ -183,40 +183,40 @@ interface textFlagLabels { const TEXT_LABEL_FLAGS: textFlagLabels[] = [ // For the time being this list is configured on the FE. // In the future it may be provided by the API. + // { + // attributeName: "fails_task", + // labelText: "Fails to follow the correct instruction / task", + // additionalExplanation: "__TODO__", + // }, + // { + // attributeName: "not_customer_assistant_appropriate", + // labelText: "Inappropriate for customer assistant", + // additionalExplanation: "__TODO__", + // }, { - attributeName: "fails_task", - labelText: "Fails to follow the correct instruction / task", - additionalExplanation: "__TODO__", - }, - { - attributeName: "not_customer_assistant_appropriate", - labelText: "Inappropriate for customer assistant", - additionalExplanation: "__TODO__", - }, - { - attributeName: "contains_sexual_content", + attributeName: "sexual_content", labelText: "Contains sexual content", }, { - attributeName: "contains_violent_content", + attributeName: "violence", labelText: "Contains violent content", }, - { - attributeName: "encourages_violence", - labelText: "Encourages or fails to discourage violence/abuse/terrorism/self-harm", - }, - { - attributeName: "denigrates_a_protected_class", - labelText: "Denigrates a protected class", - }, - { - attributeName: "gives_harmful_advice", - labelText: "Fails to follow the correct instruction / task", - additionalExplanation: - "The advice given in the output is harmful or counter-productive. This may be in addition to, but is distinct from the question about encouraging violence/abuse/terrorism/self-harm.", - }, - { - attributeName: "expresses_moral_judgement", - labelText: "Expresses moral judgement", - }, + // { + // attributeName: "encourages_violence", + // labelText: "Encourages or fails to discourage violence/abuse/terrorism/self-harm", + // }, + // { + // attributeName: "denigrates_a_protected_class", + // labelText: "Denigrates a protected class", + // }, + // { + // attributeName: "gives_harmful_advice", + // labelText: "Fails to follow the correct instruction / task", + // additionalExplanation: + // "The advice given in the output is harmful or counter-productive. This may be in addition to, but is distinct from the question about encouraging violence/abuse/terrorism/self-harm.", + // }, + // { + // attributeName: "expresses_moral_judgement", + // labelText: "Expresses moral judgement", + // }, ]; diff --git a/website/src/pages/api/set_label.ts b/website/src/pages/api/set_label.ts new file mode 100644 index 00000000..7ea589b0 --- /dev/null +++ b/website/src/pages/api/set_label.ts @@ -0,0 +1,55 @@ +import { getToken } from "next-auth/jwt"; +import prisma from "src/lib/prismadb"; + +/** + * Sets the Label in the Backend. + * + */ +const handler = async (req, res) => { + const token = await getToken({ req }); + + // Return nothing if the user isn't registered. + if (!token) { + res.status(401).end(); + return; + } + console.log(JSON.parse(req.body)); + // Parse out the local task ID and the interaction contents. + const { post_id, label_map, text } = await JSON.parse(req.body); + console.log(JSON.stringify( + { + "type": "text_labels", + "message_id": post_id, + "labels": label_map, + "text": text, + "user": { + "id": token.sub, + "display_name": token.name || token.email, + "auth_method": "local", + }})); + console.log("Here sending text_labels1..."); + // Send the interaction to the Text Label to the Backend. + const interactionRes = await fetch(`${process.env.FASTAPI_URL}/api/v1/text_labels`, { + method: "POST", + headers: { + "X-API-Key": process.env.FASTAPI_KEY, + "Content-Type": "application/json", + }, + body: JSON.stringify({ + "type": "text_labels", + "message_id": post_id, + "labels": label_map, + "text": text, + "user": { + "id": token.sub, + "display_name": token.name || token.email, + "auth_method": "local", + } + + }), + }); + console.log(interactionRes); + res.status(interactionRes.status).json(interactionRes.json()); +}; + +export default handler; From f5130b0fd8f0db4053e7306a84315164f9fb58a0 Mon Sep 17 00:00:00 2001 From: James Melvin Date: Sun, 8 Jan 2023 08:53:56 +0530 Subject: [PATCH 2/7] fix: new web route to update text_labels fix: minimum changes to textFlagLabels to keep it coherent with the backend --- website/src/components/FlaggableElement.tsx | 62 ++++++++++----------- website/src/pages/api/set_label.ts | 55 ++++++++++++++++++ 2 files changed, 86 insertions(+), 31 deletions(-) create mode 100644 website/src/pages/api/set_label.ts diff --git a/website/src/components/FlaggableElement.tsx b/website/src/components/FlaggableElement.tsx index c4fe5c58..82db4738 100644 --- a/website/src/components/FlaggableElement.tsx +++ b/website/src/components/FlaggableElement.tsx @@ -29,7 +29,7 @@ import useSWRMutation from "swr/mutation"; export const FlaggableElement = (props) => { const [isEditing, setIsEditing] = useBoolean(); - const { trigger } = useSWRMutation("/api/v1/text_labels", poster, { + const { trigger } = useSWRMutation("/api/set_label", poster, { onSuccess: () => { setIsEditing.off; }, @@ -184,40 +184,40 @@ interface textFlagLabels { const TEXT_LABEL_FLAGS: textFlagLabels[] = [ // For the time being this list is configured on the FE. // In the future it may be provided by the API. + // { + // attributeName: "fails_task", + // labelText: "Fails to follow the correct instruction / task", + // additionalExplanation: "__TODO__", + // }, + // { + // attributeName: "not_customer_assistant_appropriate", + // labelText: "Inappropriate for customer assistant", + // additionalExplanation: "__TODO__", + // }, { - attributeName: "fails_task", - labelText: "Fails to follow the correct instruction / task", - additionalExplanation: "__TODO__", - }, - { - attributeName: "not_customer_assistant_appropriate", - labelText: "Inappropriate for customer assistant", - additionalExplanation: "__TODO__", - }, - { - attributeName: "contains_sexual_content", + attributeName: "sexual_content", labelText: "Contains sexual content", }, { - attributeName: "contains_violent_content", + attributeName: "violence", labelText: "Contains violent content", }, - { - attributeName: "encourages_violence", - labelText: "Encourages or fails to discourage violence/abuse/terrorism/self-harm", - }, - { - attributeName: "denigrates_a_protected_class", - labelText: "Denigrates a protected class", - }, - { - attributeName: "gives_harmful_advice", - labelText: "Fails to follow the correct instruction / task", - additionalExplanation: - "The advice given in the output is harmful or counter-productive. This may be in addition to, but is distinct from the question about encouraging violence/abuse/terrorism/self-harm.", - }, - { - attributeName: "expresses_moral_judgement", - labelText: "Expresses moral judgement", - }, + // { + // attributeName: "encourages_violence", + // labelText: "Encourages or fails to discourage violence/abuse/terrorism/self-harm", + // }, + // { + // attributeName: "denigrates_a_protected_class", + // labelText: "Denigrates a protected class", + // }, + // { + // attributeName: "gives_harmful_advice", + // labelText: "Fails to follow the correct instruction / task", + // additionalExplanation: + // "The advice given in the output is harmful or counter-productive. This may be in addition to, but is distinct from the question about encouraging violence/abuse/terrorism/self-harm.", + // }, + // { + // attributeName: "expresses_moral_judgement", + // labelText: "Expresses moral judgement", + // }, ]; diff --git a/website/src/pages/api/set_label.ts b/website/src/pages/api/set_label.ts new file mode 100644 index 00000000..7ea589b0 --- /dev/null +++ b/website/src/pages/api/set_label.ts @@ -0,0 +1,55 @@ +import { getToken } from "next-auth/jwt"; +import prisma from "src/lib/prismadb"; + +/** + * Sets the Label in the Backend. + * + */ +const handler = async (req, res) => { + const token = await getToken({ req }); + + // Return nothing if the user isn't registered. + if (!token) { + res.status(401).end(); + return; + } + console.log(JSON.parse(req.body)); + // Parse out the local task ID and the interaction contents. + const { post_id, label_map, text } = await JSON.parse(req.body); + console.log(JSON.stringify( + { + "type": "text_labels", + "message_id": post_id, + "labels": label_map, + "text": text, + "user": { + "id": token.sub, + "display_name": token.name || token.email, + "auth_method": "local", + }})); + console.log("Here sending text_labels1..."); + // Send the interaction to the Text Label to the Backend. + const interactionRes = await fetch(`${process.env.FASTAPI_URL}/api/v1/text_labels`, { + method: "POST", + headers: { + "X-API-Key": process.env.FASTAPI_KEY, + "Content-Type": "application/json", + }, + body: JSON.stringify({ + "type": "text_labels", + "message_id": post_id, + "labels": label_map, + "text": text, + "user": { + "id": token.sub, + "display_name": token.name || token.email, + "auth_method": "local", + } + + }), + }); + console.log(interactionRes); + res.status(interactionRes.status).json(interactionRes.json()); +}; + +export default handler; From 81a47eeafdab65eacdd9e1bc7aa4678d1e8928b7 Mon Sep 17 00:00:00 2001 From: James Melvin Date: Sun, 8 Jan 2023 10:17:28 +0530 Subject: [PATCH 3/7] fix: Removed console logs. --- website/src/pages/api/set_label.ts | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/website/src/pages/api/set_label.ts b/website/src/pages/api/set_label.ts index 7ea589b0..970e1687 100644 --- a/website/src/pages/api/set_label.ts +++ b/website/src/pages/api/set_label.ts @@ -13,22 +13,10 @@ const handler = async (req, res) => { res.status(401).end(); return; } - console.log(JSON.parse(req.body)); - // Parse out the local task ID and the interaction contents. - const { post_id, label_map, text } = await JSON.parse(req.body); - console.log(JSON.stringify( - { - "type": "text_labels", - "message_id": post_id, - "labels": label_map, - "text": text, - "user": { - "id": token.sub, - "display_name": token.name || token.email, - "auth_method": "local", - }})); - console.log("Here sending text_labels1..."); - // Send the interaction to the Text Label to the Backend. + + // Parse out the local message_id, task ID and the interaction contents. + const { message_id, post_id, label_map, text } = await JSON.parse(req.body); + const interactionRes = await fetch(`${process.env.FASTAPI_URL}/api/v1/text_labels`, { method: "POST", headers: { @@ -37,7 +25,7 @@ const handler = async (req, res) => { }, body: JSON.stringify({ "type": "text_labels", - "message_id": post_id, + "message_id": message_id, "labels": label_map, "text": text, "user": { @@ -48,7 +36,6 @@ const handler = async (req, res) => { }), }); - console.log(interactionRes); res.status(interactionRes.status).json(interactionRes.json()); }; From c129ca9862336c687668da17d937e2f2ded9c325 Mon Sep 17 00:00:00 2001 From: James Melvin Date: Sun, 8 Jan 2023 10:20:38 +0530 Subject: [PATCH 4/7] fix: propagated message_id to FlaggableElement.tsx --- website/src/components/FlaggableElement.tsx | 2 +- website/src/components/Messages.tsx | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/website/src/components/FlaggableElement.tsx b/website/src/components/FlaggableElement.tsx index 82db4738..80d97a5b 100644 --- a/website/src/components/FlaggableElement.tsx +++ b/website/src/components/FlaggableElement.tsx @@ -42,7 +42,7 @@ export const FlaggableElement = (props) => { label_map.set(flag.attributeName, sliderValues[i]); } }); - trigger({ post_id: props.post_id, label_map: Object.fromEntries(label_map), text: props.text }); + trigger({ message_id: props.message_id, post_id: props.post_id, label_map: Object.fromEntries(label_map), text: props.text }); }; const [checkboxValues, setCheckboxValues] = useState(new Array(TEXT_LABEL_FLAGS.length).fill(false)); const [sliderValues, setSliderValues] = useState(new Array(TEXT_LABEL_FLAGS.length).fill(1)); diff --git a/website/src/components/Messages.tsx b/website/src/components/Messages.tsx index 7b69bc50..750d3946 100644 --- a/website/src/components/Messages.tsx +++ b/website/src/components/Messages.tsx @@ -7,14 +7,15 @@ import { FlaggableElement } from "./FlaggableElement"; export interface Message { text: string; is_assistant: boolean; + message_id: string; } export const Messages = ({ messages, post_id }: { messages: Message[]; post_id: string }) => { const items = messages.map((messageProps: Message, i: number) => { const { text } = messageProps; - + const { message_id } = messageProps; return ( - + ); From 3d469baaf55e4facf6782090ef97645a9862a575 Mon Sep 17 00:00:00 2001 From: James Melvin Date: Sun, 8 Jan 2023 10:26:32 +0530 Subject: [PATCH 5/7] fix: handling for 204 return status. Since text_labels API responds with no data --- website/src/pages/api/set_label.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/src/pages/api/set_label.ts b/website/src/pages/api/set_label.ts index 970e1687..202f30cd 100644 --- a/website/src/pages/api/set_label.ts +++ b/website/src/pages/api/set_label.ts @@ -36,7 +36,7 @@ const handler = async (req, res) => { }), }); - res.status(interactionRes.status).json(interactionRes.json()); + res.status(interactionRes.status).end(); }; export default handler; From d4fdaeca810aaedc20e9a5d152fe4c68cf602767 Mon Sep 17 00:00:00 2001 From: James Melvin Date: Sun, 8 Jan 2023 10:52:50 +0530 Subject: [PATCH 6/7] fix: pre-commit errors --- website/src/components/FlaggableElement.tsx | 7 ++++++- website/src/pages/api/set_label.ts | 23 ++++++++++----------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/website/src/components/FlaggableElement.tsx b/website/src/components/FlaggableElement.tsx index 80d97a5b..63370444 100644 --- a/website/src/components/FlaggableElement.tsx +++ b/website/src/components/FlaggableElement.tsx @@ -42,7 +42,12 @@ export const FlaggableElement = (props) => { label_map.set(flag.attributeName, sliderValues[i]); } }); - trigger({ message_id: props.message_id, post_id: props.post_id, label_map: Object.fromEntries(label_map), text: props.text }); + trigger({ + message_id: props.message_id, + post_id: props.post_id, + label_map: Object.fromEntries(label_map), + text: props.text, + }); }; const [checkboxValues, setCheckboxValues] = useState(new Array(TEXT_LABEL_FLAGS.length).fill(false)); const [sliderValues, setSliderValues] = useState(new Array(TEXT_LABEL_FLAGS.length).fill(1)); diff --git a/website/src/pages/api/set_label.ts b/website/src/pages/api/set_label.ts index 202f30cd..4db5ddaf 100644 --- a/website/src/pages/api/set_label.ts +++ b/website/src/pages/api/set_label.ts @@ -13,10 +13,10 @@ const handler = async (req, res) => { res.status(401).end(); return; } - + // Parse out the local message_id, task ID and the interaction contents. const { message_id, post_id, label_map, text } = await JSON.parse(req.body); - + const interactionRes = await fetch(`${process.env.FASTAPI_URL}/api/v1/text_labels`, { method: "POST", headers: { @@ -24,16 +24,15 @@ const handler = async (req, res) => { "Content-Type": "application/json", }, body: JSON.stringify({ - "type": "text_labels", - "message_id": message_id, - "labels": label_map, - "text": text, - "user": { - "id": token.sub, - "display_name": token.name || token.email, - "auth_method": "local", - } - + type: "text_labels", + message_id: message_id, + labels: label_map, + text: text, + user: { + id: token.sub, + display_name: token.name || token.email, + auth_method: "local", + }, }), }); res.status(interactionRes.status).end(); From e39602cf7c817bd449db8b176c62ed61085728d6 Mon Sep 17 00:00:00 2001 From: James Melvin Date: Sun, 8 Jan 2023 11:19:01 +0530 Subject: [PATCH 7/7] fix: review comments and frontend build issue in label_initial_prompt.tsx due to previous message_id changes --- website/src/components/Messages.tsx | 4 ++-- website/src/pages/label/label_initial_prompt.tsx | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/website/src/components/Messages.tsx b/website/src/components/Messages.tsx index 750d3946..226c6154 100644 --- a/website/src/components/Messages.tsx +++ b/website/src/components/Messages.tsx @@ -12,8 +12,8 @@ export interface Message { export const Messages = ({ messages, post_id }: { messages: Message[]; post_id: string }) => { const items = messages.map((messageProps: Message, i: number) => { - const { text } = messageProps; const { message_id } = messageProps; + const { text } = messageProps; return ( @@ -24,7 +24,7 @@ export const Messages = ({ messages, post_id }: { messages: Message[]; post_id: return {items}; }; -export const MessageView = ({ is_assistant, text }: Message) => { +export const MessageView = ({ is_assistant, text, message_id }: Message) => { const { colorMode } = useColorMode(); const bgColor = useMemo(() => { diff --git a/website/src/pages/label/label_initial_prompt.tsx b/website/src/pages/label/label_initial_prompt.tsx index 0c3b47be..e400e8fd 100644 --- a/website/src/pages/label/label_initial_prompt.tsx +++ b/website/src/pages/label/label_initial_prompt.tsx @@ -43,7 +43,7 @@ const LabelInitialPrompt = () => { <>
Label Initial Prompt

Provide labels for the following prompt

- +