OpenAI API“提供了无法识别的请求参数:消息”错误

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

这是我来自 Node js 项目的代码。

// Generate diet endpoint
app.post('/generate-diet', async (req, res) => {
    const { dietaryPreferences, allergies, goals } = req.body;

    const prompt = `Create a diet plan keeping in mind the following preferences: ${dietaryPreferences.join(', ')}, allergies: ${allergies.join(', ')} and aiming to achieve these goals: ${goals.join(', ')}.`;

    try {
        const messages = [
            { role: 'system', content: 'You are a helpful diet planner.' }, // System message
            { role: 'user', content: prompt }, // User message
        ];
        
        const response = await axios.post('https://api.openai.com/v1/completions', {
            model: "gpt-3.5-turbo",
            messages: messages, // Use the messages parameter
            max_tokens: 150
        }, {
            headers: {
                'Authorization': `Bearer ${process.env.OPENAI_API_KEY}`,
                'Content-Type': 'application/json'
            }
        });        
        

    } catch (error) {
        console.error(`Error fetching from OpenAI: ${error.response.data}`);
        return res.status(500).json({ error: `Server error: ${error.response.data}` });
    }    
});

每当我使用邮递员发帖时,我都会收到此错误:

{
    "error": "Server error: [object Object]"
}

我的控制台日志也这样说:

Error fetching from OpenAI: {
  error: {
    message: 'Unrecognized request argument supplied: messages',
    type: 'invalid_request_error',
    param: null,
    code: null
  }
}

我已经尝试多次更改端点链接,还添加了模型和令牌,但仍然不起作用。

node.js api prompt openai-api
1个回答
0
投票

您使用了错误的网址。

改变这个...

https://api.openai.com/v1/completions

...对此。

https://api.openai.com/v1/chat/completions
© www.soinside.com 2019 - 2024. All rights reserved.