1. Use Cases

Currently, the SiliconCloud large model API platform defaults to generating unstructured text. However, in certain use cases, you may want the model to output content in a structured format. However, instructing the large model directly with prompts may not yield the correct structured output.

As a standardized and lightweight data exchange format, the JSON schema is an important feature that supports the large model API to produce structured outputs. When you call the large model’s API to make a request, the model returns results in JSON format, which is easy for humans to read and write, and also easy for machines to parse and generate.

Now, all major language models on the SiliconCloud platform, except for the R1 series and V3 models from DeepSeek, support JSON mode, allowing the model to output JSON-formatted strings to ensure that the model outputs in the expected structure, making it easier to logically parse the output content.

For example, you can now try structured output using the SiliconCloud API for the following cases:

  • Building a news database from company-related reports, including news titles and links.
  • Extracting sentiment analysis structures from product purchase reviews, including sentiment polarity (positive, negative, neutral), sentiment intensity, and sentiment keywords.
  • Extracting product lists from purchase history, including product information, recommendation reasons, prices, and promotional information.

2. Usage

Add in the request:

response_format={"type": "json_object"}

3. Supported Model List

Currently, online, except for the DeepSeek R1 series and V3 models, all other large language models support the aforementioned parameters.

Note: The supported models may change, please refer to this document for the latest list of supported models.
Your application must detect and handle edge cases that may result in incomplete JSON objects from the model output.
Please set max_tokens reasonably to prevent JSON strings from being interrupted.

4. Usage Examples

Below is an example using OpenAI:

import json  
from openai import OpenAI

client = OpenAI(
    api_key="您的 APIKEY", # 从https://cloud.siliconflow.cn/account/ak获取
    base_url="https://api.siliconflow.cn/v1"
)

response = client.chat.completions.create(
        model="deepseek-ai/DeepSeek-V2.5",
        messages=[
            {"role": "system", "content": "You are a helpful assistant designed to output JSON."},
            {"role": "user", "content": "? 2020 年世界奥运会乒乓球男子和女子单打冠军分别是谁? "
             "Please respond in the format {\"男子冠军\": ..., \"女子冠军\": ...}"}
        ],
        response_format={"type": "json_object"}
    )

print(response.choices[0].message.content)

The model will output:

{"男子冠军": "马龙", "女子冠军": "陈梦"}