css 聊天GPT API请求问题

4dc9hkyq  于 2023-02-10  发布在  其他
关注(0)|答案(1)|浏览(302)

因此,我基本上尝试创建一个简历生成网站和聊天GPT之间的组合。我使用聊天GPT本身为我提供使用API的文档,但它一直给我一个403错误请求,并显示以下错误消息:

{
    "error": {
        "message": "You didn't provide an API key. You need to provide your API key in an Authorization header using Bearer auth (i.e. Authorization: Bearer YOUR_KEY), or as the password field (with blank username) if you're accessing the API from your browser and are prompted for a username and password. You can obtain an API key from https://platform.openai.com/account/api-keys.",
        "type": "invalid_request_error",
        "param": null,
        "code": null
    }
}

这里是我的代码的完整摘要,目前正在工作,只是让功能工作:

<!DOCTYPE html>
<html lang="en">
  <head>
    <link rel="stylesheet" href="style.css" />
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>CV GPT</title>
  </head>
  <body>
    <header>
      <h1>CV GPT</h1>
      <nav>
        <ul>
          <li><a href="#">Home</a></li>
          <li><a href="#">About</a></li>
          <li><a href="#">Contact</a></li>
        </ul>
      </nav>
    </header>

    <div class="container">
      <h2>CV Generator</h2>
      <p>Please enter your previous role:</p>

      <form>
        <input type="text" id="inputText" />
        <input type="submit" value="Submit" />
      </form>

      <p id="response"></p>
    </div>

    <footer></footer>

    <script>
      const inputText = document.getElementById("inputText");
      const response = document.getElementById("response");

      const startPromt =
        "List skills that you would have from working in this role: " +
        inputText.value;

      const form = document.querySelector("form");

      form.addEventListener("submit", function (event) {
        event.preventDefault();
        generateText(startPromt);
      });

      async function generateText(prompt) {
        const apiKey = "MY-API-KEY-HERE";
        const response = await fetch(
          `https://api.openai.com/v1/engines/text-davinci-002/jobs`,
          {
            method: "POST",
            headers: {
              "Content-Type": "application/json",
              Authorization: `Bearer ${apiKey}`,
              "Access-Control-Allow-Origin": "*",
            },
            body: JSON.stringify({
              prompt: prompt,
              max_tokens: 100,
              n: 1,
              stop: null,
              temperature: 0.5,
            }),
          }
        );

        if (!response.ok) {
          console.error("Request failed with status code: " + response.status);
          return;
        }

        const responseJson = await response.json();
        const generatedText = responseJson.choices[0].text;
        response.innerHTML = generatedText;
      }
    </script>
  </body>
</html>

显然,我必须拿出我的API密钥,但你得到的笑话。
因此,我已经仔细检查了OpenAI文档中的上述实现,我似乎以正确的方式调用了API,并检查了我使用了正确的API密钥,检查愚蠢的东西,如可能丢失字符时,复制粘贴,但没有。删除我以前的关键,并作出了一个新的只是以防万一,这是问题,但仍然得到相同的错误信息,在开发工具。 Jmeter 板也显示了我的请求,但没有任何变化。
会很感激任何帮助!

fnx2tebb

fnx2tebb1#

您需要包括https://platform.openai.com/account/api-keys中的API密钥
将此密钥作为可公开访问的html的一部分公开是一个非常糟糕的主意,任何人都可以使用它来对抗您的帐户配额,并最终计费。
最好的方法是用你知道的任何语言创建一个REST后端,在那里嵌入密钥并调用聊天gpt,然后你的页面可以发送一个REST调用到你的won后端来获得结果。

相关问题