# generate()

Canonical URL: https://developers.iagenify.com/zh/sdk/ref/assistant-session/generate/
Language: zh

`assistant.generate({ inputs, outputs }) → Promise<Result>`

问会议要什么。这是需要花掉的电话,没有最后期限的电话——一代人将持续到最后。

Status: POST /api/ai/models/:id/generate · billed · no timeout

## 参数 {#parameters}

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `inputs` | array | 必填 | 你给的东西,你看 [投入类型](/zh/sdk/concepts/inputs). |
| `outputs` | array |  | 想要回来的东西 默认为会话的形状 JSON 其有计划,案文则有。 |

将一个阵列本身作为输入,作为普通案件简表。

```javascript
await assistant.generate({ inputs: [{ type: "text", content: "Hello" }] });
await assistant.generate([{ type: "text", content: "Hello" }]);   // the same
```

## 返回值 {#returns}

```javascript
{
  ok: true,
  outputs: [{ type: "text", content: "…" }],
  usage: {
    credits: 3,            // what you were actually charged
    preAuthorized: 5,      // what was held before the call; the difference came back
    estimated: false,      // true when the provider reported no usage and it was estimated
    inputTokens: 1840,
    outputTokens: 77,
    cachedTokens: 1320,    // input served from cache, charged at a fraction
    thinkingTokens: 54
  },
  model: "gemini-3.5-flash",      // what you asked for
  servedModel: "gemini-3.5-flash", // what actually answered
  service: "text-to-text",
  settings: { },                   // on video and image: the definition, length and sound billed
  timing: { startTime, endTime, durationMs }
}
```

> **模型和已服务模式是两种不同的事实**
>
> 其中一个是意图,另一个是答案。通常匹配;如果没有,则使用模式解释结果和成本。

## 阅读成本 {#cost}

`usage.credits` 真正的收费,在对供应商报告的内容发出呼吁后解决。 `preAuthorized` 先前所持的;未使用的任何物品都回来了。 `estimated: true` 表示提供者没有报告任何,数额是得出——值得记录,因为没有衡量数字的情况就是这种情况。

```javascript
const { usage } = await assistant.generate({ inputs });

await ledger.record({
  user: userId,
  credits: usage.credits,
  estimated: usage.estimated,
  cached: usage.cachedTokens ?? 0,
  ms: /* from timing */ undefined,
});
```

## 没有关于这一呼吁的最后期限 {#timeout}

故意。服务器不会停止,因为你停止听——它完成了工作并开账单——所以一分钟后放弃意味着要支付一个你从未见过的答案。视频需要几分钟;文字过一分钟的时间足够多。

- 工作所约束的,是给提供者设定的产出上限,而不是客户方的计时器。
- 自行通过 `timeout` 如果你真的想要的话 了解客户的作为 并且不会停止
- 显示进度而不是让呼叫者等无事等待,使用 [转](/zh/sdk/ref/models/turn/streaming).

## 之后保留的内容 {#memory}

在有记忆的会话中,两种信息都储存在模型和答案上,既包括你的信息,也包括答案,二进制信息不储存:生成的图像或文件是参照而不是字节保存的。

> **自己撰写自己生成的媒体**
>
> 结果将一首字节交给您。 如果您不保存它们, 对话将记住文件已经生成, 并且无法再给你 。

## 错误 {#errors}

| 代码 | 状态 | 触发条件 |
| --- | --- | --- |
| inputs_required | 400 | 缺少或空输入 。 |
| discussion_not_found | 404 | 此账户的届会不使用此ID 。 |
| context_too_large | 400 | 比模型的窗户大一点 任何借方之前都拒绝 |
| not_enough_credits | 402 | 被提升为未足够 的批评, 携带什么 需要。 |
| generation_failed | 502 | 供应商拒绝 信息是供应商的 被编辑 |

> **discussion_not_found 是历史名称**
>
> 这正是这里缺少的会话答案所在,之所以保留它是因为融合与融合是相匹配的。 session_not_found.

```javascript
import { NotEnoughCredits, IaGenifyError } from "iagenify";

try {
  const { outputs } = await assistant.generate({ inputs });
  return outputs;
} catch (err) {
  if (err instanceof NotEnoughCredits) {
    return { error: `Needs ${err.required} credits, ${err.available} available.` };
  }
  if (err instanceof IaGenifyError && err.code === "context_too_large") {
    return { error: "This conversation is too long. Start a new one." };
  }
  if (err instanceof IaGenifyError && err.code === "generation_failed") {
    // The provider's own message, cleaned. Useful to you; usually not to your users.
    console.error("generation failed:", err.message);
    return { error: "That could not be generated." };
  }
  throw err;
}
```

`generation_failed` 提供商的信息是故意保持的:这是你请求失败的结果——一个无法读取的图像,一个不支持的组合——而细节是您可以修复它。在它到达你之前,已经清理了内部的任何信息。

- [输入类型](/zh/sdk/concepts/inputs): 文本、图片、录音、剪辑、段落——以及提供每种材料的三个方式。
- [输出类型](/zh/sdk/concepts/outputs): 回到过去,写二进制
- [额度](/zh/sdk/concepts/credits): 事先授权、和解以及资金短缺如何被拒绝。

```python
assistant.generate(inputs=[{"type": "text", "content": "Hello"}])
assistant.generate([{"type": "text", "content": "Hello"}])   # the same
```

```python
{
    "ok": True,
    "outputs": [{"type": "text", "content": "…"}],
    "usage": {
        "credits": 3,            # what you were actually charged
        "preAuthorized": 5,      # what was held before the call; the difference came back
        "estimated": False,      # True when the provider reported no usage and it was estimated
        "inputTokens": 1840,
        "outputTokens": 77,
        "cachedTokens": 1320,    # input served from cache, charged at a fraction
        "thinkingTokens": 54,
    },
    "model": "gemini-3.5-flash",        # what you asked for
    "servedModel": "gemini-3.5-flash",  # what actually answered
    "service": "text-to-text",
    "settings": {},                     # on video and image: the definition, length and sound billed
    "timing": {"startTime": ..., "endTime": ..., "durationMs": ...},
}
```

```python
result = assistant.generate(inputs=inputs)
usage = result["usage"]

ledger.record(
    user=user_id,
    credits=usage["credits"],
    estimated=usage.get("estimated"),
    cached=usage.get("cachedTokens", 0),
    ms=(result.get("timing") or {}).get("durationMs"),
)
```

```python
from iagenify import IaGenifyError, NotEnoughCredits

def ask(assistant, inputs):
    try:
        return assistant.generate(inputs=inputs)["outputs"]
    except NotEnoughCredits as error:
        return {"error": f"Needs {error.required} credits, {error.available} available."}
    except IaGenifyError as error:
        if error.code == "context_too_large":
            return {"error": "This conversation is too long. Start a new one."}
        if error.code == "generation_failed":
            # The provider's own message, cleaned. Useful to you; usually not to your users.
            print("generation failed:", error)
            return {"error": "That could not be generated."}
        raise
```
