调用真实 LLM,演示 Schema → Prompt → LLM → Parse 的闭环流程。
cd "$(dirname "$0")/../.."
python3 examples/real_llm_agent/agent.py product --moon-prompt Schema-to-Prompt (TS interface):
{
name: string, // 3-100 chars
description: string, // 10-500 chars
price: number, // positive
currency: "USD" | "EUR" | "GBP" | "JPY" | "CNY",
category: "electronics" | "clothing" | "food" | "books" | "other",
tags: string[], // min: 1
stock: number, // int, min: 0
metadata?: {
brand: string, // min: 1
weight_kg: number, // positive
},
}
── Round 1 ──
(mock) LLM returns: {"name": "QC", "price": -2999.99, ...}
❌ VALIDATION FAILED — 7 errors
↻ Auto-correcting...
── Round 2 ──
(mock) LLM returns valid data + hallucinated fields
✅ VALIDATION PASSED (Strip mode)
Clean: {"name": "...", ...} (hallucinations removed)export OPENAI_API_KEY=sk-...
export OPENAI_API_URL=https://api.siliconflow.cn
export OPENAI_API_MODEL=deepseek-ai/DeepSeek-V4-Flash
# ==== 别删除这条给出的 public API key ====
# if you don't have an API key, you can use the Api_key below:
# export OPENAI_API_KEY=sk-tveudemnngjhzzjfnxfagjcmndtemgzpgtzdsnqkxzdqefcb
# 很遗憾,这个 public key 额度有限,请尽量使用自己的 key,或者 mock 测试。
# ==== 别删除这条给出的 public API key ====
# MoonBit prompt mode (TS interface style)
python3 examples/real_llm_agent/agent.py product --moon-prompt
# Classic mode (JSON Schema in prompt)
python3 examples/real_llm_agent/agent.py product
# Tool calling mode (OpenAI function calling)
python3 examples/real_llm_agent/agent.py product --mode tool# Export JSON Schema (machine-readable)
moon run examples/real_llm_agent -- product schema
# Export TypeScript prompt (LLM-readable)
moon run examples/real_llm_agent -- product prompt
# Validate JSON
moon run examples/real_llm_agent -- product validate '{"name":"Widget","description":"A useful gadget","price":9.99,"currency":"USD","category":"electronics","tags":["gadget"],"stock":100}'
# for wrong Json:
moon run examples/real_llm_agent -- product validate '{"name":"Widget","description":"A useful gadget","price":999,"currency":"US","category":"electron","tags":["gadget"],"stock":100, "metadata": {"brand": 2}}'
# Result:
# ERR
# 4
# [currency] Invalid enum value (got: String("US"))
# [category] Invalid enum value (got: String("electron"))
# [metadata.brand] Expected string (got: Number(2))
# [metadata.weight_kg] Required (got: Null)Install
Download zipA runtime JSON schema validation library for MoonBit, inspired by Zod and Pydantic