You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
✏️ Update multiple files for clarity and structure
- Updated content in Index.razor, SiteMap.cs, and sitemap.xml.
- Enhanced readability and organization across files.
- Aimed to improve maintainability and user understanding.
Generated by Copilot
Imagine you're building a smart home system. Just as your system needs to communicate with various devices through specific protocols,
60
-
developers need to interact with AI models through APIs. OpenAI's APIs serve as this communication bridge, allowing you to harness
61
-
the power of advanced AI models like GPT-4 in your applications.
58
+
When integrating AI into real apps you don't just want creative prose — you want predictable, machine-readable data you can act on.
59
+
"Structured output" means asking the model to return a defined JSON shape (or similar) so your code can reliably parse it and wire it
60
+
into your UI, database or business logic. This page walks through how to design prompts, use function schemas, and parse results safely
61
+
so you get deterministic, structured responses from OpenAI APIs.
62
62
</p>
63
63
</Section>
64
64
@@ -83,7 +83,7 @@
83
83
84
84
<SectionHeading="When to Use OpenAI APIs?"Level="4">
85
85
<p>
86
-
Wondering when OpenAI APIs can make a real difference? Here are some moments where they shine—and you’ll probably recognize a few from your own life or work:
86
+
Wondering when OpenAI APIs can make a real difference? Here are some moments where they shine—and where structured output is especially helpful:
87
87
</p>
88
88
<ul>
89
89
<li><b>Stuck on a blank page?</b> Instantly generate blog intros, catchy headlines, or even code snippets to kickstart your creativity.</li>
@@ -92,6 +92,7 @@
92
92
<li><b>Dreaming up new designs?</b> Use AI to brainstorm product ideas, generate marketing copy, or even create images for your next campaign.</li>
93
93
<li><b>Making your app accessible?</b> Transcribe audio, translate languages, or simplify complex text so everyone can use your product.</li>
94
94
<li><b>Personalized learning or coaching?</b> Build a tutor that adapts to each student’s needs, explains tough concepts, or quizzes them in a fun way.</li>
95
+
<li><b>APIs and integrations:</b> When you need the model to return structured data—product specs, recipe objects, or event definitions—use structured output to avoid brittle text parsing.</li>
95
96
</ul>
96
97
<p>
97
98
If you’ve ever wished for a superpower to automate, accelerate, or amplify your work—OpenAI APIs are your toolkit. The best part? You don’t need to be an AI expert to get started!
<h4>3. Structured output: system prompts, function schemas, and parsing</h4>
151
152
<p>
152
-
To have a conversation with an AI model—such as asking it to summarize a movie—you'll use the <b>Chat Completions</b> endpoint. This endpoint lets you send a series of messages (like a chat history) and receive a smart, context-aware response from the model.
153
-
154
-
The request body for this endpoint typically includes:
155
-
<ul>
156
-
<li>
157
-
<b>model</b>: The name of the AI model you want to use (e.g., <code>gpt-4</code> or <code>gpt-3.5-turbo</code>). Different models have different capabilities and costs.
158
-
</li>
159
-
<li>
160
-
<b>messages</b>: An array of message objects representing the conversation so far. Each message has a <code>role</code> (such as <code>system</code>, <code>user</code>, or <code>assistant</code>) and <code>content</code> (the actual text). This structure allows the model to understand context and respond appropriately.
161
-
</li>
162
-
<li>
163
-
<b>max_tokens</b>: The maximum number of tokens (words or word pieces) in the response. This helps control the length and cost of the output.
164
-
</li>
165
-
<li>
166
-
<b>Other optional parameters</b>: You can also specify things like <code>temperature</code> (controls randomness/creativity), <code>top_p</code> (controls diversity), and more, depending on your needs.
167
-
</li>
168
-
</ul>
169
-
Here’s how you might use it in practice:
153
+
If you want machine-readable results, design for structured output. There are three main techniques that work well together:
170
154
</p>
171
-
<p><b>Example prompt:</b> <code>Give me a summary of the movie Inception.</code></p>
172
-
<CodeSnippetLanguage="csharp">
173
-
public async Task<string> GetChatResponseAsync()
<li><b>System prompt constraints</b> — tell the model to respond only with a JSON object and describe the exact fields and limits (max length, array sizes). A clear system message reduces ambiguity.</li>
157
+
<li><b>Function schema / structured definitions</b> — use the API's function schema (name + JSON Schema) to signal the desired structure. The model may return the object as a <code>function_call</code>, with the structured payload in <code>function_call.arguments</code>.</li>
158
+
<li><b>Parsing & validation</b> — never assume perfect output. Prefer <code>function_call.arguments</code> when present, strip surrounding text or fences, parse JSON safely, and enforce length / item limits in your code.</li>
159
+
</ol>
188
160
189
-
varresponse=await_httpClient.SendAsync(request);
190
-
response.EnsureSuccessStatusCode();
161
+
<p><b>Practical example — generating a movie details object</b></p>
162
+
<p>
163
+
The demo on this page uses a helper method <code>CreateMovieDetailsAsync</code> that follows this pattern: it sends a system instruction that requests a single JSON object with fields like <code>title</code>, <code>year</code>, <code>director</code>, <code>description</code>, <code>genres</code> and <code>actors</code>. It also provides a function schema describing types and limits. When the response arrives the method prefers <code>function_call.arguments</code> (if the model used a function call), extracts the JSON, and then trims and validates the fields before returning a typed <code>MovieDetails</code> record to the app.
"Constraints: title must be max 100 characters; year must be a valid year number; director must be max 80 characters; genres must be an array with at most 5 items; each genre max 30 characters; actors must be an array with at most 10 items; each actor max 60 characters.\n"+
45
-
"If you cannot create values that meet these constraints, truncate fields to the required length. Respond only with a single JSON object and nothing else.";
43
+
"You are a JSON generator. Given a movie name, produce a single JSON object ONLY (no surrounding text, no markdown) with the exact shape:\n"+
"Constraints: title must be max 100 characters; year must be a valid year number; director must be max 80 characters; description must be max 150 characters; genres must be an array with at most 5 items; each genre max 30 characters; actors must be an array with at most 10 items; each actor max 60 characters.\n"+
46
+
"If you cannot create values that meet these constraints, truncate fields to the required length. Respond only with a single JSON object and nothing else.";
46
47
47
48
varfunctions=new[]
48
49
{
@@ -58,6 +59,7 @@ public record MovieDetails(string Title, int Year, string Director, string[] Gen
58
59
title=new{type="string",maxLength=100,description="Movie title (max 100 chars)"},
0 commit comments