Files
AKERN-Langchain/parse_questions.py
Matteo Rosati 719919920f test async
2026-02-17 16:04:18 +01:00

33 lines
964 B
Python

import os
def parse_questions(rag_chain):
domande_dir = "domande"
risposte_dir = "risposte"
os.makedirs(risposte_dir, exist_ok=True)
for filename in sorted(os.listdir(domande_dir)):
if not filename.lower().endswith(".txt"):
continue
domanda_path = os.path.join(domande_dir, filename)
with open(domanda_path, "r", encoding="utf-8") as f:
contents = f.read()
print(f"========== DOMANDA ({domanda_path}) ==========")
print(contents)
response = rag_chain.invoke(contents)
print("========== RISPOSTA ==========")
print(response)
print("\n\n")
base_name = os.path.splitext(filename)[0]
suffix = "".join(ch for ch in base_name if ch.isdigit()) or base_name
risposta_path = os.path.join(risposte_dir, f"risposta{suffix}.txt")
with open(risposta_path, "w", encoding="utf-8") as f:
f.write(response)