蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

运行两个占位 JSON 和三种错误配置

PYTHONCPython 3.12.14,离线读取两份示例 JSON。inspect_config.py
inspect_config.py53 行
"""Validate local teaching configurations for two OpenCode V1 provider adapters."""
import argparse
import json
from pathlib import Path
import re
from urllib.parse import urlsplit

ADAPTERS = {
    "@ai-sdk/openai-compatible": ("chat-completions", "/chat/completions"),
    "@ai-sdk/anthropic": ("messages", "/messages"),
}
DEMO_HOST = "gateway.example.invalid"


def inspect(config):
    provider_id, model_id = config["model"].split("/", 1)
    provider = config["provider"][provider_id]
    protocol, suffix = ADAPTERS[provider["npm"]]
    endpoint = urlsplit(provider["options"]["baseURL"])
    if (endpoint.scheme, endpoint.netloc) != ("https", DEMO_HOST) or endpoint.query or endpoint.fragment:
        raise ValueError("Use the local teaching configuration's reserved HTTPS hostname")
    credential = provider["options"]["apiKey"]
    if not re.fullmatch(r"\{env:MBSKY_[A-Z_]+\}", credential):
        raise ValueError("Use an environment placeholder for the lab key")
    model = provider["models"][model_id]
    limits = model["limit"]
    context, output = limits["context"], limits["output"]
    if type(context) is not int or type(output) is not int or not 0 < output <= context:
        raise ValueError("Invalid context/output limits")
    modalities = model.get("modalities", {"input": ["text"], "output": ["text"]})
    thinking = model.get("options", {}).get("thinking", {})
    budget = thinking.get("budgetTokens")
    if budget is not None and (type(budget) is not int or not 0 < budget < output):
        raise ValueError("The lab requires a thinking budget below output")
    return dict(provider=provider_id, model=model_id, protocol=protocol, path=endpoint.path.rstrip("/") + suffix,
                image_declared="image" in modalities["input"], output_limit=output,
                context_limit=context, thinking_budget=budget, credential="environment reference")


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("config", type=Path)
    args = parser.parse_args()
    try:
        report = inspect(json.loads(args.config.read_bytes()))
    except (KeyError, ValueError, TypeError) as error:
        parser.exit(2, "Invalid teaching config: " + str(error) + "\n")
    print(json.dumps(report, indent=2))


if __name__ == "__main__":
    main()
本例需在 CPython 3.12.14,离线读取两份示例 JSON。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:CPython 3.12.14,离线读取两份示例 JSON。

入口文件:projects/vibe-coding-opencode-plan/inspect_config.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 inspect_config.py,或下载右侧课程工程包。
  2. 确认本地已准备 CPython 3.12.14,离线读取两份示例 JSON。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Vibe Coding

课程关联工程包(登记关联21章) · 含当前章节或实例 · mbskycom202612a385.zip

下载课程关联工程包:Vibe Coding

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a395.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习