跳到主要内容

推理

推理

模型调用只在自动化与 Agent 轮次中进行:transform、查询与操作都没有 AI。自动化通过 ctx.ai 使用宿主的 AI 设施——sys_2.infer 获取语言模型的答案,sys_1.decide 进行快速的结构化决策,embed 生成嵌入向量。需要这些能力的页面应发起自动化并关注其运行。

模型类别

// src/+workspace.ts (templates/field-operations)
ai: { models: ['default'], default: 'default', embeddings: ['scene'] }

// inside a generator in src/lib/suspicion-review.ts: an absent embedding provider is allowed
import { Effect } from 'effect';
const scene = yield* ctx.ai.embed([photo.photo], { model: 'scene' }).pipe(
  Effect.catchTag('Unavailable', () => Effect.succeed(null))
);
const vector = scene?.[0];
const scene_embedding = vector === undefined ? null : [...vector];

工作区在 +workspace.ts 的 ai 下声明它使用的模型类别(models、default,以及用于嵌入模型的 embeddings)。代码使用 default、fast 或 strong 等类别名,从不使用提供商的模型 id,由宿主把每个类别映射到具体模型。未映射的类别在调用时返回 Unavailable。

sys_2.infer

// src/lib/suspicion-review.ts (templates/field-operations, excerpt), called from an automation's run
export const DECISION = {
  kind: 'object',
  fields: {
    job_site_review: {
      kind: 'object',
      fields: {
        suspicious: { kind: 'bool' },
        reason: { kind: 'text' },
        evidence_asset_name: { kind: 'text', max: 256 }
      }
    }
    // …
  }
} as const;

const decision = yield* ctx.ai.sys_2.infer({
  model: 'default',
  prompt: suspicionPrompt(facts, representatives),
  files: photos, // FileRefs the run may read; the engine checks and reads them, the host encodes them
  output: DECISION
});

ctx.ai.sys_2.infer({ model, prompt, files?, output }) 返回一个符合声明的 output 类型的值——有类型且经过校验,从不是自由文本。每个映射的模型都是多模态的,因此 files 可以携带本次运行有权读取的图片与 PDF:引擎检查每次读取,并把字节交给宿主,由宿主为提供商编码。

sys_1.decide

ctx.ai.sys_1.decide({ state, questions }) 就一个结构化的纯文本状态向快速结构化决策模型提出一个或多个问题。问题可以是 noul(为真的概率)、在具名选项中进行的 choice,或在有序等级上的 score,每个答案的类型由其问题字面量推断。它是一次最多 3 秒的调用,不会回退到 sys_2。

embed

ctx.ai.embed(inputs, { model? }) 为每个输入返回一个向量,文件与文本嵌入在同一空间中。模型的 search.semantic 会自行使用同一设施。

失败

每次 I/O 响应的空闲期限为 20 秒,流式进度会刷新期限。失败是带类型的错误——Upstream、Timeout、RateLimited、Invalid、TooLarge、Unsupported 或 Unavailable——由调用抛出,或由其 .try 形式返回。没有 AI 设施的宿主返回 Unavailable,构建也从不要求必须有。调用按内容记入日志,因此重试的运行不会为同一请求付两次费用。