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
Copy file name to clipboardExpand all lines: articles/ai-foundry/model-inference/includes/use-structured-outputs/python.md
+134Lines changed: 134 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -239,6 +239,140 @@ except ValidationError as e:
239
239
print(f"Validation error: {e}")
240
240
```
241
241
242
+
## Structured outputs in images
243
+
244
+
You can use structured outputs with multi-modal models to extract information from data like images. Let's consider the following chart:
245
+
246
+
:::image type="content" source="../../media/use-structured-outputs/example_graph_treecover.png" alt-text="An example image showing a chart with the annual loss in thousand square kilometers of global tree cover across different climate zones." lightbox="../../media/use-structured-outputs/example_graph_treecover.png":::
247
+
248
+
Let's load this image:
249
+
250
+
```python
251
+
from azure.ai.inference.models import ImageContentItem, ImageUrl
252
+
253
+
image_graph = ImageUrl.load(
254
+
image_file="example_graph_treecover.png",
255
+
image_format="png"
256
+
)
257
+
```
258
+
259
+
Let's define a generic schema that can be use to encode the information contained in a chart. We use [Pyndatic objects](#use-pydantic-objects) as described before.
260
+
261
+
```python
262
+
from pydantic import BaseModel
263
+
264
+
classDataPoint(BaseModel):
265
+
x: float
266
+
y: float
267
+
serie: str
268
+
269
+
classGraph(BaseModel):
270
+
title: str
271
+
description: str
272
+
x_axis: str
273
+
y_axis: str
274
+
legend: list[str]
275
+
data: list[DataPoint]
276
+
```
277
+
278
+
Let's use structured outputs to extract the information:
279
+
280
+
```python
281
+
response = client.complete(
282
+
response_format=JsonSchemaFormat(
283
+
name="graph_schema",
284
+
schema=Graph.model_json_schema(),
285
+
description="Describes the data in the graph",
286
+
strict=False,
287
+
),
288
+
messages=[
289
+
SystemMessage("""
290
+
Extract the information from the graph. Extrapolate the values of the x axe to ensure you have the correct number
291
+
of data points for each of the years from 2001 to 2023. Scale the values of the y axes to account for the values
:::image type="content" source="../../media/use-structured-outputs/graph_treecover_plot.png" alt-text="The resulting plot of the data contained in the structured output generated by the model." lightbox="../../media/use-structured-outputs/graph_treecover_plot.png":::
374
+
375
+
242
376
### Supported schemas
243
377
244
378
There are some limitations that models may place in how schemas are defined. When using structure outputs, consider the following limitations. Notice that limitations may vary per model.
0 commit comments