我在聊天的帮助下移交了代码,但代码不起作用,我不知道可能是什么问题。
该代码本应使用开放式人工智能平台来获取响应并向发送者提供响应,但它似乎不起作用。下面是代码
const express = require('express');
const bodyParser = require('body-parser');
const axios = require('axios');
const dotenv = require('dotenv');
const cors = require('cors'); // Add this line
dotenv.config();
const app = express();
const port = 3000;
app.use(bodyParser.json());
app.use(cors()); // Add this line
class SabiDoktor {
constructor() {
this.openaiApiKey = process.env.OPENAI_API_KEY;
}
async processMessage(message) {
try {
// Call OpenAI API to generate a response
const response = await this.generateOpenAIResponse(message);
// Add additional logic for processing the response if needed
return `User: ${message}\nSabiDoktor: ${response}`;
} catch (error) {
console.error('Error processing message:', error.message);
return 'Error processing message';
}
}
async generateOpenAIResponse(prompt) {
const apiUrl = 'https://api.openai.com/v1/engines/davinci-codex/completions';
try {
const response = await axios.post(apiUrl, {
prompt: prompt,
max_tokens: 50
// Add additional parameters based on your needs
}, {
headers: {
'Authorization': `Bearer ${this.openaiApiKey}`
}
});
return (response.data.choices[0] && response.data.choices[0].text.trim()) || 'No response from SabiDoktor';
} catch (error) {
console.error('Error generating response from OpenAI:', error.message);
return 'Error generating response from OpenAI';
}
}
}
const sabiDoktor = new SabiDoktor();
app.post('/webhook', async (req, res) => {
const { message } = req.body;
const response = await sabiDoktor.processMessage(message);
res.json({ response });
});
app.listen(port, () => {
console.log(`SabiDoktor Server listening at http://localhost:${port}`);
}
);
通过记录有关错误的更多详细信息来改进错误处理。例如,记录整个错误对象以获取有关发生的错误类型的信息。
} catch (error) {
console.error('Error generating response from OpenAI:', error); // Change this line
return 'Error generating response from OpenAI';
}
如果你想用AI助手替代Google Nest Mini,可以按照这个设计。
https://www.pcbway.com/project/shareproject/Onju_Voice_d33625a1.html