def2b837 by Jonille Arreglo

28786_moderation

1 parent 27441a3a
...@@ -47,10 +47,10 @@ function App() { ...@@ -47,10 +47,10 @@ function App() {
47 let model_list = []; 47 let model_list = [];
48 for( var i = 1; i < data.models.data.length; i++ ) { 48 for( var i = 1; i < data.models.data.length; i++ ) {
49 let model = data.models.data[i]; 49 let model = data.models.data[i];
50 if( !(model.id == "whisper-1" 50 if( !(model.id === "whisper-1"
51 || model.id == "gpt-4" 51 || model.id === "gpt-4"
52 || model.id == "gpt-4-0314" 52 || model.id === "gpt-4-0314"
53 || model.id == "gpt-4-0613") ) model_list.push(model); 53 || model.id === "gpt-4-0613") ) model_list.push(model);
54 } 54 }
55 setModels(model_list) 55 setModels(model_list)
56 }) 56 })
...@@ -86,14 +86,14 @@ function App() { ...@@ -86,14 +86,14 @@ function App() {
86 } 86 }
87 } 87 }
88 88
89 let chatLogNew = [...chatLog, { user: "me", message: `${userModifiedInput}`} ] 89 let chatLogNew = [...chatLog, { user: "user", message: `${userModifiedInput}`} ]
90 setChatInput(""); 90 setChatInput("");
91 setChatLog(chatLogNew) 91 setChatLog(chatLogNew)
92 92
93 const userMessage = { user: "gpt", message: "..." }; 93 const userMessage = { user: "gpt", message: "..." };
94 setChatLog(prevChatLog => [...prevChatLog, userMessage]); 94 setChatLog(prevChatLog => [...prevChatLog, userMessage]);
95 95
96 var messages = chatLogNew.map((message) => message.message).join("\n") 96 var messages = chatLogNew.map((message) => { if(message.user !== 'me') return message.message }).join("\n")
97 if(currentModel == GPTTurbo || currentModel == GPTTurbo0301) { 97 if(currentModel == GPTTurbo || currentModel == GPTTurbo0301) {
98 // "gpt-3.5-turbo" 98 // "gpt-3.5-turbo"
99 let chatLogTurboNew = [...chatLogTurbo, { role: "user", content: chatInput }]; 99 let chatLogTurboNew = [...chatLogTurbo, { role: "user", content: chatInput }];
...@@ -116,7 +116,16 @@ function App() { ...@@ -116,7 +116,16 @@ function App() {
116 const data = await response.json(); 116 const data = await response.json();
117 const parsedData = data.message.trim(); 117 const parsedData = data.message.trim();
118 // "gpt-3.5-turbo" 118 // "gpt-3.5-turbo"
119 let chatLogTurboNew = [...chatLogTurbo, { role: "assistant", content: parsedData }]; 119 let chatLogTurboNew = chatLogTurbo;
120 if(data.success === false) {
121 setChatLog(prevChatLog => {
122 const lastMsg = prevChatLog[prevChatLog.length - 2];
123 return [...prevChatLog.slice(0, prevChatLog.length - 2), { user: "me", message: lastMsg.message }];
124 });
125 userModifiedInput = "";
126 }
127 chatLogTurboNew.push({ role: "user", content: userModifiedInput });
128 chatLogTurboNew.push({ role: "assistant", content: parsedData });
120 setChatLogTurbo(chatLogTurboNew); 129 setChatLogTurbo(chatLogTurboNew);
121 // 130 //
122 clearInterval(intervalId); 131 clearInterval(intervalId);
......
...@@ -57,6 +57,19 @@ app.post('/api', async (req, res) => { ...@@ -57,6 +57,19 @@ app.post('/api', async (req, res) => {
57 query_prompt = arr_body.join("\n") 57 query_prompt = arr_body.join("\n")
58 } 58 }
59 } 59 }
60 const moderation = await axios.post("https://api.openai.com/v1/moderations", {
61 input: query_prompt
62 }, { headers: { 'content-type': 'application/json', 'Authorization': `Bearer ${process.env.OPENAI_API_KEY}` } });
63
64 if(moderation.data.results[0].flagged) {
65 res.json({
66 success: false,
67 message: "I'm sorry, but I can't assist with that. We want everyone to use our tool safely and responsibly.\nIf you have any other questions or need advice on a different topic, feel free to ask."
68 });
69 res.end();
70 return;
71 }
72
60 try { 73 try {
61 const response = await openai.createCompletion({ 74 const response = await openai.createCompletion({
62 model: `${currentModel}`,// "text-davinci-003", 75 model: `${currentModel}`,// "text-davinci-003",
...@@ -93,6 +106,20 @@ async function runGPTTurbo(req, res) { ...@@ -93,6 +106,20 @@ async function runGPTTurbo(req, res) {
93 // "gpt-3.5-turbo" 106 // "gpt-3.5-turbo"
94 const { message, currentModel, temperature } = req.body; 107 const { message, currentModel, temperature } = req.body;
95 var input = ''; 108 var input = '';
109 const message_history = JSON.parse(message);
110 const query_prompt = message_history.length ? message_history[message_history.length - 1].content : "";
111 const moderation = await axios.post("https://api.openai.com/v1/moderations", {
112 input: query_prompt
113 }, { headers: { 'content-type': 'application/json', 'Authorization': `Bearer ${process.env.OPENAI_API_KEY}` } });
114
115 if(moderation.data.results[0].flagged) {
116 res.json({
117 success: false,
118 message: "I'm sorry, but I can't assist with that. We want everyone to use our tool safely and responsibly.\nIf you have any other questions or need advice on a different topic, feel free to ask."
119 });
120 res.end();
121 return;
122 }
96 try { 123 try {
97 const response = await openai.createChatCompletion({ 124 const response = await openai.createChatCompletion({
98 model: `${currentModel}`, 125 model: `${currentModel}`,
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!