import json from openai import OpenAI import os Sillcon_OpenAI_api_key = 'sk-mfztogyrhxnflvhhvcaccpmbpcyzfmukgmstllnufpfscjuw' Sillcon_OpenAI_BaseUrl = 'https://api.siliconflow.cn/v1' client = OpenAI(api_key=Sillcon_OpenAI_api_key, base_url=Sillcon_OpenAI_BaseUrl) # 设置路径 TXT_FOLDER = os.path.join('static', 'txt') TXT_FILE_PATH = os.path.join(TXT_FOLDER, 'health_promts.txt') # 确保目录存在(如果没有则创建) os.makedirs(TXT_FOLDER, exist_ok=True) # 设置保存Markdown文件的目录 MARKDOWN_FOLDER = os.path.join('static', 'markdown') # 确保目录存在(如果没有则创建) os.makedirs(MARKDOWN_FOLDER, exist_ok=True) # 确保文件存在,如果不存在则创建它并写入默认内容 if not os.path.exists(TXT_FILE_PATH): with open(TXT_FILE_PATH, 'w', encoding='utf-8') as f: # 写入一些默认内容 f.write("这是健康提示的默认内容,请根据需要修改。") # 读取文件内容 with open(TXT_FILE_PATH, 'r', encoding='utf-8') as f: health_prompts = f.read().strip() def health_organizing(human_input): try: health_message = [{'role': 'system', 'content': health_prompts}] input_prompt = { "role": "user", "content": human_input } health_message.append(input_prompt) response = client.chat.completions.create( model='Qwen/Qwen2.5-32B-Instruct', messages=health_message, stream=False # timeout=10 ).json() # 解析并提取 question_function response = json.loads(response) health_organizing_content = response['choices'][0]['message']['content'] print(health_organizing_content) # 保存内容到 static/markdown/generated_content.md md_file_path = os.path.join(MARKDOWN_FOLDER, 'generated_content.md') with open(md_file_path, 'w', encoding='utf-8') as md_file: md_file.write(f"{health_organizing_content}\n") # 返回生成的Markdown文件路径或其他信息 return md_file_path except Exception as e: print("获取健康组织失败!")