以下是一个基本的Node.js代码示例,用于调用OpenAI API,并使用代理连接。请注意,您需要将“YOUR_API_KEY”替换为有效的OpenAI API密钥,以及将“YOUR_PROXY_URL”替换为有效的代理URL。

const openai = require('openai'); const HttpsProxyAgent = require('https-proxy-agent');

const apiKey = 'YOUR_API_KEY'; const proxyUrl = 'YOUR_PROXY_URL';

// 设置代理 const agent = new HttpsProxyAgent(proxyUrl);

// 设置OpenAI API密钥 openai.apiKey = apiKey;

// 创建completion请求 const prompt = 'Hello, my name is'; const model = 'text-davinci-002'; const maxTokens = 5; const options = { temperature: 0.5, maxTokens: maxTokens, n: 1, stop: '\n' };

// 使用代理发出请求 openai.createCompletion(prompt, model, options, { agent: agent }) .then(response => { console.log(response.data.choices[0].text); }) .catch(error => { console.log(error); });

帮我写个代码,调用openai.createCompletion 且用代理方式,用node代码

原文地址: https://www.cveoy.top/t/topic/wSn 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录