마케팅 분석 결과 500자 요약 기능 추가

This commit is contained in:
bluebamus committed 2025-12-23 17:50:51 +09:00
1 parent 39db84c797
commit 83a72aaa46
3 files changed
+301 -732

No files matched your search

+72
View File
@@ -1,3 +1,6 @@
import json
import re
from openai import AsyncOpenAI
from config import apikey_settings
@@ -255,3 +258,72 @@ class ChatgptService:
)
message = completion.choices[0].message.content
return message or ""
async def summarize_marketing(self, text: str) -> str:
"""마케팅 텍스트를 항목으로 구분하여 500자로 요약 정리"""
prompt = f"""[ROLE]
마케팅 콘텐츠 요약 전문가
[INPUT]
{text}
[TASK]
위 텍스트를 분석하여 핵심 내용을 항목별로 구분하여 500자 이내로 요약해주세요.
[OUTPUT REQUIREMENTS]
- 항목별로 구분하여 정리 (예: 타겟 고객, 차별점, 지역 특성 등)
- 총 500자 이내로 요약
- 핵심 정보만 간결하게 포함
- 한국어로 작성
[OUTPUT FORMAT]
---
[항목별로 구분된 500자 이내 요약]
---
"""
completion = await self.client.chat.completions.create(
model=self.model, messages=[{"role": "user", "content": prompt}]
)
message = completion.choices[0].message.content
result = message or ""
# --- 구분자 제거
if result.startswith("---"):
result = result[3:].strip()
if result.endswith("---"):
result = result[:-3].strip()
return result
async def parse_marketing_analysis(self, raw_response: str) -> dict:
"""ChatGPT 마케팅 분석 응답을 파싱하고 요약하여 딕셔너리로 반환
Returns:
dict: {"report": str, "tags": list[str], "facilities": list[str]}
"""
tags: list[str] = []
facilities: list[str] = []
report = raw_response
# JSON 블록 추출 시도
json_match = re.search(r"```json\s*(\{.*?\})\s*```", raw_response, re.DOTALL)
if json_match:
try:
json_data = json.loads(json_match.group(1))
tags = json_data.get("tags", [])
facilities = json_data.get("facilities", [])
# JSON 블록을 제외한 리포트 부분 추출
report = raw_response[: json_match.start()].strip()
# --- 구분자 제거
if report.startswith("---"):
report = report[3:].strip()
if report.endswith("---"):
report = report[:-3].strip()
except json.JSONDecodeError:
pass
# 리포트 내용을 500자로 요약
if report:
report = await self.summarize_marketing(report)
return {"report": report, "tags": tags, "facilities": facilities}