Saat melakukan tugas ekstraksi informasi atau generasi data terstruktur, model bahasa besar (LLM) mungkin mengembalikan teks tambahan, seperti ```json, yang dapat menyebabkan kegagalan penguraian di sistem downstream. Gunakan structured output untuk memastikan LLM mengembalikan string JSON standar. Dengan mode JSON Schema, Anda juga dapat mengontrol secara tepat struktur dan tipe output, sehingga menghilangkan kebutuhan validasi atau retry tambahan.
Penggunaan
Structured output mendukung dua mode: JSON Object dan JSON Schema.
-
Mode JSON Object: Memastikan output berupa string JSON standar, tetapi tidak menjamin struktur tertentu. Untuk menggunakan mode ini:
-
Setel
response_format: Di badan permintaan, setel parameterresponse_formatke{"type": "json_object"}. -
Sertakan kata kunci JSON dalam prompt: Pesan sistem atau pesan pengguna harus mengandung kata kunci "JSON" (tidak case-sensitive). Jika tidak, model akan mengembalikan error berikut:
'messages' must contain the word 'json' in some form, to use 'response_format' of type 'json_object'.
-
Mode JSON Schema: Memastikan output sesuai dengan struktur yang ditentukan. Untuk menggunakan mode ini, setel parameter
response_formatke{"type": "json_schema", "json_schema": {"strict": true, ...}}.CatatanPrompt tidak perlu menyertakan kata kunci JSON.
Model yang didukung
Qwen
-
Model generasi teks
-
Seri Qwen Max: qwen3-max, qwen3-max-2026-01-23 (non-thinking mode), qwen3-max-2025-09-23, qwen3-max-preview (non-thinking mode), qwen-max, qwen-max-latest, qwen-max-2025-01-25, dan model snapshot versi selanjutnya
-
Seri Qwen Plus (non-thinking mode): qwen3.5-plus, qwen3.5-plus-2026-02-15, qwen-plus, qwen-plus-latest, qwen-plus-2024-12-20, dan model snapshot versi selanjutnya
-
Seri Qwen Flash (non-thinking mode): qwen3.5-flash, qwen3.5-flash-2026-02-23 dan model snapshot versi selanjutnya, qwen-flash, qwen-flash-2025-07-28 dan model snapshot versi selanjutnya
-
Seri Qwen Turbo (non-thinking mode): qwen-turbo, qwen-turbo-latest, qwen-turbo-2024-11-01, dan model snapshot versi selanjutnya
-
Seri Qwen Coder: qwen3-coder-plus, qwen3-coder-plus-2025-07-22, qwen3-coder-flash, qwen3-coder-flash-2025-07-28
-
Seri Qwen Long: qwen-long-latest, qwen-long-2025-01-25
-
-
Model generasi teks open source
-
Qwen3.5 (non-thinking mode): qwen3.5-397b-a17b, qwen3.5-122b-a10b, qwen3.5-27b, qwen3.5-35b-a3b
-
Qwen3 (non-thinking mode)
-
Qwen3-Coder
-
Model teks seri Qwen2.5 (tidak termasuk model math dan coder)
-
-
Model multimodal
-
Seri Qwen3-VL-Plus (non-thinking mode): qwen3-vl-plus, qwen3-vl-plus-2025-09-23, dan model snapshot versi selanjutnya
-
Seri Qwen3-VL-Flash (non-thinking mode): qwen3-vl-flash, qwen3-vl-flash-2025-10-15, dan model snapshot versi selanjutnya
-
Seri Qwen VL-Max: qwen-vl-max (tidak termasuk model latest dan snapshot)
-
Seri Qwen VL-Plus: qwen-vl-plus (tidak termasuk model latest dan snapshot)
-
-
Model multimodal open source
-
Qwen3-VL (non-thinking mode)
-
Model dalam thinking mode tidak mendukung structured output.
Kimi
kimi-k2-thinking
GLM
Non-thinking mode: glm-5, glm-4.7, glm-4.6
Untuk informasi tentang context window model, harga, dan versi snapshot, lihat Daftar model.
Mulai
Contoh ini mengekstraksi informasi dari profil pribadi.
Dapatkan Kunci API dan ekspor kunci API sebagai variabel lingkungan. Jika Anda menggunakan OpenAI SDK atau DashScope SDK untuk melakukan panggilan, instal SDK.
Kompatibel dengan OpenAI
Python
from openai import OpenAI
import os
client = OpenAI(
# Kunci API berbeda berdasarkan wilayah. Jika Anda belum mengonfigurasi variabel lingkungan, ganti baris berikut dengan: api_key="sk-xxx"
api_key=os.getenv("DASHSCOPE_API_KEY"),
# Jika Anda menggunakan model wilayah Beijing, ganti base_url dengan: https://dashscope.aliyuncs.com/compatible-mode/v1
base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1",
)
completion = client.chat.completions.create(
model="qwen-flash",
messages=[
{
"role": "system",
"content": "Extract the user's name and age, and return them in JSON format"
},
{
"role": "user",
"content": "Hi everyone, my name is Alex Brown, I'm 34 years old, my email is alexbrown@example.com, and I enjoy playing basketball and traveling",
},
],
response_format={"type": "json_object"}
)
json_string = completion.choices[0].message.content
print(json_string)Respons
{
"Name": "Alex Brown",
"Age": 34
}
Node.js
import OpenAI from "openai";
const openai = new OpenAI({
// Jika Anda belum mengonfigurasi variabel lingkungan, ganti baris berikut dengan: apiKey: "sk-xxx"
apiKey: process.env.DASHSCOPE_API_KEY,
// Untuk model wilayah Beijing, ganti baseURL dengan: https://dashscope.aliyuncs.com/compatible-mode/v1
baseURL: "https://dashscope-intl.aliyuncs.com/compatible-mode/v1"
});
const completion = await openai.chat.completions.create({
model: "qwen-flash",
messages: [
{
role: "system",
content: "Extract the user's name and age, and return them in JSON format"
},
{
role: "user",
content: "Hi everyone, my name is Alex Brown, I'm 34 years old, my email is alexbrown@example.com, and I enjoy playing basketball and traveling"
}
],
response_format: {
type: "json_object"
}
});
const jsonString = completion.choices[0].message.content;
console.log(jsonString);Respons
{
"name": "Alex Brown",
"age": 34
}
curl
# ======= Penting =======
# Kunci API berbeda berdasarkan wilayah. Untuk mendapatkan kunci API, kunjungi: https://www.alibabacloud.com/help/en/model-studio/get-api-key
# Jika Anda menggunakan model wilayah Beijing, ganti URL dengan: https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions
# === Hapus komentar ini sebelum eksekusi ===
curl -X POST https://dashscope-intl.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen-plus",
"messages": [
{
"role": "system",
"content": "You need to extract the name (string), age (string), and email (string). Output the result as a JSON string. Do not include any other irrelevant content.\nExamples:\nQ: My name is Zhang San, I am 25 years old, and my email is zhangsan@example.com\nA: {\"name\":\"Zhang San\",\"age\":\"25 years old\",\"email\":\"zhangsan@example.com\"}\nQ: My name is Li Si, I am 30 years old, and my email is lisi@example.com\nA: {\"name\":\"Li Si\",\"age\":\"30 years old\",\"email\":\"lisi@example.com\"}\nQ: My name is Wang Wu, my email is wangwu@example.com, and I am 40 years old\nA: {\"name\":\"Wang Wu\",\"age\":\"40 years old\",\"email\":\"wangwu@example.com\"}"
},
{
"role": "user",
"content": "Hello everyone, my name is Alex Brown, I am 34 years old, and my email is alexbrown@example.com"
}
],
"response_format": {
"type": "json_object"
}
}'Respons
{
"choices": [
{
"message": {
"role": "assistant",
"content": "{\"name\":\"Alex Brown\",\"age\":\"34 years old\"}"
},
"finish_reason": "stop",
"index": 0,
"logprobs": null
}
],
"object": "chat.completion",
"usage": {
"prompt_tokens": 207,
"completion_tokens": 20,
"total_tokens": 227,
"prompt_tokens_details": {
"cached_tokens": 0
}
},
"created": 1756455080,
"system_fingerprint": null,
"model": "qwen-plus",
"id": "chatcmpl-624b665b-fb93-99e7-9ebd-bb6d86d314d2"
}
DashScope
Python
import os
import dashscope
# Untuk model wilayah Beijing, ganti URL dengan: https://dashscope.aliyuncs.com/api/v1
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
messages=[
{
"role": "system",
"content": "Extract the user's name and age, and return them in JSON format"
},
{
"role": "user",
"content": "Hi everyone, my name is Alex Brown, I'm 34 years old, my email is alexbrown@example.com, and I enjoy playing basketball and traveling",
},
]
response = dashscope.Generation.call(
# Jika Anda belum mengonfigurasi variabel lingkungan, ganti baris berikut dengan: api_key="sk-xxx" (Kunci API Alibaba Cloud Model Studio),
api_key=os.getenv('DASHSCOPE_API_KEY'),
model="qwen-flash",
messages=messages,
result_format='message',
response_format={'type': 'json_object'}
)
json_string = response.output.choices[0].message.content
print(json_string)Respons
{
"name": "Alex Brown",
"age": 34
}
Java
Versi DashScope Java SDK harus 2.18.4 atau lebih tinggi.
// Versi DashScope Java SDK harus 2.18.4 atau lebih tinggi
import java.util.Arrays;
import java.lang.System;
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.common.ResponseFormat;
import com.alibaba.dashscope.protocol.Protocol;
public class Main {
public static GenerationResult callWithMessage() throws ApiException, NoApiKeyException, InputRequiredException {
// Untuk model wilayah Beijing, ganti URL dengan: https://dashscope.aliyuncs.com/api/v1
Generation gen = new Generation(Protocol.HTTP.getValue(), "https://dashscope-intl.aliyuncs.com/api/v1");
Message systemMsg = Message.builder()
.role(Role.SYSTEM.getValue())
.content("Extract the user's name and age, and return them in JSON format")
.build();
Message userMsg = Message.builder()
.role(Role.USER.getValue())
.content("Hi everyone, my name is Alex Brown, I'm 34 years old, my email is alexbrown@example.com, and I enjoy playing basketball and traveling")
.build();
ResponseFormat jsonMode = ResponseFormat.builder().type("json_object").build();
GenerationParam param = GenerationParam.builder()
// Jika Anda belum mengonfigurasi variabel lingkungan, ganti baris berikut dengan: .apiKey("sk-xxx") (Kunci API Alibaba Cloud Model Studio)
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
.model("qwen-flash")
.messages(Arrays.asList(systemMsg, userMsg))
.resultFormat(GenerationParam.ResultFormat.MESSAGE)
.responseFormat(jsonMode)
.build();
return gen.call(param);
}
public static void main(String[] args) {
try {
GenerationResult result = callWithMessage();
System.out.println(result.getOutput().getChoices().get(0).getMessage().getContent());
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
// Catat pengecualian menggunakan framework logging
System.err.println("Terjadi kesalahan saat memanggil layanan generasi: " + e.getMessage());
}
}
}
Respons
{
"name": "Alex Brown",
"age": 34
}
curl
# ======= Catatan penting =======
# Untuk model wilayah Beijing, ganti URL dengan: https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation
# Kunci API berbeda berdasarkan wilayah. Dapatkan kunci API: https://www.alibabacloud.com/help/zh/model-studio/get-api-key
# === Hapus komentar ini sebelum menjalankan ===
curl -X POST https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/text-generation/generation \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen-flash",
"input": {
"messages": [
{
"role": "system",
"content": "Extract the user's name and age, and return them in JSON format"
},
{
"role": "user",
"content": "Hi everyone, my name is Alex Brown, I'm 34 years old, my email is alexbrown@example.com, and I enjoy playing basketball and traveling"
}
]
},
"parameters": {
"result_format": "message",
"response_format": {
"type": "json_object"
}
}
}'Respons
{
"name": "Alex Brown",
"age": 34
}
Pemrosesan data gambar dan video
Selain teks, model multimodal mendukung structured output untuk gambar dan video, memungkinkan ekstraksi informasi visual, lokalisasi objek, dan deteksi event.
Untuk batasan file gambar dan video, lihat Pemahaman Gambar dan Video.
Kompatibel dengan OpenAI
Python
import os
from openai import OpenAI
client = OpenAI(
# Kunci API berbeda berdasarkan wilayah. Dapatkan kunci API: https://www.alibabacloud.com/help/zh/model-studio/get-api-key
api_key=os.getenv("DASHSCOPE_API_KEY"),
# Untuk model wilayah Beijing, ganti base_url dengan: https://dashscope.aliyuncs.com/compatible-mode/v1
base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1"
)
completion = client.chat.completions.create(
model="qwen3-vl-plus",
messages=[
{
"role": "system",
"content": [{"type": "text", "text": "You are a helpful assistant."}],
},
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {
"url": "http://duguang-labelling.oss-cn-shanghai.aliyuncs.com/demo_ocr/receipt_zh_demo.jpg"
},
},
{"type": "text", "text": "Extract ticket (array type, including travel_date, trains, seat_num, arrival_site, price) and invoice information (array type, including invoice_code and invoice_number) from the image. Output a JSON containing both ticket and invoice arrays"},
],
},
],
response_format={"type": "json_object"}
)
json_string = completion.choices[0].message.content
print(json_string)
Respons
{
"ticket": [
{
"travel_date": "2013-06-29",
"trains": "stream",
"seat_num": "371",
"arrival_site": "Development Zone",
"price": "8.00"
}
],
"invoice": [
{
"invoice_code": "221021325353",
"invoice_number": "10283819"
}
]
}
Node.js
import OpenAI from "openai";
const openai = new OpenAI({
// Kunci API berbeda berdasarkan wilayah. Dapatkan kunci API: https://www.alibabacloud.com/help/zh/model-studio/get-api-key
// Jika Anda belum mengonfigurasi variabel lingkungan, ganti baris berikut dengan: apiKey: "sk-xxx" (Kunci API Model Studio)
apiKey: process.env.DASHSCOPE_API_KEY,
// Untuk model wilayah Beijing, ganti base_url dengan https://dashscope.aliyuncs.com/compatible-mode/v1
baseURL: "https://dashscope-intl.aliyuncs.com/compatible-mode/v1"
});
async function main() {
const response = await openai.chat.completions.create({
model: "qwen3-vl-plus",
messages: [{
role: "system",
content: [{
type: "text",
text: "You are a helpful assistant."
}]
},
{
role: "user",
content: [{
type: "image_url",
image_url: {
"url": "http://duguang-labelling.oss-cn-shanghai.aliyuncs.com/demo_ocr/receipt_zh_demo.jpg"
}
},
{
type: "text",
text: "Extract ticket (array type, including travel_date, trains, seat_num, arrival_site, price) and invoice information (array type, including invoice_code and invoice_number) from the image. Output a JSON containing both ticket and invoice arrays"
}
]
}
],
response_format: {type: "json_object"}
});
console.log(response.choices[0].message.content);
}
main()
Respons
{
"ticket": [
{
"travel_date": "2013-06-29",
"trains": "stream",
"seat_num": "371",
"arrival_site": "Development Zone",
"price": "8.00"
}
],
"invoice": [
{
"invoice_code": "221021325353",
"invoice_number": "10283819"
}
]
}
curl
# ======= Catatan penting =======
# Untuk model wilayah Beijing, ganti base_url dengan: https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions
# Kunci API berbeda berdasarkan wilayah. Dapatkan kunci API: https://www.alibabacloud.com/help/zh/model-studio/get-api-key
# === Hapus komentar ini sebelum menjalankan ===
curl --location 'https://dashscope-intl.aliyuncs.com/compatible-mode/v1/chat/completions' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
"model": "qwen3-vl-plus",
"messages": [
{"role":"system",
"content":[
{"type": "text", "text": "You are a helpful assistant."}]},
{
"role": "user",
"content": [
{"type": "image_url", "image_url": {"url": "http://duguang-labelling.oss-cn-shanghai.aliyuncs.com/demo_ocr/receipt_zh_demo.jpg"}},
{"type": "text", "text": "Extract ticket (array type, including travel_date, trains, seat_num, arrival_site, price) and invoice information (array type, including invoice_code and invoice_number) from the image. Output a JSON containing both ticket and invoice arrays"}
]
}],
"response_format":{"type": "json_object"}
}'
Respons
{
"ticket": [
{
"travel_date": "2013-06-29",
"trains": "stream",
"seat_num": "371",
"arrival_site": "Development Zone",
"price": "8.00"
}
],
"invoice": [
{
"invoice_code": "221021325353",
"invoice_number": "10283819"
}
]
}
DashScope
Python
import os
import dashscope
# Untuk model wilayah Beijing, ganti URL dengan: https://dashscope.aliyuncs.com/api/v1
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
messages = [
{
"role": "system",
"content": [
{"text": "You are a helpful assistant."}]
},
{
"role": "user",
"content": [
{"image": "http://duguang-labelling.oss-cn-shanghai.aliyuncs.com/demo_ocr/receipt_zh_demo.jpg"},
{"text": "Extract ticket (array type, including travel_date, trains, seat_num, arrival_site, price) and invoice information (array type, including invoice_code and invoice_number) from the image. Output a JSON containing both ticket and invoice arrays"}]
}]
response = dashscope.MultiModalConversation.call(
# Jika Anda belum mengonfigurasi variabel lingkungan, ganti baris berikut dengan: api_key ="sk-xxx" (Kunci API Model Studio)
api_key = os.getenv('DASHSCOPE_API_KEY'),
model = 'qwen3-vl-plus',
messages = messages,
response_format={'type': 'json_object'}
)
json_string = response.output.choices[0].message.content[0]["text"]
print(json_string)
Respons
{
"ticket": [
{
"travel_date": "2013-06-29",
"trains": "Liushui",
"seat_num": "371",
"arrival_site": "Development Zone",
"price": "8.00"
}
],
"invoice": [
{
"invoice_code": "221021325353",
"invoice_number": "10283819"
}
]
}
Java
// Versi DashScope Java SDK harus 2.21.4 atau lebih tinggi
import java.util.Arrays;
import java.util.Collections;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversation;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationParam;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationResult;
import com.alibaba.dashscope.common.MultiModalMessage;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.exception.UploadFileException;
import com.alibaba.dashscope.common.ResponseFormat;
import com.alibaba.dashscope.utils.Constants;
public class Main {
// Untuk model wilayah Beijing, ganti URL dengan: https://dashscope.aliyuncs.com/api/v1
static {
Constants.baseHttpApiUrl="https://dashscope-intl.aliyuncs.com/api/v1";
}
public static void simpleMultiModalConversationCall()
throws ApiException, NoApiKeyException, UploadFileException {
MultiModalConversation conv = new MultiModalConversation();
MultiModalMessage systemMessage = MultiModalMessage.builder().role(Role.SYSTEM.getValue())
.content(Arrays.asList(
Collections.singletonMap("text", "You are a helpful assistant."))).build();
MultiModalMessage userMessage = MultiModalMessage.builder().role(Role.USER.getValue())
.content(Arrays.asList(
Collections.singletonMap("image", "http://duguang-labelling.oss-cn-shanghai.aliyuncs.com/demo_ocr/receipt_zh_demo.jpg"),
Collections.singletonMap("text", "Extract ticket (array type, including travel_date, trains, seat_num, arrival_site, price) and invoice information (array type, including invoice_code and invoice_number) from the image. Output a JSON containing both ticket and invoice arrays"))).build();
ResponseFormat jsonMode = ResponseFormat.builder().type("json_object").build();
MultiModalConversationParam param = MultiModalConversationParam.builder()
// Jika Anda belum mengonfigurasi variabel lingkungan, ganti baris berikut dengan: .apiKey("sk-xxx") (Kunci API Model Studio)
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
.model("qwen3-vl-plus")
.messages(Arrays.asList(systemMessage, userMessage))
.responseFormat(jsonMode)
.build();
MultiModalConversationResult result = conv.call(param);
System.out.println(result.getOutput().getChoices().get(0).getMessage().getContent().get(0).get("text"));
}
public static void main(String[] args) {
try {
simpleMultiModalConversationCall();
} catch (ApiException | NoApiKeyException | UploadFileException e) {
System.out.println(e.getMessage());
}
}
}
Respons
{
"ticket": [
{
"travel_date": "2013-06-29",
"trains": "stream",
"seat_num": "371",
"arrival_site": "Development Zone",
"price": "8.00"
}
],
"invoice": [
{
"invoice_code": "221021325353",
"invoice_number": "10283819"
}
]
}
curl
# ======= Catatan penting =======
# Untuk model wilayah Beijing, ganti URL dengan: https://dashscope.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation
# Kunci API berbeda berdasarkan wilayah. Dapatkan kunci API: https://www.alibabacloud.com/help/zh/model-studio/get-api-key
# === Hapus komentar ini sebelum menjalankan ===
curl -X POST https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
"model": "qwen3-vl-plus",
"input": {
"messages": [
{
"role": "system",
"content": [
{
"text": "You are a helpful assistant."
}
]
},
{
"role": "user",
"content": [
{
"image": "http://duguang-labelling.oss-cn-shanghai.aliyuncs.com/demo_ocr/receipt_zh_demo.jpg"
},
{
"text": "Extract ticket (array type, including travel_date, trains, seat_num, arrival_site, price) and invoice information (array type, including invoice_code and invoice_number) from the image. Output a JSON containing both ticket and invoice arrays"
}
]
}
]
},
"parameters": {
"response_format": {
"type": "json_object"
}
}
}'
Respons
{
"output": {
"choices": [
{
"message": {
"content": [
{
"text": "{\n \"ticket\": [\n {\n \"travel_date\": \"2013-06-29\",\n \"trains\": \"train number\",\n \"seat_num\": \"371\",\n \"arrival_site\": \"Development Zone\",\n \"price\": \"8.00\"\n }\n ],\n \"invoice\": [\n {\n \"invoice_code\": \"221021325353\",\n \"invoice_number\": \"10283819\"\n }\n ]\n}"
}
],
"role": "assistant"
},
"finish_reason": "stop"
}
]
},
"usage": {
"total_tokens": 598,
"input_tokens_details": {
"image_tokens": 418,
"text_tokens": 68
},
"output_tokens": 112,
"input_tokens": 486,
"output_tokens_details": {
"text_tokens": 112
},
"image_tokens": 418
},
"request_id": "b129dce1-0d5d-4772-b8b5-bd3a1d5cde63"
}
Optimalkan prompt
Prompt yang ambigu (seperti "kembalikan informasi pengguna") dapat menyebabkan model menghasilkan hasil yang tidak diharapkan. Jelaskan skema yang diharapkan secara akurat dalam prompt Anda, termasuk tipe field, status wajib, persyaratan format (seperti format tanggal), dan contoh.
Kompatibel dengan OpenAI
Python
from openai import OpenAI
import os
import json
import textwrap # Menangani indentasi untuk string multi-baris guna meningkatkan keterbacaan kode
# Respons contoh yang telah didefinisikan sebelumnya untuk menunjukkan format output yang diharapkan kepada model
# Contoh 1: Respons lengkap dengan semua field
example1_response = json.dumps(
{
"info": {"name": "Zhang San", "age": "25 years old", "email": "zhangsan@example.com"},
"hobby": ["singing"]
},
ensure_ascii=False
)
# Contoh 2: Respons dengan beberapa hobi
example2_response = json.dumps(
{
"info": {"name": "Li Si", "age": "30 years old", "email": "lisi@example.com"},
"hobby": ["dancing", "swimming"]
},
ensure_ascii=False
)
# Contoh 3: Respons tanpa field hobby (hobby opsional)
example3_response = json.dumps(
{
"info": {"name": "Zhao Liu", "age": "28 years old", "email": "zhaoliu@example.com"}
},
ensure_ascii=False
)
# Contoh 4: Respons lain tanpa field hobby
example4_response = json.dumps(
{
"info": {"name": "Sun Qi", "age": "35 years old", "email": "sunqi@example.com"}
},
ensure_ascii=False
)
# Inisialisasi klien OpenAI
client = OpenAI(
# Jika Anda belum mengonfigurasi variabel lingkungan, ganti baris berikut dengan: api_key="sk-xxx"
# Kunci API berbeda berdasarkan wilayah. Dapatkan kunci API: https://www.alibabacloud.com/help/en/model-studio/get-api-key
api_key=os.getenv("DASHSCOPE_API_KEY"),
# Ini adalah URL dasar wilayah Beijing. Jika Anda menggunakan model wilayah Singapura, ganti URL dasar dengan: https://dashscope-intl.aliyuncs.com/compatible-mode/v1
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
)
# dedent menghapus spasi awal umum dari setiap baris, memungkinkan string di-indentasi dengan rapi dalam kode tanpa menyertakan spasi ekstra saat runtime
system_prompt = textwrap.dedent(f"""\
Extract personal information from the user input and output it in the specified JSON Schema format:
[Output format requirements]
The output must strictly follow this JSON structure:
{{
"info": {{
"name": "string type, required field, user's name",
"age": "string type, required field, format 'number years old', e.g., '25 years old'",
"email": "string type, required field, standard email format, e.g., 'user@example.com'"
}},
"hobby": ["string array type, optional field, contains all user hobbies; omit entirely if not mentioned"]
}}
[Field extraction rules]
1. name: Identify the user's name from the text, must extract
2. age: Identify age information, convert to 'number years old' format, must extract
3. email: Identify email address, keep original format, must extract
4. hobby: Identify user hobbies, output as string array; omit hobby field entirely if hobbies are not mentioned
[Reference examples]
Example 1 (with hobby):
Q: My name is Zhang San, I'm 25 years old, my email is zhangsan@example.com, and my hobby is singing
A: {example1_response}
Example 2 (with multiple hobbies):
Q: My name is Li Si, I'm 30 years old, my email is lisi@example.com, and I enjoy dancing and swimming
A: {example2_response}
Example 3 (without hobby):
Q: My name is Zhao Liu, I'm 28 years old, and my email is zhaoliu@example.com
A: {example3_response}
Example 4 (without hobby):
Q: I'm Sun Qi, 35 years old, and my email is sunqi@example.com
A: {example4_response}
Extract information and output JSON strictly according to the above format and rules. Do not include the hobby field if the user doesn't mention hobbies.\
""")
# Panggil API LLM untuk ekstraksi informasi
completion = client.chat.completions.create(
model="qwen-plus",
messages=[
{
"role": "system",
"content": system_prompt
},
{
"role": "user",
"content": "Hi everyone, my name is Alex Brown, I'm 34 years old, my email is alexbrown@example.com, and I enjoy playing basketball and traveling",
},
],
response_format={"type": "json_object"}, # Tentukan pengembalian format JSON
)
# Ekstrak dan cetak hasil JSON yang dihasilkan model
json_string = completion.choices[0].message.content
print(json_string)Respons
{
"info": {
"name": "Alex Brown",
"age": "34 years old",
"email": "alexbrown@example.com"
},
"hobby": ["Basketball", "Traveling"]
}
Node.js
import OpenAI from "openai";
// Respons contoh yang telah didefinisikan sebelumnya (untuk menunjukkan format output yang diharapkan kepada model)
// Contoh 1: Respons lengkap dengan semua field
const example1Response = JSON.stringify({
info: { name: "Zhang San", age: "25 years old", email: "zhangsan@example.com" },
hobby: ["singing"]
}, null, 2);
// Contoh 2: Respons dengan beberapa hobi
const example2Response = JSON.stringify({
info: { name: "Li Si", age: "30 years old", email: "lisi@example.com" },
hobby: ["dancing", "swimming"]
}, null, 2);
// Contoh 3: Respons tanpa field hobby (hobby opsional)
const example3Response = JSON.stringify({
info: { name: "Zhao Liu", age: "28 years old", email: "zhaoliu@example.com" }
}, null, 2);
// Contoh 4: Respons lain tanpa field hobby
const example4Response = JSON.stringify({
info: { name: "Sun Qi", age: "35 years old", email: "sunqi@example.com" }
}, null, 2);
// Inisialisasi konfigurasi klien OpenAI
const openai = new OpenAI({
// Jika Anda belum mengonfigurasi variabel lingkungan, ganti baris berikut dengan: apiKey: "sk-xxx" (Kunci API Alibaba Cloud Model Studio),
// Kunci API berbeda berdasarkan wilayah. Dapatkan kunci API: https://www.alibabacloud.com/help/en/model-studio/get-api-key
apiKey: process.env.DASHSCOPE_API_KEY,
// Ini adalah URL dasar wilayah Beijing. Jika Anda menggunakan model wilayah Singapura, ganti URL dasar dengan: https://dashscope-intl.aliyuncs.com/compatible-mode/v1
baseURL: "https://dashscope.aliyuncs.com/compatible-mode/v1"
});
// Buat permintaan penyelesaian chat menggunakan prompt terstruktur untuk meningkatkan akurasi output
const completion = await openai.chat.completions.create({
model: "qwen-plus",
messages: [
{
role: "system",
content: `Extract personal information from the user input and output it in the specified JSON Schema format:
[Output format requirements]
The output must strictly follow this JSON structure:
{
"info": {
"name": "string type, required field, user's name",
"age": "string type, required field, format 'number years old', e.g., '25 years old'",
"email": "string type, required field, standard email format, e.g., 'user@example.com'"
},
"hobby": ["string array type, optional field, contains all user hobbies; omit entirely if not mentioned"]
}
[Field extraction rules]
1. name: Identify the user's name from the text, must extract
2. age: Identify age information, convert to 'number years old' format, must extract
3. email: Identify email address, keep original format, must extract
4. hobby: Identify user hobbies, output as string array; omit hobby field entirely if hobbies are not mentioned
[Reference examples]
Example 1 (with hobby):
Q: My name is Zhang San, I'm 25 years old, my email is zhangsan@example.com, and my hobby is singing
A: ${example1Response}
Example 2 (with multiple hobbies):
Q: My name is Li Si, I'm 30 years old, my email is lisi@example.com, and I enjoy dancing and swimming
A: ${example2Response}
Example 3 (without hobby):
Q: My name is Zhao Liu, I'm 28 years old, and my email is zhaoliu@example.com
A: ${example3Response}
Example 4 (without hobby):
Q: I'm Sun Qi, 35 years old, and my email is sunqi@example.com
A: ${example4Response}
Extract information and output JSON strictly according to the above format and rules. Do not include the hobby field if the user doesn't mention hobbies.`
},
{
role: "user",
content: "Hi everyone, my name is Alex Brown, I'm 34 years old, my email is alexbrown@example.com, and I enjoy playing basketball and traveling"
}
],
response_format: {
type: "json_object"
}
});
// Ekstrak dan cetak hasil JSON yang dihasilkan model
const jsonString = completion.choices[0].message.content;
console.log(jsonString);Respons
{
"info": {
"name": "Alex Brown",
"age": "34 years old",
"email": "alexbrown@example.com"
},
"hobby": [
"playing basketball",
"traveling"
]
}
DashScope
Python
import os
import json
import dashscope
# Jika Anda menggunakan model wilayah Singapura, hapus komentar pada baris berikut
# dashscope.base_http_api_url = "https://dashscope-intl.aliyuncs.com/api/v1"
# Respons contoh yang telah didefinisikan sebelumnya (untuk menunjukkan format output yang diharapkan kepada model)
example1_response = json.dumps(
{
"info": {"name": "Zhang San", "age": "25 years old", "email": "zhangsan@example.com"},
"hobby": ["singing"]
},
ensure_ascii=False
)
example2_response = json.dumps(
{
"info": {"name": "Li Si", "age": "30 years old", "email": "lisi@example.com"},
"hobby": ["dancing", "swimming"]
},
ensure_ascii=False
)
example3_response = json.dumps(
{
"info": {"name": "Wang Wu", "age": "40 years old", "email": "wangwu@example.com"},
"hobby": ["Rap", "basketball"]
},
ensure_ascii=False
)
messages=[
{
"role": "system",
"content": f"""Extract personal information from the user input and output it in the specified JSON Schema format:
[Output format requirements]
The output must strictly follow this JSON structure:
{{
"info": {{
"name": "string type, required field, user's name",
"age": "string type, required field, format 'number years old', e.g., '25 years old'",
"email": "string type, required field, standard email format, e.g., 'user@example.com'"
}},
"hobby": ["string array type, optional field, contains all user hobbies; omit entirely if not mentioned"]
}}
[Field extraction rules]
1. name: Identify the user's name from the text, must extract
2. age: Identify age information, convert to 'number years old' format, must extract
3. email: Identify email address, keep original format, must extract
4. hobby: Identify user hobbies, output as string array; omit hobby field entirely if hobbies are not mentioned
[Reference examples]
Example 1 (with hobby):
Q: My name is Zhang San, I'm 25 years old, my email is zhangsan@example.com, and my hobby is singing
A: {example1_response}
Example 2 (with multiple hobbies):
Q: My name is Li Si, I'm 30 years old, my email is lisi@example.com, and I enjoy dancing and swimming
A: {example2_response}
Example 3 (with multiple hobbies):
Q: My email is wangwu@example.com, I'm 40 years old, my name is Wang Wu, and I can Rap and play basketball
A: {example3_response}
Extract information and output JSON strictly according to the above format and rules. Do not include the hobby field if the user doesn't mention hobbies."""
},
{
"role": "user",
"content": "Hi everyone, my name is Alex Brown, I'm 34 years old, my email is alexbrown@example.com, and I enjoy playing basketball and traveling",
},
]
response = dashscope.Generation.call(
# Jika Anda belum mengonfigurasi variabel lingkungan, ganti baris berikut dengan: api_key="sk-xxx" (Kunci API Alibaba Cloud Model Studio),
api_key=os.getenv('DASHSCOPE_API_KEY'),
model="qwen-plus",
messages=messages,
result_format='message',
response_format={'type': 'json_object'}
)
json_string = response.output.choices[0].message.content
print(json_string)Respons
{
"info": {
"name": "Alex Brown",
"age": "34 years old",
"email": "alexbrown@example.com"
},
"hobby": [
"playing basketball",
"traveling"
]
}
Java
import java.util.Arrays;
import java.lang.System;
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.common.ResponseFormat;
import com.alibaba.dashscope.protocol.Protocol;
public class Main {
public static GenerationResult callWithMessage() throws ApiException, NoApiKeyException, InputRequiredException {
// Jika Anda menggunakan model wilayah Beijing, Anda harus mengganti URL dengan: https://dashscope.aliyuncs.com/api/v1
Generation gen = new Generation(Protocol.HTTP.getValue(), "https://dashscope-intl.aliyuncs.com/api/v1");
Message systemMsg = Message.builder()
.role(Role.SYSTEM.getValue())
.content("""
Extract personal information from the user input and output it in the specified JSON Schema format:
[Output Format Requirements]
The output must strictly follow the JSON structure below:
{
"info": {
"name": "String type, required field, user's name",
"age": "String type, required field, in the format of 'Number years old', for example, '25 years old'",
"email": "String type, required field, standard email format, for example, 'user@example.com'"
},
"hobby": ["String array type, optional field, contains all of the user's hobbies. If no hobbies are mentioned, do not include this field in the output."]
}
[Field Extraction Rules]
1. name: Identify the user's name from the text. This is a required field.
2. age: Identify the age information and transform it into the 'Number years old' format. This is a required field.
3. email: Identify the email address and keep its original format. This is a required field.
4. hobby: Identify the user's hobbies and output them as a string array. If no hobbies are mentioned, completely omit the hobby field.
[Examples]
Example 1 (with a hobby):
Q: My name is Zhang San, I am 25 years old, my email is zhangsan@example.com, and my hobby is singing.
A: {"info":{"name":"Zhang San","age":"25 years old","email":"zhangsan@example.com"},"hobby":["singing"]}
Example 2 (with multiple hobbies):
Q: My name is Li Si, I am 30 years old, my email is lisi@example.com, and I like dancing and swimming.
A: {"info":{"name":"Li Si","age":"30 years old","email":"lisi@example.com"},"hobby":["dancing","swimming"]}
Example 3 (without hobbies):
Q: My name is Wang Wu, my email is wangwu@example.com, and I am 40 years old.
A: {"info":{"name":"Wang Wu","age":"40 years old","email":"wangwu@example.com"}}""")
.build();
Message userMsg = Message.builder()
.role(Role.USER.getValue())
.content("Hello everyone, my name is Alex Brown, I am 34 years old, my email is alexbrown@example.com, and I enjoy playing basketball and traveling.")
.build();
ResponseFormat jsonMode = ResponseFormat.builder().type("json_object").build();
GenerationParam param = GenerationParam.builder()
// Jika Anda menggunakan model wilayah Beijing, Anda perlu menggunakan kunci API untuk wilayah Beijing. Dapatkan kunci dari: https://bailian.console.alibabacloud.com/?tab=model#/api-key
// Jika Anda belum mengonfigurasi variabel lingkungan, ganti baris berikut dengan Kunci API Alibaba Cloud Model Studio Anda: .apiKey("sk-xxx")
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
.model("qwen-plus")
.messages(Arrays.asList(systemMsg, userMsg))
.resultFormat(GenerationParam.ResultFormat.MESSAGE)
.responseFormat(jsonMode)
.build();
return gen.call(param);
}
public static void main(String[] args) {
try {
GenerationResult result = callWithMessage();
System.out.println(result.getOutput().getChoices().get(0).getMessage().getContent());
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
// Gunakan framework logging untuk mencatat pengecualian.
System.err.println("Terjadi kesalahan saat memanggil layanan generasi: " + e.getMessage());
}
}
}
Respons
{
"info": {
"name": "Alex Brown",
"age": "34 years old",
"email": "alexbrown@example.com"
},
"hobby": [
"Playing basketball",
"Traveling"
]
}
Mulai Beroperasi
-
Validasi
Sebelum meneruskan output ke layanan downstream, validasi dengan alat seperti jsonschema (Python), Ajv (JavaScript), atau Everit (Java). Ini mencegah kegagalan parsing akibat field yang hilang, kesalahan tipe, atau masalah format. Jika gagal, lakukan retry atau tulis ulang output dengan LLM.
-
Nonaktifkan
max_tokensJangan setel
max_tokenssaat structured output diaktifkan. Parameter ini membatasi token output dan secara default menggunakan maksimum model. Menyetelnya dapat memotong string JSON dan menyebabkan kegagalan parsing.
FAQ
T: Bagaimana model thinking mode Qwen menghasilkan structured output?
J: Model thinking mode Qwen tidak mendukung structured output. Untuk mendapatkan string JSON standar dalam thinking mode, gunakan model yang mendukung JSON Mode untuk memperbaiki output jika parsing JSON gagal.
-
Dapatkan output dari mode berpikir
Panggil model thinking mode untuk mendapatkan output berkualitas tinggi, yang mungkin bukan string JSON standar.
Jangan setel parameter
response_formatke{"type": "json_object"}saat mengaktifkan thinking mode, atau Anda akan mengalami error.completion = client.chat.completions.create( model="qwen-plus", messages=[ {"role": "system", "content": system_prompt}, { "role": "user", "content": "Hi everyone, my name is Alex Brown, I'm 34 years old, my email is alexbrown@example.com, and I enjoy playing basketball and traveling", }, ], # Aktifkan thinking mode; jangan setel parameter response_format ke {"type": "json_object"}, atau Anda akan mendapat error extra_body={"enable_thinking": True}, # Output streaming diperlukan dalam thinking mode stream=True ) # Ekstrak dan cetak hasil JSON yang dihasilkan model json_string = "" for chunk in completion: if chunk.choices[0].delta.content is not None: json_string += chunk.choices[0].delta.content -
Validasi dan perbaiki output
Coba uraikan
json_stringdari langkah sebelumnya:-
Jika model menghasilkan string JSON standar, uraikan dan kembalikan langsung.
-
Jika model menghasilkan string JSON non-standar, panggil model yang mendukung structured output (sebaiknya model cepat dan berbiaya rendah seperti qwen-flash dalam non-thinking mode) untuk memperbaiki formatnya.
import json from openai import OpenAI import os # Inisialisasi klien OpenAI (jika variabel client tidak didefinisikan di blok kode sebelumnya, hapus komentar pada baris di bawah) # client = OpenAI( # api_key=os.getenv("DASHSCOPE_API_KEY"), # base_url="https://dashscope.aliyuncs.com/compatible-mode/v1", # ) try: json_object_from_thinking_model = json.loads(json_string) print("Menghasilkan string JSON standar") except json.JSONDecodeError: print("Tidak menghasilkan string JSON standar; memperbaiki dengan model yang mendukung structured output") completion = client.chat.completions.create( model="qwen-flash", messages=[ { "role": "system", "content": "You are a JSON format expert. Fix the user's JSON string to standard format", }, { "role": "user", "content": json_string, }, ], response_format={"type": "json_object"}, ) json_object_from_thinking_model = json.loads(completion.choices[0].message.content) -
Kode error
Jika pemanggilan model gagal dan mengembalikan pesan error, lihat Pesan error untuk resolusi.