api.models

化学

api.models.create({ schema })

给会议一个 JSON 系统会解答对象而不是流言。这就是将模型变成你代码中其余部分可以调用的东西的原因。

set at creationchanges the default outputJSON Schema
Using Python?

Switch any code block to Python: the choice applies to every page. Python SDK guide and resource reference.

没有一个方案,你就能得到文字,而谁消费它,谁必须读英语。有一个方案,你就能得到一个价值,而两者的区别不是方便,而是下一个函数能否写出来。

设置一

const extractor = await api.models.create({
  model: "gemini-3.5-flash",
  settings: { temperature: 0 },
  schema: {
    type: "object",
    properties: {
      supplier: { type: "string" },
      total:    { type: "number", description: "Amount including tax" },
      currency: { type: "string", description: "ISO 4217, e.g. EUR" },
      dueDate:  { type: "string", description: "ISO 8601 date" },
      lines: {
        type: "array",
        items: {
          type: "object",
          properties: { label: { type: "string" }, amount: { type: "number" } },
          required: ["label", "amount"],
        },
      },
    },
    required: ["supplier", "total", "currency"],
  },
});

const { outputs } = await extractor.generate({
  inputs: [{ type: "text", content: "Extract this invoice." },
           { type: "file", url: "https://example.com/invoice.pdf" }],
});

outputs[0].content;   // an object, not a string
extractor = api.models.create(
    model="gemini-3.5-flash",
    settings={"temperature": 0},
    schema={
        "type": "object",
        "properties": {
            "supplier": {"type": "string"},
            "total": {"type": "number", "description": "Amount including tax"},
            "currency": {"type": "string", "description": "ISO 4217, e.g. EUR"},
            "dueDate": {"type": "string", "description": "ISO 8601 date"},
            "lines": {
                "type": "array",
                "items": {
                    "type": "object",
                    "properties": {"label": {"type": "string"}, "amount": {"type": "number"}},
                    "required": ["label", "amount"],
                },
            },
        },
        "required": ["supplier", "total", "currency"],
    },
)

outputs = extractor.generate(inputs=[
    {"type": "text", "content": "Extract this invoice."},
    {"type": "file", "url": "https://example.com/invoice.pdf"},
])["outputs"]

outputs[0]["content"]   # an object, not a string

它会改变会以默认方式回答的问题

带有一种计划式答案的会话 JSON 除非呼叫要求别的东西; 届会没有一份答复文本。 您可以覆盖每个呼叫 。

await extractor.generate({ inputs });                          // JSON — the schema
await extractor.generate({ inputs, outputs: [{ type: "text" }] }); // prose, this once
extractor.generate(inputs=inputs)                              # JSON — the schema
extractor.generate(inputs=inputs, outputs=[{"type": "text"}])  # prose, this once

说明不是文件

它们是按模型阅读的,是解决模棱两可之处。 total 仅请人猜测税收; total 与“包括税收在内的数额”不相干。

字段无说明与一个
总计共计税前或税后?每个电话可能不同。“包括税收在内的数额——已结算。
日期日期日期模型喜欢任何格式。« ISO 8601 `-可分析。
状态状态状态状态状态状态状态自由文本,无约束。使用号:设定为合同。
{
  status: { type: "string", enum: ["paid", "pending", "overdue"] },
  confidence: { type: "number", description: "0 to 1. Below 0.5 means the document was unclear." }
}
{
    "status": {"type": "string", "enum": ["paid", "pending", "overdue"]},
    "confidence": {"type": "number", "description": "0 to 1. Below 0.5 means the document was unclear."},
}
要求不确定,不要推断

模型填满给定的每个字段。一个信任编号,或一个无效字段,在文件不说“无效”的情况下,是一个空格,它如何从一个猜测中告诉读数,它花费了几个符号。

Validate it anyway

要求的形状强烈,你仍然应该检查它,然后才能采取行动——就像你验证来自你无法控制的任何服务的反应一样。

const invoice = outputs[0].content;

if (typeof invoice?.total !== "number" || !invoice.currency) {
  throw new Error("the extraction did not come back in the expected shape");
}
invoice = outputs[0]["content"]

if not isinstance(invoice, dict) or not isinstance(invoice.get("total"), (int, float)) or not invoice.get("currency"):
    raise ValueError("the extraction did not come back in the expected shape")

只有一个调用功能的形状

不需要打开会话来获得形状。 要求按电话要求—— 当每个问题都想要不同的问题时, 这样做是有用的。

await assistant.generate({
  inputs: [{ type: "text", content: "Three risks of shipping on a Friday." }],
  outputs: [{ type: "json", schema: {
    type: "object",
    properties: { risks: { type: "array", items: { type: "string" } } },
    required: ["risks"],
  } }],
});
assistant.generate(
    inputs=[{"type": "text", "content": "Three risks of shipping on a Friday."}],
    outputs=[{"type": "json", "schema": {
        "type": "object",
        "properties": {"risks": {"type": "array", "items": {"type": "string"}}},
        "required": ["risks"],
    }}],
)

保持答案大小

棋盘是随电话发送的, 所以一个大一个是每个答案的常备成本。 询问您将使用的字段; 一个包含三十种选题的棋盘, 您从不阅读的字段, 其支付金额是30倍以上 。