api.models

الفترة

api.models.create({ schema })

منح الدورة JSON Shema و هو يجيب على الأشياء بدلا من النثر. هذا ما يحول النموذج إلى شيء يمكن لباقي شفرتك أن تتصل به.

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.

بدون مخطط تحصل على النص، ومن يستهلك عليه أن يقرأ الإنجليزية. مع واحد، تحصل على قيمة -- والفرق ليس من السهل، هو ما إذا كان يمكن كتابة الدالة التالية على الإطلاق.

1

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 ( متى ٢٤ : ٢٨ ) — يمكن ان يُحلَّق .
الوضعنص حر، غير مُحَرّر.ا_عْطِى المُعَيَّنَةَ.
{
  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."},
}
اسأل عن عدم اليقين، لا تُخَمَّده

نموذج يملأ كل حقل من المجالات التي تعطى له، رقم ثقة، أو حقل قابل للإلغاء مع "البطل عندما لا تقول الوثيقة، هو كيف يمكنك أن تقول قراءة من تخمين - ويكلف بضعة علامات.

معاينة عليه على سبيل

الشكل مطلوب بقوة ، ويجب ان تفحصه قبل ان تعمل عليه ، بنفس الطريقة التي تتحقق بها من رد اي خدمة لا تتحكم فيها .

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

A شكل لـ نداء واحد فقط

لا داعي لأن تفتحوا جلسة لتتكوَّنوا على شكلها ، فاسألوا عنها عند الاتصال بها ، مفيدة عندما يريد كل سؤال سؤالا مختلفا .

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

حافظ عليه حجم الجواب

تم إرسال المخطط مع كل نداء، لذا فإن الكبير هو تكلفة دائمة لكل إجابة. اسأل عن الحقول التي ستستخدمها؛ مخطط مع ثلاثين حقل اختياري لم تقرأها أبداً يدفع ثمنها ثلاثين مرة أكثر.