なぜ分子の融点が◯◯℃なのかをGPT-4に考えさせる際の試行錯誤メモ

参考記事


はじめに

「トルエンの沸点= 110℃」のような化学・材料データベースが無数に存在します。ただ、基本的には構造と物性の情報のみが収録されており、その理由がわかりません。
実験結果を与えられるのみでは、人間や大規模言語モデルが困るので、その理由をGPTに考えさせて、物性+言語データベースを作ろうというアイデアが出てきました。

試行1: ChatGPTを使う

チャットのやりとりはこちら

わりとうまく行きました。
実験結果を与えて理由を考えさせる → 出てきた理由が本当に正しいか、考察させる → 結果をJSONでまとめる
というフローになっています。

依頼

Provide the quantitative reasons within 300 words so that a scientist, who does not know the melting point, can predict the value.
We must quantitatively consider how the melting point shifts.
I absolutely forbid you to make qualitative generalizations.

#Example
- Target compound: Toluene
- Basic unit, benzene has a boiling point of 80.
- Methyl group: +30 (due to larger molecular weight)
- The predicted value will be 80+30=110

# Data
{'name': '3-phthalimidopropionic acid',
 'value': 152.0,
 'smiles': 'c1ccc2c(c1)C(=O)N(C2=O)CCC(=O)O',
}
#Output: Reason

回答

To predict the melting point of 3-phthalimidopropionic acid quantitatively, we need to break down its structure and compare it with known reference compounds, considering the effects of different functional groups and structural features. The compound's SMILES notation, c1ccc2c(c1)C(=O)N(C2=O)CCC(=O)O, describes its molecular structure, which includes a phthalimide group and a propionic acid group.

    Basic Unit (Benzene Core): Benzene has a melting point of 5.5°C. The phthalimide structure in the target compound is a benzene ring with two carbonyl groups and a nitrogen atom. Each carbonyl group in aromatic compounds generally increases the melting point due to enhanced pi-pi stacking and hydrogen bonding capabilities. For instance, benzoic acid, with one carbonyl group, has a melting point of 122°C. Assuming a similar increase for each carbonyl, the addition of two carbonyl groups could raise the benzene base by approximately 234°C (117°C per carbonyl group).

    Nitrogen in Phthalimide: The nitrogen atom in the phthalimide group contributes to the rigidity of the molecule and can form hydrogen bonds, further increasing the melting point. Given the effect of a similar nitrogen substitution in other aromatic compounds, this could add approximately 20°C to the melting point.

    Propionic Acid Group: The propionic acid group (-CH2CH2COOH) contributes additional molecular weight and potential for hydrogen bonding. Propionic acid itself has a melting point of -20.8°C. However, when attached to a larger, rigid structure like phthalimide, the incremental increase is likely to be less pronounced, estimated at around 10°C.

    Overall Structural Considerations: The overall structure of 3-phthalimidopropionic acid is relatively rigid and planar, leading to good stacking interactions and higher melting point.

Based on these considerations:

    Base (Benzene Core): 5.5°C
    Increase due to Two Carbonyl Groups: +234°C
    Increase due to Nitrogen in Phthalimide: +20°C
    Increase due to Propionic Acid Group: +10°C

Predicted Melting Point = 5.5 + 234 + 20 + 10 = 269.5°C

This predicted value is significantly higher than the actual melting point (152.0°C), indicating that the quantitative effects of each functional group might be less pronounced or interact in a more complex manner than accounted for in this simplified model. Further refinement of the model would require more detailed quantitative data on the specific effects of these functional groups in similar molecular contexts.

理由が結果と整合していないので、rethinkさせます。
(rethinkと一言指示)

To predict the melting point of 3-phthalimidopropionic acid quantitatively, we need to consider the structural elements and their known impacts on melting points, without making qualitative generalizations.

    Phthalimide Core: The base unit here is the phthalimide core, derived from phthalic anhydride and an amine. Phthalic anhydride has a melting point of 131°C. The introduction of an amine group to form phthalimide typically increases melting points due to additional hydrogen bonding. Let's estimate this increase to be around 20°C based on the behavior of similar amide formations.

    Propionic Acid Side Chain: The propionic acid side chain (-CH2CH2COOH) introduces further hydrogen bonding potential. Propionic acid itself has a melting point of -20.8°C, but when attached to a larger, rigid aromatic system, its effect is not about its own melting point but how it modifies the core structure. Given the additional hydrogen bonding and increased molecular weight, let’s estimate an increase of 15°C.

    Aromaticity and Rigidity: The overall aromatic and rigid structure of the molecule would typically increase the melting point due to stronger intermolecular forces like pi-pi stacking and hydrogen bonding. This effect is difficult to quantify precisely but let's conservatively estimate an additional 10°C.

Based on these considerations:

    Base (Phthalimide Core): 131°C (from phthalic anhydride)
    Increase due to Amine in Phthalimide: +20°C
    Increase due to Propionic Acid Side Chain: +15°C
    Increase due to Aromaticity and Rigidity: +10°C

Predicted Melting Point = 131 + 20 + 15 + 10 = 176°C

This prediction is slightly higher than the actual value of 152.0°C. The discrepancy could be due to the overestimation of the impact of the amine and the propionic acid side chain, or other structural factors not accounted for in this simple model. Accurate prediction would require more detailed data on the specific effects of similar functional groups in comparable molecular contexts.

とりまとめ(JSON)

jsonで出すように指示します。

User
Prepare json data:
-Name
-SMILES
-Reason
-Value
-Prediction

綺麗な結果になりました。

{
  "Name": "3-phthalimidopropionic acid",
  "SMILES": "c1ccc2c(c1)C(=O)N(C2=O)CCC(=O)O",
  "Reason": "The phthalimide core, derived from phthalic anhydride, provides a base melting point. The introduction of an amine group for forming phthalimide typically increases melting points due to additional hydrogen bonding, estimated as a 20°C increase. The propionic acid side chain (-CH2CH2COOH) introduces further hydrogen bonding potential, estimated as a 15°C increase. The overall aromatic and rigid structure of the molecule typically increases the melting point due to stronger intermolecular forces like pi-pi stacking and hydrogen bonding, estimated as an additional 10°C increase.",
  "Value": 152.0,
  "Prediction": 176.0
}

課題

一連の作業を、最大2万件くらいの分子で行いたいです。
手作業はきついので、APIで動かしたいところです。

試行2: BabyAGI

まずは、自律エージェントであるBabyAGIに丸投げしてみることにしました。BabyAGIの使い方は以下のサイトを参照。

ChatGPT的なノリで適当な指示を出しました。


OBJECTIVE=Make quantitative logical reasons why 3-phthalimidopropionic acid(SMILES: c1ccc2c(c1)C(=O)N(C2=O)CCC(=O)O) has a melting point of 152 Celsius degree.
INITIAL_TASK=List quantitative contributions of chemical units (e.g., methyl group: +30).

結果はイマイチでした。

*****CONFIGURATION*****

Name  : BabyAGI
Mode  : alone
LLM   : gpt-3.5-turbo

*****OBJECTIVE*****

Make quantitative logical reasons why 3-phthalimidopropionic acid(SMILES: c1ccc2c(c1)C(=O)N(C2=O)CCC(=O)O) has a melting point of 152 Celsius degree.

Initial task: List quantitative contributions of chemical units (e.g., methyl group: +30).

Using results storage: Chroma (Default)

*****TASK LIST*****

 • List quantitative contributions of chemical units (e.g., methyl group: +30).

*****NEXT TASK*****

List quantitative contributions of chemical units (e.g., methyl group: +30).

*****TASK RESULT*****

Based on the existing literature and scientific articles, the following quantitative contributions of chemical units can be identified for 3-phthalimidopropionic acid:

1. Phthalimide group: The phthalimide group (-C6H4C(O)NH-) is known to have a relatively high melting point due to its rigid and planar structure. This group can contribute a significant amount to the overall melting point of the compound. 

2. Propionic acid group: The propionic acid group (-CH2CH2COOH) is a carboxylic acid group that can contribute to the melting point of the compound. Carboxylic acids generally have higher melting points compared to other organic compounds due to the presence of strong intermolecular hydrogen bonding.

3. Aromatic ring: The aromatic ring in 3-phthalimidopropionic acid (-C6H4-) can also contribute to the melting point. Aromatic compounds tend to have higher melting points compared to aliphatic compounds due to the presence of pi-pi stacking interactions and stronger intermolecular forces.

4. Amide group: The amide group (-C(O)NH-) in the phthalimide moiety can contribute to the melting point. Amides have higher melting points compared to similar compounds without the amide group due to the presence of strong intermolecular hydrogen bonding.

5. Ester group: The ester group (-COO-) in the propionic acid moiety can also contribute to the melting point. Ester compounds generally have lower melting points compared to carboxylic acids due to weaker intermolecular forces.

It is important to note that the quantitative contributions of these chemical units to the melting point of 3-phthalimidopropionic acid cannot be determined precisely without experimental data. The contributions mentioned above are based on general trends and assumptions in organic chemistry.

*****TASK CREATION AGENT PROMPT****

You are to use the result from an execution agent to create new tasks with the following objective: Make quantitative logical reasons why 3-phthalimidopropionic acid(SMILES: c1ccc2c(c1)C(=O)N(C2=O)CCC(=O)O) has a melting point of 152 Celsius degree..
The last completed task has the result: 
Based on the existing literature and scientific articles, the following quantitative contributions of chemical units can be identified for 3-phthalimidopropionic acid:

1. Phthalimide group: The phthalimide group (-C6H4C(O)NH-) is known to have a relatively high melting point due to its rigid and planar structure. This group can contribute a significant amount to the overall melting point of the compound. 

2. Propionic acid group: The propionic acid group (-CH2CH2COOH) is a carboxylic acid group that can contribute to the melting point of the compound. Carboxylic acids generally have higher melting points compared to other organic compounds due to the presence of strong intermolecular hydrogen bonding.

3. Aromatic ring: The aromatic ring in 3-phthalimidopropionic acid (-C6H4-) can also contribute to the melting point. Aromatic compounds tend to have higher melting points compared to aliphatic compounds due to the presence of pi-pi stacking interactions and stronger intermolecular forces.

4. Amide group: The amide group (-C(O)NH-) in the phthalimide moiety can contribute to the melting point. Amides have higher melting points compared to similar compounds without the amide group due to the presence of strong intermolecular hydrogen bonding.

5. Ester group: The ester group (-COO-) in the propionic acid moiety can also contribute to the melting point. Ester compounds generally have lower melting points compared to carboxylic acids due to weaker intermolecular forces.

It is important to note that the quantitative contributions of these chemical units to the melting point of 3-phthalimidopropionic acid cannot be determined precisely without experimental data. The contributions mentioned above are based on general trends and assumptions in organic chemistry.
This result was based on this task description: List quantitative contributions of chemical units (e.g., methyl group: +30)..
Based on the result, return a list of tasks to be completed in order to meet the objective. 
Return one task per line in your response. The result must be a numbered list in the format:

ひたすら長い文章が生成され、「実験で検証します」的なタスクが大量に出てきました。

課題

  • 丸投げだとタスクが発散する

    • やってほしくないタスクをやりだす

    • 無駄にトークン数が増える

エージェントの使い方を、色々と工夫する必要がありそうです。

試行3: 再帰的な呼び出しを想定したプロンプトを作る

フローは決まっているので、Agentに丸投げするよりも、再帰呼び出しを想定したプロンプトを作っちゃった方が良いという考えに至りました。

コード実装はこちら

以下、日本語に訳したものです。

システムプロンプト(固定)

融点を知らない科学者でも値を予測できるように、定量的な理由と予測を提供します。

#コマンド
・各官能基に着目して融点がどのように推移するかを定量的に検討する必要がある。
- 実際の値と予測が大きく異なる場合は、理由を再考してください。
- 予測に各官能基効果の数値が含まれていない場合は、理由を再考してください



#理由の例
・対象化合物:トルエン
- 基本単位であるベンゼンの沸点は 80 です。
- メチル基: +30 (分子量が大きいため)
- 予測: 110

太字部分が、再帰的な命令に対応します。

ユーザープロンプト(毎回変化)

はじめは、化合物名と物性値のみを与えます。二回目以降の呼び出しでは、過去にGPTが考えた「理由」と「予測値」を付記します。

def gen_prompt(chemical_record,reason="",prediction=""):
    name=chemical_record["name"]
    smiles=chemical_record["smiles"]
    value=chemical_record["mpC"]
    prompt=f"""
#Data
-Name: {name}
-SMILES: {smiles} 
-Actual value: {value}
-Reason: {reason}
-Prediction: {prediction}

#Output (JSON keys)
- Reason, Prediction
"""
    return prompt


#ask gpt
def json_generate(prompt,model="gpt-3.5-turbo-1106"):
    response = openai.chat.completions.create(
    model=model,
    messages=[
        {
            "role": "system",
            "content": system_prompt,
        },
        {
            "role": "user",
            "content": f"""{prompt}"""
        }  
    ],
    response_format={ "type": "json_object" }
    )

    return (json.loads(response.choices[0].message.content))

再帰呼び出しの箇所

#このコマンドを繰り返す
r=json_generate(
    gen_prompt(gen_record,
               reason=gen_record["Reason"],
               prediction=gen_record["Prediction"]
    ),
    model=model,
)
gen_record.update(r)
record_history.append(copy.deepcopy(gen_record))

一回目の呼び出し結果

理由: 「3-フタルイミドプロピオン酸の融点を予測するには、基本的な参照化合物と比較して、その官能基と構造がその融点に及ぼす影響を考慮しましょう。3-フタルイミドプロピオン酸の重要な官能基と構造的特徴は次のとおりです。 フタルイミド構造とプロピオン酸側鎖から構成されています。フタルイミドの基本構造は無水フタル酸で、その融点は約 131°C です。フタルイミドの硬い平面構造と、強力な分子間水素結合を形成する能力により、融点が大幅に上昇します。 これらの要因により、フタルイミド単位は +50°C の上昇に寄与する可能性があります。プロピオン酸側鎖はカルボン酸基と同様に水素結合を形成することができるため、融点がさらに上昇するはずです。通常、 ベンゼン環にカルボン酸官能基を導入すると、融点が約 +40 ℃上昇します。-COOH 基とフタルイミド窒素の間に分子内水素結合が形成されるため、相乗効果があり、結晶が安定化する可能性があります。 構造をさらに強化すると、さらに +10°C が上昇する可能性があります。 要約すると、予測は次のようになります: 131 (塩基構造) + 50 (フタルイミド構造) + 40 (プロピオン酸基) + 10 (相乗的安定化効果) = 231°C.",
「予測」: '231'

色々とそれらしい考察をしてますが、実験値が152なので、理由としては不適当です。

二回目の呼び出し結果

次に、この結果を与えつつ、再帰的に呼び出します。

ここで、先述のシステムコマンドが本領を発揮します。

- 実際の値と予測が大きく異なる場合は、理由を再考してください。
- 予測に各官能基効果の数値が含まれていない場合は、理由を再考してください

理由: 「3-フタルイミドプロピオン酸の融点を予測するには、関連するより単純な化合物から始めて、その融点に対する官能基の影響を考慮する必要があります。」 無水フタル酸のような基本構造の融点は約 131°C です。 私たちの標的化合物のフタルイミド構造は剛性と平面性を強化し、より強い分子間相互作用、特に水素結合と可能性のあるパイ-パイスタッキングに貢献します。 これにより、融点が約 +20°C 上昇する可能性があります。 プロピオン酸側鎖には、強い分子間水素結合を形成できる極性カルボン酸基があり、これにより一般に融点が上昇します。 ベンゼン化合物にカルボン酸基を追加すると、通常、融点が約 +40°C 上昇します。 ただし、同じ分子内のカルボキシル基とフタルイミド窒素間の分子内水素結合を考慮すると、融点に対する全体的な影響は、官能基がベンゼンにある場合よりもわずかに小さい可能性があります。 したがって、+40°C ではなく、+30°C の上昇に寄与する可能性があります。 これらの特徴を組み合わせた効果は、131 (基本構造) + 20 (フタルイミド強化) + 30 (カルボン酸基) = 181°C となります。
「予測」: '181'

実測の152と随分、近くなりました!

(3回目も予測は181のままでした)

コスト試算

1単語 = 1 tokenとして、gpt-4-1106-preview(input: $0.01/1k tokens, output: $0.03/3k tokens)で2回再帰したときのコストを試算しました。

Input tokens: 298
Output tokens: 205
Cost: 0.01826 USD
Cost: 2.739 JPY

2万件くらいの融点データがあるので、数万円くらいの予算でデータセットを作れそうです。

まとめ

GPT-4を再帰的に呼び出しながら、実験結果を考察させるシステムを作りました。
自律エージェントだとタスクが発散してしまい、うまく使いこなせなかったので、ややプログラミング的な発想でフレームワークを作ることで、安定したシステムを回せるようになりました。


この記事が気に入ったらサポートをしてみませんか?