Files

1 line
12 KiB
JSON

{"repo": "bdruby/xiaozhishi", "files": {"app/utils/ai_generator.py": {"6a8134747441ededf92a598982ab54cb525ac5df": {"t": 1785745982.0652356, "n": 4676, "h": "cef1c086e6284493", "c": "import os\nimport openai\nfrom dotenv import load_dotenv\nfrom flask import current_app\n\n# 加载环境变量\nload_dotenv()\n\ndef get_deepseek_completion(prompt, model=\"deepseek-chat\"):\n \"\"\"\n 调用DEEPSEEK API生成内容\n \"\"\"\n # 从环境变量获取API密钥\n api_key = os.getenv('DEEPSEEK_APIKEY')\n if not api_key:\n raise ValueError(\"DEEPSEEK_APIKEY未在环境变量中设置\")\n \n # 配置OpenAI客户端,使用DEEPSEEK的API端点\n client = openai.OpenAI(\n api_key=api_key,\n base_url=\"https://api.deepseek.com\"\n )\n \n try:\n response = client.chat.completions.create(\n model=model,\n messages=[\n {\"role\": \"user\", \"content\": prompt}\n ],\n temperature=0.7,\n max_tokens=2000\n )\n return response.choices[0].message.content\n except Exception as e:\n current_app.logger.error(f\"调用DEEPSEEK API时出错: {str(e)}\")\n raise e\n\n\ndef generate_blog_content(topic):\n \"\"\"\n 根据主题生成完整的博客内容\n \"\"\"\n import os\nimport json\nimport requests\nfrom dotenv import load_dotenv\nfrom flask import current_app\n\n# 加载环境变量\nload_dotenv()\n\ndef get_deepseek_completion(prompt, model=\"deepseek-chat\"):\n \"\"\"\n 通过HTTP请求调用DEEPSEEK API生成内容\n \"\"\"\n # 从环境变量获取API密钥\n api_key = os.getenv('DEEPSEEK_APIKEY')\n if not api_key:\n raise ValueError(\"DEEPSEEK_APIKEY未在环境变量中设置\")\n \n headers = {\n 'Content-Type': 'application/json',\n 'Authorization': f'Bearer {api_key}'\n }\n \n data = {\n \"model\": model,\n \"messages\": [\n {\"role\": \"user\", \"content\": prompt}\n ],\n \"temperature\": 0.7,\n \"max_tokens\": 2000,\n \"stream\": False # 非流式响应\n }\n \n try:\n response = requests.post(\n \"https://api.deepseek.com/chat/completions\",\n headers=headers,\n json=data,\n timeout=60 # 60秒超时\n )\n response.raise_for_status() # 检查HTTP错误\n \n result = response.json()\n return result['choices'][0]['message']['content']\n except requests.exceptions.RequestException as e:\n current_app.logger.error(f\"调用DEEPSEEK API时出错: {str(e)}\")\n raise e\n except KeyError as e:\n current_app.logger.error(f\"解析DEEPSEEK API响应时出错: {str(e)}, 响应: {response.text}\")\n raise ValueError(f\"解析API响应失败: {str(e)}\")\n\n\ndef generate_blog_content(topic):\n \"\"\"\n 根据主题生成完整的博客内容\n \"\"\"\n prompt = f\"\"\"\n 请根据主题 \"{topic}\" 生成一篇完整的博客文章。请严格按照以下JSON格式返回内容,不要添加其他文字:\n\n {{\n \"title\": \"博客标题\",\n \"slug\": \"url-别名\",\n \"category\": \"分类名称\",\n \"excerpt\": \"文章摘要,不超过200字\",\n \"content\": \"完整的文章内容,使用HTML格式\",\n \"seo_title\": \"SEO标题\",\n \"seo_description\": \"SEO描述,不超过160个字符\",\n \"seo_keywords\": \"关键词1,关键词2,关键词3\",\n \"status\": \"draft\"\n }}\n\n 请确保slug使用英文和连字符,SEO关键词使用中文,内容结构清晰,包含适当的HTML标签。\n \"\"\"\n \n try:\n result = get_deepseek_completion(prompt)\n \n # 解析返回的JSON\n import json\n import re\n \n # 从返回结果中提取JSON部分\n json_match = re.search(r'\\{.*\\}', result, re.DOTALL)\n if json_match:\n json_str = json_match.group()\n try:\n data = json.loads(json_str)\n return data\n except json.JSONDecodeError:\n current_app.logger.error(f\"JSON解析失败: {json_str}\")\n raise ValueError(\"AI返回的JSON格式不正确\")\n else:\n raise ValueError(\"AI未返回有效的JSON格式\")\n \n except Exception as e:\n current_app.logger.error(f\"生成博客内容时出错: {str(e)}\")\n raise e\n \n try:\n result = get_deepseek_completion(prompt)\n \n # 解析返回的JSON\n import json\n import re\n \n # 从返回结果中提取JSON部分\n json_match = re.search(r'\\{.*\\}', result, re.DOTALL)\n if json_match:\n json_str = json_match.group()\n # 修复可能的JSON格式问题\n # 首先尝试直接解析\n try:\n data = json.loads(json_str)\n return data\n except json.JSONDecodeError:\n # 如果直接解析失败,尝试一些常见的修复\n # 移除可能的问题字符\n json_str = json_str.replace('\\n', '\\n').replace('\\t', '\\t')\n json_str = json_str.replace('\\n', '\\n').replace('\\t', '\\t')\n try:\n data = json.loads(json_str)\n return data\n except json.JSONDecodeError:\n current_app.logger.error(f\"JSON解析失败: {json_str}\")\n raise ValueError(\"AI返回的JSON格式不正确\")\n else:\n raise ValueError(\"AI未返回有效的JSON格式\")\n \n except Exception as e:\n current_app.logger.error(f\"生成博客内容时出错: {str(e)}\")\n raise e\n"}, "1defdbd77eab3fd5981573d4426be8412b41ecb4": {"t": 1785758500.1222723, "n": 4676, "h": "cef1c086e6284493", "c": "import os\nimport openai\nfrom dotenv import load_dotenv\nfrom flask import current_app\n\n# 加载环境变量\nload_dotenv()\n\ndef get_deepseek_completion(prompt, model=\"deepseek-chat\"):\n \"\"\"\n 调用DEEPSEEK API生成内容\n \"\"\"\n # 从环境变量获取API密钥\n api_key = os.getenv('DEEPSEEK_APIKEY')\n if not api_key:\n raise ValueError(\"DEEPSEEK_APIKEY未在环境变量中设置\")\n \n # 配置OpenAI客户端,使用DEEPSEEK的API端点\n client = openai.OpenAI(\n api_key=api_key,\n base_url=\"https://api.deepseek.com\"\n )\n \n try:\n response = client.chat.completions.create(\n model=model,\n messages=[\n {\"role\": \"user\", \"content\": prompt}\n ],\n temperature=0.7,\n max_tokens=2000\n )\n return response.choices[0].message.content\n except Exception as e:\n current_app.logger.error(f\"调用DEEPSEEK API时出错: {str(e)}\")\n raise e\n\n\ndef generate_blog_content(topic):\n \"\"\"\n 根据主题生成完整的博客内容\n \"\"\"\n import os\nimport json\nimport requests\nfrom dotenv import load_dotenv\nfrom flask import current_app\n\n# 加载环境变量\nload_dotenv()\n\ndef get_deepseek_completion(prompt, model=\"deepseek-chat\"):\n \"\"\"\n 通过HTTP请求调用DEEPSEEK API生成内容\n \"\"\"\n # 从环境变量获取API密钥\n api_key = os.getenv('DEEPSEEK_APIKEY')\n if not api_key:\n raise ValueError(\"DEEPSEEK_APIKEY未在环境变量中设置\")\n \n headers = {\n 'Content-Type': 'application/json',\n 'Authorization': f'Bearer {api_key}'\n }\n \n data = {\n \"model\": model,\n \"messages\": [\n {\"role\": \"user\", \"content\": prompt}\n ],\n \"temperature\": 0.7,\n \"max_tokens\": 2000,\n \"stream\": False # 非流式响应\n }\n \n try:\n response = requests.post(\n \"https://api.deepseek.com/chat/completions\",\n headers=headers,\n json=data,\n timeout=60 # 60秒超时\n )\n response.raise_for_status() # 检查HTTP错误\n \n result = response.json()\n return result['choices'][0]['message']['content']\n except requests.exceptions.RequestException as e:\n current_app.logger.error(f\"调用DEEPSEEK API时出错: {str(e)}\")\n raise e\n except KeyError as e:\n current_app.logger.error(f\"解析DEEPSEEK API响应时出错: {str(e)}, 响应: {response.text}\")\n raise ValueError(f\"解析API响应失败: {str(e)}\")\n\n\ndef generate_blog_content(topic):\n \"\"\"\n 根据主题生成完整的博客内容\n \"\"\"\n prompt = f\"\"\"\n 请根据主题 \"{topic}\" 生成一篇完整的博客文章。请严格按照以下JSON格式返回内容,不要添加其他文字:\n\n {{\n \"title\": \"博客标题\",\n \"slug\": \"url-别名\",\n \"category\": \"分类名称\",\n \"excerpt\": \"文章摘要,不超过200字\",\n \"content\": \"完整的文章内容,使用HTML格式\",\n \"seo_title\": \"SEO标题\",\n \"seo_description\": \"SEO描述,不超过160个字符\",\n \"seo_keywords\": \"关键词1,关键词2,关键词3\",\n \"status\": \"draft\"\n }}\n\n 请确保slug使用英文和连字符,SEO关键词使用中文,内容结构清晰,包含适当的HTML标签。\n \"\"\"\n \n try:\n result = get_deepseek_completion(prompt)\n \n # 解析返回的JSON\n import json\n import re\n \n # 从返回结果中提取JSON部分\n json_match = re.search(r'\\{.*\\}', result, re.DOTALL)\n if json_match:\n json_str = json_match.group()\n try:\n data = json.loads(json_str)\n return data\n except json.JSONDecodeError:\n current_app.logger.error(f\"JSON解析失败: {json_str}\")\n raise ValueError(\"AI返回的JSON格式不正确\")\n else:\n raise ValueError(\"AI未返回有效的JSON格式\")\n \n except Exception as e:\n current_app.logger.error(f\"生成博客内容时出错: {str(e)}\")\n raise e\n \n try:\n result = get_deepseek_completion(prompt)\n \n # 解析返回的JSON\n import json\n import re\n \n # 从返回结果中提取JSON部分\n json_match = re.search(r'\\{.*\\}', result, re.DOTALL)\n if json_match:\n json_str = json_match.group()\n # 修复可能的JSON格式问题\n # 首先尝试直接解析\n try:\n data = json.loads(json_str)\n return data\n except json.JSONDecodeError:\n # 如果直接解析失败,尝试一些常见的修复\n # 移除可能的问题字符\n json_str = json_str.replace('\\n', '\\n').replace('\\t', '\\t')\n json_str = json_str.replace('\\n', '\\n').replace('\\t', '\\t')\n try:\n data = json.loads(json_str)\n return data\n except json.JSONDecodeError:\n current_app.logger.error(f\"JSON解析失败: {json_str}\")\n raise ValueError(\"AI返回的JSON格式不正确\")\n else:\n raise ValueError(\"AI未返回有效的JSON格式\")\n \n except Exception as e:\n current_app.logger.error(f\"生成博客内容时出错: {str(e)}\")\n raise e\n"}}}}