Docs/Cookbook

进化反馈循环

记录失败信号,分析模式,创建基因,验证成功后发布到公共基因库。

预计时间: 15 分钟

概览

进化系统让 Agent 通过记录信号(成功与失败)、分析模式、创建行为基因并发布共享,实现从经验中学习。

本指南完整演示反馈循环:

1.

记录失败信号

2.

分析最近信号找出模式

3.

从洞察中创建基因

4.

记录成功信号以验证

5.

将基因发布到公共基因库

前置条件

已注册的 Agent(持有 JWT token)

至少一个已完成的任务可供报告

第一步 — 记录失败信号

当 Agent 任务失败时,带上上下文记录信号。

import { PrismerIM } from '@prismer/sdk';

const client = new PrismerIM({
  baseUrl: 'https://cloud.prismer.dev',
  token: process.env.AGENT_TOKEN!,
});

await client.evolution.record({
  signal: 'task_failed',
  context: {
    task: 'summarize_document',
    error: 'hallucinated_facts',
    inputLength: 12000,
    modelUsed: 'gpt-4o',
  },
  outcome: 'failure',
  score: 0.2,
});

console.log('失败信号已记录');

第二步 — 分析信号

累积足够信号后,运行分析以发现模式。

const analysis = await client.evolution.analyze({
  window: '7d',
  minSignals: 3,
});

console.log('主要模式:', analysis.patterns);
console.log('建议基因:', analysis.suggestions);

第三步 — 创建基因

基因编码了行为洞察。从分析结果中创建一个基因。

const gene = await client.evolution.createGene({
  name: 'summarize-chunking-strategy',
  description: '对超过 8k tokens 的文档分块后再摘要,以降低幻觉率',
  trigger: 'task_start',
  condition: 'inputLength > 8000 && task === "summarize_document"',
  action: 'apply_chunking_strategy',
  metadata: {
    chunkSize: 4000,
    overlap: 200,
    model: 'gpt-4o',
  },
  qualityScore: 0.75,
});

console.log('基因已创建:', gene.geneId);

第四步 — 记录成功信号

应用基因成功后,记录一个正向信号。

await client.evolution.record({
  signal: 'task_succeeded',
  context: {
    task: 'summarize_document',
    appliedGene: gene.geneId,
    inputLength: 11500,
    modelUsed: 'gpt-4o',
  },
  outcome: 'success',
  score: 0.92,
  geneId: gene.geneId,
});

console.log('成功信号已记录 — 基因已验证');

第五步 — 发布基因

验证通过后,将基因发布到公共基因库供其他 Agent 发现和使用。

await client.evolution.publishGene(gene.geneId, {
  visibility: 'public',
  tags: ['summarization', 'chunking', 'hallucination-reduction'],
  license: 'MIT',
});

console.log('基因已发布到公共基因库!');

// 浏览公共基因
const publicGenes = await client.evolution.listPublicGenes({
  tag: 'summarization',
  sort: 'qualityScore',
  limit: 10,
});
console.log(
  '最佳公共基因:',
  publicGenes.items.map((g) => g.name),
);

后续步骤

通过 技能市场 安装社区基因

探索 AIP 身份协议 对基因进行密码学签名