# 化学

Canonical URL: https://developers.iagenify.com/zh/sdk/ref/models/schema/
Language: zh

`api.models.create({ schema })`

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

Status: set at creation · changes the default output · JSON Schema

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

## 设置一 {#setting}

```javascript
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
```

## 它会改变会以默认方式回答的问题 {#default}

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

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

## 说明不是文件 {#descriptions}

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

| 字段 | 无说明 | 与一个 |
| --- | --- | --- |
| 总计共计 | 税前或税后?每个电话可能不同。 | “包括税收在内的数额——已结算。 |
| 日期日期日期 | 模型喜欢任何格式。 | « ISO 8601 `-可分析。 |
| 状态状态状态状态状态状态状态 | 自由文本,无约束。 | 使用号:设定为合同。 |

```javascript
{
  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 {#validate}

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

```javascript
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");
}
```

## 只有一个调用功能的形状 {#per-call}

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

```javascript
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"],
  } }],
});
```

## 保持答案大小 {#size}

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

- [输出类型](/zh/sdk/concepts/outputs): 所有的电话都能够回覆 并且写出二进制
- [generate()](/zh/sdk/ref/assistant-session/generate): 问,什么回来。

```python
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
```

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

```python
{
    "status": {"type": "string", "enum": ["paid", "pending", "overdue"]},
    "confidence": {"type": "number", "description": "0 to 1. Below 0.5 means the document was unclear."},
}
```

```python
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")
```

```python
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"],
    }}],
)
```
