204 lines
7.9 KiB
Python
204 lines
7.9 KiB
Python
|
from openai import OpenAI
|
|||
|
import subprocess # 新增导入
|
|||
|
|
|||
|
from Config.Config import ALY_LLM_API_KEY, ALY_LLM_MODEL_NAME, ALY_LLM_BASE_URL
|
|||
|
|
|||
|
if __name__ == '__main__':
|
|||
|
# 初始化OpenAI客户端
|
|||
|
client = OpenAI(
|
|||
|
api_key=ALY_LLM_API_KEY,
|
|||
|
base_url=ALY_LLM_BASE_URL
|
|||
|
)
|
|||
|
|
|||
|
reasoning_content = "" # 定义完整思考过程
|
|||
|
answer_content = "" # 定义完整回复
|
|||
|
is_answering = False # 判断是否结束思考过程并开始回复
|
|||
|
|
|||
|
# 3、 分块追加:在上一步代码基础上,加一条动态垂直线段,连接圆上的动点与 x 轴,并显示线段长度数值。
|
|||
|
prompt = """
|
|||
|
你是一位资深 Manim 动画工程师,专注于教育场景。请遵循以下规范:
|
|||
|
1. 仅输出 Python 代码,不解释思路。
|
|||
|
2. 使用 Manim 社区版 v0.18.0 语法。
|
|||
|
3. 严格按提示的步骤进行编码,不要发挥、扩展。
|
|||
|
4. 所有公式用 MathTex,文字用 Text(更好地支持中文),颜色统一用 Manim 内置调色板。
|
|||
|
5. 关键坐标用常量定义在类顶部,例如 LEFT_MARGIN = 3.2。
|
|||
|
6. 每行不超过 88 字符,函数名用 snake_case。
|
|||
|
7. 动画时长 3-5 秒 / 步,默认 ease-in-out。
|
|||
|
8. 请严格按照以下模板:坐标轴范围、颜色、字体统一使用 Manim 社区版默认主题。
|
|||
|
9. 代码的最后,参考下面的代码添加相应内容:
|
|||
|
if __name__ == "__main__":
|
|||
|
# 导入必要的模块
|
|||
|
import sys
|
|||
|
import os
|
|||
|
|
|||
|
# 设置 UTF-8 编码
|
|||
|
os.environ['PYTHONIOENCODING'] = 'utf-8'
|
|||
|
sys.stdout.reconfigure(encoding='utf-8')
|
|||
|
sys.stderr.reconfigure(encoding='utf-8')
|
|||
|
|
|||
|
# 全局指定 ctex 模板(一次性)
|
|||
|
config.tex_template = TexTemplateLibrary.ctex
|
|||
|
# 使用临时配置渲染场景(配置只在with块内有效)
|
|||
|
with tempconfig({{
|
|||
|
"quality": "high_quality",
|
|||
|
"background_color": WHITE,
|
|||
|
"preview": True
|
|||
|
}}):
|
|||
|
# 实例化场景类
|
|||
|
scene = ParabolaUp()
|
|||
|
# 执行渲染流程(包含文件生成和预览)
|
|||
|
scene.render()
|
|||
|
10. 【输出要求】
|
|||
|
(1). 完整可运行 Python 脚本,文件名 ParabolaUp.py。
|
|||
|
(2). 场景类名 ParabolaUp。
|
|||
|
(3). 包含 `if __name__ == "__main__":` 可直接 `manim -pql ParabolaUp.py ParabolaUp`。
|
|||
|
(4). 代码内只出现中文注释,方便二次修改。
|
|||
|
本成的具体需求如下:
|
|||
|
{{XuQiu}}
|
|||
|
"""
|
|||
|
|
|||
|
# XuQiu = """
|
|||
|
# 任务:生成一条 45 秒的抛物线教学动画。
|
|||
|
# 面向:初二学生,零前置知识。
|
|||
|
# 风格:明亮、卡通、无暴力特效。
|
|||
|
#
|
|||
|
# 【场景结构】
|
|||
|
# 1. 开口向上(15 s)
|
|||
|
# 显示标题:a是正数,开口向上
|
|||
|
# 画面:公式栏出现 y = ax² 逐步缩放。
|
|||
|
# 动画:
|
|||
|
# - 先出现 y = x²,再乘以 a = 0.5 → 1 → 2,曲线随之变胖/变瘦。
|
|||
|
# - 同步出现滑杆数值 a = 0.5 / 1 / 2。
|
|||
|
#
|
|||
|
# 2. 动画:留 5 秒静止帧,方便老师暂停提问。
|
|||
|
# """
|
|||
|
|
|||
|
LLM_XuQiu="""
|
|||
|
任务:用 Manim 生成 20 秒极简抛物线动画
|
|||
|
风格:明亮卡通,黑色背景
|
|||
|
|
|||
|
要求
|
|||
|
1. 背景 BLACK
|
|||
|
2. 坐标系:Axes(x_range=[-4,4,1], y_range=[-3,5,1]),带箭头
|
|||
|
3. 曲线:y = a x²,a 从 0.5→2 匀速变化,始终过原点
|
|||
|
4. 颜色:曲线 YELLOW_E,坐标轴 WHITE
|
|||
|
5. 动画:无滑块,无文字,仅曲线随 a 缩放 15 秒后静止 5 秒
|
|||
|
|
|||
|
输出
|
|||
|
文件名 ParabolaUp.py,场景类 ParabolaUp,可直接 `manim -pql` 运行
|
|||
|
"""
|
|||
|
|
|||
|
prompt = prompt.format(XuQiu=LLM_XuQiu)
|
|||
|
# 创建聊天完成请求
|
|||
|
completion = client.chat.completions.create(
|
|||
|
model=ALY_LLM_MODEL_NAME,
|
|||
|
messages=[
|
|||
|
{
|
|||
|
"role": "user",
|
|||
|
"content": [
|
|||
|
{"type": "text",
|
|||
|
"text": prompt},
|
|||
|
],
|
|||
|
},
|
|||
|
],
|
|||
|
stream=True,
|
|||
|
)
|
|||
|
|
|||
|
print("\n" + "=" * 20 + "思考过程" + "=" * 20 + "\n")
|
|||
|
|
|||
|
for chunk in completion:
|
|||
|
# 如果chunk.choices为空,则打印usage
|
|||
|
if not chunk.choices:
|
|||
|
print("\nUsage:")
|
|||
|
print(chunk.usage)
|
|||
|
else:
|
|||
|
delta = chunk.choices[0].delta
|
|||
|
# 打印思考过程
|
|||
|
if hasattr(delta, 'reasoning_content') and delta.reasoning_content != None:
|
|||
|
print(delta.reasoning_content, end='', flush=True)
|
|||
|
reasoning_content += delta.reasoning_content
|
|||
|
else:
|
|||
|
# 开始回复
|
|||
|
if delta.content != "" and is_answering is False:
|
|||
|
print("\n" + "=" * 20 + "完整回复" + "=" * 20 + "\n")
|
|||
|
is_answering = True
|
|||
|
# 打印回复过程
|
|||
|
print(delta.content, end='', flush=True)
|
|||
|
answer_content += delta.content
|
|||
|
|
|||
|
|
|||
|
# 保存提示词和生成结果到文件
|
|||
|
with open("/Manim/TiShiCi.txt", "w", encoding="utf-8") as f:
|
|||
|
f.write("====== 提示词 ======\n")
|
|||
|
f.write(prompt)
|
|||
|
f.write("\n\n====== 生成结果 ======\n")
|
|||
|
f.write(answer_content)
|
|||
|
|
|||
|
print("提示词和生成结果已保存到 TiShiCi.txt 文件中。")
|
|||
|
|
|||
|
# 将生成结果另存为ParabolaUp.py
|
|||
|
# 逐行读取并只写入```python和```之间的内容
|
|||
|
clean_content = []
|
|||
|
start_writing = False
|
|||
|
|
|||
|
# 逐行处理answer_content
|
|||
|
for line in answer_content.split('\n'):
|
|||
|
# 检查是否开始写入
|
|||
|
if '```python' in line:
|
|||
|
start_writing = True
|
|||
|
continue # 跳过这一行
|
|||
|
# 检查是否结束写入
|
|||
|
elif line.strip() == '```':
|
|||
|
start_writing = False
|
|||
|
break # 结束写入
|
|||
|
# 如果正在写入,则添加到clean_content
|
|||
|
elif start_writing:
|
|||
|
# 替换全角符号为半角符号
|
|||
|
line = line.replace(',', ',')
|
|||
|
line = line.replace(';', ';')
|
|||
|
line = line.replace(':', ':')
|
|||
|
line = line.replace('。', '.')
|
|||
|
line = line.replace('(', '(')
|
|||
|
line = line.replace(')', ')')
|
|||
|
clean_content.append(line)
|
|||
|
|
|||
|
# 组合成完整的内容
|
|||
|
clean_content = '\n'.join(clean_content)
|
|||
|
|
|||
|
# 在代码开头添加必要的编码声明
|
|||
|
clean_content = '# -*- coding: utf-8 -*-\n' + clean_content
|
|||
|
|
|||
|
with open("/Manim/ParabolaUp.py", "w", encoding="utf-8") as f:
|
|||
|
f.write(clean_content)
|
|||
|
|
|||
|
print("生成结果已另存为 ParabolaUp.py 文件中。")
|
|||
|
|
|||
|
# 运行 ParabolaUp.py 文件
|
|||
|
print("开始运行 ParabolaUp.py 文件...")
|
|||
|
try:
|
|||
|
# 先检查Python文件语法是否正确
|
|||
|
syntax_check = subprocess.run(
|
|||
|
["python", "-m", "py_compile", "d:\dsWork\dsProject\dsLightRag\Manim\ParabolaUp.py"],
|
|||
|
capture_output=True,
|
|||
|
text=True,
|
|||
|
encoding='utf-8'
|
|||
|
)
|
|||
|
|
|||
|
if syntax_check.returncode != 0:
|
|||
|
print(f"ParabolaUp.py 语法检查失败: {syntax_check.stderr}")
|
|||
|
else:
|
|||
|
print("ParabolaUp.py 语法检查通过,开始运行...")
|
|||
|
# 使用manim命令行运行动画
|
|||
|
result = subprocess.run(
|
|||
|
["manim", "-pql", "d:\dsWork\dsProject\dsLightRag\Manim\ParabolaUp.py", "ParabolaUp"],
|
|||
|
check=True,
|
|||
|
capture_output=True,
|
|||
|
text=True,
|
|||
|
encoding='utf-8'
|
|||
|
)
|
|||
|
print("ParabolaUp.py 运行成功!")
|
|||
|
print(f"输出: {result.stdout}")
|
|||
|
except subprocess.CalledProcessError as e:
|
|||
|
print(f"ParabolaUp.py 运行失败: {e}")
|
|||
|
print(f"错误输出: {e.stderr}")
|