gpt4 book ai didi

javascript - 打开 AI 错误返回 429 和不安全的 header

转载 作者:行者123 更新时间:2023-12-02 05:48:02 24 4
gpt4 key购买 nike

所以这是不确定为什么开放式 AI 不工作的代码。请让我知道谢谢下面是代码。

import PropTypes from 'prop-types'
import React , {useState} from 'react'
const { Configuration, OpenAIApi } = require("openai");



function TextForm(props){
const key = //key
const configuration = new Configuration({
apiKey: key,
});
const openai = new OpenAIApi(configuration);


const [text,setText] = useState("Enter text here");
const [wordCount,setWordCount] = useState(text.length);
const [responseText,setResponseText] = useState("");

const header = {
'Content-Type': 'application/json',
'Authorization': 'Bearer //key',//also not sure what Bearer is
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36',
}

const previewCase = async (event) => {
event.preventDefault();
const response = await openai.createCompletion({
model: "text-davinci-003",
prompt: "Say this is a test",
temperature: 0,
max_tokens: 7,
headers: header,
}).then((response) => {console.log(response.data);}).catch((error) => {console.log(error);});
setResponseText(response.data);
}

const convertToUpperCase = (event) => {
event.preventDefault()
console.log("Converting to upper case...");
setText(text.toUpperCase());


};

const convertToLowerCase = (event) => {
event.preventDefault()
console.log("Converting to lower case...");
setText(text.toLowerCase());

};

const handleManageCount = (event) => {
setText(event.target.value)
const Array = text.split(" ");
setWordCount(Array.length);
if (text.length === 0){
setWordCount(0);
}
}
return (
<>
<form>
<div className="row d-flex justify-content-center">
<div className="form-group col-lg-8 mt-3" >
<label htmlFor="exampleFormControlTextarea1 row"><h3>{props.title}</h3></label>
<textarea className="form-control mt-3" id="exampleFormControlTextarea1" rows="20" value={text} onChange={handleManageCount}></textarea>
<h6 className='mt-3'><span>Word:</span>{wordCount}</h6>
<div className=''>
<button className='btn btn-secondary mt-3 mx-2' onClick={convertToUpperCase} >Convert to Upper case</button>
<button className='btn btn-secondary mt-3 mx-2' onClick={convertToLowerCase}>Convert to Lower case</button>
<button className='btn btn-secondary mt-3 mx-2' onClick={previewCase}>Preview Sumarry</button>
</div>

</div>

</div>

</form>

<div className="container">
<p><h1>
Preview Summary
</h1>
{responseText}
</p>
</div>



</>
)
TextForm.propTypes = {title:PropTypes.string.isRequired};
TextForm.defaultProps = {title : "Write Here!"};
}

export default TextForm;

[ enter image description here ]( /image/PvMb1.png )

所以我试图为它提供一个提示,用户将在文本表单上键入,并在单击预览按钮后他将收到摘要返回

最佳答案

您正在使用 openai npm 包。在这种情况下,您不需要随请求发送 header 。您会在 header 中注意到您正在尝试设置 API key 。

除非您已经使用配置变量设置了 API key 。

我已经重写了您代码的开头,它对我有用。

function TextForm(props){
const key = //key
const configuration = new Configuration({
apiKey: key,
});
const openai = new OpenAIApi(configuration);


const [text,setText] = useState("Enter text here");
const [wordCount,setWordCount] = useState(text.length);
const [responseText,setResponseText] = useState("");

const previewCase = async (event) => {
event.preventDefault();
const response = await openai.createCompletion({
model: "text-davinci-003",
prompt: "Say this is a test",
temperature: 0,
max_tokens: 7
}).then((response) => {console.log(response.data.choices[0].text);}).catch((error) => {console.log(error);});
setResponseText(response.data.choices[0].text);
}

如果您直接使用 API 端点,您只会使用 header ,但因为您使用的是 NPM 包,它会直接为您设置 header 。

通过从您的代码中删除 header ,您应该不会收到不安全 header 警报。

请注意,我还修复了您回复的控制台日志。为了获得实际的响应文本,您必须进入响应对象的多个层:

response.data.choices[0].text

关于javascript - 打开 AI 错误返回 429 和不安全的 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75465228/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com