-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_test_data.py
157 lines (132 loc) · 4.79 KB
/
generate_test_data.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import argparse
import csv
import asyncio
from ragas.llms import LangchainLLMWrapper
from ragas.embeddings import LangchainEmbeddingsWrapper
from langchain_openai import ChatOpenAI
from langchain_openai import OpenAIEmbeddings
from ragas.testset import TestsetGenerator
from langchain_community.document_loaders.unstructured import UnstructuredFileLoader
from ragas.testset.synthesizers import default_query_distribution
from ragas.testset.synthesizers.single_hop.specific import (
SingleHopSpecificQuerySynthesizer,
)
from langchain_community.cache import SQLiteCache
from langchain.globals import set_llm_cache
# from ragas.testset.evolutions import simple, reasoning, multi_context
async def main(file_list, output_file, num_test_cases, with_multi_hop, use_japanese):
generator_llm = LangchainLLMWrapper(ChatOpenAI(model="gpt-4o-mini"))
generator_embeddings = LangchainEmbeddingsWrapper(OpenAIEmbeddings())
if with_multi_hop:
# Default query distribution:
# [
# (SingleHopSpecificQuerySynthesizer(llm=llm), 0.5),
# (MultiHopAbstractQuerySynthesizer(llm=llm), 0.25),
# (MultiHopSpecificQuerySynthesizer(llm=llm), 0.25),
# ]
distribution = default_query_distribution(generator_llm)
else:
synthesizer = SingleHopSpecificQuerySynthesizer(llm=generator_llm)
# Change the property name from default "entities" to "headlines" because Entity Extraction seems to be fragile and leads to empty results
synthesizer.property_name = "headlines"
distribution = [
(synthesizer, 1.0),
]
testset_list = []
if use_japanese:
for query, _ in distribution:
prompts = await query.adapt_prompts("japanese", llm=generator_llm)
query.set_prompts(**prompts)
all_docs = []
for file in file_list:
file_name = file.split("/")[-1]
print("Processing file: ", file_name)
loader = UnstructuredFileLoader(file)
docs = loader.load()
print("Loaded documents: ", len(docs))
generator = TestsetGenerator(
llm=generator_llm, embedding_model=generator_embeddings
)
if not with_multi_hop:
dataset = generator.generate_with_langchain_docs(
docs, testset_size=num_test_cases, query_distribution=distribution
)
testset = dataset.to_list()
for test in testset:
test["document_name"] = file_name
testset_list += testset
else:
all_docs += docs
if with_multi_hop:
dataset = generator.generate_with_langchain_docs(
all_docs,
testset_size=num_test_cases * len(file_list),
query_distribution=distribution,
)
testset_list = dataset.to_list()
with open(output_file, "w", encoding="utf-8-sig") as f:
writer = csv.DictWriter(
f,
[
"query",
"expected_answer",
"reference_contexts",
"synthesizer_name",
"document_name",
],
)
writer.writeheader()
for test in testset_list:
writer.writerow(
{
"query": test["user_input"],
"expected_answer": test["reference"],
"reference_contexts": test["reference_contexts"],
"synthesizer_name": test["synthesizer_name"],
"document_name": test["document_name"],
}
)
def parse_arguments():
parser = argparse.ArgumentParser(
description="Generate test data for RAG with the given documents."
)
parser.add_argument(
"files", type=str, nargs="+", help="The input files to generate test data from"
)
parser.add_argument(
"-o",
"--output",
default="test_data.csv",
type=str,
help="The output file to save the results",
)
parser.add_argument(
"--with_multi_hop",
action="store_true",
help="Whether to generate test cases which require reasoning over multiple documents",
)
parser.add_argument(
"-n",
"--num_test_cases",
default=5,
type=int,
help="The number of test cases to generate for each document",
)
parser.add_argument(
"--use_japanese",
action="store_true",
help="Whether to use Japanese language for the test data",
)
return parser.parse_args()
if __name__ == "__main__":
args = parse_arguments()
set_llm_cache(SQLiteCache(database_path=".langchain.db"))
asyncio.run(
main(
args.files,
args.output,
args.num_test_cases,
args.with_multi_hop,
args.use_japanese,
)
)