Spaces:
Sleeping
Sleeping
Rename generators/summaru.py to generators/summary.py
Browse files- generators/summaru.py +0 -17
- generators/summary.py +69 -0
generators/summaru.py
DELETED
|
@@ -1,17 +0,0 @@
|
|
| 1 |
-
from rag.retriever import retrieve, format_citations
|
| 2 |
-
from rag.prompts import SUMMARY_SYS, SUMMARY_USER
|
| 3 |
-
from irpr.deps import generate_chat
|
| 4 |
-
|
| 5 |
-
def make_summary(query: str):
|
| 6 |
-
hits = retrieve(query, top_k=10)
|
| 7 |
-
links = format_citations(hits)
|
| 8 |
-
link_lines = "\n".join([f"[{i}] {u}" for i, u in links])
|
| 9 |
-
ctx = "\n".join([f"[chunk:{h['chunk_id']}] {h['text']}" for h in hits])
|
| 10 |
-
|
| 11 |
-
messages = [
|
| 12 |
-
{"role": "system", "content": SUMMARY_SYS},
|
| 13 |
-
{"role": "user", "content": SUMMARY_USER.format(links=link_lines, contexts=ctx)}
|
| 14 |
-
]
|
| 15 |
-
|
| 16 |
-
out = generate_chat(messages, max_new_tokens=800, temperature=0.2)
|
| 17 |
-
return out, links
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
generators/summary.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# generators/summary.py
|
| 2 |
+
from __future__ import annotations
|
| 3 |
+
from typing import List, Tuple
|
| 4 |
+
from irpr.deps import search, generate_chat
|
| 5 |
+
|
| 6 |
+
SYS = "あなたは日本語のIRアナリストです。引用部分から経営サマリを作ります。冗長な敬語は避け、要点を構造化してください。マークダウンで出力。"
|
| 7 |
+
|
| 8 |
+
TPL = """以下は関連文書の抜粋です。これを根拠に、経営サマリを作成してください。
|
| 9 |
+
|
| 10 |
+
# 出力フォーマット
|
| 11 |
+
業績ハイライト:
|
| 12 |
+
- 箇条書きで3-5項目
|
| 13 |
+
|
| 14 |
+
見通し:
|
| 15 |
+
- 2-4項目
|
| 16 |
+
|
| 17 |
+
セグメント:
|
| 18 |
+
- 主要セグメントごとに1-2行
|
| 19 |
+
|
| 20 |
+
財務:
|
| 21 |
+
- 売上/利益、CF、BSの要点
|
| 22 |
+
|
| 23 |
+
株主還元:
|
| 24 |
+
- 配当/自社株買い等
|
| 25 |
+
|
| 26 |
+
ESG:
|
| 27 |
+
- 重要事項があれば簡潔に
|
| 28 |
+
|
| 29 |
+
リスク:
|
| 30 |
+
- 2-3項目
|
| 31 |
+
|
| 32 |
+
# 根拠抜粋
|
| 33 |
+
{context}
|
| 34 |
+
"""
|
| 35 |
+
|
| 36 |
+
def make_summary(query: str, k: int = 10) -> Tuple[str, List[str]]:
|
| 37 |
+
hits = search(query, top_k=k)
|
| 38 |
+
links, ctx_lines = [], []
|
| 39 |
+
for i, h in enumerate(hits, 1):
|
| 40 |
+
src = h.get("source_url") or ""
|
| 41 |
+
if src and src not in links: links.append(src)
|
| 42 |
+
ctx_lines.append(f"[{i}] {h.get('title') or ''} {src}\n{h['text'][:1200]}")
|
| 43 |
+
context = "\n\n".join(ctx_lines) if ctx_lines else "(根拠なし)"
|
| 44 |
+
|
| 45 |
+
# LLM試行(失敗しても握りつぶす)
|
| 46 |
+
try:
|
| 47 |
+
text = generate_chat(
|
| 48 |
+
[{"role":"system","content":SYS},
|
| 49 |
+
{"role":"user","content":TPL.format(context=context)}],
|
| 50 |
+
max_new_tokens=700
|
| 51 |
+
)
|
| 52 |
+
if text.strip():
|
| 53 |
+
return text.strip(), links
|
| 54 |
+
except Exception:
|
| 55 |
+
pass
|
| 56 |
+
|
| 57 |
+
# フォールバック(抽出)
|
| 58 |
+
if not hits:
|
| 59 |
+
return "根拠が見つかりませんでした。", links
|
| 60 |
+
bullets = []
|
| 61 |
+
for h in hits[:5]:
|
| 62 |
+
for line in (h["text"][:500]).splitlines():
|
| 63 |
+
line = line.strip()
|
| 64 |
+
if 6 <= len(line) <= 120:
|
| 65 |
+
bullets.append(f"- {line}")
|
| 66 |
+
if len(bullets) >= 5: break
|
| 67 |
+
if len(bullets) >= 5: break
|
| 68 |
+
text = "業績ハイライト:\n" + "\n".join(bullets) + "\n\n見通し:\n- (要約生成なし/抽出)"
|
| 69 |
+
return text, links
|