Re:source

Mastering Large Models 02: Are Large Models Only for Dialogue? No! Large Models Are a Universal Interface

In our daily use of large models, we typically interact with them through dialogue. Their outputs are always presented to us in natural language. However, in certain scenarios, we prefer to obtain structured outputs. Is it possible to leverage the 'intelligence' of large models to create a smart interface that can output any data we desire in a structured format?

:::note[Translation Notice] This content is automatically translated from Chinese by AI. While we strive for accuracy, there might be nuances that are lost in translation. :::

Introduction

In our daily use of large models, we typically interact with them through dialogue. Their outputs are always presented to us in natural language. However, in certain scenarios, we prefer to obtain structured outputs. Is it possible to leverage the ‘intelligence’ of large models to create a smart interface that can output any data we desire in a structured format?

In this article, we will ultimately implement a service that uses a large model to recommend playlists.

Natural Language Dialogue

The most basic use case is to find a large model application and directly ask it to recommend some playlists. For example, on DeepSeek, we can ask it to recommend some songs by Jay Chou:

Image.png

The answer is quite good, but the output is very natural language-like, making it difficult for us to extract the song titles. So, what can we do?

The Art of Prompting

One advantage of large models is that they can follow our instructions. If we want them to output structured information, we can simply tell them to do so. Adding a few-shot approach can make it even better.

Image.png

At this point, a playlist recommendation service is already set up. However, using prompts to restrict the output format of large models can sometimes lead to errors, requiring additional work to verify their accuracy. Are there other ways?

response_format

response_format is a parameter provided by the OpenAI API to restrict the output format of the model. Currently, it can only restrict the output to JSON format, with two options:

  • json_object: This is a very simple option. Once set, the model will output in JSON format. However, you still need to specify in the prompt that this option only ensures the output format is JSON at the interface level.
  • json_schema: This is a powerful option because it not only ensures JSON output but also ensures that the output follows the json_schema you set. You can think of json_schema as defining the fields of the JSON output.

Image.png

References

OpenAI Platform