File size: 7,395 Bytes
a7c0211
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
304
305
306
307
308

# Requires vllm>=0.8.5
import torch
import vllm
from vllm import LLM
from transformers import AutoTokenizer
from pathlib import Path
import os
import jsonlines

os.environ["CUDA_VISIBLE_DEVICES"] = "1"

# 必须在设置 CUDA_VISIBLE_DEVICES 之后
import multiprocessing as mp
mp.set_start_method("spawn", force=True)


def get_detailed_instruct(task_description: str, query: str) -> str:
    return f'Instruct: {task_description}\nQuery:{query}'


keywords = ["Quantum mechanics",
    "Gene editing",
    "Folding",
    "System biology",
    "Antibody",
    "Heterogeneity",
    "Ligand",
    "Drug repurpose",
    "Kinetics",
    "Next-generation sequencing",
    "Pharmacogenetics",
    "Phase-field technique",
    "Human",
    "Potential",
    "Hartree-Fock",
    "Flow matching",
    "Lipid",
    "Biomedical",
    "Antigen",
    "Stochastic modeling",
    "Coupled cluster",
    "Quantum biology",
    "Spatial biology",
    "Antagonist",
    "Free energy perturbation",
    "Cycle",
    "Pharmacology",
    "Redox",
    "Physiology",
    "Protein-Protein Interactions",
    "Single-cell",
    "Screening",
    "Hydrophobic",
    "First-principles based DFT",
    "Molecular biology",
    "Mechanism",
    "Reproduction number",
    "Spatial Transcriptomics",
    "Ion",
    "Computational Materials",
    "Absorption",
    "Pharmacometrics",
    "GAN",
    "Compartmental model",
    "Diagnostics",
    "Lead discovery",
    "QAPR",
    "Rosettafold",
    "Autoregressive",
    "Pharmacokinetics",
    "Biotechnology",
    "Hydrophilic",
    "3D",
    "Protein",
    "QM/MM",
    "Activation",
    "AMR",
    "Networks",
    "Genotype",
    "Gene regulatory networks",
    "Biologics",
    "Phenotype",
    "Nowcasting",
    "DFT",
    "AlphaFold",
    "Pandemic",
    "Immunology",
    "Pathology",
    "Chemical space",
    "Transformer",
    "Homeostasis",
    "Score",
    "High-throughput",
    "Cheminformatics",
    "Hit-to-lead",
    "Sequencing",
    "Enzyme",
    "Antimicrobial resistance modeling",
    "Allosteric",
    "Inhibition",
    "Computational Biochemistry",
    "Bioinformatics",
    "Transcriptomics",
    "Diffusion",
    "Anomaly detection",
    "Multi-omics",
    "Biology",
    "Pathway",
    "Metabolomics",
    "Synthetic biology",
    "Microbial",
    "Proteomics",
    "Pharmaceutics",
    "Organoid",
    "Network pharmacology",
    "Imaging",
    "Generative adversarial networks",
    "Microbiology",
    "Organ-on-a-chip",
    "De novo",
    "Substrate",
    "Personalized",
    "Drug",
    "Transcription",
    "RNA",
    "Explainable AI",
    "Generate",
    "Docking",
    "Pathogens",
    "Bio foundation model",
    "Reinforcement learning",
    "Mechanism of action",
    "Generative",
    "Metabolic",
    "Metabolic Flux Analysis",
    "Computational Chemistry",
    "Vaccine",
    "Biophysics",
    "Integration",
    "Biochemistry",
    "Physiologically based pharmacokinetics model",
    "Medicine",
    "Crystal",
    "Conjugate",
    "Variational autoencoders",
    "In Silico",
    "Protein-protein",
    "CRISPR",
    "Spatial transcriptomics",
    "Gene",
    "Translation",
    "Glycomics",
    "Lead optimization",
    "Pharmacodynamics",
    "Ab initio",
    "System immunology",
    "Pseudotime analysis",
    "Generative AI",
    "RNN",
    "Regulatory networks",
    "PBPK model",
    "Beta-blocker",
    "Lipidomics",
    "Reaction",
    "Bio",
    "Genesis",
    "Evolution",
    "Computational Biology",
    "VAE",
    "Pharmacogenomics",
    "Assay",
    "Sensors",
    "Conformation",
    "Finite element method",
    "Human atlas",
    "Translational medicine",
    "Neurology",
    "Genomics",
    "Cell biology",
    "Porous",
    "Biomarker",
    "Bioengineering",
    "Allele",
    "Recurrent neural networks",
    "Carbohydrate",
    "Metamaterial",
    "Virtual human",
    "DNA",
    "Omics",
    "Agonist",
    "Receptor",
    "Cofactor",
    "Metabolic flux analysis",
    "Cell atlas",
    "Signaling",
    "Electronic structure",
    "Monte Carlo",
    "Genomic surveillance",
    "Agent-based model",
    "Biosensors",
    "2D",
    "QSAR",
    "Codon",
    "Coenzyme",
    "Nucleic acids",
    "Dynamics",
    "Ensemble",
    "Spectrometry",
    "Multi-scale modeling",
    "ADMET",
    "Marker",
    "Toxicology",
    "Profiling",
    "Design",
    "Viral",
    "Chemistry",
    "Epigenetics",
    "Homo-Lumo",
    "Modeling",
    "Prediction",
    "Quantum Chemistry",
    "Half-life",
    "Material",
    "Disease",
    "Phylodynamic model",
    "Metagenomics",
    "Digital twin",
    "Cancer biology",
    "Discovery",
    "Bioavailability",
    "Digital PCR"
    ]

# Each query must come with a one-sentence instruction that describes the task
task = 'Given a web search query, retrieve relevant passages that answer the query'

queries = [
    get_detailed_instruct(task, ' '.join(keywords))
]

model = LLM(model="Qwen/Qwen3-Embedding-0.6B",
            task="embed",
            tensor_parallel_size=1,
            data_parallel_size=1)


def get_functions_contents(dir):
    subdirs = sorted([d for d in os.listdir(dir) if os.path.isdir(os.path.join(dir, d))])
    for subdir in subdirs:
        if subdir == 'ElectronicStructureLibrary___libxc':
            continue
        print(subdir)
        json_path = os.path.join(dir, subdir, 'functions.jsonl')
        contents = []
        if os.path.exists(json_path):
            objs = []
            has_scored = False
            has_read = False
            with jsonlines.open(json_path) as reader:
                has_read = True
                for obj in reader:
                    if 'score' in obj:
                        has_scored = True
                        break
                    file_path = obj['file']
                    with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
                        func_content = ''.join(f.readlines()[obj['start_line']-1:obj['end_line']])[:32000]
                        # obj['content'] = func_content
                        contents.append(func_content)
                        objs.append(obj)
                if has_read and not has_scored:
                    scores = get_scores(contents)
                    for i, obj in enumerate(objs):
                        obj['score'] = scores[i]
            if has_read and not has_scored:
                with jsonlines.open(json_path, 'w', flush=True) as writer:
                    writer.write_all(objs)
                    print("finish ", subdir)
            

tokenizer = AutoTokenizer.from_pretrained(
    "Qwen/Qwen3-Embedding-0.6B",
    trust_remote_code=True
)

MAX_TOKENS = 30000   # 留点 buffer
  
def truncate_to_max_tokens(text, max_tokens=MAX_TOKENS):
    tokens = tokenizer(
        text,
        truncation=True,
        max_length=max_tokens,
        return_tensors=None
    )
    return tokenizer.decode(tokens["input_ids"], skip_special_tokens=True)

def get_scores(documents):
    safe_queries = [truncate_to_max_tokens(q) for q in queries]
    safe_docs = [truncate_to_max_tokens(d) for d in documents]

    input_texts = safe_queries + safe_docs
    outputs = model.embed(input_texts)
    embeddings = torch.tensor([o.outputs.embedding for o in outputs])
    scores = (embeddings[0] @ embeddings[1:].T)
    return scores.tolist()

get_functions_contents('/home/weifengsun/tangou1/step2/step22/dataset')