惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Engineering at Meta
Engineering at Meta
The GitHub Blog
The GitHub Blog
博客园_首页
T
The Blog of Author Tim Ferriss
H
Hackread – Cybersecurity News, Data Breaches, AI and More
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
腾讯CDC
I
InfoQ
量子位
J
Java Code Geeks
P
Proofpoint News Feed
有赞技术团队
有赞技术团队
Webroot Blog
Webroot Blog
Martin Fowler
Martin Fowler
D
Docker
F
Fortinet All Blogs
云风的 BLOG
云风的 BLOG
V
Vulnerabilities – Threatpost
罗磊的独立博客
P
Proofpoint News Feed
T
The Exploit Database - CXSecurity.com
Cyberwarzone
Cyberwarzone
P
Privacy & Cybersecurity Law Blog
Last Week in AI
Last Week in AI
爱范儿
爱范儿
The Hacker News
The Hacker News
S
SegmentFault 最新的问题
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
V
V2EX
Simon Willison's Weblog
Simon Willison's Weblog
AI
AI
Y
Y Combinator Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
GbyAI
GbyAI
V
Visual Studio Blog
H
Heimdal Security Blog
S
Secure Thoughts
B
Blog RSS Feed
雷峰网
雷峰网
T
Tenable Blog
C
Check Point Blog
G
Google Developers Blog
大猫的无限游戏
大猫的无限游戏
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
A
About on SuperTechFans
Recent Commits to openclaw:main
Recent Commits to openclaw:main

博客园 - Zhentiw

[GenAI] Migration Plan: Flat API → Layered Architecture [Skill] Frontend Design Skill Claude Code Hooks: Complete Guide [GenAI] Pre-retieval overview [GenAI] About Indexing [GenAI] Indexing overview [Vibe Coding] 降低大模型幻觉 - 重试机制 [Vibe coding] 降低大模型幻觉 - JSON 安全输出提示词 [Node.js] WebSocket基础知识 [LangGraph] 应用结构 [LangGrpah] Unit testing [LangGraph] Functional API [LangGraph] 中断注意事项 [LangGraph] 中断相关细节 [LangGrpah] 静态断点 [LangGrpah] 非阻塞式中断 [LangGraph] 阻塞式中断 [LangGraph] 语义搜索 [LangGraph] 长期记忆 [LangGraph] 管理短期记忆 [LangGraph] 自定义checkpointer [LangGraph] 短期记忆 [LangGraph] 时间旅行 [LangGraph] checkpoint常用API [LangGraph] 流 [Vitest] mockClear, mockReset, mockRestore [LangGraph] 将子图添加为节点
[LangGraph] 元数据标记
Zhentiw · 2026-03-04 · via 博客园 - Zhentiw

元数据标记,就是在创建模型时附加tag,这些tag会出现在流式输出或metadata中,从而用于标记、归类不同模型调用。

举个例子,假设一个节点里调用了两个模型:

  • 一个模型负责写笑话
  • 一个模型负责写诗

它们都是流式输出token。

这个时候就会遇到一个问题:

如果前端想“只显示笑话模型的 token”,就必须知道:每个token来自哪个模型?

这就是tags的作用:模型被打上tag后,所有token的metadata中都会附带这些tag。

前端可以在流式监听中写:

if (metadata.tags?.includes("joke")) {
  // 只处理 joke 模型的 token
}

这意味着:

  • tags = LLM 调用的身份标记
  • tags可以在流式输出中精准识别模型来源

这在一个节点内、多模型并行情况下尤为关键 。

Server:

async function streamModelTokens(
  res: express.Response,
  topic: string,
  model: ChatOpenAI,
  kind: "joke" | "poem",
) {
  // 该方法很简单,请求模型,拿到模型的回复,返回给前端

  // 提示词
  const prompt =
    kind === "joke"
      ? `使用中文写一个关于${topic}的笑话`
      : `使用中文写一个关于${topic}的诗歌`;
  const events = await model.streamEvents(
    [{ role: "human", content: prompt }],
    {
      version: "v2",
    },
  );
  for await (const event of events) {
    // 遍历事件流,对特定的事件类型做处理
    if (event.event === "on_chat_model_stream") {
      const token = event.data.chunk?.content;
      // 说明当前触发的事件,能够拿到对应的token
      // 将这个token交给前端
      if (token) {
        res.write(
          `data: ${JSON.stringify({
            msg: { content: token },
            metadata: { tags: event.tags },
          })}\n\n`,
        );
      }
    }
  }
}```

frontend:

```ts
function startStream() {
  // 1. 获取当前用户在输入框输入的内容
  const currentTopic = topic.value.trim();

  if (!currentTopic) {
    window.alert("请先输入一个主题,例如猫、狗....");
    return;
  }

  jokeTokens.value = "";
  poemTokens.value = "";

  // 代码来到这里,说明输入框有东西

  // 建立SSE连接
  if (sse) {
    sse.close();
    sse = null;
  }

  const url = `http://localhost:3002/stream-messages?topic=${encodeURIComponent(currentTopic)}`;
  sse = new EventSource(url);

  sse.onmessage = (ev) => {
    const data: StreamEvent = JSON.parse(ev.data);

    const { msg, metadata } = data;

    // 关键:根据metadata上面的tag来判断当前传递过来的token是哪一个模型
    if (metadata.tags?.includes("joke")) {
      // 说明是笑话的token
      jokeTokens.value += msg.content;
    }

    if (metadata.tags?.includes("poem")) {
      // 说明是诗歌的token
      poemTokens.value += msg.content;
    }
  };

  sse.onerror = () => {
    console.log("SSE连接失败!");
    sse?.close();
  };
}