Spaces:
Sleeping
Sleeping
Update generators/qa.py
Browse files- generators/qa.py +11 -7
generators/qa.py
CHANGED
|
@@ -6,7 +6,7 @@ from irpr.deps import search, generate_chat
|
|
| 6 |
SYS = "あなたは日本語のIR担当です。投資家からの想定質問と模範回答を、根拠に基づいて簡潔に作成します。"
|
| 7 |
|
| 8 |
TPL = """次の抜粋を根拠に、投資家向けの想定Q&Aを {n} 問作成してください。
|
| 9 |
-
各問は "Q: ... / A: ..." の2行
|
| 10 |
|
| 11 |
# 抜粋
|
| 12 |
{context}
|
|
@@ -21,21 +21,25 @@ def make_qa(query: str, n: int = 30) -> Tuple[List[dict], List[str]]:
|
|
| 21 |
ctx.append(f"[{i}] {h.get('title') or ''} {src}\n{h['text'][:1000]}")
|
| 22 |
context = "\n\n".join(ctx) if ctx else "(根拠なし)"
|
| 23 |
|
| 24 |
-
#
|
| 25 |
qa_list: List[dict] = []
|
| 26 |
try:
|
| 27 |
out = generate_chat(
|
| 28 |
[{"role":"system","content":SYS},
|
| 29 |
{"role":"user","content":TPL.format(n=n, context=context)}],
|
| 30 |
-
max_new_tokens=
|
| 31 |
)
|
|
|
|
| 32 |
for line in out.splitlines():
|
| 33 |
line = line.strip()
|
| 34 |
if line.startswith("Q:"):
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
| 39 |
if qa_list:
|
| 40 |
return qa_list[:n], links
|
| 41 |
except Exception:
|
|
|
|
| 6 |
SYS = "あなたは日本語のIR担当です。投資家からの想定質問と模範回答を、根拠に基づいて簡潔に作成します。"
|
| 7 |
|
| 8 |
TPL = """次の抜粋を根拠に、投資家向けの想定Q&Aを {n} 問作成してください。
|
| 9 |
+
各問は "Q: ... / A: ..." の2行。Aは2-4文以内。根拠があれば括弧で短く示してください。
|
| 10 |
|
| 11 |
# 抜粋
|
| 12 |
{context}
|
|
|
|
| 21 |
ctx.append(f"[{i}] {h.get('title') or ''} {src}\n{h['text'][:1000]}")
|
| 22 |
context = "\n\n".join(ctx) if ctx else "(根拠なし)"
|
| 23 |
|
| 24 |
+
# OpenAI で生成
|
| 25 |
qa_list: List[dict] = []
|
| 26 |
try:
|
| 27 |
out = generate_chat(
|
| 28 |
[{"role":"system","content":SYS},
|
| 29 |
{"role":"user","content":TPL.format(n=n, context=context)}],
|
| 30 |
+
max_new_tokens=1500
|
| 31 |
)
|
| 32 |
+
cur = None
|
| 33 |
for line in out.splitlines():
|
| 34 |
line = line.strip()
|
| 35 |
if line.startswith("Q:"):
|
| 36 |
+
if cur and cur.get("q") and cur.get("a"):
|
| 37 |
+
qa_list.append(cur)
|
| 38 |
+
cur = {"q": line[2:].strip(), "a": ""}
|
| 39 |
+
elif line.startswith("A:") and cur:
|
| 40 |
+
cur["a"] = line[2:].strip()
|
| 41 |
+
if cur and cur.get("q") and cur.get("a"):
|
| 42 |
+
qa_list.append(cur)
|
| 43 |
if qa_list:
|
| 44 |
return qa_list[:n], links
|
| 45 |
except Exception:
|