1 · 安装
npm install iagenify
# yarn add iagenifypython -m pip install iagenify2 · 配置密钥
首次调用时,密钥会被交换为会话,之后不再发送。请将密钥保存在服务端。
import { IaGenify } from "iagenify";
const api = new IaGenify({
apiKey: process.env.IAGENIFY_API_KEY, // ia_...
baseUrl: "https://api.iagenify.com", // or set IAGENIFY_API_URL instead
});
console.log(api.env, api.framework); // "node" "node"
console.log(await api.getUser()); // { id, email, credits, pack }import os
from iagenify import IaGenify
api = IaGenify(
api_key=os.environ["IAGENIFY_API_KEY"],
base_url="https://api.iagenify.com",
)
print(api.env, api.framework) # "python" "python"
print(api.get_user())
api.close()3 · 生成内容
const assistant = await api.models.create({
model: "gemini-3.5-flash",
settings: { role: "Technical writer", instructions: "Answer in one paragraph." },
memory: true,
});
const result = await assistant.generate({
inputs: [{ type: "text", content: "What is an execution trail?" }],
});
console.log(result.outputs[0].content);
console.log(result.usage); // { credits: 4, inputTokens: 38, outputTokens: 96 }
await assistant.destroy();assistant = api.models.create(
model="gemini-3.5-flash",
settings={"role": "Technical writer", "instructions": "Answer in one paragraph."},
memory=True,
)
result = assistant.generate(inputs=[
{"type": "text", "content": "What is an execution trail?"},
])
print(result["outputs"][0]["content"])
print(result["usage"])
assistant.destroy()使用后删除脚本创建的会话
memory: true 会保存对话。如果测试脚本没有清理这些会话,账户中就会堆积无人查看的对话。
使用结构化输出代替自由文本
传入 JSON Schema,回答就会遵循指定结构。此时 outputs[0].content 是一个需要解析的 JSON 字符串。
const extractor = await api.models.create({
model: "gemini-3.5-flash",
memory: false,
schema: {
type: "object",
properties: {
company: { type: "string" },
amount: { type: "number" },
dueDate: { type: "string" },
},
required: ["company", "amount"],
},
});
const { outputs } = await extractor.generate({
inputs: [{ type: "text", content: invoiceText }],
});
const invoice = JSON.parse(outputs[0].content);import json
extractor = api.models.create(
model="gemini-3.5-flash",
memory=False,
schema={
"type": "object",
"properties": {
"company": {"type": "string"},
"amount": {"type": "number"},
"dueDate": {"type": "string"},
},
"required": ["company", "amount"],
},
)
response = extractor.generate(inputs=[{"type": "text", "content": invoice_text}])
invoice = json.loads(response["outputs"][0]["content"])读取文件、图片或录音
输入具有明确的类型。支持七种类型:text、image、file、video、audio、speech 和 passages。
import { readFileSync } from "node:fs";
await assistant.generate({
inputs: [
{ type: "text", content: "What does this contract commit us to?" },
{ type: "image", url: "https://example.com/page-1.png" },
{ type: "audio", content: readFileSync("./meeting.m4a").toString("base64"), mime: "audio/mp4" },
],
});import base64
from pathlib import Path
result = assistant.generate(inputs=[
{"type": "text", "content": "What does this contract commit us to?"},
{"type": "image", "url": "https://example.com/page-1.png"},
{"type": "audio", "content": base64.b64encode(Path("meeting.m4a").read_bytes()).decode(), "mime": "audio/mp4"},
])4 · 让智能体执行任务
模型会话回答问题,智能体则执行任务:读取、搜索、编辑、运行命令,并在执行无法撤销的操作前停下来征求许可。
const pc = await api.storage.get(); // this computer, as a storage
const agent = api.agents.create({
runtime: "local",
machine: pc,
model: "gemini-3.5-flash",
mode: "ask", // ask · auto · plan · full
budget: 200, // credits; the run stops rather than overrun
onPermission: async ({ tool, args, category }) => {
console.log("asking for", tool, category);
return "once"; // "once" | "always" | "deny"
},
});
const run = await agent.run({
message: "Summarise every .md file in Notes/ into Notes/summary.md.",
onEvent: (e) => {
if (e.type === "text") process.stdout.write(e.delta ?? "");
if (e.type === "tool_start") console.log("\n→", e.tool, e.summary ?? "");
if (e.type === "done") console.log("\n", agent.usage);
},
});
console.log(run.text);
console.log(agent.usage); // { credits, turns, toolCalls }
console.log(agent.context); // { tokens, limit, window, cachedTokens, parts }machine = api.storage.get()
agent = api.agents.create(
runtime="local",
machine=machine,
model="gemini-3.5-flash",
mode="ask",
budget=200,
on_permission=lambda request: "once",
)
def on_event(event):
if event["type"] == "text":
print(event.get("text", ""), end="", flush=True)
elif event["type"] == "tool_start":
print("\n", event.get("tool"), event.get("summary", ""))
run = agent.run(
message="Summarize every .md file in Notes/ into Notes/summary.md.",
on_event=on_event,
)
print(run["text"])
print(run["usage"])观察推理过程
onEvent 会接收十二种事件。界面通常基于以下五种事件构建。
| 事件 | 携带内容 |
|---|---|
| text | delta — 以流式方式传输的回答 |
| tool_start | id, tool, args, summary, category |
| tool_end | id, tool, ok, summary, credits, durationMs |
| permission | 你的 onPermission 函数要处理的请求 |
| done | 运行已结束;agent.usage 为最终用量 |