-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestionMaker.py
73 lines (54 loc) · 2.21 KB
/
questionMaker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from langchain_core.output_parsers import StrOutputParser
from langchain_groq import ChatGroq
from langchain.prompts import PromptTemplate
import argparse
import os,re
from dotenv import load_dotenv
from langchain_core.output_parsers import StrOutputParser
output_parser = StrOutputParser()
model = None
prompt = PromptTemplate(
template="""your goal is to create a question that would accept the following text as answer:
<{text}>. IMPORTANT: only provide the question, no other sentence""",
input_variables=["text"],
)
chain = None
long_chain = None
def handleQA(fname,text):
text = text.strip().replace('"', "'")
res = "question,answer\n"
t= chain.invoke({"text": text})
csv_filename = f"q-{fname}.csv"
with open(csv_filename, 'w', encoding='utf-8') as file:
file.write(res+f'"{t}","' + text+'"')
def main():
parser = argparse.ArgumentParser(description="Process each .txt file in a given directory.")
parser.add_argument("directory", type=str, help="The directory to process")
args = parser.parse_args()
directory = args.directory
if not os.path.exists(directory):
print(f"The directory {directory} does not exist.")
return
load_dotenv()
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
os.environ["LANGCHAIN_TRACING_V2"] = os.getenv("LANGCHAIN_TRACING_V2")
os.environ["LANGCHAIN_API_KEY"] = os.getenv("LANGCHAIN_API_KEY")
os.environ["LANGCHAIN_PROJECT"] = os.getenv("LANGCHAIN_PROJECT")
groq_model = os.getenv("GROK_MODEL")
api_key = os.getenv("GROK_API_KEY")
global model
model = ChatGroq(groq_api_key=api_key,model_name=groq_model, temperature=0.0)
print(model)
global chain
chain = prompt | model | output_parser
for filename in os.listdir(directory):
if filename.endswith(".txt"):
file_path = os.path.join(directory, filename)
with open(file_path, 'r', encoding='utf-8') as file:
text = file.read()
base_filename = os.path.splitext(filename)[0]
fname = os.path.join(directory, base_filename)
handleQA(fname,text)
print(f"Processed {filename}")
if __name__ == "__main__":
main()