llm_classifier.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. from typing import List
  2. from applications.config import Chunk
  3. from applications.api import fetch_deepseek_completion
  4. class LLMClassifier:
  5. @staticmethod
  6. def generate_prompt(chunk_text: str) -> str:
  7. raw_prompt = """
  8. 你是一个文本分析助手。
  9. 我会给你一段文本,请你输出以下信息:
  10. 1. **主题标签 (topic)**:一句话概括文本主题
  11. 2. **关键词 (keywords)**:3-5 个,便于检索
  12. 3. **摘要 (summary)**:50字以内简要说明
  13. 4. **领域 (domain)**:该文本所属领域(如:AI 技术、体育、金融)
  14. 5. **任务类型 (task_type)**:文本主要任务类型(如:解释、教学、动作描述、方法提出)
  15. 6. **核心知识点 (concepts)**:涉及的核心知识点或概念
  16. 7. **显示/隐式问题 (questions)**:文本中隐含或显式的问题
  17. 8. **实体(entities)**: 文本中的提到的命名实体
  18. 请用 JSON 格式输出,例如:
  19. {
  20. "topic": "RAG 技术与分块策略",
  21. "summary": "介绍RAG技术并提出主题感知的分块方法。",
  22. "domain": "AI 技术",
  23. "task_type": "方法提出",
  24. "keywords": ["RAG", "检索增强", "文本分块", "知识图谱"],
  25. "concepts": ["RAG", "文本分块", "知识图谱"],
  26. "questions": ["如何提升RAG的检索效果?"]
  27. "entities": ["entity1"]
  28. }
  29. 下面是文本:
  30. """
  31. return raw_prompt.strip() + chunk_text
  32. async def classify_chunk(self, chunk: Chunk) -> Chunk:
  33. text = chunk.text.strip()
  34. prompt = self.generate_prompt(text)
  35. response = await fetch_deepseek_completion(
  36. model="DeepSeek-V3", prompt=prompt, output_type="json"
  37. )
  38. print(response)
  39. return Chunk(
  40. chunk_id=chunk.chunk_id,
  41. doc_id=chunk.doc_id,
  42. text=text,
  43. tokens=chunk.tokens,
  44. topic_purity=chunk.topic_purity,
  45. summary=response.get("summary"),
  46. topic=response.get("topic"),
  47. domain=response.get("domain"),
  48. task_type=response.get("task_type"),
  49. concepts=response.get("concepts", []),
  50. keywords=response.get("keywords", []),
  51. questions=response.get("questions", []),
  52. entities=response.get("entities", []),
  53. )