首页 > web前端 > js教程 > 正文

在 JavaScript 中使用生成式 AI 的五种方法

WBOY
发布: 2024-07-29 07:13:43
原创
516 人浏览过

机器学习和人工智能开发传统上由 Python 主导,因此教程、库和示例的生态系统主要由 Python 主导。然而,随着 AI 工程师概念的兴起,我们看到越来越多的全栈 Web 开发人员开始从事 AI 工作,随之而来的是对 JavaScript/Typescript 兼容工具的需求上升。事实上,2024 年 2 月,Vercel 的 Jared Palmer 甚至声称“未来的 AI 工程师是 TypeScript 工程师”。

Jared Palmer standing in front of a black slide with white text reading

在这篇博文中,我们将介绍作为 JavaScript 开发人员如何在不温习 Python 技能的情况下使用不同的生成式 AI 工具的五种方法。

云API

如果您刚刚开始,特别是如果您计划使用大型语言模型(LLM),例如 OpenAI 的 GPT 模型或 Anthropic 的 Claude 模型,直接使用其 API 可能是一个很好的开始。

只需一次提取调用即可与模型交互。

雷雷

事实上,OpenAI 的“聊天完成”API 已经成为许多其他模型提供商的事实上的标准。 Groq 或 Together.ai 等提供商提供 OpenAI 兼容性,这意味着您只需更改 URL 即可切换到不同的提供商来选择不同的模型。

如果您想使用其他模型,还有像 Replicate 这样的提供商专门提供具有一致的 REST API 的开源模型,甚至公开 API 来微调其平台上的某些模型。

码头工人

虽然云 API 非常适合入门,但有时您不想依赖云托管提供商来满足您的用例。例如,也许您想直接在本地计算机上显式运行 Llama 3 8B 等模型,或者您想使用像 Unstructed.io 这样用 Python 编写的开源库,并在 JavaScript 项目中使用它,而无需支付托管费用API.

出于这个原因,一些项目提供了 Docker 容器,该容器在运行时会公开 HTTP API。例如,您可以通过运行以下命令来启动非结构化 API Docker 容器:

雷雷

容器运行后,您现在拥有本地主机版本的非结构化 API,您可以使用它对文档进行分块,以便稍后存储在矢量数据库中。

雷雷

类似地,您可以使用 llama.cpp docker 容器或 Ollama 为您的 LLM 模型(例如 Llama 3)运行本地 API。

如果您正在与训练自己模型的 ML 团队合作,或者您想要在 Huggingface 上托管任何模型并使用相同的 Docker 容器方法,您还可以通过 Replicate 查看 cog。它包装了 Docker,专为为 ML 模型创建 Docker 容器而设计。

如果您要执行的任务表面积相对较小并且可组合性有限,那么所有这些都非常有用。

JavaScript 原生库

现在这可能是最明显的选择,但最好的选择仍然是选择一个用 JavaScript 或 TypeScript 原生编写的库或工具,幸运的是,这个生态系统仍在不断发展。

大多数云 API 模型提供商都提供 JavaScript 本机 SDK,包括。 OpenAI、Anthropic 和 Google。

此外,两个最流行的开源 LLM 框架 Langchain 和 LlamaIndex 提供了其框架的 TypeScript 版本。 Vercel 还提供从头开始构建的人工智能 SDK,更注重将法学硕士及其支持的前端体验结合在一起。尽管文档主要关注 Vercel 自己的 Next.js 框架,但该 SDK 也适用于其他框架。

雷雷

然而,由于其中大多数最终将其他工具和框架包装为集成,因此与 Python 工具和框架相比,您通常仍然拥有更多有限的功能。例如,Langchain 的 Python 版本有 18 种不同的文档转换器集成,而 JavaScript 版本有 5 种。

本地 LLM API

现在这个还是比较有前瞻性的。 Google Chrome 最近向 Chrome Dev 和 Chrome Canary 渠道发布了一组实验性 API,可公开对本地运行的 Gemini Nano 模型的访问。

雷雷

由于与最先进的模型(包括 GPT-4o mini 或 Llama 3.1 8B 等较小模型)相比,该模型是如此之小,因此您可能会很难可靠地提示这一点。随着模型开发的步伐,这种情况可能会很快改变。

While this API is still experimental and only spearheaded by Chrome, the trend of local LLMs might change this quickly as more companies get interested. Mozilla, for example, recently announced that they are focused on moving "local AI" forward incl. creating a new dedicated accelerator program and Apple is already using local models for their new Apple Intelligence feature.

If you want to give the window.ai API a shot, check out Google's explainer repository as well as the chrome-ai package for Vercel's ai SDK to get started.

Pythonia

One interesting approach to using Python tools in JavaScript is pythonia. It's one half of the JSPyBridge project that creates an interface to call JavaScript from Python and Python from JavaScript by facilitating the interprocess communication so that you can write code in the language of your choice.

It uses inter-process communication (IPC) and JavaScript Proxies to enable you to almost use identical code when calling a Python library in JavaScript than in Python and then actually executing it in Python.

For example, here's a code snippet taken from the getting started guide of the Python library haystack-ai:

from haystack import Pipeline, PredefinedPipeline

pipeline = Pipeline.from_template(PredefinedPipeline.CHAT_WITH_WEBSITE)
result = pipeline.run({
    "fetcher": {"urls": ["https://haystack.deepset.ai/overview/quick-start"]},
    "prompt": {"query": "Which components do I need for a RAG pipeline?"}}
)
print(result["llm"]["replies"][0])
登录后复制

By using the pythonia npm package we can write the same equivalent code:

import { python } from "pythonia";

const haystack = await python("haystack");
const { Pipeline, PredefinedPipeline } = await haystack;

const template = await PredefinedPipeline("chat_with_website");
const pipeline = await Pipeline.from_template(template);
const result = await pipeline.run({
 fetcher: { urls: ["https://haystack.deepset.ai/overview/quick-start"] },
 prompt: { query: "Which components do I need for a RAG pipeline?" },
});

console.log((await result.valueOf()).llm.replies[0]);
python.exit();
登录后复制

You might notice that this code is slightly longer and heavily uses await. That's because of the IPC communication. pythonia does a lot of optimizations behind the scenes to effectively communicate between the channels. For example, the actual data is not being sent back from Python to Node.js unless you call valueOf(). However, outside of that the code is very similar and is using native Python libraries.

Performance of pythoia

One concern for you might be performance and while it would be slower than entirely running in Python, the actual performance might surprise you. If you want to use a Python library, like RAGatoille, but the rest of your system is written in JavaScript, really the only alternative to pythonia is exposing the library through an HTTP API and using fetch to bridge the systems.

If we run a benchmark where we use the haystack-ai code snippet from above and run it both using pythonia and expose it using FastAPI, both requests are slow because of their calls to OpenAI but pythonia actually slightly wins the race.

Five ways to use Generative AI in JavaScript

Using `pythonia` results in an average of 13% faster results but both take longer than 1.2s per run

Overall while there is a performance hit of using pythonia over using only native Python, given the long-running nature of most generative AI calls, the overhead becomes relatively negligible especially when compared to making local HTTP requests.

Conclusion

While more and more JavaScript developers are getting into the Generative AI space, we still have ways to go to catch up to an ecosystem that has the breadth of the Python space. Cloud APIs, running local Docker containers, and bridging projects such as pythonia are great options to tap into this space without moving all of your logic into Python. Ultimately it's up to us though to either grow the space of available AI JavaScript tools by contributing to existing open-source projects or even starting new ones if you want to maintain a project. In the meantime, AI tools such as GitHub Copilot, Cursor, or Codeium can help you with writing some Python code.

以上是在 JavaScript 中使用生成式 AI 的五种方法的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!