{"repo": "caojh0/academic-auto-tracker", "files": {"backend/app/deepseek.py": {"84d6bc0b8ed597268bdee55b4351a55ecc1e16da": {"t": 1785746387.4071481, "n": 5864, "h": "32c551f1167a02f7", "c": "import requests\nimport logging\nimport pdfplumber\nimport chardet\nfrom typing import Union, Dict\n\n# client = OpenAI(api_key=\"sk-d49eaad9e4694df4a44462f7043496f9\", base_url=\"https://api.deepseek.com\")\n\n# response = client.chat.completions.create(\n# model=\"deepseek-chat\",\n# messages=[\n# {\"role\": \"system\", \"content\": \"You are a helpful assistant\"},\n# {\"role\": \"user\", \"content\": \"Hello, tell me about the some claasic research in AI.\"},\n# ],\n# stream=False,\n# file=\n# )\n\n# print(response.choices[0].message.content)\n\n# 配置常量\nAPI_URL = \"https://api.deepseek.com/v1/chat/completions\" # DeepSeek API 的 URL\nLOG_FILENAME = \"deepseek_dashboard.log\" # 日志文件名\nMAX_CONTEXT_MESSAGES = 8 # 最大上下文消息数\nMAX_FILE_CONTENT = 1000 # 读取文件内容的最大字符数\n\n# 日志配置\ndef configure_logging():\n \"\"\"配置日志记录器,设置日志格式和处理器。\"\"\"\n formatter = logging.Formatter(\n \"%(asctime)s - %(name)s - %(levelname)s - %(message)s\"\n )\n\n # 创建文件处理器和控制台处理器\n file_handler = logging.FileHandler(LOG_FILENAME)\n file_handler.setFormatter(formatter)\n\n console_handler = logging.StreamHandler()\n console_handler.setFormatter(formatter)\n\n # 配置基本的日志设置\n logging.basicConfig(\n level=logging.INFO,\n handlers=[file_handler, console_handler]\n )\n\n# 调用日志配置函数\nconfigure_logging()\nlogger = logging.getLogger(\"DeepSeekDashboard\") # 创建日志记录器\n\n# 处理上传文件\ndef process_uploaded_files(file_paths) -> str:\n \"\"\"处理上传的文件,提取文本内容并返回处理后的结果。\n \n Args:\n file_paths (list): 上传的文件列表。\n \n Returns:\n str: 提取的文件内容。\n \"\"\"\n processed_content = [] # 存储处理后的内容\n for file_path in file_paths:\n try:\n if file_path.endswith(\".pdf\"):\n # 使用 pdfplumber 处理 PDF 文件\n with pdfplumber.open(file_path) as pdf:\n text = \"\"\n for page in pdf.pages:\n text += page.extract_text() or \"\" # 提取每一页的文本\n processed_content.append(f\"PDF_CONTENT:{file_path}: {text[:MAX_FILE_CONTENT]}...\") # 保存 PDF 内容\n else:\n # 自动检测文本文件编码\n with open(file_path, 'rb') as f:\n raw_data = f.read() # 读取文件\n detected_encoding = chardet.detect(raw_data) # 检测文件编码\n encoding = detected_encoding.get(\"encoding\", \"utf-8-sig\") # 默认使用 utf-8-sig\n content = raw_data.decode(encoding, errors=\"replace\") # 解码文件内容\n processed_content.append(f\"FILE_CONTENT:{file_path}: {content[:MAX_FILE_CONTENT]}...\") # 保存文本内容\n except Exception as e:\n processed_content.append(f\"ERROR: 处理文件 {file_path} 时出错: {str(e)}\") # 提示用户错误\n return \"\\n\".join(processed_content) # 返回处理后的内容\n\n# DeepSeek API 请求\ndef query_deepseek(prompt: str, system_prompt: str, api_key: str, model: str = \"deepseek-chat\",\n temperature: float = 0.7) -> Union[Dict, None]:\n \"\"\"向 DeepSeek API 发送请求并获取响应。\n \n Args:\n prompt (str): 用户的输入提示。\n system_prompt (str): 系统角色描述。\n api_key (str): 用户的 DeepSeek API 密钥。\n model (str, optional): 使用的模型名称。默认为 'deepseek-chat'。\n temperature (float, optional): 控制生成的创造力级别,默认值为 0.7。\n \n Returns:\n Union[Dict, None]: 返回 API 响应数据字典或 None。\n \"\"\"\n headers = {\n \"Authorization\": f\"Bearer {api_key}\", # 使用提供的 API 密钥进行身份验证\n \"Content-Type\": \"application/json\", # 设置请求内容类型\n }\n\n try:\n # 构建请求负载\n payload = {\n \"model\": model,\n \"messages\": [{\"role\": \"system\", \"content\": system_prompt}] + [{\"role\": \"user\", \"content\": prompt}],\n \"temperature\": temperature,\n }\n\n logger.info(f\"发送 {len(payload['messages'])} 条消息到 DeepSeek API...\") # 记录发送的消息数量\n\n response = requests.post(API_URL, headers=headers, json=payload) # 发送 POST 请求\n response.raise_for_status() # 检查请求是否成功\n\n if response.status_code == 200:\n response_data = response.json() # 解析 JSON 响应\n assistant_response = response_data.get(\"choices\", [{}])[0].get(\"message\", {}).get(\"content\", \"\")\n\n return assistant_response # 返回助手的响应内容\n\n logger.error(f\"API 返回了非 200 状态: {response.status_code}\") # 记录错误状态\n return None\n\n except requests.exceptions.RequestException as e:\n logger.error(f\"API 请求失败: {str(e)}\", exc_info=True) # 记录请求异常\n return None # 返回 None 表示请求失败\n\n# 示例如何使用以上函数\nif __name__ == \"__main__\":\n\n # files 是上传的文件列表\n files = [\n \"downloaded_pdfs/How ‘organized looting and plunder’ drove Britain’s second scientific revolution.pdf\",\n \"downloaded_pdfs/Who were the ancient Denisovans? Fossils reveal secrets about the mysterious humans.pdf\"\n ]\n\n # 处理上传的文件\n extracted_content = process_uploaded_files(files)\n print(\"提取的文件内容:\")\n print(extracted_content)\n\n # 示例 DeepSeek API 请求\n api_key = \"sk-d49eaad9e4694df4a44462f7043496f9\" # 请替换为您的实际 API 密钥\n system_prompt = \"\"\"\n 你是一个学术论文助手,用户会提供一篇文章的基本信息。请你根据这些信息判断文章是否与生态学研究相关,并且推断文章的类型(例如:研究性论文article、综述review、新闻news或者其他other)。\n 如果文章与生态学研究相关,请返回“True”,否则返回“False”。以JSON格式输出英文。\n 输出示例:\n 标题: \"Are groundbreaking science discoveries becoming harder to find?\",作者: [{\"name\": \"David Matthews\"}],摘要: \"
Nature, Published online: 21 May 2025; doi:10.1038/d41586-025-01548-4
Researchers are arguing over whether ‘disruptive’ or ‘novel’ science is waning – and how to remedy the problem.\".\n 输出示例: \n {\n \"is_related\": False,\n \"type\": \"other\"\n }\n \"\"\"\n user_prompt = \"我会提供文件pdf给你,请你根据文件内容提炼出每个文件的核心内容\"\n\n full_prompt = f\"{user_prompt}\\n{extracted_content}\" # 将提取的内容与用户提示结合\n\n response = query_deepseek(full_prompt, system_prompt, api_key, model=\"deepseek-chat\", temperature=0)\n print(\"DeepSeek API 响应:\")\n print(response)"}, "45d444f32e30caaeaefc2f25b08351a702589acd": {"t": 1785763199.2085109, "n": 5864, "h": "32c551f1167a02f7", "c": "import requests\nimport logging\nimport pdfplumber\nimport chardet\nfrom typing import Union, Dict\n\n# client = OpenAI(api_key=\"sk-d49eaad9e4694df4a44462f7043496f9\", base_url=\"https://api.deepseek.com\")\n\n# response = client.chat.completions.create(\n# model=\"deepseek-chat\",\n# messages=[\n# {\"role\": \"system\", \"content\": \"You are a helpful assistant\"},\n# {\"role\": \"user\", \"content\": \"Hello, tell me about the some claasic research in AI.\"},\n# ],\n# stream=False,\n# file=\n# )\n\n# print(response.choices[0].message.content)\n\n# 配置常量\nAPI_URL = \"https://api.deepseek.com/v1/chat/completions\" # DeepSeek API 的 URL\nLOG_FILENAME = \"deepseek_dashboard.log\" # 日志文件名\nMAX_CONTEXT_MESSAGES = 8 # 最大上下文消息数\nMAX_FILE_CONTENT = 1000 # 读取文件内容的最大字符数\n\n# 日志配置\ndef configure_logging():\n \"\"\"配置日志记录器,设置日志格式和处理器。\"\"\"\n formatter = logging.Formatter(\n \"%(asctime)s - %(name)s - %(levelname)s - %(message)s\"\n )\n\n # 创建文件处理器和控制台处理器\n file_handler = logging.FileHandler(LOG_FILENAME)\n file_handler.setFormatter(formatter)\n\n console_handler = logging.StreamHandler()\n console_handler.setFormatter(formatter)\n\n # 配置基本的日志设置\n logging.basicConfig(\n level=logging.INFO,\n handlers=[file_handler, console_handler]\n )\n\n# 调用日志配置函数\nconfigure_logging()\nlogger = logging.getLogger(\"DeepSeekDashboard\") # 创建日志记录器\n\n# 处理上传文件\ndef process_uploaded_files(file_paths) -> str:\n \"\"\"处理上传的文件,提取文本内容并返回处理后的结果。\n \n Args:\n file_paths (list): 上传的文件列表。\n \n Returns:\n str: 提取的文件内容。\n \"\"\"\n processed_content = [] # 存储处理后的内容\n for file_path in file_paths:\n try:\n if file_path.endswith(\".pdf\"):\n # 使用 pdfplumber 处理 PDF 文件\n with pdfplumber.open(file_path) as pdf:\n text = \"\"\n for page in pdf.pages:\n text += page.extract_text() or \"\" # 提取每一页的文本\n processed_content.append(f\"PDF_CONTENT:{file_path}: {text[:MAX_FILE_CONTENT]}...\") # 保存 PDF 内容\n else:\n # 自动检测文本文件编码\n with open(file_path, 'rb') as f:\n raw_data = f.read() # 读取文件\n detected_encoding = chardet.detect(raw_data) # 检测文件编码\n encoding = detected_encoding.get(\"encoding\", \"utf-8-sig\") # 默认使用 utf-8-sig\n content = raw_data.decode(encoding, errors=\"replace\") # 解码文件内容\n processed_content.append(f\"FILE_CONTENT:{file_path}: {content[:MAX_FILE_CONTENT]}...\") # 保存文本内容\n except Exception as e:\n processed_content.append(f\"ERROR: 处理文件 {file_path} 时出错: {str(e)}\") # 提示用户错误\n return \"\\n\".join(processed_content) # 返回处理后的内容\n\n# DeepSeek API 请求\ndef query_deepseek(prompt: str, system_prompt: str, api_key: str, model: str = \"deepseek-chat\",\n temperature: float = 0.7) -> Union[Dict, None]:\n \"\"\"向 DeepSeek API 发送请求并获取响应。\n \n Args:\n prompt (str): 用户的输入提示。\n system_prompt (str): 系统角色描述。\n api_key (str): 用户的 DeepSeek API 密钥。\n model (str, optional): 使用的模型名称。默认为 'deepseek-chat'。\n temperature (float, optional): 控制生成的创造力级别,默认值为 0.7。\n \n Returns:\n Union[Dict, None]: 返回 API 响应数据字典或 None。\n \"\"\"\n headers = {\n \"Authorization\": f\"Bearer {api_key}\", # 使用提供的 API 密钥进行身份验证\n \"Content-Type\": \"application/json\", # 设置请求内容类型\n }\n\n try:\n # 构建请求负载\n payload = {\n \"model\": model,\n \"messages\": [{\"role\": \"system\", \"content\": system_prompt}] + [{\"role\": \"user\", \"content\": prompt}],\n \"temperature\": temperature,\n }\n\n logger.info(f\"发送 {len(payload['messages'])} 条消息到 DeepSeek API...\") # 记录发送的消息数量\n\n response = requests.post(API_URL, headers=headers, json=payload) # 发送 POST 请求\n response.raise_for_status() # 检查请求是否成功\n\n if response.status_code == 200:\n response_data = response.json() # 解析 JSON 响应\n assistant_response = response_data.get(\"choices\", [{}])[0].get(\"message\", {}).get(\"content\", \"\")\n\n return assistant_response # 返回助手的响应内容\n\n logger.error(f\"API 返回了非 200 状态: {response.status_code}\") # 记录错误状态\n return None\n\n except requests.exceptions.RequestException as e:\n logger.error(f\"API 请求失败: {str(e)}\", exc_info=True) # 记录请求异常\n return None # 返回 None 表示请求失败\n\n# 示例如何使用以上函数\nif __name__ == \"__main__\":\n\n # files 是上传的文件列表\n files = [\n \"downloaded_pdfs/How ‘organized looting and plunder’ drove Britain’s second scientific revolution.pdf\",\n \"downloaded_pdfs/Who were the ancient Denisovans? Fossils reveal secrets about the mysterious humans.pdf\"\n ]\n\n # 处理上传的文件\n extracted_content = process_uploaded_files(files)\n print(\"提取的文件内容:\")\n print(extracted_content)\n\n # 示例 DeepSeek API 请求\n api_key = \"sk-d49eaad9e4694df4a44462f7043496f9\" # 请替换为您的实际 API 密钥\n system_prompt = \"\"\"\n 你是一个学术论文助手,用户会提供一篇文章的基本信息。请你根据这些信息判断文章是否与生态学研究相关,并且推断文章的类型(例如:研究性论文article、综述review、新闻news或者其他other)。\n 如果文章与生态学研究相关,请返回“True”,否则返回“False”。以JSON格式输出英文。\n 输出示例:\n 标题: \"Are groundbreaking science discoveries becoming harder to find?\",作者: [{\"name\": \"David Matthews\"}],摘要: \"Nature, Published online: 21 May 2025; doi:10.1038/d41586-025-01548-4
Researchers are arguing over whether ‘disruptive’ or ‘novel’ science is waning – and how to remedy the problem.\".\n 输出示例: \n {\n \"is_related\": False,\n \"type\": \"other\"\n }\n \"\"\"\n user_prompt = \"我会提供文件pdf给你,请你根据文件内容提炼出每个文件的核心内容\"\n\n full_prompt = f\"{user_prompt}\\n{extracted_content}\" # 将提取的内容与用户提示结合\n\n response = query_deepseek(full_prompt, system_prompt, api_key, model=\"deepseek-chat\", temperature=0)\n print(\"DeepSeek API 响应:\")\n print(response)"}}}}