[错误:无效的URL(POST /v1/chat/completions/)]:React Native/ChatGPT API in Android problem

wgeznvg7  于 6个月前  发布在  React
关注(0)|答案(1)|浏览(108)

我正在使用Expo在React Native上构建一个简单的ChatGPT API连接,当我在Web浏览器中运行应用程序时没有问题,当我通过Expo Go在Android上运行时出现问题,我得到了这个错误:[错误:无效的URL(POST /v1/chat/completions/)]
Error at running React Native App in Android with Expo Go
App running on Web
我不知道这是相关的,但我得到了这个错误时,在网络上运行(它仍然工作)
Error when running App on Web Browser
这是连接的源代码:

import React, { useState } from 'react';
import { View, Text, TextInput, Button, StyleSheet, Alert } from 'react-native';
import OpenAI from "openai";
import theme from '../theme';

const ChatGPT = () => {

    const openai = new OpenAI({
        apiKey: (here goes my api key),
        dangerouslyAllowBrowser: true
    });
    
    
  const [input, setInput] = useState('');
  const [messages, setMessages] = useState([]);

  const sendMessage = async () => {
    if (!input) return;
  
    // Send the user's message to the ChatGPT API
    try {
      const response = await openai.chat.completions.create({
        model: "gpt-3.5-turbo",
        messages: [
          {
            "role": "user",
            "content": input
          },
        ],
        temperature: 1,
        max_tokens: 256,
        top_p: 1,
        frequency_penalty: 0,
        presence_penalty: 0,
      });
      console.log(response);
      setMessages([...messages, { text: input, user: true }, { text: response.choices[0].message.content, user: false }]);
      setInput('');
    } catch (error) {
      console.log(error)
    }
    
    
    // Add the user's message and the AI's response to the chat
    
  };

  return (
    <View style={{marginTop: 40}}>
      <View>
        {
          //Adds the message
        messages.map((message, index) => (
          <View style>
            <Text key={index} style={styles.chatContainer}>
              {message.text}
            </Text>
          </View>
          
        ))}
      </View>
      <View>
        <TextInput
          value={input}
          onChangeText={setInput}
          placeholder="Escriba su consulta..."
        />
        <Button title="Send" onPress={sendMessage} />
      </View>
    </View>
  );
};

const styles = StyleSheet.create({
    chatContainer: {
      flex: 1,
      backgroundColor: theme.colors.backgroundPrimary,
      alignItems: 'center',
      justifyContent: 'center',
      color: theme.colors.textPrimary
    },
    text: {
        color: "#fff"
    }
  });

export default ChatGPT;

字符串
如果需要,这是我的package.json

{
  "name": "pisapp-app",
  "version": "1.0.0",
  "main": "node_modules/expo/AppEntry.js",
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "web": "expo start --web"
  },
  "dependencies": {
    "@expo/webpack-config": "^19.0.0",
    "@react-navigation/drawer": "^6.6.3",
    "@react-navigation/native": "^6.1.7",
    "@react-navigation/native-stack": "^6.9.13",
    "@react-navigation/stack": "^6.3.17",
    "@types/react": "~18.2.14",
    "axios": "^1.5.0",
    "dotenv": "^16.3.1",
    "expo": "~49.0.8",
    "expo-status-bar": "~1.6.0",
    "firebase": "^10.4.0",
    "openai": "^4.4.0",
    "react": "18.2.0",
    "react-dom": "18.2.0",
    "react-native": "0.72.4",
    "react-native-gesture-handler": "~2.12.0",
    "react-native-reanimated": "~3.3.0",
    "react-native-safe-area-context": "4.6.3",
    "react-native-screens": "~3.22.0",
    "react-native-web": "~0.19.6",
    "typescript": "^5.1.3"
  },
  "devDependencies": {
    "@babel/core": "^7.20.0",
    "@babel/plugin-transform-export-namespace-from": "^7.22.11",
    "babel-eslint": "^10.1.0",
    "eslint-config-standard": "^17.1.0",
    "eslint-config-standard-jsx": "^11.0.0",
    "eslint-config-standard-react": "^13.0.0",
    "eslint-plugin-import": "^2.28.1",
    "eslint-plugin-node": "^11.1.0",
    "eslint-plugin-promise": "^6.1.1",
    "eslint-plugin-react": "^7.33.2"
  },
  "private": true,
  "eslintConfig": {
    "parser": "babel-eslint",
    "extends": [
      "standard",
      "standard-jsx",
      "standard-react"
    ]
  }
}


我尝试清除缓存,甚至更改chatGPT API模型,但没有结果。

ao218c7q

ao218c7q1#

目前,似乎不支持React Native。这是因为你需要在后端处理它。看这里

相关问题