deepseek_utils.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. from typing import List
  2. import json
  3. from openai import OpenAI
  4. def create_segmentation_prompt(text):
  5. prompt = f"""
  6. 请对以下文本进行分段处理,要求如下:
  7. 1. **内容相近原则**:将语义相近、主题一致的内容划分到同一段落
  8. 2. **长度适中**:每段不要太长(一般不超过200字)也不要太短(一般不少于50字)
  9. 3. **语意完整**:确保每段都能表达一个相对完整的意思或观点
  10. 4. **结构清晰**:保持原文的逻辑顺序,不要改变原意
  11. 5. **过渡自然**:段落之间应有合理的过渡或衔接
  12. 6. **关键词提取**:提取5-8个最能代表当前分析范围(整体或段落)核心内容的关键词或短语。避免使用过于通用和宽泛的词汇。
  13. 7. **实体提取**:识别并分类以下类型的实体(如果存在):
  14. * 人物姓名、别名、称号。
  15. * 组织、公司、机构、政府部门。
  16. * 地点、国家、城市、区域。
  17. * 绝对或相对的日期、时间、年份、时期。
  18. * 产品、作品、物品的名称。
  19. * 历史事件、会议、活动名称。
  20. (*注:实体需要去重)
  21. 请直接输出分段后的文本,不需要额外解释或标注
  22. 请返回以下JSON格式:
  23. {{
  24. "segments": [
  25. {{
  26. "id": 1,
  27. "content": "第一段内容",
  28. "summary": "本段摘要",
  29. "keywords": ["关键词1", "关键词2", ...],
  30. "entities": ["实体1", "实体2", ...]
  31. }},
  32. {{
  33. "id": 2,
  34. "content": "第二段内容",
  35. "summary": "本段摘要",
  36. "keywords": ["关键词1", "关键词2", ...],
  37. "entities": ["实体1", "实体2", ...]
  38. }}
  39. ],
  40. "total_segments": 2
  41. }}
  42. 需要分段的文本:
  43. "{text}"
  44. """
  45. return prompt
  46. def text_segment(text: str):
  47. prompt = create_segmentation_prompt(text)
  48. res = chat_with_deepseek(prompt)
  49. return res
  50. def create_question_generation_prompt(text):
  51. """
  52. 创建生成文本内容问题的 prompt
  53. 参数:
  54. text (str): 需要生成问题的文本
  55. num_questions (int): 需要生成的问题数量
  56. 返回:
  57. str: 格式化后的 prompt
  58. """
  59. prompt = f"""
  60. 请基于以下文本内容生成相关问题。要求:
  61. 1. 问题应覆盖文本的主要内容和关键信息
  62. 2. 问题类型多样(事实性、理解性、分析性等)
  63. 3. 问题应清晰明确,易于理解
  64. 4. 问题不应过于简单或复杂,适合文本内容的理解深度
  65. 请以严格的 JSON 数组格式返回结果,每个元素是一个问题对象,包含以下字段:
  66. - id: 问题序号(从1开始)
  67. - question: 问题文本
  68. 请返回以下JSON格式:
  69. {{
  70. "questions": ["问题1", "问题2", ...]
  71. }}
  72. 文本内容:
  73. "{text}"
  74. """
  75. return prompt
  76. def text_question(text_to_question: str):
  77. prompt = create_question_generation_prompt(text_to_question)
  78. return chat_with_deepseek(prompt)
  79. def create_keyword_summary_prompt(text, keyword):
  80. prompt = f"""
  81. 请基于以下关于关键词"{keyword}"的多条知识,生成一段全面、准确且连贯的知识。
  82. ## 描述内容:
  83. {text}
  84. ## 知识要求:
  85. 1. 识别重叠与重复:找出不同文本中表述不同但含义相同的内容。
  86. 2. 解决矛盾与冲突:当不同文本的信息不一致时,需要辨别或调和。
  87. 3. 建立联系与结构:将分散的知识点连接起来,形成逻辑层次。
  88. 4. 提炼与升华:从合并后的信息中总结出更高层次的洞察和结论。
  89. 请返回以下JSON格式:
  90. {{
  91. "keyword_summary": "关键词知识内容"
  92. }}
  93. """
  94. return prompt
  95. def get_keyword_summary(text, keyword):
  96. prompt = create_keyword_summary_prompt(text, keyword)
  97. return chat_with_deepseek(prompt)
  98. def update_keyword_summary_prompt(text, keyword, new_content):
  99. prompt = f"""
  100. 请基于以下关于关键词"{keyword}"的相关知识,融合最新的知识到现有的知识中。
  101. ## 知识要求:
  102. 1. 识别重叠与重复:找出不同文本中表述不同但含义相同的内容。
  103. 2. 解决矛盾与冲突:当不同文本的信息不一致时,需要辨别或调和。
  104. 3. 建立联系与结构:将分散的知识点连接起来,形成逻辑层次。
  105. 4. 提炼与升华:从合并后的信息中总结出更高层次的洞察和结论。
  106. ## 现有知识:
  107. {text}
  108. ## 新知识:
  109. {new_content}
  110. 请返回以下JSON格式:
  111. {{
  112. "keyword_summary": "关键词知识内容"
  113. }}
  114. """
  115. return prompt
  116. def update_keyword_summary(text, keyword, new_content):
  117. prompt = update_keyword_summary_prompt(text, keyword, new_content)
  118. return chat_with_deepseek(prompt)
  119. def create_keyword_prompt(text):
  120. """
  121. 创建生成关键词总结的 prompt
  122. 参数:
  123. text (str): 需要生成总结的文本
  124. keyword (int): 关键词
  125. 返回:
  126. str: 格式化后的 prompt
  127. """
  128. prompt = f"""
  129. 提取最能代表当前分析范围(整体或段落)核心内容的关键词或短语。避免使用过于通用和宽泛的词汇
  130. ## 描述内容:
  131. {text}
  132. 请返回以下JSON格式:
  133. {{
  134. "keywords": ["关键词1", "关键词2", ...]
  135. }}
  136. """
  137. return prompt
  138. def get_keywords(text):
  139. prompt = create_keyword_prompt(text)
  140. return chat_with_deepseek(prompt)
  141. def chat_with_deepseek(prompt, model="deepseek-chat", max_tokens=8192, temperature=0.7):
  142. """
  143. 使用OpenAI兼容方式调用DeepSeek API
  144. 参数:
  145. prompt (str): 用户输入的提示文本
  146. model (str): 使用的模型,默认为deepseek-chat
  147. max_tokens (int): 生成的最大token数
  148. temperature (float): 控制生成随机性的参数(0-1)
  149. 返回:
  150. str: API的回复内容
  151. """
  152. # 初始化客户端,指定DeepSeek的API端点
  153. client = OpenAI(
  154. api_key='sk-cfd2df92c8864ab999d66a615ee812c5',
  155. base_url="https://api.deepseek.com/v1", # DeepSeek的API端点
  156. )
  157. try:
  158. # 创建聊天完成请求
  159. response = client.chat.completions.create(
  160. model=model,
  161. messages=[{"role": "user", "content": prompt}],
  162. max_tokens=max_tokens,
  163. temperature=temperature,
  164. stream=False,
  165. response_format={"type": "json_object"}
  166. )
  167. # 返回回复内容
  168. return json.loads(response.choices[0].message.content)
  169. except Exception as e:
  170. return f"发生异常: {str(e)}"
  171. if __name__ == '__main__':
  172. print(chat_with_deepseek('你好啊'))