From 2f81a88b03d64a678e5c21561c5d7759f7bfeda1 Mon Sep 17 00:00:00 2001 From: Mikayla Dobson <93477693+innocuous-symmetry@users.noreply.github.com> Date: Wed, 18 May 2022 08:43:45 -0500 Subject: [PATCH] properly renders new responses --- src/App.css | 4 ++ src/App.js | 10 ++--- src/components/GPT3.js | 18 ++++++++ src/components/ResponseSection.js | 69 +++++++++++++++++-------------- src/components/SubmitSection.js | 19 +++------ 5 files changed, 68 insertions(+), 52 deletions(-) create mode 100644 src/components/GPT3.js diff --git a/src/App.css b/src/App.css index 8b4e0b9..b442033 100644 --- a/src/App.css +++ b/src/App.css @@ -62,6 +62,10 @@ button:active { flex-direction: column; } +select { + border-radius: 5px; +} + /* Response section styles */ #responses { diff --git a/src/App.js b/src/App.js index b6a95ec..1ad6c34 100644 --- a/src/App.js +++ b/src/App.js @@ -1,4 +1,4 @@ -import { useCallback, useState, useRef, useEffect } from "react"; +import { useCallback, useState, useRef } from "react"; import ResponseSection from './components/ResponseSection'; import './App.css'; import SubmitSection from "./components/SubmitSection"; @@ -7,7 +7,7 @@ function App() { const [userInput, setUserInput] = useState(''); const [response, setResponse] = useState(null); - const responseRef = useRef(null); + const responseRef = useRef(); const promptCallback = useCallback((userPrompt) => { setUserInput(userPrompt); @@ -17,10 +17,6 @@ function App() { setResponse(APIresponse); }, []); - useEffect(() => { - response && responseRef.current(); - }, [response]); - return (

Fun with AI

@@ -33,7 +29,7 @@ function App() { - +
); } diff --git a/src/components/GPT3.js b/src/components/GPT3.js new file mode 100644 index 0000000..dc5f029 --- /dev/null +++ b/src/components/GPT3.js @@ -0,0 +1,18 @@ +export const getResponse = async (input) => { + let data = { prompt: input }; + + let response = await fetch("https://api.openai.com/v1/engines/text-curie-001/completions", { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${process.env.REACT_APP_API_SECRET}` + }, + body: JSON.stringify(data) + }).then(res => res.json()); + + console.log(response); + + let text = response.choices[0].text; + + return text; +} diff --git a/src/components/ResponseSection.js b/src/components/ResponseSection.js index 49180e7..0efb5c1 100644 --- a/src/components/ResponseSection.js +++ b/src/components/ResponseSection.js @@ -1,42 +1,47 @@ -import { useEffect, useState } from "react"; +import { useState, useEffect } from "react"; export default function ResponseSection({ userInput, response, responseRef }) { const [resCount, setResCount] = useState(1); - - const [contents, setContents] = useState([ -
-

Prompt:

-

Your prompt will appear here...

-

Response:

-

... and the AI response will appear here!

-
- ]); - - const appendResponse = () => { - let newState = contents; - let newCount; - response && (newCount = setContents(newState.unshift( -
-

Prompt:

-

{userInput}

-

Response:

-

{response}

-
- ))); - - setResCount(newCount); - - console.log(contents); - } + const [contents, setContents] = useState([]); + const [responseToRender, setResponseToRender] = useState(null); useEffect(() => { - responseRef.current = appendResponse; - }, []); + response && setResponseToRender(response); + }, [response]); + + useEffect(() => { + if (responseToRender) { + setContents((prev) => { + return [ +
+

Prompt:

+

{userInput}

+

Response:

+

{responseToRender}

+
, + ...prev] + }); + setResCount(resCount + 1); + setResponseToRender(null); + } + }, [responseToRender, contents, resCount, userInput]); + + useEffect(() => { + console.log(contents); + }, [contents]); return (

Responses

- {contents} +
+ {contents.length > 0 ? contents : null} +
+

Prompt:

+

Your prompt will appear here...

+

Response:

+

... and the AI response will appear here!

+
+
- ) -} \ No newline at end of file + ); +} diff --git a/src/components/SubmitSection.js b/src/components/SubmitSection.js index b2dc404..bf0bcc4 100644 --- a/src/components/SubmitSection.js +++ b/src/components/SubmitSection.js @@ -1,16 +1,9 @@ -export default function SubmitSection({ promptCallback, userInput, responseCallback }) { - const getResponse = async () => { - let data = { prompt: userInput }; +import { getResponse } from "./GPT3.js"; - await fetch("https://api.openai.com/v1/engines/text-curie-001/completions", { - method: "POST", - headers: { - "Content-Type": "application/json", - Authorization: `Bearer ${process.env.REACT_APP_API_SECRET}` - }, - body: JSON.stringify(data) - }).then(res => res.json()) - .then(x => responseCallback(x.choices[0].text)); +export default function SubmitSection({ promptCallback, userInput, responseCallback }) { + const createNewEntry = async () => { + if (!userInput) return; + await getResponse(userInput).then((x) => responseCallback(x)); } return ( @@ -24,7 +17,7 @@ export default function SubmitSection({ promptCallback, userInput, responseCallb - + ) }