OpenAI API:无法分配给只有 getter 的属性“search”

问题描述 投票:0回答:1

我正在使用 OpenAI 助手和我自己的上下文数据来获取对我的查询的上下文响应:

const openai = new OpenAI({ apiKey: xxx });

async function queryNoFileSearch(prompt: string): Promise<string> {
    const assistant = await openai.beta.assistants.create({
        name: "name",
        instructions: "contextual instructions",
        tools: [{ type: "code_interpreter" }],
        model: 'gpt-3.5-turbo'
    });

    const thread = await openai.beta.threads.create();
    await openai.beta.threads.messages.create(
        thread.id,
        {
            role: 'user',
            content: prompt
        }
    );
    let run = await openai.beta.threads.runs.createAndPoll(
        thread.id,
        {
            assistant_id: assistant.id,
            instructions: "contextual instructions",
        }
    );
    let res = '';
    if (run.status === 'completed') {
        const messages = await openai.beta.threads.messages.list(
          run.thread_id
        );
        for (const message of messages.data.reverse()) {
            const x = message.content[0] as any;
            res += x.text.value + '\n';
        }
    } else {
        res = run.status;
    }
    return res;
}

(async () => {
    const result = await queryNoFileSearch("What bowls do you have available?");
    console.log(`Result: ${result}`);
})();

export default queryNoFileSearch;

当我使用

ts-node file.ts
运行此文件(单独)时,我得到了按预期返回的结果。它是一个包含 OpenAI 响应的字符串。这太棒了。

但是,我无法在 React Native 组件中显示此文本并将其显示在屏幕上。我收到的错误是:

Error: Cannot assign to property 'search' which has only a getter

这是迄今为止我的 React Native 组件。

const PredictorScreen = () => {
  const [output, setOutput] = useState<string>('');
  const [loading, setLoading] = useState<boolean>(false);
  const [error, setError] = useState<string | null>(null);
  const handlePredict = async () => {
    setLoading(true);
    setError(null);
    try {
      const result = await queryNoFileSearch("User makes query here");
      setOutput(result);
    } catch (err) {
      setError('Error: ' + (err as Error).message);
    } finally {
      setLoading(false);
    }
  };
  return (
    <View style={styles.container}>
      <Button title="xxx" onPress={handlePredict} />
      <ScrollView style={styles.outputContainer}>
        {loading ? (
          <Text style={styles.outputText}>Loading...</Text>
        ) : error ? (
          <Text style={styles.outputText}>{error}</Text>
        ) : (
          <Text style={styles.outputText}>{output}</Text>
        )}
      </ScrollView>
    </View>
  );
};

如何让

queryNoFileSearch
的结果显示在 React Native 组件中?当我在自己的文件中单独运行该函数时,它会很好地返回并记录到控制台,但是当我尝试在 React Native 组件中显示结果时,我总是面临这个
Cannot assign to property 'search' which has only a getter
错误,但我没有这样做知道为什么。我在网上找到的任何内容都无法解决我的问题。

react-native async-await openai-api openai-assistants-api
1个回答
0
投票

OpenAI 不提供适用于 React Native 的专用 SDK。要将 OpenAI 的 API 集成到 React Native 和 Expo 项目中,您应该使用直接 HTTP 请求。我个人使用 firebase 云功能。

© www.soinside.com 2019 - 2024. All rights reserved.