node.js和nest.js做智能体开发需要会哪些东西

113 阅读3分钟

✅ 一、学习路线图(从零到上线)

我把大模型应用开发拆成 6 个阶段:

阶段 1:基础能力(Node.js + TS)

  • JavaScript ES6+
  • TypeScript(类型、泛型、类、装饰器)
  • Node.js 基础:async/await、fs、path、events
  • npm / pnpm 基本指令

阶段 2:Nest.js 后端基础

  • 项目创建(CLI)
  • 模块、控制器、服务
  • 依赖注入(DI)
  • DTO + Pipe
  • Interceptor
  • 全局异常过滤
  • Swagger 文档生成

阶段 3:数据库 + 服务端能力

  • TypeORM / Prisma
  • MySQL / Postgres
  • Redis(缓存 + 限流)
  • 登录鉴权(JWT)
  • 文件上传、HTTP 请求
  • WebSocket / SSE(AI 流式响应)

阶段 4:大模型核心能力(重点)

  • OpenAI / DeepSeek / 通义 API 调用
  • Chat Completions / Responses
  • Function Calling(智能体最重要)
  • JSON 模式(用 AI 准确生成结构化数据)
  • 多模态(图片理解 / 生成)
  • 提示词工程 Prompt Engineering

阶段 5:AI 智能体(Agent)开发能力

  • Agent 工作流(workflow)
  • Memory(会话记忆)
  • 工具调用(Tools)
  • LangChain.js / LangGraph.js
  • 多智能体协作
  • 系统角色 Prompt 设计
  • 决策 Agent(Planner + Executor)

阶段 6:RAG(检索增强生成)知识库能力

  • 文档切片(chunking)
  • 文档解析(PDF/Word/网页)
  • Embedding(向量化)
  • 向量数据库(Milvus、Pinecone、Qdrant、Chroma)
  • 混合检索(关键词 + 向量)
  • RAG Pipeline(召回 → 重写 → 生成)

阶段 7:部署与工程化

  • Docker 容器化
  • Nginx
  • Linux 基本运维
  • 日志系统(Winston/Pino)
  • 监控(Sentry)
  • CI/CD(GitHub Actions)

🧭 二、详细学习顺序(逐步升级技能)

你只要按照下面这个顺序走 1~2 个月就能开发 AI 智能体项目。


📌 Step 1:TypeScript 基础(2 天)

掌握:

  • interface & type
  • class
  • 泛型
  • 装饰器(Nest.js 会大量使用)

📌 Step 2:Node.js 核心(2–4 天)

重点:

  • async/await
  • fs / path
  • HTTP 请求(axios)
  • 事件循环

📌 Step 3:Nest.js 基础(7 天)

掌握:

  • Controller、Service
  • Module + DI
  • CRUD
  • DTO + Pipe
  • Interceptor(日志 / 响应包装)
  • Swagger(API 文档)
  • 全局异常过滤

📌 Step 4:OpenAI / DeepSeek 接口(3–5 天)

掌握:

  • Chat API
  • 流式输出
  • Function Calling (工具调用)
  • JSON mode

📌 Step 5:智能体框架(10–14 天)

  • LangChain.js
  • LangGraph.js(打造真正的 Agent Workflow)
  • Memory(会话记忆)
  • Agent + Tools(工具调用)
  • 多智能体协作(Agent Team)

📌 Step 6:RAG(10–20 天)

  • Embedding
  • 向量数据库(Milvus / Qdrant / Pinecone / Chroma)
  • 文档解析
  • Query 重写
  • 混合检索
  • 最终构建一个知识库问答系统

📌 Step 7:部署(2–5 天)

  • Docker
  • 服务器部署
  • Nginx 反向代理
  • CI/CD

🧩 三、智能体 Demo(Nest.js + OpenAI)

1. 安装依赖

npm install openai

2. 新建 AIService(核心智能体服务)

// ai.service.ts
import { Injectable } from '@nestjs/common';
import OpenAI from 'openai';

@Injectable()
export class AIService {
  private client = new OpenAI({
    apiKey: process.env.OPENAI_API_KEY,
  });

  async ask(prompt: string) {
    const response = await this.client.chat.completions.create({
      model: "gpt-4o-mini",
      messages: [{ role: "user", content: prompt }],
    });

    return response.choices[0].message.content;
  }
}

3. Controller

// ai.controller.ts
import { Controller, Get, Query } from '@nestjs/common';
import { AIService } from './ai.service';

@Controller('ai')
export class AIController {
  constructor(private aiService: AIService) {}

  @Get('ask')
  async ask(@Query('q') q: string) {
    return await this.aiService.ask(q);
  }
}

4. Function Calling(智能体最重要)

const res = await client.chat.completions.create({
  model: "gpt-4o-mini",
  messages: [
    { role: "user", content: "给我查询北京今天的天气" }
  ],
  tools: [
    {
      type: "function",
      function: {
        name: "getWeather",
        description: "获取指定城市的天气",
        parameters: {
          type: "object",
          properties: {
            city: { type: "string" }
          },
          required: ["city"]
        }
      }
    }
  ]
});

🧰 四、AI 插件模板(OpenAI Plugin / Intelligent Agent Tool)

你可以做一个标准插件让 ChatGPT 调用你的 API。

目录结构

/well-known/ai-plugin.json
/openapi.yaml
/api/your-endpoint

ai-plugin.json 示例

{
  "schema_version": "v1",
  "name_for_human": "My AI Tools",
  "name_for_model": "my_tools",
  "description_for_model": "提供天气查询等工具",
  "auth": { "type": "none" },
  "api": {
    "type": "openapi",
    "url": "https://your-domain.com/openapi.yaml"
  }
}

openapi.yaml 示例

openapi: 3.0.1
info:
  title: My AI API
paths:
  /weather:
    get:
      summary: 获取天气
      parameters:
        - name: city
          in: query
          required: true
          schema:
            type: string

完成后即可被 ChatGPT / Claude 作为工具调用。


📚 五、RAG 示例项目(从零到可用)

1. 解析文档(PDF / txt)

import * as pdfParse from 'pdf-parse';

const data = await pdfParse(fileBuffer);

2. 文档切片

function chunkText(text: string, size = 500) {
  let chunks = [];
  for (let i = 0; i < text.length; i += size) {
    chunks.push(text.slice(i, i + size));
  }
  return chunks;
}

3. 向量化 Embedding

const embedding = await client.embeddings.create({
  model: "text-embedding-3-small",
  input: chunk
});

4. 存入向量数据库(以 Chroma 为例)

import { ChromaClient } from "chromadb";

5. 检索召回

const results = await collection.query({
  query_embeddings: [queryEmbedding],
  n_results: 5
});

6. 让大模型根据召回内容回答

await llm.chat.completions.create({
  model: "gpt-4o-mini",
  messages: [
    { role: "system", content: "你是一个知识库助手" },
    { role: "user", content: question },
    { role: "assistant", content: retrievedText }
  ]
});