81 lines
2.5 KiB
Python
81 lines
2.5 KiB
Python
|
|
import json
|
|||
|
|
import subprocess
|
|||
|
|
import sys
|
|||
|
|
from json_openai import llm_format_text, EXTRACT_OUNTLINE_SYSTEM_PROMPT
|
|||
|
|
|
|||
|
|
def run_typescript_converter(markdown_content):
|
|||
|
|
"""运行TypeScript的markdownToJSON函数"""
|
|||
|
|
try:
|
|||
|
|
# 创建临时文件存储markdown内容
|
|||
|
|
with open('temp_markdown.md', 'w', encoding='utf-8') as f:
|
|||
|
|
f.write(markdown_content)
|
|||
|
|
|
|||
|
|
# 运行TypeScript转换器
|
|||
|
|
result = subprocess.run([
|
|||
|
|
'node', 'dist/test_markdown.js'
|
|||
|
|
], capture_output=True, text=True, input=markdown_content)
|
|||
|
|
|
|||
|
|
if result.returncode == 0:
|
|||
|
|
return result.stdout
|
|||
|
|
else:
|
|||
|
|
print(f"TypeScript转换错误: {result.stderr}")
|
|||
|
|
return None
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"运行TypeScript转换器时出错: {e}")
|
|||
|
|
return None
|
|||
|
|
|
|||
|
|
def main():
|
|||
|
|
# 测试配置
|
|||
|
|
model = "glm-4.5"
|
|||
|
|
base_url = "https://open.bigmodel.cn/api/paas/v4/"
|
|||
|
|
api_key = "ce39bdd4fcf34ec0aec75072bc9ff988.hAp7HZTVUwy7vImn"
|
|||
|
|
|
|||
|
|
# 测试内容
|
|||
|
|
test_content = """
|
|||
|
|
人工智能的发展历程
|
|||
|
|
|
|||
|
|
早期发展
|
|||
|
|
人工智能的概念最早可以追溯到1950年代。图灵测试的提出标志着AI研究的开始。
|
|||
|
|
|
|||
|
|
图灵测试
|
|||
|
|
图灵测试是判断机器是否具有智能的重要标准。
|
|||
|
|
|
|||
|
|
现代发展
|
|||
|
|
近年来,深度学习技术的突破推动了AI的快速发展。
|
|||
|
|
|
|||
|
|
深度学习
|
|||
|
|
深度学习通过神经网络模拟人脑的工作方式。
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
# 构建消息
|
|||
|
|
messages = [
|
|||
|
|
{"role": "system", "content": EXTRACT_OUNTLINE_SYSTEM_PROMPT},
|
|||
|
|
{"role": "user", "content": f"请分析以下文章内容并转换为Markdown格式:\n\n{test_content}"}
|
|||
|
|
]
|
|||
|
|
|
|||
|
|
print("=== 第一步:AI处理生成Markdown ===")
|
|||
|
|
try:
|
|||
|
|
markdown_result = llm_format_text(model, base_url, api_key, messages, 2000)
|
|||
|
|
print("AI生成的Markdown:")
|
|||
|
|
print(markdown_result)
|
|||
|
|
print("\n" + "="*50 + "\n")
|
|||
|
|
|
|||
|
|
print("=== 第二步:Markdown转换为JSON ===")
|
|||
|
|
json_result = run_typescript_converter(markdown_result)
|
|||
|
|
if json_result:
|
|||
|
|
print("转换后的JSON结构:")
|
|||
|
|
print(json_result)
|
|||
|
|
else:
|
|||
|
|
print("转换失败")
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"处理失败: {e}")
|
|||
|
|
print("可能的原因:")
|
|||
|
|
print("1. API密钥无效或过期")
|
|||
|
|
print("2. 网络连接问题")
|
|||
|
|
print("3. API服务不可用")
|
|||
|
|
print("4. 模型名称不正确")
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
main()
|