自动填写。
问题
状态
state 是你要提问的文本或 JSON 对象(也可以是数组),类型为 JSONContent。它本身不能为 None,但对象内部的值可以是 None。详见状态。
问题对象
用 Noul、Choice、Score 以关键字参数定义问题。它们都是不可变(frozen)的 pydantic v2 模型:type 字段由类自动填写,无需传入;未知字段会被拒绝,拼错字段名会在构造时抛出 pydantic 的 ValidationError。
from xiangxin import Choice, Noul, Score三种问题在 HTTP 请求体中的格式见 API 参考 · 问题类型。
xiangxin.NoulCriteria
基类:TypedDict(total=False)
“是”与“否”两种结果的可选描述。详见 Noul 原语。
true
true: JSONContent | None“是 / 成立”时的含义,可以是文本、JSON 对象或数组;None 或省略表示不加描述。
false
false: JSONContent | None“否 / 不成立”时的含义,可以是文本、JSON 对象或数组;None 或省略表示不加描述。
xiangxin.Noul
pydantic 模型
是非题(noul 即 “no or yes”),附带对两种结果的可选描述。答案是陈述成立的概率。详见 Noul 原语。
字段
typeLiteral['noul']默认 'noul'instructionsJSONContent | None默认 None要问的问题或要判断的陈述,可以是文本、JSON 对象或数组。
criteriaNoulCriteria | None默认 None“是”与“否”的可选描述,形如 {"true": ..., "false": ...}。
Noul(
instructions="这段商品描述是否含有“最”“第一”等绝对化用语?",
criteria={"true": "出现“最好”“全网第一”“顶级”等表述", "false": "没有此类表述"},
)xiangxin.Choice
pydantic 模型
在若干命名选项中选出最可能的一个。详见 Choice 原语。
字段
typeLiteral['choice']默认 'choice'自动填写。
criteriadict[str, JSONContent | None]必填选项名 → 描述(文本、对象或数组),None 表示不加描述。至少 1 个选项,否则构造时报错;API 最多接受 255 个。
instructionsJSONContent | None默认 None要问的问题,可以是文本、JSON 对象或数组。
Choice(
instructions="这张发票属于哪一类?",
criteria={
"vat_special": "增值税专用发票,可抵扣进项",
"vat_normal": "增值税普通发票",
"e_ticket": "电子行程单、火车票等票据",
"other": None,
},
)提示
选项名本身也会被模型读到,请用有意义的名字(vat_special 比 opt1 好)。见已知短板。
xiangxin.Score
pydantic 模型
按有序量表打分,返回期望分数。详见 Score 原语。
字段
typeLiteral['score']默认 'score'自动填写。
criterialist[JSONContent]必填非空、有序的档位描述列表,第 0 项对应 0 分,依次递增。API 接受 2–10 档。
instructionsJSONContent | None默认 None要问的问题,可以是文本、JSON 对象或数组。
Score(
instructions="候选人的 Python 工程经验处于哪个水平?",
criteria=[
"没有提到 Python",
"学过或做过课程作业",
"在工作中用 Python 完成过项目",
"长期以 Python 为主力语言,有线上系统维护经验",
],
)to_dict
to_dict() -> dict[str, Any]三个问题类都提供此方法,返回请求体中的问题字典,省略值为 None 的顶层字段:
Noul(instructions="是否提到了退款?").to_dict()
# {'type': 'noul', 'instructions': '是否提到了退款?'}xiangxin.Question
类型别名
Question: TypeAlias = Noul | Choice | Score | QuestionDict问题对象或问题字典。
xiangxin.Questions
类型别名
Questions: TypeAlias = Mapping[str, Question]问题名 → 问题,即 system_one 的 questions 参数。答案以相同的名字返回。
问题字典
问题也可以写成与 HTTP 请求体一致的字典,用 type 键区分:"noul"、"choice" 或 "score"。同一个请求中可以混用问题字典和问题对象。字典适合把问题作为数据保存在 YAML、JSON 或数据库里。
SDK 对字典只做最基本的检查——必须有非空的 type,Choice / Score 的 criteria 不能为空,否则抛出 XiangxinError——其余字段原样发送,交给服务端校验,不合法时会收到 422 错误。
questions = {
"is_spam": {"type": "noul", "instructions": "这条评论是否是广告或引流信息?"},
"sentiment": {
"type": "choice",
"instructions": "整体态度是?",
"criteria": {"positive": "满意、推荐", "neutral": None, "negative": "不满、差评"},
},
}xiangxin.NoulDict
基类:TypedDict
type="noul" 的是非题字典。详见 Noul 原语。
type
type: Literal["noul"]instructions
instructions: NotRequired[JSONContent | None]要问的问题,可以是文本、JSON 对象或数组;可省略。
criteria
criteria: NotRequired[NoulCriteria | None]“是”与“否”的可选描述。
xiangxin.ChoiceDict
基类:TypedDict
type="choice" 的单选题字典。详见 Choice 原语。
type
type: Literal["choice"]instructions
instructions: NotRequired[JSONContent | None]要问的问题,可以是文本、JSON 对象或数组;可省略。
criteria
criteria: Mapping[str, JSONContent | None]选项名 → 描述(文本、对象或数组),None 表示不加描述。
xiangxin.ScoreDict
基类:TypedDict
type="score" 的打分题字典。详见 Score 原语。
type
type: Literal["score"]instructions
instructions: NotRequired[JSONContent | None]要问的问题,可以是文本、JSON 对象或数组;可省略。
criteria
criteria: Sequence[JSONContent]非空、有序的档位描述列表,第 0 项对应 0 分。
xiangxin.QuestionDict
类型别名
QuestionDict: TypeAlias = NoulDict | ChoiceDict | ScoreDict以 type 键区分的问题字典。

