استخدام Structured Outputs وJSON مع نماذج AI | Using Structured Outputs and JSON with AI Models
تعلّم ببساطة، خطوة بخطوة
Learn simply, step by step
معلومة واضحة، ثم خطوة جديدة.
Clear knowledge, one step at a time.
استخدام Structured Outputs وJSON مع نماذج AI Using Structured Outputs and JSON with AI Models
ما هي Structured Outputs في AI؟
تساعد Structured Outputs في AI على جعل استجابة نموذج الذكاء الاصطناعي منظمة في شكل يمكن للتطبيقات وأنظمة الأتمتة التعامل معه بسهولة، بدلًا من الحصول على نص حر يصعب معالجته برمجيًا.
على سبيل المثال، إذا طلبنا من نموذج AI تحليل معلومات عن عملة رقمية، فقد يعيد إجابة نصية عادية. لكن التطبيق قد يحتاج إلى قيم محددة مثل اسم العملة والتصنيف ومستوى المخاطر.
هنا يمكن استخدام بيانات منظمة مثل:
{
"name": "Bitcoin",
"category": "Cryptocurrency",
"risk": "Medium"
}
بهذه الطريقة يستطيع التطبيق قراءة كل قيمة واستخدامها بشكل مستقل.
ما هو JSON؟
JSON اختصار لـ JavaScript Object Notation، وهو تنسيق شائع لتبادل البيانات بين التطبيقات وAPIs والأنظمة المختلفة.
يتكون JSON عادةً من أسماء للحقول وقيم مرتبطة بها. على سبيل المثال:
{
"title": "AI News",
"category": "Artificial Intelligence",
"priority": "High"
}
في هذا المثال توجد ثلاثة حقول واضحة: title وcategory وpriority.
النص العادي أم البيانات المنظمة؟
إذا كانت النتيجة مخصصة للقراءة فقط، فقد يكون النص العادي مناسبًا. لكن عندما تحتاج النتيجة إلى الانتقال إلى برنامج أو API أو Workflow، تصبح البيانات المنظمة أكثر فائدة.
على سبيل المثال، بدل أن يكتب النموذج:
عنوان الخبر هو AI News، والتصنيف Artificial Intelligence، والأولوية High.
يمكن إرجاع نفس المعلومات في JSON، ثم يستطيع الـWorkflow استخدام كل حقل في خطوة مختلفة.
كيف تعمل Structured Outputs في AI؟
الفكرة الأساسية هي تحديد البنية التي نريد أن تتبعها استجابة النموذج. ويمكن أن تتضمن هذه البنية أسماء الحقول وأنواع البيانات المطلوبة والقواعد التي يجب أن تتبعها النتيجة.
بشكل مبسط:
Prompt → AI Model → Structured Output → JSON → Application / Workflow
وهذا يجعل Structured Outputs في AI مفيدة جدًا عند ربط نماذج الذكاء الاصطناعي بالتطبيقات وأنظمة الأتمتة.
ما هو Schema؟
الـSchema هو وصف للبنية التي يجب أن تتبعها البيانات. فهو يحدد الحقول التي نتوقعها، ويمكن أيضًا أن يحدد نوع كل قيمة.
على سبيل المثال، قد نريد من النموذج إرجاع:
{
"coin": "Bitcoin",
"score": 85,
"safe": true
}
هنا لدينا أنواع مختلفة من البيانات:
- coin — نص.
- score — رقم.
- safe — قيمة منطقية true أو false.
هل طلب JSON في Prompt يكفي دائمًا؟
ليس بالضرورة. يمكنك أن تطلب من النموذج داخل الـPrompt أن يعيد JSON فقط، وقد تحصل على النتيجة المطلوبة، لكن التعليمات النصية وحدها لا تعني دائمًا أن البنية ستكون مطابقة تمامًا لما يتوقعه التطبيق.
عندما يوفر الـAPI ميزة Structured Outputs أو آلية مشابهة تعتمد على Schema، يمكن استخدامها لفرض بنية أكثر تحديدًا وموثوقية بدل الاعتماد فقط على تعليمات الـPrompt.
مثال عملي داخل Workflow
لنفترض أن لدينا Workflow يستقبل خبرًا ويرسله إلى نموذج AI لتحديد العنوان والتصنيف والأولوية.
يمكن أن تكون النتيجة:
{
"title": "New AI Model Released",
"category": "Artificial Intelligence",
"priority": "High"
}
بعد ذلك يستطيع الـWorkflow استخدام:
- title لنشر عنوان الخبر.
- category لاختيار التصنيف المناسب.
- priority لتحديد أولوية المعالجة أو النشر.
وهنا تظهر أهمية البيانات المنظمة في الأتمتة؛ فالنتيجة لم تعد مجرد نص للقراءة، بل أصبحت بيانات يستطيع النظام استخدامها تلقائيًا.
لماذا Structured Outputs مهمة لتطبيقات AI؟
تسمح Structured Outputs في AI بتحويل نتائج النماذج إلى بيانات أكثر سهولة في المعالجة، ويمكن استخدامها في قواعد البيانات وAPIs وWorkflows وواجهات التطبيقات.
وهي مفيدة خصوصًا عندما تريد أن تتخذ الخطوة التالية في النظام قرارًا اعتمادًا على قيمة محددة داخل استجابة النموذج.
استخدام Prompts مع البيانات المنظمة
حتى عند استخدام Structured Outputs، يظل من المهم كتابة تعليمات واضحة تحدد المهمة المطلوبة من النموذج والبيانات التي يجب تحليلها.
يمكنك تجربة إنشاء تعليمات منظمة باستخدام
AI Prompt Generator من TheCrypTechAI.
تعرّف أكثر على Structured Outputs
يمكنك الاطلاع على
دليل Structured Outputs الرسمي من OpenAI
لرؤية كيفية استخدام Schema للحصول على استجابات منظمة عند بناء تطبيقات AI.
الخلاصة
تساعد Structured Outputs في AI على جعل نتائج نماذج الذكاء الاصطناعي منظمة وقابلة للاستخدام داخل التطبيقات والـWorkflows. ويعتبر JSON من أكثر التنسيقات شيوعًا لتمثيل هذه البيانات.
وفهم JSON وSchema والفرق بين النص الحر والبيانات المنظمة يمهد للخطوة التالية: ربط نموذج AI بمعلومات ومصادر خارجية بدل الاعتماد فقط على السياق المرسل مباشرة إلى النموذج.
What Are AI Structured Outputs?
AI Structured Outputs help make an AI model's response organized in a format that applications and automation systems can process easily, instead of returning free-form text that may be difficult to handle programmatically.
For example, if we ask an AI model to analyze information about a cryptocurrency, it may return a normal text response. However, an application may need specific values such as the coin name, category, and risk level.
In this case, we can use structured data such as:
{
"name": "Bitcoin",
"category": "Cryptocurrency",
"risk": "Medium"
}
This allows the application to read and use each value separately.
What Is JSON?
JSON stands for JavaScript Object Notation. It is a common format used to exchange data between applications, APIs, and different systems.
JSON usually contains field names and their associated values. For example:
{
"title": "AI News",
"category": "Artificial Intelligence",
"priority": "High"
}
This example contains three clear fields: title, category, and priority.
Normal Text or Structured Data?
If the result is only intended to be read by a person, normal text may be enough. However, when the result needs to be passed to an application, API, or Workflow, structured data becomes much more useful.
Instead of returning:
The article title is AI News, the category is Artificial Intelligence, and the priority is High.
The model can return the same information as JSON, allowing a Workflow to use each field in a different step.
How Do AI Structured Outputs Work?
The basic idea is to define the structure that we want the model's response to follow. This structure can specify field names, expected data types, and rules that the result should follow.
In a simplified form:
Prompt → AI Model → Structured Output → JSON → Application / Workflow
This makes AI Structured Outputs especially useful when connecting AI models to applications and automation systems.
What Is a Schema?
A Schema describes the structure that the data should follow. It defines the fields we expect and can also specify the type of each value.
For example, we may want the model to return:
{
"coin": "Bitcoin",
"score": 85,
"safe": true
}
Here we have different data types:
- coin — text.
- score — number.
- safe — a Boolean value of true or false.
Is Asking for JSON in a Prompt Always Enough?
Not necessarily. You can ask a model in the Prompt to return only JSON, and it may produce the expected result. However, text instructions alone do not always guarantee that the structure will exactly match what an application expects.
When an API provides Structured Outputs or a similar Schema-based feature, it can be used to enforce a more specific and reliable structure instead of relying only on Prompt instructions.
A Practical Workflow Example
Imagine a Workflow that receives a news article and sends it to an AI model to determine its title, category, and priority.
The result could look like this:
{
"title": "New AI Model Released",
"category": "Artificial Intelligence",
"priority": "High"
}
The Workflow can then use:
- title to publish the article title.
- category to select the appropriate category.
- priority to determine processing or publishing priority.
This demonstrates why structured data is important in automation. The result is no longer just text for a person to read; it becomes data that a system can use automatically.
Why Are Structured Outputs Important for AI Applications?
AI Structured Outputs make model results easier to process and use in databases, APIs, Workflows, and application interfaces.
They are particularly useful when the next step in a system needs to make a decision based on a specific value returned by the model.
Using Prompts with Structured Data
Even when using Structured Outputs, clear instructions are still important. The model needs to understand the task and the information it should analyze.
You can practice creating structured instructions with the
TheCrypTechAI AI Prompt Generator.
Learn More About Structured Outputs
You can explore the
official OpenAI Structured Outputs guide
to see how schemas can be used to produce structured responses when building AI applications.
Summary
AI Structured Outputs make AI model responses organized and easier to use inside applications and Workflows. JSON is one of the most common formats used to represent this structured data.
Understanding JSON, Schemas, and the difference between free-form text and structured data prepares us for the next step: connecting AI models to external information and data sources instead of relying only on the context provided directly to the model.
اختبر فهمك
Check Your Understanding
سؤالان سريعان لتثبيت أهم ما تعلمته.
Two quick questions to reinforce the key ideas.