Update README.md
Browse files
README.md
CHANGED
|
@@ -5,4 +5,96 @@ language:
|
|
| 5 |
pipeline_tag: text-generation
|
| 6 |
tags:
|
| 7 |
- chemistry
|
| 8 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
pipeline_tag: text-generation
|
| 6 |
tags:
|
| 7 |
- chemistry
|
| 8 |
+
---
|
| 9 |
+
# RetroDFM-R: Reasoning-Driven Retrosynthesis Prediction with Large Language Models via Reinforcement Learning
|
| 10 |
+
|
| 11 |
+
RetroDFM-R is a reasoning-driven large language model designed for chemical retrosynthesis. Unlike traditional graph-based or sequence models, it incorporates large-scale reinforcement learning with chemically verifiable rewards, enabling stronger generalization, higher prediction reliability, and improved interpretability. Comprehensive evaluations show that RetroDFM-R outperforms existing state-of-the-art approaches across standard benchmarks. Double-blind human assessments further confirm the chemical plausibility and practical usefulness of its predictions. The model also successfully reconstructs multistep routes for real drug molecules and complex materials reported in the literature. Its explicit reasoning process offers clear, human-interpretable insights, enhancing trust and real-world applicability in retrosynthesis planning.
|
| 12 |
+
|
| 13 |
+
## News
|
| 14 |
+
|
| 15 |
+
* <font color="#935000">**2025-11-22**:</font> The parameter of [RetroDFM-R-8B](https://huggingface.co/OpenDFM/RetroDFM-R-8B) is open-sourced!
|
| 16 |
+
* <font color="#935000">**2025-07-23**</font>: The paper of RetroDFM-R is released on arXiv: [Reasoning-Driven Retrosynthesis Prediction with Large Language Models via Reinforcement Learning](https://arxiv.org/abs/2507.17448).
|
| 17 |
+
|
| 18 |
+
## Training Details
|
| 19 |
+
|
| 20 |
+
RetroDFM-R is trained through a three-stage pipeline: (1) continual pretraining on retrosynthesis-focused chemical data, (2) supervised fine-tuning on distilled chain-of-thought reasoning samples, and (3) reinforcement learning to further enhance step-by-step reasoning and prediction quality.
|
| 21 |
+
|
| 22 |
+
## Usage Details
|
| 23 |
+
|
| 24 |
+
### Local Inference
|
| 25 |
+
|
| 26 |
+
To load and run RetroDFM-R locally, here is an example:
|
| 27 |
+
|
| 28 |
+
```python
|
| 29 |
+
import re
|
| 30 |
+
import torch
|
| 31 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
|
| 32 |
+
|
| 33 |
+
model_name_or_id = "OpenDFM/RetroDFM-R-8B"
|
| 34 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name_or_id)
|
| 35 |
+
model = AutoModelForCausalLM.from_pretrained(model_name_or_id, torch_dtype=torch.bfloat16, device_map="auto")
|
| 36 |
+
|
| 37 |
+
target_smiles = "<target mol in SMILES format>"
|
| 38 |
+
instruction = f"<SMILES> {target_smiles} </SMILES> Given the product SMILES, your task is to predict the reactants SMILES using your experienced chemical Retrosynthesis knowledge. Please reason step by step, and put your final answer within <answer> answer here </answer>."
|
| 39 |
+
|
| 40 |
+
message = [
|
| 41 |
+
{"role": "user", "content": instruction}
|
| 42 |
+
]
|
| 43 |
+
|
| 44 |
+
input_text = tokenizer.apply_chat_template(message, tokenize=False, add_generation_prompt=True)
|
| 45 |
+
inputs = tokenizer(input_text, return_tensors="pt").to("cuda")
|
| 46 |
+
generation_config = GenerationConfig(
|
| 47 |
+
do_sample=True,
|
| 48 |
+
top_k=20,
|
| 49 |
+
top_p=0.9,
|
| 50 |
+
temperature=0.6,
|
| 51 |
+
max_new_tokens=1024,
|
| 52 |
+
eos_token_id=tokenizer.eos_token_id
|
| 53 |
+
)
|
| 54 |
+
outputs = model.generate(**inputs, generation_config=generation_config)
|
| 55 |
+
|
| 56 |
+
generated_text = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
|
| 57 |
+
input_text = tokenizer.decode(inputs["input_ids"][0], skip_special_tokens=True)
|
| 58 |
+
generated_text = generated_text[len(input_text):].strip()
|
| 59 |
+
print(f"{generated_text=}")
|
| 60 |
+
|
| 61 |
+
thinking, answer = re.match(r'<think>(.*?)</think>\s?<answer>(.*?)</answer>', generated_text, re.DOTALL).groups()
|
| 62 |
+
thinking, answer = thinking.strip(), answer.strip()
|
| 63 |
+
print(f"{thinking=}")
|
| 64 |
+
print(f"{answer=}")
|
| 65 |
+
```
|
| 66 |
+
|
| 67 |
+
### SMILES preprocess
|
| 68 |
+
|
| 69 |
+
When there involves SMILES notation in your input, we recommend to preprocess the SMILES with the `rdkit` package to canonicalize the SMILES. Here is an example:
|
| 70 |
+
```python
|
| 71 |
+
from rdkit import Chem
|
| 72 |
+
def canonicalize_smiles(smiles):
|
| 73 |
+
mol = Chem.MolFromSmiles(smiles)
|
| 74 |
+
if mol is None:
|
| 75 |
+
return None
|
| 76 |
+
return Chem.MolToSmiles(mol, isomericSmiles=True, kekuleSmiles=False)
|
| 77 |
+
```
|
| 78 |
+
or directly:
|
| 79 |
+
```python
|
| 80 |
+
from rdkit import Chem
|
| 81 |
+
def canonicalize_smiles(smiles):
|
| 82 |
+
return Chem.CanonSmiles(smiles, useChiral=True)
|
| 83 |
+
```
|
| 84 |
+
|
| 85 |
+
## Citation
|
| 86 |
+
```bibtex
|
| 87 |
+
@misc{zhang2025retrodfmr,
|
| 88 |
+
title={Reasoning-Driven Retrosynthesis Prediction with Large Language Models via Reinforcement Learning},
|
| 89 |
+
author={Zhang, Situo and Li, Hanqi and Chen, Lu and Zhao, Zihan and Lin, Xuanze and Zhu, Zichen and Chen, Bo and Chen, Xin and Yu, Kai},
|
| 90 |
+
year={2025},
|
| 91 |
+
eprint={2507.17448},
|
| 92 |
+
archivePrefix={arXiv},
|
| 93 |
+
primaryClass={cs.CE},
|
| 94 |
+
url={https://arxiv.org/abs/2507.17448},
|
| 95 |
+
}
|
| 96 |
+
```
|
| 97 |
+
|
| 98 |
+
## Disclaimer
|
| 99 |
+
Current version of RetroDFM-R may generate incorrect or misleading information. Please use it with caution and verify the results with domain experts before making any decisions based on the results.
|
| 100 |
+
|