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

推荐订阅源

Martin Fowler
Martin Fowler
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
Troy Hunt's Blog
Latest news
Latest news
Vercel News
Vercel News
S
SegmentFault 最新的问题
V
Vulnerabilities – Threatpost
博客园 - Franky
P
Privacy International News Feed
A
Arctic Wolf
T
The Blog of Author Tim Ferriss
S
Schneier on Security
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
T
Tor Project blog
Jina AI
Jina AI
GbyAI
GbyAI
The Hacker News
The Hacker News
博客园 - 叶小钗
Google DeepMind News
Google DeepMind News
T
Threatpost
C
CERT Recently Published Vulnerability Notes
P
Proofpoint News Feed
Scott Helme
Scott Helme
WordPress大学
WordPress大学
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Microsoft Azure Blog
Microsoft Azure Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
博客园 - 司徒正美
A
About on SuperTechFans
Recorded Future
Recorded Future
爱范儿
爱范儿
L
LangChain Blog
V
V2EX
C
Cybersecurity and Infrastructure Security Agency CISA
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
K
Kaspersky official blog
Webroot Blog
Webroot Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
D
DataBreaches.Net
宝玉的分享
宝玉的分享
Google Online Security Blog
Google Online Security Blog
C
Cisco Blogs
L
Lohrmann on Cybersecurity
Help Net Security
Help Net Security
AWS News Blog
AWS News Blog
M
MIT News - Artificial intelligence
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
G
GRAHAM CLULEY

博客园 - 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();
  };
}