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
* A chat completions model deployment with JSON and structured outputs support. If you don't have one read [Add and configure models to Azure AI services](../../how-to/create-model-deployments.md).
30
30
31
+
* You can check which models support structured outputs by checking the column **Response format** in the [Models](../../concepts/models.md) article.
32
+
31
33
* This article uses `Cohere-command-r-plus-08-2024`.
32
34
33
35
* Initialize a client to consume the model:
@@ -109,6 +111,14 @@ with open("github_issue_schema.json", "r") as f:
109
111
github_issue_schema = json.load(f)
110
112
```
111
113
114
+
When defining schemas, follow these recommendations:
115
+
116
+
> [!div class="checklist"]
117
+
> * Use clear and expressive keys.
118
+
> * Use `_` if you need to separate words to convey meaning.
119
+
> * Create clear titles and descriptions for important keys in your structure.
120
+
> * Evaluate multiple structures until finding the one that works best for your use case.
121
+
112
122
### Use structure outputs
113
123
114
124
We can use structure outputs with the defined schema as follows:
Maintaining JSON schemas by hand is difficult and prone to errors. AI developers usually use [Pydantic](https://docs.pydantic.dev/) objects to describe the shapes of a given object. Pydantic is an open-source data validation library where you can flexibly define data structures for your applications.
160
170
171
+
### Define the schema
172
+
161
173
The following example shows how you can use Pydantic to define an schema for a GitHub issue.
162
174
163
175
```python
@@ -175,15 +187,17 @@ class Issue(BaseModel, extra="forbid"):
175
187
Some things to notice:
176
188
177
189
> [!div class="checklist"]
178
-
> We represent schemas using a class that inherits from `BaseModel`.
179
-
> We set `extra="forbid"` to instruct Pyndantic to do not accept additional properties from what we have specified.
180
-
> We use type annotations to indicate the expected types.
181
-
> `Literal` indicates we expect specific fixed values.
190
+
> *We represent schemas using a class that inherits from `BaseModel`.
191
+
> *We set `extra="forbid"` to instruct Pyndantic to do not accept additional properties from what we have specified.
192
+
> *We use type annotations to indicate the expected types.
193
+
> *`Literal` indicates we expect specific fixed values.
182
194
183
195
```python
184
196
github_issue_schema = Issue.model_json_schema()
185
197
```
186
198
199
+
### Use structure outputs
200
+
187
201
Let's see how we can use the schema in the same way:
Structured Outputs can still contain mistakes. If you see mistakes, try adjusting your instructions, providing examples in the system instructions, or splitting tasks into simpler subtasks.
246
+
247
+
The following example uses validators in Pydantic to verify the schema:
When working with software, it's more challenging to parse free-form text outputs coming from language models. Structured outputs, like JSON, provide a clear format that software routines can read and process. This article explains how to use structured outputs to generate specific JSON schemas with the chat completions API with models deployed to Azure AI model inference in Azure AI services.
20
20
21
-
## Prerequisites
22
-
23
-
To use chat completion models in your application, you need:
21
+
To use structured outputs with chat completions models in your application, you need:
* A chat completions model deployment with JSON and structure outputs support. If you don't have one read [Add and configure models to Azure AI services](../../how-to/create-model-deployments.md).
25
+
* A chat completions model deployment with JSON and structured outputs support. If you don't have one read [Add and configure models to Azure AI services](../../how-to/create-model-deployments.md).
0 commit comments