我正在创建一个应用程序,利用openai API根据用户输入制作行程。我面临的问题是,当我在expo上运行应用程序并单击按钮生成时,我的日志中不断出现以下错误:
错误404 Error
我不确定是什么原因导致的404,因为openai组件做抓取。提前谢谢你!
import { StyleSheet, Text, TextInput, TouchableOpacity, View } from 'react-native'
import React, { useState } from 'react'
import OpenAI from 'openai';
const Home = () => {
const [result, setResult] = useState('');
const openai = new OpenAI({
apiKey: 'my api key goes here'
});
const promptGen = (numOfPeople, location, startDate, endDate, address, tripType, dietaryReq, isDisabled, activities, budget) => {// returns a crafted prompt based on the inputs}
const generate = async () => {
try {
const generatedPrompt = promptGen('3','Bali','10 September 2023', '15 september 2023', 'Sp, Jl. Camplung Tanduk Jl. Dhyana Pura No.103, Seminyak, Kuta, Badung Regency, Bali 80361, Indonesia', 'Single trip', 'Halal', false, 'Hiking, beach, nightclubs', '500'); // I pass this as the prompt
console.log(generatedPrompt);
const res = await openai.completions.create({
model: "text-davinci-003",
prompt: generatedPrompt,
max_tokens: 3000
});
console.log(res);
setResult(res.choices[0].text);
} catch (error) {
if (error instanceof OpenAI.APIError) {
console.error(error.status); // e.g. 401
console.error(error.message); // e.g. The authentication token you passed was invalid...
console.error(error.code); // e.g. 'invalid_api_key'
console.error(error.type); // e.g. 'invalid_request_error'
} else {
// Non-API error
console.log(error);
}
}
}
return (
<View>
<Text>itinerary Gen</Text>
<View>
<TouchableOpacity onPress={generate}>
<Text>Generate itinerary</Text>
</TouchableOpacity>
<Text>{result}</Text>
</View>
</View>
)
}
export default Home
const styles = StyleSheet.create({})
字符串
我试着添加一个基本URL,但我还是遇到了同样的问题。我确实在一个React项目中使用了OpenAI API,它似乎工作得很好。
1条答案
按热度按时间rfbsl7qr1#
OpenAI API不支持从React Native调用来保护你的API密钥,你可以在你的服务器上调用API。希望对你有帮助。