1. Use Cases

By default, the HorayAI API platform generates unstructured text as output. However, in certain applications, you may need the model to produce output in a structured format. Simply instructing the model through prompts often fails to yield properly structured output.

As a standardized and lightweight data exchange format, JSON mode enables large language models (LLMs) to generate structured outputs. When you send a request to the API, the model’s response can be returned in JSON format, making it easy for both humans to read and write, and for machines to parse and generate.

Now, most language models on the HorayAI platform support JSON mode, ensuring that the model outputs strings in JSON format. This allows you to process the output logically and ensures it adheres to the expected structure.

For instance, you can use the HorayAI API to attempt structured output in the following cases:

  • Building a news database from company-related reports, including news titles and links.
  • Extracting sentiment analysis structures from product reviews, such as sentiment polarity (positive, negative, neutral), sentiment intensity, and sentiment keywords.

2. Usage

Include the following parameter in your request:

response_format={"type": "json_object"}

3. Supported Models

Currently, all available large language models support the above parameter.

The list of supported models may change over time. Refer to this documentation for the latest updates.
Your application must handle edge cases where the model might output incomplete JSON objects.
Set max_tokens appropriately to prevent JSON strings from being truncated.

4. Example Usage

Here is an example using the openai library:

import json  
from openai import OpenAI

client = OpenAI(
    api_key="Your API KEY", # get from https://dash.horay.ai/account/ak
    base_url="https://api.horay.ai/v1"
)

response = client.chat.completions.create(
        model="deepseek-ai/DeepSeek-V2-Chat",
        messages=[
            {"role": "system", "content": "You are a helpful assistant designed to output JSON."},
            {"role": "user", "content": "? Who were the men's and women's singles champions of the 2020 Olympic Games table tennis? "
             "Please respond in the format {\"Men's Champion\": ..., \"Women's Champion\": ...}"}
        ],
        response_format={"type": "json_object"}
    )

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

模型将输出:

{"Men's Champion": "Ma Long", "Women's Champion": "Chen Meng"}