Abstract
In-context learning (ICL) has emerged as a powerful paradigm for Large Language Models (LLMs), yet its application to the molecular domain remains challenging due to the structural complexity of chemical data. Traditional retrieval methods often rely on local fragments (Morgan fingerprints), overlooking global molecular topology. We present GAMIC (Graph Aligned Molecular In-Context learning), a framework that aligns graph-based molecular representations with textual descriptions through contrastive learning. By employing a diversity-focused retrieval strategy using Maximum Marginal Relevance (MMR), GAMIC significantly enhances the performance of medium-sized LLMs on tasks such as property prediction and molecule captioning, outperforming standard methods by up to 45%.
Introduction
Large Language Models (LLMs) have demonstrated remarkable versatility through in-context learning (ICL), where a model performs a task by observing a few demonstrations within the prompt. However, bridging the gap between natural language and specialized scientific modalities like molecular graphs is non-trivial. Most existing molecular ICL approaches utilize Morgan fingerprints for example retrieval. While effective at capturing local chemical environments, these fingerprints often fail to represent the global structural context necessary for complex reasoning. Furthermore, while frontier models like GPT-4 show promise, there is a pressing need to empower smaller, open-source models (under 10B parameters) for specialized chemical applications.
Methodology
The GAMIC framework introduces a two-stage approach to improve molecular ICL:
- Graph-Language Alignment: To reconcile the structural nature of molecules with the textual nature of LLMs, GAMIC utilizes a Graph Attention Network (GAT) to encode molecular graphs. This encoder is trained via contrastive learning to align graph embeddings with textual captions processed by SciBERT. A key innovation is the "Morgan Sampler," which identifies chemically similar molecules to serve as positive pairs, grounding the latent space in established chemical informatics.
- Diverse Retrieval via MMR: Standard retrieval often selects the most similar examples, which can lead to redundant information in the prompt. GAMIC employs Maximum Marginal Relevance (MMR) to select a set of demonstrations that are both relevant to the query and diverse from each other. This provides the LLM with a broader chemical context, facilitating better generalization.
Experimental Evaluation
The framework was rigorously tested across nine benchmark datasets, including ChEBI-20 (captioning), Suzuki-Miyaura and Buchwald-Hartwig (yield prediction), and various property prediction tasks (BBBP, BACE, HIV, Tox21, and ClinTox). Evaluations were conducted using three prominent 7B-8B parameter models: Mistral-7B, OpenChat-8B, and Zephyr-7B.
Results and Discussion
GAMIC consistently outperformed baseline retrieval methods (Random, Scaffold, and GAE) across all evaluated tasks. In property prediction, improvements in F1-score reached up to 45%. In molecule captioning on the ChEBI-20 dataset, GAMIC achieved a BLEU-4 score of 0.439 with Mistral-7B, a substantial increase over the 0.281 achieved by scaffold-based retrieval.
The results indicate that the "modality bridge" created by graph-language alignment allows smaller models to leverage structural information more effectively. The MMR strategy further underscores the importance of diversity in demonstrations, preventing the model from being biased by a narrow set of similar examples.
Conclusion and Future Work
GAMIC demonstrates that aligning molecular graph structures with language and prioritizing diversity in example retrieval can significantly boost the chemical reasoning capabilities of medium-sized LLMs. While current work focuses on 2D connectivity, the authors suggest that incorporating 3D conformational information could further enhance performance. Additionally, exploring how these graph-aligned retrievers scale with even larger models or Mixture-of-Experts (MoE) architectures remains a promising avenue for future research. This approach paves the way for more accessible and high-performing AI tools in the chemical sciences.
Summary updated