2025 年值得關注的九個現代化開源 JavaScript AI 開發庫
雖然 Python 在 AI 領域有絕對優勢,生態非常完整,但 JS/TS 也有它的獨到之處,足以成為開發 AI 應用時的一匹“黑馬”:
- 性能優勢:JavaScript/TypeScript 異步和非阻塞 I/O 的模式,在很多基于 Web 的場景里,性能表現并不遜色。
- 無縫集成:想把 AI 功能嵌入到網頁端,JS/TS 跟前端技術能完美配合,無需不斷切換語言。
- 包管理:npm、Yarn 等工具用起來很順手,管理依賴和發布包都相對便捷。
所以,盡管 Python 仍是各種 AI 庫的“官方首選”(大部分新庫都會先出 Python 版本),但在 Web 開發領域,JavaScript 的地位依然難以撼動。
如果你正打算在前端或后端用 JS/TS 來做 AI 應用,這里整理了 9 個我認為最有價值的開源庫,可以幫你大幅提升開發效率!可以去它們的 GitHub 倉庫看看,貢獻代碼、提 issue、或者順手點個 Star 支持一下。
1. Composio ?? —— 快速構建可靠的 AI Agent
很多人想用 AI Agent 來自動執行外部工具(比如 Discord、Slack、Calendar 等)的復雜工作流,但發現做起來并不輕松。Composio 就是專門為此而生的。
image.png
Composio 是一個支持多平臺集成的 AI 開發工具,和 100+ 常見平臺無縫連接,比如 CRM、效率工具、HR、開發者生態等,能輕松打造自動化的復雜工作流。它本身提供了原生的 JavaScript 支持,只要安裝它就能在你的 JS/TS 項目里調用 AI Agent 相關功能。
npm install composio-core openai
# yarn add composio-core openai
# pnpm add composio-core openai
下面舉個簡單的例子,假設我們想讓用戶連接他們的 GitHub 賬號,然后自動幫他們在 GitHub 上給某個倉庫點 Star:
import { OpenAI } from "openai";
import { OpenAIToolSet } from "composio-core";
const toolset = new OpenAIToolSet({
apiKey: process.env.COMPOSIO_API_KEY,
});
async function setupUserConnectionIfNotExists(entityId) {
const entity = await toolset.client.getEntity(entityId);
const connection = await entity.getConnection('github');
if (!connection) {
const connection = await entity.initiateConnection(appName);
console.log("Log in via: ", connection.redirectUrl);
return connection.waitUntilActive(60);
}
return connection;
}
async function executeAgent(entityName) {
const entity = await toolset.client.getEntity(entityName);
await setupUserConnectionIfNotExists(entity.id);
const tools = await toolset.get_actions({ actions: ["github_activity_star_repo_for_authenticated_user"] }, entity.id);
const instruction = "Star a repo ComposioHQ/composio on GitHub";
const client = new OpenAI({ apiKey: process.env.OPEN_AI_API_KEY });
const response = await client.chat.completions.create({
model: "gpt-4-turbo",
messages: [{
role: "user",
content: instruction,
}],
tools: tools,
tool_choice: "auto",
});
console.log(response.choices[0].message.tool_calls);
await toolset.handle_tool_call(response, entity.id);
}
executeAgent("joey");
Composio 還能無縫支持 LangChain、LlamaIndex、CrewAi 等熱門框架,讓 Agent 直接幫你執行代碼。可以去它的官方文檔或倉庫查看更詳細的示例和高級玩法。
地址:https://github.com/composiohq/composio?utm_source=dev-to
2. Instructor-JS —— 用結構化方式提取 LLM 數據
過去從大型語言模型 (LLM) 的輸出里提取信息一直很麻煩,總要想辦法解析并校驗數據。Instructor-JS 則提供了一個簡潔的解決方案,讓你可以用結構化的方式抽取并驗證 LLM 返回的結果。在 JavaScript 里,它用 Zod 進行數據校驗,也同時支持 Python 環境。
只需要幾行代碼就能把無結構的 LLM 輸出,變得有跡可循。示例:
npm i @instructor-ai/instructor zod openai
import Instructor from "@instructor-ai/instructor";
import OpenAI from "openai";
import { z } from "zod";
const oai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
organization: process.env.OPENAI_ORG_ID
});
const client = Instructor({
client: oai,
mode: "TOOLS"
});
const UserSchema = z.object({
age: z.number().describe("The age of the user"),
name: z.string()
});
const user = await client.chat.completions.create({
messages: [{ role: "user", content: "Jason Liu is 30 years old" }],
model: "gpt-3.5-turbo",
response_model: {
schema: UserSchema,
name: "User"
}
});
console.log(user);
// { age: 30, name: "Jason Liu" }
去它的官方文檔或倉庫看看,可以更靈活地處理各種數據提取與驗證場景。
地址:https://github.com/instructor-ai/instructor-js
3. CopilotKit —— 一鍵打造 React AI 助手
如果你正在做一個需要嵌入 AI 助手的 React 項目,可以試試 CopilotKit。它封裝了一套非常方便的 React 組件,比如文本框、聊天面板、側邊欄等,讓你幾乎零配置就能給應用添加 AI 功能。
image.png
npm i @copilotkit/react-core @copilotkit/react-ui
示例:引入一個 CopilotSidebar,再包裹進 CopilotKit Provider。這樣就能在你的應用里自由切換聊天和助手功能,還能自己定制 UI 風格。
"use client";
import { CopilotKit } from "@copilotkit/react-core";
import { CopilotSidebar } from "@copilotkit/react-ui";
import "@copilotkit/react-ui/styles.css";
export default function RootLayout({children}) {
return (
<CopilotKit publicApiKey="你的 API Key 或者自行托管">
<CopilotSidebar>
{children}
</CopilotSidebar>
</CopilotKit>
);
}
更多細節和定制方法,可以去它的官方文檔查看。
地址:https://github.com/CopilotKit/CopilotKit
4. E2B —— 為 AI 應用提供安全的代碼執行環境
有些 AI Web 應用場景下,LLM 需要實時執行代碼,比如 AI 數據分析、軟件開發助理等。E2B 的 Code Interpreter 就是為此準備的:它提供了一個安全可控的云端沙盒,讓 LLM 可以“動手”跑代碼,而且支持長時間運行、調試、導入 GitHub 倉庫等。
image.png
使用它的 Code Interpreter SDK,可以輕松把 AI 生成的代碼放在 E2B 的 Sandbox 上跑,甚至包含 Jupyter Server,能做各種復雜計算。安裝方法也很簡單:
npm i @e2b/code-interpreter
然后使用示例:
import { CodeInterpreter } from '@e2b/code-interpreter';
const sandbox = await CodeInterpreter.create();
await sandbox.notebook.execCell('x = 1');
const execution = await sandbox.notebook.execCell('x += 1; x');
console.log(execution.text); // 輸出 2
await sandbox.close();
如果你想讓 GPT-4 或其他模型幫你寫代碼,然后自動執行檢驗,可以考慮在你的后端或服務端嵌入 E2B,穩定又安全。
地址:https://github.com/e2b-dev/E2B
5. LanceDB —— 高性能向量數據庫
很多 AI 應用需要存儲、檢索文本、圖像、語音等內容的嵌入向量,向量數據庫就成了必不可少的組件。LanceDB 是一個速度快、功能全的開源向量數據庫,并且直接支持 JavaScript。
image.png
它擁有如下特性:
- 高速向量檢索
- 多模態支持
- Zero-copy 架構
- 自動數據版本管理
- GPU 加速查詢
安裝方法:
npm install @lancedb/lancedb
簡單示例:創建表并做向量檢索。
import * as lancedb from "@lancedb/lancedb";
const db = await lancedb.conect("data/sample-lancedb");
const table = await db.createTable("vectors", [
{ id: 1, vector: [0.1, 0.2], item: "foo", price: 10 },
{ id: 2, vector: [1.1, 1.2], item: "bar", price: 50 },
], { mode: 'overwrite' });
const query = table.vectorSearch([0.1, 0.3]).limit(2);
const results = await query.toArray();
console.log(results);
適用于做相似度檢索、推薦系統或多模態檢索等場景。可去官方文檔了解更多。
地址:https://github.com/lancedb/lancedb
6. Trigger.Dev —— 長時后臺任務,不再擔心超時
Trigger.Dev 是一個開源平臺與 SDK,主打“長時間運行的異步后臺任務”,讓你再也不用因為超時問題而絞盡腦汁。它還支持自動重試、可視化追蹤與調試,和各種 AI API 一起配合非常順手。
image.png
import { task } from "@trigger.dev/sdk/v3";
// 用 Dall-E 3 生成圖片的例子
export const generateContent = task({
id: "generate-content",
retry: {
maxAttempts: 3,
},
run: async ({ theme, description }: Payload) => {
const textResult = await openai.chat.completions.create({
model: "gpt-4o",
messages: generateTextPrompt(theme, description),
});
if (!textResult.choices[0]) {
throw new Error("No content, retrying…");
}
const imageResult = await openai.images.generate({
model: "dall-e-3",
prompt: generateImagePrompt(theme, description),
});
if (!imageResult.data[0]) {
throw new Error("No image, retrying…");
}
return {
text: textResult.choices[0],
image: imageResult.data[0].url,
};
},
});
如果你有一些調用 AI 接口可能耗時幾十秒甚至更長的任務,Trigger.Dev 會讓它們執行得更加平穩。
地址:https://github.com/triggerdotdev/trigger.dev
7. Vercel AI SDK —— 全棧 TypeScript AI 開發套件
想用 Next.js、React、Vue 或 SvelteKit 來搭配 AI,Vercel AI SDK 幾乎是當下最整合友好的選擇之一。它有三大模塊:
- AI SDK Core:統一封裝了對各種 LLM 的調用接口。
- AI SDK UI:提供了無框架限制的 hooks 來快速構建聊天或其他交互式的生成式 UI。
- AI SDK RSC:讓你在 React 服務端組件 (RSC) 里做流式的生成式渲染。
安裝方法也很簡單:
npm install ai
然后選擇你喜歡的模型服務,比如 OpenAI,做一次示例調用:
import { generateText } from 'ai';
import { openai } from '@ai-sdk/openai'; // 記得設置 OPENAI_API_KEY
async function main() {
const { text } = await generateText({
model: openai('gpt-4-turbo'),
system: 'You are a friendly assistant!',
prompt: 'Why is the sky blue?',
});
console.log(text);
}
main();
可以去它的官方文檔查看更多在前端或全棧場景下的最佳實踐。
地址:https://github.com/vercel/ai
8. Julep —— AI 應用的托管式后端
如果你想構建擁有“長期記憶”的 AI 應用,那么 Julep(一個開源的 AI 平臺)就能派上用場。它的定位類似“給 AI 用的 Firebase/Supabase”,提供了以下功能:
- Memory:支持用戶管理、存儲各種交互上下文。
- Knowledge:自帶 RAG(檢索增強生成)和上下文管理。
- Tools:無縫對接 Composio 或其他工具。
- Tasks(即將上線)
配合 JS/TS 也很簡單,更多細節可去它的倉庫或文檔查看。
地址:https://github.com/julep-ai/julep
9. Gateway —— 一條 API 同時接入 200+ 種 LLM
image.png
最后,如果你的應用需要集成好幾個 LLM 提供商的模型,或者你想在服務端支持多種大模型做負載均衡,那么 Gateway 可以讓你一次性搞定。一條統一的 API,就能訪問 200+ 個開放或閉源的模型,而且還自帶:
- 緩存、重試、超時等高級特性
- Fallback 機制
- 負載均衡
- 支持 Edge 部署,獲取更低延遲
本地快速啟動:
npx @portkey-ai/gateway
然后就能用統一的方式對接各種 LLM。官方 GitHub 有詳細的配置和使用示例。
地址:https://github.com/portkey-ai/gateway?source=post_page-----c29524999bcf---------------------------------------
總結
AI 開發生態日新月異,要想快速構建強大的 AI 應用,這些 JavaScript 庫值得你優先關注:
- Composio —— 一站式集成多平臺、快速構建 AI Agent
- Instructor-JS —— 結構化提取并校驗 LLM 輸出
- CopilotKit —— 在 React 項目中快速嵌入 AI 助手
- E2B —— 安全的云端代碼執行環境
- LanceDB —— 高性能向量數據庫
- Trigger.Dev —— 穩定且可重試的長時異步任務
- Vercel AI SDK —— 全棧 TS AI 開發首選
- Julep —— 為 AI 提供類似 Firebase 的后端支持
- Gateway —— 一個接口整合 200+ LLM,便于快速擴展
這些庫不僅能簡化你的開發流程,也能讓你的 AI 應用更靈活、更高效。可以試著逐一體驗,尋找最適合自己業務需求的組合。
你平時在開發中還用過哪些不錯的 AI 工具或框架?歡迎分享心得,讓我們一起把 AI 產品做得更好!