diff --git a/src/App.js b/src/App.js index 2ffbc75..8669068 100644 --- a/src/App.js +++ b/src/App.js @@ -1,5 +1,5 @@ -import ResponseSection from './components/ResponseSection'; import './App.css'; +import ResponseSection from './components/ResponseSection'; import { useState } from "react"; function App() { diff --git a/src/components/ResponseSection.js b/src/components/ResponseSection.js index 414c927..9bdb2a3 100644 --- a/src/components/ResponseSection.js +++ b/src/components/ResponseSection.js @@ -1,7 +1,7 @@ import { useState } from "react"; export default function ResponseSection({ userInput }) { - let secret = process.env.API_SECRET; + let secret = process.env.REACT_APP_API_SECRET; const [resCount, setResCount] = useState(1); const [contents, setContents] = useState([ @@ -18,13 +18,23 @@ export default function ResponseSection({ userInput }) { ]); - const help = () => { - console.log(process.env.API_SECRET); - } - const addNewResponse = async () => { + let data = { prompt: userInput }; + + let result = await fetch("https://api.openai.com/v1/engines/text-curie-001/completions", { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${secret}` + }, + body: JSON.stringify(data) + }).then(res => res.json()); + + let AIresponse = result.choices[0].text; + let newState = contents; - let newCount = newState.unshift( + let newCount; + AIresponse && (newCount = newState.unshift(

Prompt:

@@ -33,33 +43,21 @@ export default function ResponseSection({ userInput }) {

Response:

-

.......

+

{AIresponse}

- ); + )); setResCount(newCount); setContents(newState); - - let data = { prompt: userInput }; - - await fetch("https://api.openai.com/v1/engines/text-curie-001/completions", { - method: "POST", - headers: { - "Content-Type": "application/json", - Authorization: `Bearer ${secret}` - }, - body: JSON.stringify(data) - }).then(x => console.log(x)); } return ( - <> - - -
- {contents} +
+ +
+ {contents} +
- ) } \ No newline at end of file