如何保存从API到状态的响应,以便显示旧的搜索/响应?

发布于 2025-01-28 21:37:08 字数 1772 浏览 4 评论 0原文

这是我要做的事情:首先,我想显示我从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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

网名女生简单气质 2025-02-04 21:37:08

您可能想将上一个搜索词和结果对存储在数组中。当发出新请求时,将当前提示>提示>提示>响应状态>状态>状态>列表状态数组。

例子:

export default function App() {
  const [companyType, setCompanyType] = useState("");
  const [prompt, setPrompt] = useState("");
  const [response, setResponse] = useState("");
  const [list, setList] = useState([]); // <-- array

  ...

  const searchForProductIdeas = (event) => {
    event.preventDefault();

    openai
      .createCompletion("text-davinci-002", {
        ...
      })
      .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
        if (prompt && response) {
          setList(list => [...list, { prompt, response }]); // <-- append into new array
        }
      });
  };

  return (
    <div className="app">
      <Navbar />
      
      <div>{prompt}</div>
      <div>{response}</div>
      <div>
        <ul>
          <p>Previous prompts and responses</p>
          {list.map(({ prompt, response }, i) => ( // <-- map previous entries
            <li key={i}>
              <p>{prompt}</p>
              <p>{response}</p>
            </li>
          ))}
        </ul>
      </div>
    </div>
  );
}

You probably want to store the previous search term and result pairs in an array. Move the current prompt and response state into the list state array when a new request is made.

Example:

export default function App() {
  const [companyType, setCompanyType] = useState("");
  const [prompt, setPrompt] = useState("");
  const [response, setResponse] = useState("");
  const [list, setList] = useState([]); // <-- array

  ...

  const searchForProductIdeas = (event) => {
    event.preventDefault();

    openai
      .createCompletion("text-davinci-002", {
        ...
      })
      .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
        if (prompt && response) {
          setList(list => [...list, { prompt, response }]); // <-- append into new array
        }
      });
  };

  return (
    <div className="app">
      <Navbar />
      
      <div>{prompt}</div>
      <div>{response}</div>
      <div>
        <ul>
          <p>Previous prompts and responses</p>
          {list.map(({ prompt, response }, i) => ( // <-- map previous entries
            <li key={i}>
              <p>{prompt}</p>
              <p>{response}</p>
            </li>
          ))}
        </ul>
      </div>
    </div>
  );
}
峩卟喜欢 2025-02-04 21:37:08

因此,我假设当我现在查看您的代码时,该行:

<div>{list}</div>

不起作用,因为您的问题是如何显示旧请求列表。

我要做的是将列表更改为对象的数组:

const [list, setList] = useState([]);

我将您的提示和响应添加为内部对象。沿线的内容:

const idea = response.data.choices[0].text;
const newPrompt = `List new product ideas for a ${companyType} company.`;
setPrompt(newPrompt);
setResponse(idea);

setList([
...list,
{ 
prompt: newPrompt
response: idea
}]);

渲染您将使用地图功能:

{
list.map((item) => (
<>
   <div> item.prompt </div
   <div> item.response </div>
</>
))}``

So i assume when i look at your code that right now the line :

<div>{list}</div>

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 :

const [list, setList] = useState([]);

And i would add your Prompt and response as an object inside. Something along the line of :

const idea = response.data.choices[0].text;
const newPrompt = `List new product ideas for a ${companyType} company.`;
setPrompt(newPrompt);
setResponse(idea);

setList([
...list,
{ 
prompt: newPrompt
response: idea
}]);

And to render that you would use the map function :

{
list.map((item) => (
<>
   <div> item.prompt </div
   <div> item.response </div>
</>
))}``
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文