当前位置: 首页 > news >正文

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(同时从你的后端向前端流式传输),这样用户可以在响应生成时实时看到结果。

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 大话C++:第5篇 再识作用域
  • NLP入门
  • 简单树上问题
  • Java核心API——exception类认识java异常处理机制
  • 网页html版——在线查字典的一个web服务器
  • “智能化自动化” 架构之3:中间建模脑的领域驱动设计的“同声传译”技能
  • TeamTalk路由服务器
  • 深度学习100问44:如何避免模型出现过拟合现象
  • 解决Selenium已安装,在pycharm导入时报错
  • K8s系列之:K8s OPERATOR是什么
  • Python matplotlib绘图 plt.barh 水平条形图调整顺序逆序排列
  • Docker 的安全优化
  • Git版本控制策略:Rebase还是Merge?详解优缺点与适用场景
  • 【 OpenHarmony 系统应用源码魔改 】-- Launcher 之「桌面布局定制」
  • 012 MPLS技术在企业网络中的应用
  • Angular 2 DI - IoC DI - 1
  • express如何解决request entity too large问题
  • flask接收请求并推入栈
  • JavaScript类型识别
  • LeetCode541. Reverse String II -- 按步长反转字符串
  • Less 日常用法
  • node和express搭建代理服务器(源码)
  • SpiderData 2019年2月13日 DApp数据排行榜
  • VirtualBox 安装过程中出现 Running VMs found 错误的解决过程
  • vue 个人积累(使用工具,组件)
  • WordPress 获取当前文章下的所有附件/获取指定ID文章的附件(图片、文件、视频)...
  • 如何胜任知名企业的商业数据分析师?
  • 我这样减少了26.5M Java内存!
  • 小程序上传图片到七牛云(支持多张上传,预览,删除)
  • 直播平台建设千万不要忘记流媒体服务器的存在 ...
  • 昨天1024程序员节,我故意写了个死循环~
  • ​2020 年大前端技术趋势解读
  • ​html.parser --- 简单的 HTML 和 XHTML 解析器​
  • ​secrets --- 生成管理密码的安全随机数​
  • # Java NIO(一)FileChannel
  • # 移动硬盘误操作制作为启动盘数据恢复问题
  • #AngularJS#$sce.trustAsResourceUrl
  • #Spring-boot高级
  • (20)目标检测算法之YOLOv5计算预选框、详解anchor计算
  • (Matalb回归预测)PSO-BP粒子群算法优化BP神经网络的多维回归预测
  • (pytorch进阶之路)CLIP模型 实现图像多模态检索任务
  • (超简单)使用vuepress搭建自己的博客并部署到github pages上
  • (二)springcloud实战之config配置中心
  • (删)Java线程同步实现一:synchronzied和wait()/notify()
  • (四)c52学习之旅-流水LED灯
  • (转)Java socket中关闭IO流后,发生什么事?(以关闭输出流为例) .
  • (轉貼) UML中文FAQ (OO) (UML)
  • (自适应手机端)响应式服装服饰外贸企业网站模板
  • .Family_物联网
  • .gitignore文件设置了忽略但不生效
  • .NET / MSBuild 扩展编译时什么时候用 BeforeTargets / AfterTargets 什么时候用 DependsOnTargets?
  • .net core 控制台应用程序读取配置文件app.config
  • .Net Redis的秒杀Dome和异步执行
  • .NET 发展历程
  • .Net高阶异常处理第二篇~~ dump进阶之MiniDumpWriter