import json
import os
from openai import OpenAI
with OpenAI(
base_url="https://api.apione.ai/v1",
api_key=os.environ["APIONE_API_KEY"],
) as client:
response = client.chat.completions.create(
model=os.environ["APIONE_MODEL"],
messages=[
{"role": "system", "content": '仅返回 JSON,包含 category 字符串,取值为 feature、bug 或 other。'},
{"role": "user", "content": "为帮助中心增加搜索。"},
],
response_format={"type": "json_object"},
)
choice = response.choices[0]
if choice.finish_reason != "stop" or not choice.message.content:
raise ValueError("输出不完整或没有 JSON 正文")
data = json.loads(choice.message.content)
if (
not isinstance(data, dict)
or not isinstance(data.get("category"), str)
or data["category"] not in {"feature", "bug", "other"}
):
raise ValueError("category 字段不符合业务要求")
print(data)