本文主要是介绍Call openai-node in the backend or call https in the frontend?,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题意:在后端调用 `openai-node` 还是在前端调用 `https`?
问题背景:
I have a web application by ReactJS and Nodejs. This application calls OpenAI APIs.
我有一个使用 ReactJS 和 Node.js 开发的 Web 应用程序。这个应用程序调用 OpenAI 的 API。
Previously, when a user launches a request in the frontend, we send a request to the endpoint in our backend, call createChatCompletion
of https://github.com/openai/openai-node in the backend, and returns the result to the frontend. Note that the server of our frontend and the server of our backend are separate and not in the same location; users are everywhere in the world.
之前,当用户在前端发起请求时,我们会将请求发送到后端的一个端点,在后端调用 `https://github.com/openai/openai-node` 的 `createChatCompletion`,然后将结果返回到前端。需要注意的是,我们的前端服务器和后端服务器是分开的,且不在同一地点,用户遍布世界各地。
We just realized that we can also request directly https://api.openai.com/v1/chat/completions
in the frontend as follows:
我们刚刚意识到,我们也可以直接在前端请求 `https://api.openai.com/v1/chat/completions`,如下所示:
const res = await fetch("https://api.openai.com/v1/chat/completions", {method: 'POST',headers: {"Content-Type": "application/json",Authorization: `Bearer ${API_KEY}`},body: JSON.stringify({model: model,messages: [{ role: "user", content: prompt }]})})
At the moment, our pain-point is the time from sending a request by a user to seeing the result in the application is too long. From this perspective of speed, does anyone know which approach is better and why?
目前,我们的问题是从用户发送请求到在应用程序中看到结果的时间太长。从速度的角度来看,有谁知道哪种方法更好,以及原因是什么?
问题解决:
One thing to keep in mind is that everything in your front end is essentially public. In this case, if you do the call directly from the browser, it's trivial for users to capture your api key.
需要记住的一点是,前端的一切本质上都是公开的。在这种情况下,如果你直接从浏览器发起调用,用户可以很容易地获取到你的 API 密钥。
Removing the call to your server likely won't make a significant difference anyway; AI is rather slow. A better solution may be to use the streaming API (and also stream from your backend to your frontend) so the users can see the response as it's generated.
即使去掉对你服务器的调用,也不会有显著的差别;人工智能的响应速度本身较慢。一个更好的解决方案可能是使用流式 API(同时从你的后端向前端流式传输),这样用户可以在响应生成时实时看到结果。
这篇关于Call openai-node in the backend or call https in the frontend?的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!