如何在HTML中通过JS加载OpenAI API配置?

我在HTML中尝试通过JS发送请求,让OpenAI进行分析并返回响应,但如果我在JS中加入以下代码:

const { Configuration, OpenAIApi } = require("openai");const configuration = new Configuration({apiKey: "sk-0000000000000ZXXXXXXXXXXXXXX",});const openai = new OpenAIApi(configuration);async function test() {console("test")const response = await openai.createCompletion("text-davinci-002", {prompt: "hello",temperature: 0.7,max_tokens: 64,top_p: 1,frequency_penalty: 0,presence_penalty: 0,});console.log(response)}test();

返回控制台显示以下错误:

Uncaught ReferenceError: require is not definedat buttons.js:94:38

我尝试使用Node.js进行安装,运行正常,但不知道如何在我的HTML中实现这个功能。


回答:

提供当前有效的正确链接:

var url = "https://api.openai.com/v1/completions";

以下是使用davinci-003引擎进行问答的数据格式示例:

var data = `{              "model": "text-davinci-003",              "prompt": "${prompt_text+question_out}",              "temperature": 0.9,              "max_tokens": 150,              "top_p": 1,              "frequency_penalty": 0.0,              "presence_penalty": 0.6,              "stop": [" Human:", " AI:"]            }`;

这里的prompt_text是我发送给引擎的提示文本,question_out是我希望引擎根据提示回答的问题。

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注