javascript 打开AI错误,返回429和不安全标头

f45qwnt8  于 2023-02-18  发布在  Java
关注(0)|答案(1)|浏览(210)

所以这是代码不知道为什么不开放的人工智能不工作。请让我知道谢谢下面是代码。

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;

[一个月一次](一个月一次)
因此,我试图提供一个提示,用户将键入文本形式,并在单击预览按钮,他将收到sumarry backk

bkhjykvo

bkhjykvo1#

你使用的是openai npm软件包,在这种情况下你不需要发送请求头,你会注意到在你的请求头中你正在尝试设置API密钥。
除非您已经使用配置变量设置了API密钥。
我已经重写了你的代码的开头,它对我很有效。

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端点,您将只使用头,但是因为您使用的是NPM包,它将直接为您设置头。
通过从代码中删除标头,您应该不会收到不安全标头警报。
注意,我还修改了响应的控制台日志,为了得到实际的响应文本,必须进入响应对象的几个层:

response.data.choices[0].text

相关问题