Abstract
In-context learning (ICL) effectively conditions large language models (LLMs) for molecular tasks, such as property prediction and molecule captioning, by embedding carefully selected demonstration examples into the input prompt. This approach eliminates the computational overhead of extensive pre-training and fine-tuning. However, current prompt retrieval methods for molecular tasks rely on molecule feature similarity, such as Morgan fingerprints, which do not adequately capture the global molecular and atom-binding relationships. As a result, these methods fail to represent the full complexity of molecular structures during inference. Moreover, medium-sized LLMs, which offer simpler deployment requirements in specialized systems, have remained largely unexplored in the molecular ICL literature. To address these gaps, we propose a self-supervised learning technique, GAMIC (Graph-Aligned Molecular In-Context learning), which aligns global molecular structures, represented by graph neural networks (GNNs), with textual captions (descriptions) while leveraging local feature similarity through Morgan fingerprints. In addition, we introduce a Maximum Marginal Relevance (MMR) based diversity heuristic during retrieval to optimize input prompt demonstration samples. Our experimental findings using diverse benchmark datasets show GAMIC outperforms simple Morgan-based ICL retrieval methods across all tasks by up to 45%.
Introduction
Large Language Models (LLMs) have demonstrated significant potential in chemical analysis, yet their performance often hinges on effective In-Context Learning (ICL). By providing a few relevant examples within a prompt, researchers can guide models toward accurate property predictions or descriptions. However, the primary bottleneck remains the retrieval process: selecting the most informative examples from vast chemical databases.
Traditional methods rely on Morgan fingerprints, which represent molecules as bitstrings of local sub-structures. While computationally efficient, these fingerprints often overlook global molecular architecture and complex spatial relationships. This "blindness" can lead to the retrieval of examples that appear structurally similar but exhibit vastly different chemical behaviors. Furthermore, while massive models like GPT-4 are powerful, there is a critical need to optimize medium-sized open-source models (under 10B parameters) for specialized, secure, or low-latency chemical applications. GAMIC addresses these challenges by aligning graph-based structural data with scientific text.
Methodology: Graph-Aligned Molecular In-Context Learning
GAMIC introduces a framework that transcends simple fingerprint matching through three core innovations:
- Graph-Aligned Projection: GAMIC utilizes a Graph Neural Network (GNN) to encode a molecule's full connectivity. To ensure this representation is interpretable by an LLM, the GNN's output is aligned with SciBERT embeddings—a text model trained on scientific literature—using contrastive learning. This process ensures that a molecular graph and its corresponding scientific description reside in the same neighborhood within a latent space.
- Morgan-Augmented Training: To bridge the "modality gap" between graphs and text, Morgan fingerprints are integrated during the training phase. This helps the model identify structurally related molecules, providing a more robust signal for the alignment process.
- Diversity-Aware Retrieval (MMR): Rather than simply selecting the "Top-K" most similar items, GAMIC employs a Maximum Marginal Relevance (MMR) heuristic. This strategy balances relevance (similarity to the target) with diversity (dissimilarity among selected examples), preventing redundant prompts and providing the LLM with a broader contextual map of the chemical space.
Experimental Setup and Datasets
The researchers evaluated GAMIC across nine benchmark datasets covering three distinct tasks:
- Molecule Captioning: ChEBI-20 and PubChem (translating structures into natural language descriptions).
- Property Prediction: BBBP, BACE, HIV, Tox21, and ClinTox (predicting biological activity or toxicity).
- Yield Prediction: Suzuki-Miyaura and Buchwald-Hartwig (predicting the efficiency of chemical reactions).
The study specifically focused on 7B and 8B parameter models, including Mistral, OpenChat, and Zephyr, to demonstrate the efficacy of high-quality context on smaller architectures.
Results and Key Insights
GAMIC consistently outperformed standard Morgan-based retrieval (Scaffold) across all tasks, achieving improvements of up to 45% in specific metrics. Notably, the method achieved State-of-the-Art (SOTA) results on 26 out of 27 tested scenarios across various datasets and LLM backbones.
A key insight from the study is the importance of global context. While local fingerprints are useful for quick lookups, LLMs require an understanding of global architecture to reason effectively about molecular behavior. Furthermore, the MMR strategy proved that showing an LLM diverse examples is more effective than showing nearly identical ones, as it allows the model to better triangulate the correct answer within the chemical space.
Conclusion and Future Work
GAMIC demonstrates that graph-aligned context can empower medium-sized models to rival the performance of much larger architectures without expensive fine-tuning. However, limitations remain. The current system uses SciBERT as an encoder-only model; future iterations may explore using the internal representations of the LLMs themselves for tighter alignment. Additionally, while ICL reduces fine-tuning costs, the initial alignment training still requires significant computational resources. Future research will also look toward incorporating 3D spatial conformations to improve predictions for complex biological tasks like protein-ligand binding.
Summary updated