Corin1998 commited on
Commit
2779f4d
·
verified ·
1 Parent(s): eacb67f

Create pa.py

Browse files
Files changed (1) hide show
  1. generators/pa.py +28 -0
generators/pa.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ from rag.retriever import retrieve, format_citations
3
+ from rag.prompts import QA_SYS , QA_USER
4
+ from app.deps import gen
5
+
6
+ def make_qa(query: str, n=30):
7
+ hits = retrieve(query, top_k=16)
8
+ links = format_citations(hits)
9
+ link_lines="\n".join([f"[{i}] {u}" for i, u in links])
10
+ ctx = "\n".join([f"[chunk:{h['chunk_id']}] {h['text']}" for h in hits])
11
+
12
+ prompt = [{"role":"system", "content":QA_SYS},
13
+ {"role":"user","content":QA_USER.format(links=link_lines, contexts=ctx)}]
14
+
15
+ raw = gen(prompt, do_sample=False, temperature=0.2, max_new_tokens=1800)[0]["generated_text"]
16
+ # モデル出力がJSON配列でない場合の最低限フォールバック
17
+ try:
18
+ data = json.load(raw)
19
+ except Exception:
20
+ data = []
21
+ for line in raw.splitlines():
22
+ if "Q:" in line and "A:" in line:
23
+ q = line.split("Q:",1)[1].split("A:",1)[0].strip()
24
+ a = line.split("A:",1)[1].strip()
25
+ data.append({"q":q,"a":a,"sources":[]})
26
+
27
+ # n件に整える
28
+ return data[:n], links