赞
踩
NodeJs使用openai流式请求与非流式请求
一次性返回所有数据,请求时间较久,兼容性好。
- router.post("/openai", async (req, res) => {
- let url = 'https://api.openai.com/v1/engines/text-davinci-003/completions' // 3.5可用
- const response = await axios.post(url, {
- max_tokens: 1000,
- prompt: req.body.content,
- }, {
- headers: {
- 'Authorization': 'Bearer ' + chat_key,
- 'Content-Type': 'application/json'
- }
- });
- responseJSON(res, response)
- })
- // gpt3.5 - 完成
- router.post("/chart", async (req, res) => {
- let param = req.body
- var data = JSON.stringify({
- messages: [
- {
- "role": "user",
- "content": param.content
- }],
- model: chatConfig.model || 'gpt-3.5-turbo',
- frequency_penalty: chatConfig.frequency_penalty || 0,
- max_tokens: chatConfig.max_tokens || 1000,
- presence_penalty: chatConfig.presence_penalty || 0,
- temperature: chatConfig.temperature || 1,
- top_p: chatConfig.top_p || 1
- });
- var config = {
- method: 'POST',
- url: 'https://api.openai.com/v1/chat/completions', // 反向代理可用 无反向代理请求超时
- headers: {
- 'Authorization': 'Bearer ' + chat_key,
- 'Content-Type': 'application/json',
- },
- timeout: 60 * 1000,
- data: data, // POST 反向代理可用
- };
- axios(config)
- .then((response) => {
- console.log(JSON.stringify(response.data));
- responseJSON(res, response.data.choices[0].message)
- })
- .catch((error) => {
- console.log(error);
- responseJSON(res, error)
- });
- })
即时数据响应,请求时间短,交互效果好,不一定适配所有系统。
- let sendyhChatSteam = async (req, res, param) => {
- let _res = res;
- let data = {
- data: JSON.stringify({
- messages: [
- {
- "role": "user",
- "content": param.content
- }],
- frequency_penalty: chatConfig.frequency_penalty || 0,
- max_tokens: chatConfig.max_tokens || 1000,
- presence_penalty: chatConfig.presence_penalty || 0,
- temperature: chatConfig.temperature || 1,
- top_p: chatConfig.top_p || 1
- })
- };
-
- let options = {
- hostname: 'api.openai-proxy.com',
- path: 'gpt-3.5-turbo',
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json'
- }
- };
- let question = ''
- let request = http.request(options, (response) => {
- response.setEncoding('utf8');
-
- // 监听 'data' 事件,获取返回的数据
- response.on('data', (chunk) => {
- question += chunk
- // 在这里处理每个数据块
- console.log(question);
- });
-
- // 监听 'end' 事件,表示数据传输完成
- response.on('end', () => {
- console.log('------------------监听 end 事件,表示数据传输完成-----------------------------');
- console.log(question);
- });
- });
- // 可以通过流式方式将数据写入请求
- request.write(JSON.stringify(data));
- // 结束请求
- request.end();
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。