如何保存从API到状态的响应,以便显示旧的搜索/响应?
这是我要做的事情:首先,我想显示我从API中收到的响应(我可以这样做),但是我想显示先前对API的先前的提示和响应。
import { useEffect, useState } from "react";
import Navbar from "./components/navbar/Navbar";
import Form from "./components/form/Form";
import Ideas from "./pages/Ideas";
import "./app.scss";
const { Configuration, OpenAIApi } = require("openai");
export default function App() {
const [companyType, setCompanyType] = useState("");
const [prompt, setPrompt] = useState("");
const [response, setResponse] = useState("");
const [list, setList] = useState({});
useEffect(() => {
console.log("use effect ran");
}, []);
const configuration = new Configuration({
apiKey: process.env.REACT_APP_API_KEY,
});
const openai = new OpenAIApi(configuration);
const searchForProductIdeas = (event) => {
event.preventDefault();
openai
.createCompletion("text-davinci-002", {
prompt: `List new product ideas for a ${companyType} company.`,
temperature: 1,
max_tokens: 256,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
})
.then((response) => {
// creates a variable to store response in
const idea = response.data.choices[0].text;
//sets the prompt
setPrompt(`List new product ideas for a ${companyType} company.`);
//sets the response
setResponse(idea);
// set the previous prompts and responses
setList({...prompt, ...idea})
});
};
return (
<div className="app">
<Navbar />
<div>{prompt}</div>
<div>{response}</div>
<div>{list}</div>
</div>
);
}
欢迎任何帮助。还想知道我是否需要axios供OpenAI API呼叫?
Here's what I'm trying to do: First, I want to display the response I receive from the API (I can do that), but then I want to display the previous prompts and responses from prior calls to the API.
import { useEffect, useState } from "react";
import Navbar from "./components/navbar/Navbar";
import Form from "./components/form/Form";
import Ideas from "./pages/Ideas";
import "./app.scss";
const { Configuration, OpenAIApi } = require("openai");
export default function App() {
const [companyType, setCompanyType] = useState("");
const [prompt, setPrompt] = useState("");
const [response, setResponse] = useState("");
const [list, setList] = useState({});
useEffect(() => {
console.log("use effect ran");
}, []);
const configuration = new Configuration({
apiKey: process.env.REACT_APP_API_KEY,
});
const openai = new OpenAIApi(configuration);
const searchForProductIdeas = (event) => {
event.preventDefault();
openai
.createCompletion("text-davinci-002", {
prompt: `List new product ideas for a ${companyType} company.`,
temperature: 1,
max_tokens: 256,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
})
.then((response) => {
// creates a variable to store response in
const idea = response.data.choices[0].text;
//sets the prompt
setPrompt(`List new product ideas for a ${companyType} company.`);
//sets the response
setResponse(idea);
// set the previous prompts and responses
setList({...prompt, ...idea})
});
};
return (
<div className="app">
<Navbar />
<div>{prompt}</div>
<div>{response}</div>
<div>{list}</div>
</div>
);
}
Any and all help is welcome. Also wondering if I need to have axios for openai API calls?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能想将上一个搜索词和结果对存储在数组中。当发出新请求时,将当前
提示>提示>提示>
响应状态>状态>状态>列表状态数组。例子:
You probably want to store the previous search term and result pairs in an array. Move the current
prompt
andresponse
state into thelist
state array when a new request is made.Example:
因此,我假设当我现在查看您的代码时,该行:
不起作用,因为您的问题是如何显示旧请求列表。
我要做的是将列表更改为对象的数组:
我将您的提示和响应添加为内部对象。沿线的内容:
渲染您将使用地图功能:
So i assume when i look at your code that right now the line :
Is not working since your question is how do i show the list of old requests.
What i would do is change the list to be an array of object instead :
And i would add your Prompt and response as an object inside. Something along the line of :
And to render that you would use the map function :