3

I am creating an app whereby i utilise the openai api to craft an itinerary based on user inputs. The issue I am facing is that when i run the app on expo and click the button to generate, I keep getting the following error in my log :

ERROR 404 ERROR Invalid URL (POST /v1/completions/) ERROR null ERROR invalid_request_error

I am unsure as to what causes the 404 because the openai component does the fetching. Thank you in advance !

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({})

I tried to maybe add a base url but nonetheless I get the same issue. I did use openai api for a react project and it seemed to work fine.

1 Answer 1

0

OpenAI API not support to call from React Native to protect your API key, you can call API in your server. Hope it help Info here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.