我可以在后端使用 Node.js 并在 AI 计算中使用 Python 吗?

Can I use Node.js for the back end and Python for the AI calculations?

我正在尝试在 Node.js 中创建一个网站。不过,因为我正在上一门关于如何使用人工智能的课程,并希望将其应用到我的程序中。因此,我想知道是否可以轻松地将 Python Spyder 连接到基于 Node.js 的 Web 应用程序。

我知道有两种方法可以做到这一点 1) 使用 child_process 库

const { exec } = require('child_process');
exec('python yourscript.py', (err, stdout, stderr) => {
if (err) {
return;
}
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});

如果你想要一步一步的指导,你可以查看这篇文章。 https://www.geeksforgeeks.org/run-python-script-node-js-using-child-process-spawn-method/

2) 更好的方法是创建一个 API,稍后您可以使用 AJAX 或您喜欢的任何库调用它。您可以使用诸如 flask 之类的微框架来创建一个服务器,然后您可以使用 NodeJs 调用该服务器。 将机器学习代码转换为 API 的教程: https://towardsdatascience.com/publishing-machine-learning-api-with-python-flask-98be46fb2440

axios.post( your_api_address , {
MLparametre1: 'somevalue',
MLparametre2: 'somevalue',
MLparametre3: 'somevalue',
MLparametre4: 'somevalue',
})

或者您可以只传递一个列表或字典对象。 https://flaviocopes.com/node-axios/ 你可以在这里了解更多关于 axios 的信息。

是的。这是可能的。有几种方法可以做到这一点。如上所述,您可以使用 child_process 库。或者,你可以有一个 Python API 来处理 AI 的东西,你的 Node 应用程序与之通信。

我更喜欢后一个例子,因为我的大多数项目 运行 在容器上作为 Kubernates 上的微服务。