Get startedGet started for free

Generating structured output

1. Generating structured output

Welcome back to this video on how to structure responses that are standardized and compatible with JSON.

2. Structured output in JSON

Imagine working at a food and beverage analytics company that provides automated market reports to restaurant chains. Instead of manually reviewing sales data and trends, we want Llama to generate structured insights that can be a dashboard's input data. Plain text responses won't work well for this use case, and we'll need structured data that will be processed automatically. Let's look at how we can use Llama to generate structured data that's compatible with JSON.

3. JSON responses with chat completion

To get JSON responses, we use the chat completions function and specify 'json_object' as the 'response_format'. Let's say we want to see top-selling beverages for the current year. We use the first dictionary in the messages list to set the system role, defining Llama as a food industry market analyst for this task. We then pass the user request, where we ask for our structured data on top-selling beverages.

4. JSON responses with chat completion

Once we have set up our messages list, we call create_chat_completion(), passing in the messages. We set one more key parameter here, the 'response_format', that we specify as 'json_object'. This ensures that Llama generates a structured JSON response rather than free-flowing text.

5. Extracting the JSON response

The model now returns a well-organized JSON object, making it easy, for example, to integrate the data into an automated reporting system.

6. Defining a schema

We can further control JSON responses by specifying a schema: a set of rules that define how the data should be formatted. Let's say we need a structured sales report for food products with specific fields. We can specify this as a dictionary where 'type' is 'object', to specify the JSON object, and 'properties' are the fields needed, such as the product name, category, and sales growth. Each of these should have a 'type', specifying the expected data type, such as string or float.

7. Defining a schema

This ensures that every response follows the same format, preventing inconsistent data from being generated.

8. Let's practice!

Let's generate some structured outputs to feed into our data pipelines.