File size: 3,876 Bytes
3824ea0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import re
import json
from openai import OpenAI
from tqdm import tqdm

from rdkit import Chem
from rdkit import RDLogger
RDLogger.DisableLog('rdApp.*')

def canonicalize_smiles(smiles):
    mol = Chem.MolFromSmiles(smiles)
    return Chem.MolToSmiles(mol, canonical=True)


class Agent():
    def __init__(self, url="http://localhost:8000/v1"):

        self.client = OpenAI(
            api_key="0",
            base_url=url
        )
        print ("[Client Init] done")

    def generate(self, query):

        messages = [
            {
                "role": "user", 
                "content": query
            }
        ]
        result = self.client.chat.completions.create(
            messages=messages,
            model="general")
        
        return result.choices[0].message.content

class AgentMolLM(Agent):
    
    def __init__(self, url="http://localhost:8000/v1"):

        super(AgentMolLM, self).__init__(url=url)

    def generate(self, query, temperature=0.01, top_p=0.01, n_resp=1, add_no_think=False):

        query = query+" /no_think" if add_no_think else query
        messages = [
            {
                "role": "user",
                "content": query
            }
        ]
        result = self.client.chat.completions.create(
            messages=messages,
            model="mol_lm",
            temperature=temperature,
            top_p=top_p,
            n=n_resp)
        
        if n_resp>1:
            results = list()
            for i in range(n_resp):
                results.append(result.choices[i].message.content)
            return results
        else:
            return result.choices[0].message.content


class ExtractorSMILES():
    def __init__(self):
        self.pattern = r"<SMILES>(.*?)</SMILES>"
    def extract(self, text):
        res = list()
        for smi in re.findall(self.pattern, text, re.DOTALL):
            res.append(smi.replace(";", "."))
        return res

class ExtractorFormula():
    def __init__(self):
        self.pattern = r"<MOLFORMULA>(.*?)</MOLFORMULA>"
    def extract(self, text):
        return re.findall(self.pattern, text, re.DOTALL)

class ExtractorIUPAC():
    def __init__(self):
        self.pattern = r"<IUPAC>(.*?)</IUPAC>"
    def extract(self, text):
        return re.findall(self.pattern, text, re.DOTALL)

class ExtractorValue():
    def __init__(self):
        self.pattern = r"[+-]?\d*\.\d+"
    def extract(self, text):
        return re.findall(self.pattern, text)

class ExtractorYN():
    def __init__(self):
        pass

    def extract(self, text):
        if "yes" in text.lower():
            return ["yes"]
        elif "no" in text.lower():
            return ["no"]
        else:
            return ["N/A"]

class ExtractorText():
    def __init__(self):
        pass

    def extract(self, text):
        return [text]

class Extractor():
    def __init__(self, task_type="smiles"):
        if task_type == "smiles":
            self.extractor = ExtractorSMILES()
        elif task_type == "formula":
            self.extractor = ExtractorFormula()
        elif task_type == "iupac":
            self.extractor = ExtractorIUPAC()
        elif task_type == "value":
            self.extractor = ExtractorValue()
        elif task_type == "bool":
            self.extractor = ExtractorYN()
        else:
            self.extractor = ExtractorText()
        
    def extract(self, text):
        text = text.split("</think>\n")[-1]
        resp = self.extractor.extract(text=text)
        res = resp[0].strip() if len(resp) else ""
        return res



if __name__ == "__main__":
    agent = AgentMolLM()

    QUERY = "你是谁"
    ADD_NO_THINK = True

    # resp = agent.generate(QUERY, add_no_think=ADD_NO_THINK)
    resp = agent.generate(QUERY, add_no_think=ADD_NO_THINK, temperature=0.6, top_p=0.95, n_resp=2)

    print (resp)