-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathretriever.py
303 lines (248 loc) · 9.2 KB
/
retriever.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import ast
import logging
import os
from pathlib import Path
from typing import List, Optional
import bm25s
import pandas as pd
import weave
from datasets import load_dataset
from joblib import Parallel, delayed
from sentence_transformers import SentenceTransformer
from sentence_transformers.util import cos_sim
from simple_parsing import ArgumentParser
from utils import Problem, Solution, clean_code_string, remove_extra_newlines
logging.basicConfig(
format="%(asctime)s : %(levelname)s : %(message)s", level=logging.INFO
)
logger = logging.getLogger(__name__)
# Data Loading
LANGUAGE_MAP = {
3: "Python3",
}
def clean_code(row: dict) -> dict:
outputs = []
for item in row["code"]:
item = clean_code_string(item)
outputs.append(item)
return {"code": outputs}
def get_solution(row: dict) -> dict:
solutions = row["solutions"]
languages = solutions["language"]
solutions = solutions["solution"]
outputs = []
for language, solution in zip(languages, solutions):
language = LANGUAGE_MAP.get(language)
if language:
outputs.append(solution)
return {"code": outputs}
def get_test_cases(row: dict) -> dict:
tests = row["public_tests"]
return {
"sample_inputs": "".join(tests["input"]),
"sample_outputs": "".join(tests["output"]),
}
def clean_description(row: dict) -> dict:
description = row["description"]
description = remove_extra_newlines(description)
return {"description": description}
def get_code_contests_data(cache_file: Path, reload_cache: bool = False):
if cache_file.exists() and not reload_cache:
logger.info(f"Loading cached raw data from {cache_file}")
return pd.read_json(cache_file, lines=True)
logger.info(f"Loading raw data from dataset")
ds = load_dataset("deepmind/code_contests")
train_ds = ds["train"].map(get_solution, num_proc=4)
train_ds = train_ds.filter(lambda x: not x["is_description_translated"], num_proc=4)
train_ds = train_ds.filter(lambda x: len(x["code"]) > 0, num_proc=4)
train_ds = train_ds.map(clean_code, num_proc=4)
train_ds = train_ds.map(clean_description, num_proc=4)
train_ds = train_ds.map(get_test_cases, num_proc=4)
train_ds = train_ds.remove_columns(
[
col
for col in train_ds.column_names
if col not in ["description", "code", "sample_inputs", "sample_outputs"]
]
)
train_df = train_ds.to_pandas()
train_df = train_df.explode("code").reset_index(drop=True)
train_df = train_df.drop_duplicates(subset=["code"], keep="first")
train_df.to_json(cache_file, orient="records", lines=True)
return train_df
# Data Preprocessing
# Define a mapping from AST node types to token names
TOKEN_MAP = {
ast.FunctionDef: "FUNC_DEF",
ast.ClassDef: "CLASS_DEF",
ast.BinOp: "BIN_OP",
ast.Assign: "ASSIGN",
ast.Expr: "EXPR",
ast.Call: "FUNC_CALL",
ast.If: "IF",
ast.For: "FOR",
ast.While: "WHILE",
ast.Import: "IMPORT",
ast.Return: "RETURN",
ast.List: "LIST",
ast.Dict: "DICT",
ast.Name: "VAR",
ast.Num: "NUMBER", # For older Python versions (< 3.8)
ast.Constant: lambda node: (
"NUMBER"
if isinstance(node.value, (int, float, complex))
else (
"STRING"
if isinstance(node.value, str)
else (
"BOOLEAN"
if isinstance(node.value, bool)
else "NONE" if node.value is None else "UNKNOWN"
)
)
),
}
def tokenize_node(node):
"""Tokenizes an AST node using the TOKEN_MAP dictionary."""
node_type = type(node)
# Handle the case where the node type is in the TOKEN_MAP
if node_type in TOKEN_MAP:
token = TOKEN_MAP[node_type]
if callable(
token
): # If the token is a function (for complex cases like ast.Constant)
yield token(node)
else:
yield token
# Recursively process child nodes
for child in ast.iter_child_nodes(node):
yield from tokenize_node(child)
def normalize_code(code: str) -> Optional[str]:
"""Tokenizes and normalizes any Python code snippet."""
try:
tree = ast.parse(code)
except SyntaxError as e:
return None
tokens = list(tokenize_node(tree))
return " ".join(tokens)
@weave.op
def normalize_code_list(code_list: list[str]) -> list[str]:
if len(code_list) > 1000:
return Parallel(n_jobs=-1)(delayed(normalize_code)(code) for code in code_list)
else:
return [normalize_code(code) for code in code_list]
def preprocess_data(
input_path: Path, output_path: Path, reload_cache: bool = False
) -> pd.DataFrame:
if output_path.exists() and not reload_cache:
logger.info(f"Loading cached preprocessed data from {output_path}")
return pd.read_json(output_path, lines=True)
logger.info(f"Preprocessing data from {input_path}")
data_df = pd.read_json(input_path, lines=True)
data_df["normalized_code"] = normalize_code_list(data_df["code"].tolist())
data_df = data_df.dropna(subset=["normalized_code"])
data_df.to_json(output_path, orient="records", lines=True)
return data_df
class Retriever:
def __init__(self, path: str = "param-bharat/rag-hackercup"):
ds = load_dataset(path, split="train")
data_df = ds.to_pandas()
self.docs = data_df.to_dict(orient="records")
self.corpus = data_df["normalized_code"]
self.retriever = self.index()
def index(self):
corpus = self.corpus.tolist()
corpus_tokens = bm25s.tokenize(corpus, stopwords=None)
retriever = bm25s.BM25(corpus=corpus)
retriever.index(corpus_tokens)
return retriever
@weave.op
def retrieve(self, query: str, k: int = 10):
clean_query = clean_code_string(query)
normalized_query = normalize_code(clean_query)
query_tokens = bm25s.tokenize(normalized_query, stopwords=None)
docs, _ = self.retriever.retrieve(query_tokens, k=k, corpus=self.docs)
return docs[0, :].tolist()
def index_data(
input_path: Path,
output_path: Path,
reload_cache: bool = False,
):
if output_path.exists() and not reload_cache:
logger.info(f"Loading cached retriever from {output_path}")
return Retriever.load(output_path)
logger.info(f"Creating retriever from {input_path}")
data_df = pd.read_json(input_path, lines=True, orient="records")
retriever = Retriever(data_df=data_df)
retriever.index()
retriever.save(output_path)
return retriever
class RerankModel:
def __init__(self):
self.model = SentenceTransformer(
"jinaai/jina-embeddings-v2-base-code", trust_remote_code=True
)
# control your input sequence length up to 8192
self.model.max_seq_length = 1024
@weave.op
def __call__(
self,
problem: Problem,
solution: Solution,
retrieved_docs: List[dict],
top_k: int = 3,
):
query_text = problem.problem_description + " " + solution.source_code
context_text = [
doc["description"] + " " + doc["code"] for doc in retrieved_docs
]
query_embeddings = self.model.encode([query_text])
context_embeddings = self.model.encode(context_text, batch_size=2)
similarities = cos_sim(query_embeddings, context_embeddings)
docs_df = pd.DataFrame(retrieved_docs)
docs_df["similarity"] = similarities[0]
docs_df = docs_df.sort_values(by="similarity", ascending=False)
docs_df = docs_df.drop_duplicates(
subset=["description"],
keep="first",
)
return docs_df.head(top_k).to_dict(orient="records")
rerank_model = RerankModel()
@weave.op
async def rerank_docs(
problem: Problem,
solution: Solution,
retrieved_docs: List[dict],
top_k: int = 3,
) -> List[dict]:
return rerank_model(problem, solution, retrieved_docs, top_k)
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("-c", "--cache-directory", type=Path, default="data/cache")
parser.add_argument("--reload-cache", action="store_true")
args = parser.parse_args()
if not args.cache_directory.exists():
args.cache_directory.mkdir(parents=True)
if (args.cache_directory / "retriever").exists():
retriever = Retriever.load(args.cache_directory / "retriever")
elif (args.cache_directory / "preprocessed.jsonl").exists():
preprocessed_df = preprocess_data(
args.cache_directory / "raw.jsonl",
args.cache_directory / "preprocessed.jsonl",
args.reload_cache,
)
retriever = Retriever(data_df=preprocessed_df)
retriever.index()
retriever.save(args.cache_directory / "retriever")
else:
raw_df = get_code_contests_data(
args.cache_directory / "raw.jsonl", args.reload_cache
)
preprocessed_df = preprocess_data(
args.cache_directory / "raw.jsonl",
args.cache_directory / "preprocessed.jsonl",
args.reload_cache,
)
retriever = Retriever(data_df=preprocessed_df)
retriever.index()
retriever.save(args.cache_directory / "retriever")