2026-11-27 · Prompt Optimization
When you run data extraction or structured pipelines through the BigModel API, JSON is where things usually break: the model adds a friendly sentence, renames your fields, or slips commentary into an array. The good news is that GLM responds well to format constraints — you don't need a 27-rule manifesto. Three elements, clearly stated, and stability is good enough for production.
Compress the requirement into two parts: a declaration that says "output JSON only," and a skeleton example. GLM follows this "declare + demonstrate" combo closely — it's cheaper than a long rule list and more reliable.
Extract information from the user review below. Output JSON only — no other text, no code fence.
JSON structure:
{
"sentiment": "positive | negative | neutral",
"product": "string, product name, null if not mentioned",
"keywords": ["array of keyword strings"]
}
Review: {comment}
Breaking down why it works:
positive | negative | neutral) drift far less than a prose description.If output still occasionally includes commentary, add a single input–output pair. One is enough; pick a boundary case — product missing, keywords empty:
Extract information from the review. Output JSON only.
Example:
Review: these earbuds have noticeable latency
Output: {"sentiment": "negative", "product": null, "keywords": ["latency"]}
Review: {comment}
Output:
Leave Output: dangling at the end. The model will continue straight into JSON instead of opening with "Sure, here's the extraction."
On the BigModel API, set temperature low (0–0.3 is plenty for extraction) and use a system prompt like "You are a JSON extraction engine. Output valid JSON only." GLM's tool-calling capability offers an even harder constraint: define your JSON fields as a tool's parameter schema and let the model return through the function-call channel, where the schema validates the output for you. Exact parameter names and response formats follow the official BigModel documentation.
No matter how tight the prompt, add one parsing safeguard. The most common dirty output is a ```json code fence, which a two-line regex strips:
python
import json, re
def safe_json(text):
text = re.sub(r"^```(json)?|```$", "", text.strip(), flags=re.M).strip()
return json.loads(text)
Pair this with a response_format-style parameter if the API supports it (check the official docs), and failed parses drop to nearly zero.
GLM is strong in both languages, but in mixed tasks the language of field values can drift. Add one line to the structure spec — "all string values in Chinese" or "keep the product field in its original English" — and the model follows it exactly.
Structured output at the prompt level boils down to "declaration at the top + skeleton example + typed fields." Anything beyond that is mostly reassurance. Open the playground, run the template above against a handful of real records, check the edge cases, then wire it into your API pipeline — the whole thing takes minutes.
本文为使用方法整理,内容选自网上公开资料,供参考;本站为非官方资讯站,产品能力以官方页面为准。