"""
终端输出:课堂演示用,突出 Few-shot CoT 与零样本 CoT 的差异。
"""
import sys
import textwrap
from config import API_KEY, MODEL_NAME, SEED, TEMPERATURE
from examples import FEW_SHOT_EXAMPLES
def _configure_stdout() -> None:
if hasattr(sys.stdout, "reconfigure"):
try:
sys.stdout.reconfigure(encoding="utf-8")
except Exception:
pass
_configure_stdout()
def print_banner(title: str) -> None:
line = "=" * 60
print(f"\n{line}\n {title}\n{line}")
def print_section(title: str) -> None:
print(f"\n{'-' * 60}\n>> {title}\n{'-' * 60}")
def print_kv(label: str, value: str) -> None:
print(f"{label}")
print(textwrap.indent(textwrap.fill(value, width=72), " "))
def print_model_reply(reply: str) -> None:
print("\n【模型回复】")
print(textwrap.indent(reply, " "))
def _mask_api_key(key: str | None) -> str:
if not key:
return "未配置"
if len(key) <= 8:
return "已配置(长度较短,已隐藏)"
return f"已配置({key[:4]}...{key[-4:]})"
def print_intro() -> None:
print_banner("Few-shot CoT · 用示例教模型「怎么推理」")
print(
textwrap.fill(
"本项目和 cot_demo 的区别:cot_demo 用数学题对比三种写法;"
"本项目聚焦「Few-shot CoT」——在提示词里放入带推理过程的示例,"
"让模型不仅学会答对,还学会按相同格式展开思维链。",
width=72,
)
)
print_section("给同学的一句话解释 CoT")
print(
textwrap.fill(
"CoT(思维链)= 先写推理步骤,再给最终答案。"
"就像做应用题要先列式再写结果,不能心算跳步。"
"模型也一样:复杂题直接要答案,容易「想当然」;"
"让它把中间步骤写出来,就像给了草稿纸。",
width=72,
)
)
print_section("Few-shot 和 Few-shot CoT 的区别")
print(" · 普通 Few-shot:示例通常只有「输入 → 最终标签/答案」")
print(" 例:这段新闻 → 类别:财务报告")
print(" · Few-shot CoT:示例是「输入 → 完整推理过程 → 最终答案」")
print(" 例:年龄题 → 第一步…第二步… → 最终答案:141 岁")
print(" · 作用:普通 Few-shot 教格式;Few-shot CoT 教「怎么想 + 怎么写」")
print_section("本项目内置 Few-shot 示例")
for idx, example in enumerate(FEW_SHOT_EXAMPLES, start=1):
print(f"\n [{idx}] {example.title}")
print_kv(" 教学点", example.teaching_point)
print_kv(" 示例问题", example.user)
print_section("题目和文档在哪(找不到先看这里)")
print(" 主题目(小明 70 岁) → question.py → MAIN_QUESTION")
print(" Few-shot 示例题 → examples.py → FEW_SHOT_EXAMPLES")
print(" 三种模式提示词 → prompts.py")
print(" 完整说明 → docs/项目说明.md")
print(" 老师讲课话术 → docs/课堂讲解指南.md")
print_section("运行环境")
print_kv("模型", MODEL_NAME)
print_kv("API Key", _mask_api_key(API_KEY))
print_kv("参数", f"temperature={TEMPERATURE}, seed={SEED}")
print_kv(
"真实性",
"每次模型回复都是实时调用百炼 API 返回的结果,会消耗 Token。",
)