|
| 1 | +""" |
| 2 | +[구현 의도] |
| 3 | +세 가지 설명을 단일 LLM 호출로 생성한다. |
| 4 | + 1. definition : 용어의 일반적 정의 |
| 5 | + 2. article_context : 사용자가 읽는 기사에서 이 용어가 쓰인 맥락 (기사 있을 때만) |
| 6 | + 3. recent_trend : 최근 뉴스 기사들에서 이 용어의 동향 (ChromaDB 결과 있을 때만) |
| 7 | +
|
| 8 | +항목이 없을 때 null을 반환하도록 프롬프트에 명시한다. |
| 9 | +""" |
| 10 | + |
| 11 | + |
| 12 | +class TermDefinitionPrompts: |
| 13 | + |
| 14 | + _BASE = """당신은 경제/금융 전문 설명가입니다. |
| 15 | +아래 지시에 따라 용어를 설명하고, 반드시 JSON 형식으로만 응답하세요. |
| 16 | +
|
| 17 | +용어: "{term}" |
| 18 | +
|
| 19 | +{article_section}{trend_section} |
| 20 | +다음 JSON 형식으로 응답하세요: |
| 21 | +{{ |
| 22 | + "definition": "이 용어의 일반적인 정의 (2-3문장, 비전문가도 이해할 수 있게)", |
| 23 | + "article_context": {article_context_instruction}, |
| 24 | + "recent_trend": {recent_trend_instruction} |
| 25 | +}}""" |
| 26 | + |
| 27 | + _ARTICLE_SECTION = """[사용자가 읽고 있는 기사] |
| 28 | +{article_content} |
| 29 | +
|
| 30 | +""" |
| 31 | + |
| 32 | + _TREND_SECTION = """[최근 관련 뉴스 기사들] |
| 33 | +{trend_context} |
| 34 | +
|
| 35 | +""" |
| 36 | + |
| 37 | + @classmethod |
| 38 | + def build( |
| 39 | + cls, |
| 40 | + term: str, |
| 41 | + article_content: str | None, |
| 42 | + trend_docs: list[dict], |
| 43 | + ) -> str: |
| 44 | + article_section = "" |
| 45 | + article_context_instruction = "null" |
| 46 | + |
| 47 | + if article_content and article_content.strip(): |
| 48 | + article_section = cls._ARTICLE_SECTION.format( |
| 49 | + article_content=article_content[:3000] |
| 50 | + ) |
| 51 | + article_context_instruction = ( |
| 52 | + '"위 기사에서 이 용어가 어떤 역할/맥락으로 사용되었는지 2문장으로 설명"' |
| 53 | + ) |
| 54 | + |
| 55 | + trend_section = "" |
| 56 | + recent_trend_instruction = "null" |
| 57 | + |
| 58 | + if trend_docs: |
| 59 | + trend_lines = [] |
| 60 | + for i, doc in enumerate(trend_docs, 1): |
| 61 | + meta = doc.get("metadata", {}) |
| 62 | + title = meta.get("title", "") |
| 63 | + published = meta.get("publishedAt", "") |
| 64 | + text = doc.get("text", "")[:500] |
| 65 | + trend_lines.append(f"[기사{i}] {title} ({published})\n{text}") |
| 66 | + trend_section = cls._TREND_SECTION.format( |
| 67 | + trend_context="\n\n".join(trend_lines) |
| 68 | + ) |
| 69 | + recent_trend_instruction = ( |
| 70 | + '"위 최근 기사들을 바탕으로, 현재 이 용어와 관련된 뉴스 동향을 2-3문장으로 설명"' |
| 71 | + ) |
| 72 | + |
| 73 | + return cls._BASE.format( |
| 74 | + term=term, |
| 75 | + article_section=article_section, |
| 76 | + trend_section=trend_section, |
| 77 | + article_context_instruction=article_context_instruction, |
| 78 | + recent_trend_instruction=recent_trend_instruction, |
| 79 | + ) |
0 commit comments