gpt4 book ai didi

react-native - 是否修改了react-native openai-api模块以访问ChatGPT API?

转载 作者:行者123 更新时间:2023-12-02 22:45:47 26 4
gpt4 key购买 nike

react-native openai-api 模块是否已修改为访问 ChatGPT API?
使用 expo 时,出现以下错误:

WARN Possible Unhandled Promise Rejection (id: 0):
TypeError: Cannot read property 'create' of undefined

使用openai.ChatCompletion.create似乎不适用于react-native

代码:

import React, { useState, useEffect } from "react";
import { View, TextInput, Button, FlatList, Text } from "react-native";
import OpenAI from "openai-api";

const openai = new OpenAI("YOUR_API_KEY");

export default function App() {
const [inputText, setInputText] = useState("");
const [messages, setMessages] = useState([]);
const [responseText, setResponseText] = useState("");

useEffect(() => {
async function generateResponse() {
if (messages.length > 0) {
const response = await openai.ChatCompletion.create({
model: "gpt-3.5-turbo",
messages,
});
print(response);
setResponseText(response.choices[0].text);
}
}
generateResponse();
}, [messages]);

最佳答案

截至2023 年 3 月 4 日openai-api库尚不支持聊天完成功能。因此,您尝试访问的 ChatCompletion 属性不存在,因此您会收到 Cannot read property 'create' of undefined 消息。

我强烈建议您使用官方 openai该库可在浏览器中运行并完全支持新的聊天完成。

您可以通过将代码更改为以下内容并将 YOUR API KEY 字符串替换为您自己的 key 来实现此目的:

import React, { useState, useEffect } from "react";
import { View, TextInput, Button, FlatList, Text } from "react-native";
import { Configuration, OpenAIApi } from 'openai';

const configuration = new Configuration({
apiKey: "YOUR API KEY",
});
const openai = new OpenAIApi(configuration);

export default function App() {
const [inputText, setInputText] = useState("");
const [messages, setMessages] = useState([]);
const [responseText, setResponseText] = useState("");

useEffect(() => {
async function generateResponse() {
if (messages.length > 0) {
const response = await openai.createChatCompletion({
model: 'gpt-3.5-turbo',
messages,
});
print(response);
setResponseText(completion.data.choices[0].message.content);
}
}
generateResponse();
}, [messages]);

关于react-native - 是否修改了react-native openai-api模块以访问ChatGPT API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75633269/

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