analysis_service.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. """标书解析服务。"""
  2. from typing import AsyncGenerator
  3. from ..utils.openai_util import OpenAIUtil
  4. from ..utils.prompts.analysis_prompts import build_analysis_messages
  5. class AnalysisService:
  6. """负责标书解析与文档分析。"""
  7. def __init__(self, ai: OpenAIUtil | None = None):
  8. self.ai = ai or OpenAIUtil()
  9. async def stream_document_analysis(
  10. self,
  11. file_content: str,
  12. analysis_type: str,
  13. ) -> AsyncGenerator[str, None]:
  14. """流式分析文档内容。"""
  15. messages = build_analysis_messages(file_content, analysis_type)
  16. async for chunk in self.ai.stream_chat_completion(messages, temperature=0.3):
  17. yield chunk
  18. async def stream_chat_completion(
  19. self,
  20. messages: list[dict[str, str]],
  21. temperature: float = 0.7,
  22. response_format: dict | None = None,
  23. ) -> AsyncGenerator[str, None]:
  24. """兼容直接透传底层聊天接口。"""
  25. async for chunk in self.ai.stream_chat_completion(
  26. messages,
  27. temperature=temperature,
  28. response_format=response_format,
  29. ):
  30. yield chunk