Views
No views yet

1import torch
2from modelscope import AutoTokenizer, LlamaForCausalLM, GenerationConfig
3
4model_name_or_id = "OpenDFM/ChemDFM-v1.5-8B"
5tokenizer = AutoTokenizer.from_pretrained(model_name_or_id)
6model = LlamaForCausalLM.from_pretrained(model_name_or_id, torch_dtype=torch.float16, device_map="auto")
7
8input_text = "Can you please give detailed descriptions of the molecule below?\nCl.O=C1c2c(O)cccc2-c2nn(CCNCCO)c3ccc(NCCNCCO)c1c23"
9input_text = f"[Round 0]\nHuman: {input_text}\nAssistant:"
10
11inputs = tokenizer(input_text, return_tensors="pt").to("cuda")
12generation_config = GenerationConfig(
13 do_sample=True,
14 top_k=20,
15 top_p=0.9,
16 temperature=0.9,
17 max_new_tokens=1024,
18 repetition_penalty=1.05,
19 eos_token_id=tokenizer.eos_token_id
20)
21
22outputs = model.generate(**inputs, generation_config=generation_config)
23generated_text = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0][len(input_text):]
24print(generated_text.strip()){'current_query': current_query, 'history': [(query1, answer1), (query2, answer2), ...]}1def formatting_input(current_query, history):
2 input_text = ''
3 for idx, (query, answer) in history:
4 input_text += f"[Round {idx}]\nHuman: {query}\nAssistant: {answer}\n"
5 input_text += f"[Round {len(history)}]\nHuman: {current_query}\nAssistant:"
6 return input_textrdkit package to canonicalize the SMILES. Here is an example:1from rdkit import Chem
2def canonicalize_smiles(smiles):
3 mol = Chem.MolFromSmiles(smiles)
4 if mol is None:
5 return None
6 return Chem.MolToSmiles(mol, isomericSmiles=True, kekuleSmiles=False)1from rdkit import Chem
2def canonicalize_smiles(smiles):
3 return Chem.CanonSmiles(smiles, useChiral=True)1@article{zhao2025developing,
2 title={Developing ChemDFM as a large language foundation model for chemistry},
3 author={Zhao, Zihan and Ma, Da and Chen, Lu and Sun, Liangtai and Li, Zihao and Xia, Yi and Chen, Bo and Xu, Hongshen and Zhu, Zichen and Zhu, Su and others},
4 journal={Cell Reports Physical Science},
5 volume={6},
6 number={4},
7 year={2025},
8 publisher={Elsevier}
9}