|
|
|
@ -0,0 +1,70 @@
|
|
|
|
|
# pip install dashscope python-dotenv
|
|
|
|
|
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
import dashscope
|
|
|
|
|
from dashscope import Generation
|
|
|
|
|
from dashscope.api_entities.dashscope_response import DashScopeAPIResponse
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestDeepSeek:
|
|
|
|
|
# 模型版本常量
|
|
|
|
|
R1 = "deepseek-r1"
|
|
|
|
|
V3 = "deepseek-v3"
|
|
|
|
|
# API密钥(建议从环境变量读取)
|
|
|
|
|
API_KEY = "sk-01d13a39e09844038322108ecdbd1bbc"
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def get_system_prompt() -> str:
|
|
|
|
|
"""读取系统提示文件"""
|
|
|
|
|
md_path = Path(r"D:\dsWork\QingLong\PptGenerator\md-file\readme\default.md")
|
|
|
|
|
if not md_path.exists():
|
|
|
|
|
raise FileNotFoundError(f"提示文件不存在: {md_path}")
|
|
|
|
|
return md_path.read_text(encoding='utf-8')
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def call_with_message(cls) -> DashScopeAPIResponse:
|
|
|
|
|
"""调用生成API"""
|
|
|
|
|
system_prompt = (
|
|
|
|
|
"返回的教案内容请严格按照指定的markdown语法格式返回,用来生成ppt。"
|
|
|
|
|
"需要注意:1、最开头的Title:..,Author: ... Date:... 不能省略掉,有用处。"
|
|
|
|
|
"2、第一级用#,第二级用##,第二级是独立的PPT一个页,每页中的数据必须有> 开头,"
|
|
|
|
|
"如果是页面中的条目,必须用 - 开头,结果不要返回图片!请仔细阅读参考格式:"
|
|
|
|
|
) + cls.get_system_prompt()
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
return Generation.call(
|
|
|
|
|
model=cls.R1,
|
|
|
|
|
api_key=cls.API_KEY,
|
|
|
|
|
messages=[
|
|
|
|
|
{"role": "system", "content": system_prompt},
|
|
|
|
|
{"role": "user", "content": "请帮我生成一个小学数学内角和的教案,按标准格式输出。"}
|
|
|
|
|
],
|
|
|
|
|
result_format='message' # 与Java的GenerationParam.ResultFormat.MESSAGE对应
|
|
|
|
|
)
|
|
|
|
|
except Exception as e: # 根据实际SDK异常类型细化
|
|
|
|
|
raise RuntimeError(f"API调用失败: {str(e)}") from e
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def main(cls):
|
|
|
|
|
"""主执行方法"""
|
|
|
|
|
try:
|
|
|
|
|
result = cls.call_with_message()
|
|
|
|
|
if result.status_code == 200 and result.output:
|
|
|
|
|
md_content = result.output.choices[0].message.content
|
|
|
|
|
print(md_content)
|
|
|
|
|
|
|
|
|
|
output_path = Path(r"D:\dsWork\QingLong\PptGenerator\md-file\readme\5.md")
|
|
|
|
|
output_path.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
output_path.write_text(md_content, encoding='utf-8')
|
|
|
|
|
print(f"结果已保存至: {output_path}")
|
|
|
|
|
else:
|
|
|
|
|
print(f"请求失败,状态码: {result.status_code}, 原因: {result.message}")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"发生错误: {str(e)}")
|
|
|
|
|
print("请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
dashscope.api_key = TestDeepSeek.API_KEY
|
|
|
|
|
TestDeepSeek.main()
|