Skip to main content
Nature Portfolio logoLink to Nature Portfolio
. 2026 Jun 11;2(1):69. doi: 10.1038/s44387-026-00123-0

Data-efficient biomedical in-context learning: a diversity-enhanced submodular perspective

Jun Wang 1, Zaifu Zhan 1, Qixin Zhang 2, Mingquan Lin 1, Meijia Song 3, Rui Zhang 1,✉
PMCID: PMC13612249  PMID: 42798420

Abstract

Recent progress in large language models (LLMs) has leveraged their in-context learning (ICL) abilities to enable quick adaptation to unseen biomedical NLP tasks. By incorporating only a few input-output examples into prompts, LLMs can rapidly perform these new tasks. While the impact of these demonstrations on LLM performance has been extensively studied, most existing approaches prioritize representativeness over diversity when selecting examples from large corpora. To address this gap, we propose Dual-Div, a diversity-enhanced data-efficient framework for demonstration selection in biomedical ICL. Dual-Div employs a two-stage retrieval and ranking process: First, it identifies a limited set of candidate examples from a corpus by optimizing both representativeness and diversity (with optional annotation for unlabeled data). Second, it ranks these candidates against test queries to select the most relevant and non-redundant demonstrations. Evaluated on three biomedical NLP tasks (named entity recognition (NER), relation extraction (RE), and text classification (TC)) using LLaMA 3.1 and Qwen 2.5 for inference, along with three retrievers (BGE-Large, BMRetriever, MedCPT), Dual-Div consistently outperforms baselines-achieving up to 5% higher macro-F1 scores—while demonstrating robustness to prompt permutations and class imbalance. Our findings establish that diversity in initial retrieval is more critical than ranking-stage optimization, and limiting demonstrations to 3-5 examples maximizes performance efficiency.

Subject terms: Computational biology and bioinformatics, Mathematics and computing

Introduction

Recent years have witnessed the rapid advancement of large language models (LLMs), exemplified by prominent instances like ChatGPT1, Llama2, and Qwen3 series. These models have significantly enhanced few-shot capabilities4 across numerous natural language processing (NLP) tasks. Within the biomedical domain, however, a critical challenge persists: the scarcity of high-quality training data. This scarcity arises from two primary factors. Firstly, stringent privacy regulations, such as HIPAA, and patient consent requirements strictly limit access to sensitive patient information. Secondly, rare diseases often suffer from a paucity of structured clinical records and well-defined features5, demanding robust and generalizable algorithmic solutions6. Consequently, the few-shot learning paradigm, particularly its ability to perform tasks without task-specific training data, becomes critical7.

In-Context Learning (ICL)8, a prominent few-shot technique, offers significant promise for biomedical NLP. ICL leverages task-specific prompts containing a few annotated examples, enabling pre-trained LLMs to perform unseen tasks without parameter updates. This approach typically involves retrieving a small set of relevant examples from a large unlabeled corpus and annotating them according to task requirements. ICL confers several key advantages for biomedical NLP. Firstly, it simplifies the deployment of pretrained LLMs by replacing computationally intensive fine-tuning with prompt design. More importantly, since a task-specific fine-tuning dataset is no longer required, ICL also reduces the need for labeled data for downstream tasks. By avoiding parameter updates, ICL can potentially improve model stability across diverse tasks.

Crucially, the performance of ICL is highly dependent on the quality of the selected demonstrations. Effective demonstrations provide essential context, supplementing label information and relationships beyond what was learned during pre-training9. This is especially vital in biomedicine, where texts are dense with specialized terminology (e.g., genes, cells, pathways). Since domain-specific knowledge cannot be easily integrated post-hoc into general LLMs, carefully curated demonstrations become the primary mechanism for activating or supplementing the relevant internal knowledge encoded during pre-training.

A significant challenge for existing ICL frameworks lies in selecting optimal demonstrations. Traditional methods often rely on a single metric, typically prioritizing examples most representative of or semantically similar to the test query. More recent approaches10 have adopted active learning principles to select groups that maximally cover the semantic space of the corpus. However, a key limitation persists: the neglect of diversity among the selected examples. Providing diverse demonstrations enhances model robustness by exposing it to varied scenarios. In biomedicine, this diversity is critical not only for performance but also for improving fairness in disease representation and supporting more comprehensive clinical decision-making.

To address this gap, we propose Dual-Div, a novel diversity-enhanced ICL framework for biomedical NLP that explicitly optimizes demonstration selection across multiple dimensions – representativeness and diversity. Dual-Div operates in two stages (Fig. 1), i.e., demonstration retrieval and ranking. In the first stage, we recall a limited set of candidate examples from a large (often unlabeled) corpus, followed by annotation if necessary. In the second stage, these candidates are ranked in conjunction with test queries, selecting the top demonstrations for prompt construction. Dual-Div provides a systematic and effective solution for balancing broad semantic coverage with intrinsic diversity within the selected demonstrations. Our comprehensive evaluations demonstrate that this approach yields significant improvements in ICL performance over prior methods. Our key contributions are summarized as follows.

  • To the best of our knowledge, this is the first study to systematically integrate diversity metrics into the demonstration selection process for in-context learning.

  • We propose a novel two-stage submodular optimization framework that jointly maximizes semantic coverage and diversity of selected demonstrations.

  • We conduct extensive experiments, evaluating the framework across 2 LLMs and 3 retrievers on 3 datasets, demonstrating state-of-the-art results and providing comprehensive insights.

Fig. 1. The workflow of Dual-Div, a two-stage in-context learning framework.

Fig. 1

Source: Created by the authors using draw.io (https://www.draw.io/).

Results

Experimental Setup

We evaluate the ICL performance on three core biomedical NLP tasks, including named entity recognition (NER), relation extraction (RE), and text classification (TC). These tasks can comprehensively demonstrate the ability of LLMs in biomedical natural language understanding and clinical decision support. For each task, we select one typical dataset – ChemProt for NER, DDI for RE, and HealthAdvice for TC.

  • ChemProt11 This dataset consists of 1,820 PubMed abstracts with chemical-protein interactions annotated by domain experts. Therefore, it can also be used to evaluate the systems that are able to detect the biomedical terminology related to chemical compounds/drugs and genes/proteins.

  • DDI12. This dataset is identified as a typical biomedical relation extraction benchmark to evaluate the ability of extracting drug-drug interactions. The potential types includes advice, effect, mechanism, and int. All drug entities and interactions in sentences are annotated from biomedical literature and drug product information sources.

  • HealthAdvice13. This dataset is a diverse collection of health-related advice, annotated for text classification tasks related to health information and advisory content. Structured to facilitate automatic classification of health advice into relevant categories, it can support applications such as misinformation detection, personalized health recommendations, and automated triaging of medical inquiries.

We note that the label distributions in the selected datasets are highly imbalanced. For instance, in the DDI dataset, only 979 out of 5,761 test queries contain one of the four target relations (effect, advice, mechanism, or int), whereas this ratio is 23.6% for the HealthAdvice dataset. Given this imbalance, we employ macro-F1 score – in addition to accuracy – to better reflect in-context learning performance across tasks. Unlike micro-F1, macro-F1 equally weights all classes, making it more rational for biomedical applications where rare classes are often as critical as frequent ones14.

For baseline comparisons, we employ the heuristic Random-Similar algorithm in addition to the most recent Div-S310 method. Similar to our Dual-Div approach, both of these methods follow a two-stage procedure. In the first stage, Random refers to randomly selecting examples from the entire set V, whereas Div defines f(S) solely as C(S), omitting the diversity term D(S). In the second stage, Similar chooses the most similar examples conditioned with test set Q (with the highest average cosine similarity scores), while the S3 simplifies the utility function by reducing f(T ∣ Q) to C(T∣Q). To clearly evaluate the impact of the diversity metrics D(S) and D(T∣Q), we also introduce two variants of Div-S3: Div*-S3 and Div-S3*. Div*-S3 adopts the same objective as our Dual-Div in the first stage while keeping the second stage unchanged, whereas Div-S3* applies the original first stage of Div-S3 but uses our proposed second-stage objective.

The involved language models cover two categories. The smaller retriever models are utilized to quickly obtain the semantic representations of biomedical resources. With these semantic vectors, we can determine the demonstration examples in natural language and derive the final prompts for LLMs. In contrast, the LLMs, with many more parameters, are more powerful in solving the concrete downstream tasks.

For retriever models, we select three different ones, including BMRetriever15, MedCPT16, and BGE-Large17. Among them, BMRetriever and MedCPT are trained on biomedical resources, whereas BGE-Large is a family of embedding models trained on general data. For the inference LLMs, we select the representative Qwen2.5-7B3 and Llmama 3.1-8B2 to balance the performance and efficiency during evaluation.

For the remaining implementation details, we supplement them in Supplementary Section 1. In particular, we list the prompt template we utilize in Supplementary Section 2.

ICL Performance

Table 1 presents the in-context learning performance (accuracy and macro-F1) across different tasks and methods. Utilizing the BGE-large retriever model, Dual-Div achieves the highest macro-F1 score regardless of whether Qwen2.5 or Llama serves as the inference LLM. This result indicates that the demonstration examples selected by Dual-Div effectively enhance the inference capability of LLMs on biomedical resources. Notably, Dual-Div also maintains a strong balance between accuracy and macro-F1 performance. This balance likely stems from our method’s trade-off between coverage and diversity when selecting demonstrations. Given the significant class imbalance inherent in biomedical queries (where instances with meaningful relations are vastly outnumbered by non-biomedical ones), we argue that macro-F1 here serves as a more reliable and persuasive evaluation metric than accuracy. Results for the other two retriever models are provided in Supplementary Table 2 and Supplementary Table 3; overall, these findings align with our observations using the BGE-Large retriever. We additionally compare the three retriever models and observe that no single model consistently outperforms the others across all datasets. For instance, BMRetriever achieves the best results on ChemProt, MedCPT performs strongest on DDI, and BGE-Large excels on HealthAdvice—highlighting dataset-dependent performance variations.

Table 1.

ICL Performance on different NLP datasets using Llama 3.1 (8B) and Qwen 2.5 (7B) as inference LLMs and BGE-Large as the retriever (Diff rows report Dual-Div − Div-S3 as mean difference with 95% CI and paired t-test p-value from 10-fold cross-validation)

LLM Method ChemProt (NER) DDI (RE) HealthAdvice (TC)
acc. macro-F1 acc. macro-F1 acc. macro-F1
Qwen 2.5 Random-Simliar 0.6488 0.6764 0.7837 0.2515 0.7592 0.3306
Div-S3 0.7585 0.7067 0.7004 0.2843 0.7661 0.3330
Div*-S3 0.7691 0.7029 0.8047 0.2794 0.7615 0.3604
Div-S3* 0.7212 0.7004 0.7086 0.2849 0.7598 0.3623
Dual-Div 0.7598 0.7117 0.7477 0.2949 0.7642 0.3686
Diff of acc. [0.0015, 0.0139]** [0.0309, 0.0473]*** [− 0.0125, 0.0018]
Diff of macro-F1 [0.0006, 0.0118]* [− 0.0007, 0.0225] [0.0101, 0.0449]**
Llama 3.1 Random-Simliar 0.6046 0.5819 0.6888 0.2646 0.6832 0.2780
Div-S3 0.6921 0.6026 0.7301 0.2653 0.7160 0.2892
Div*-S3 0.6912 0.6345 0.7351 0.3086 0.7586 0.2886
Div-S3* 0.6558 0.6183 0.7256 0.2664 0.6872 0.2875
Dual-Div 0.6910 0.6387 0.7283 0.3129 0.7241 0.2948
Diff of acc. [− 0.0133, 0.0147] [−0.0019, 0.0208]* [0.0075, 0.0393]***
Diff of macro-F1 [0.0211, 0.0432]*** [0.0272, 0.0640]*** [− 0.0015, 0.0108]

Note: *p < 0.05, **p < 0.01, ***p < 0.001.

To further validate the statistical significance of the improvements achieved by our Dual–Div method over Div–S3, we performed a 10–fold paired t–test based on a 10–fold partitioning of the entire dataset. Specifically, for given method pair, we partitioned the full dataset into 10 folds using a fixed random seed, and in each fold we evaluated the algorithms on the held–out fold. We then computed per–fold performance metrics for both methods on the same test split, derived fold–wise performance differences, and conducted a paired t–test, reporting the mean difference, the standard deviation of differences, the p–value, and the 95% confidence interval of the mean difference (based on the t–distribution). Among all 36 comparisons across different settings, Dual-Div outperforms Div-S3 in 31 cases, with 23 of these advantages being statistically significant. Focusing on the more critical macro-F1 metric, our method shows superior performance in all 18 results, 11 of which are statistically significant.

For the comparison of the two LLMs, Qwen2.5 significantly outperforms Llama 3.1 on NER and TC tasks, regardless of the retrievers used. This suggests Qwen2.5 may have a stronger potential for transferring biomedical knowledge from external inputs. In practice, we also observed more formatting errors in Llama 3.1’s outputs than in Qwen2.5’s. For example, Llama 3.1 responses for relation types sometimes include (partial) parentheses. This issue diminishes as the number of fine-tuning steps over the corpus V increases.

Another critical finding emerges from our detailed comparison of introducing the diversity term D(S) at different stages of the pipeline, as reflected by the performance of Div*-S3 and Div-S3*. We find that incorporating diversity regularization in Stage 1 consistently leads to substantially improved ICL performance, often yielding the highest accuracy across settings. This indicates that retrieving a highly diverse candidate set from the original corpus V is more influential than applying a sophisticated ranking strategy in Stage 2. The effect is particularly pronounced when the reduction in candidate size during Stage 1 (N − k1) is larger than that in Stage 2 (k1 − k), highlighting the decisive role of early-stage diversity in shaping final outcomes.

More broadly, the superior accuracy achieved by our Div variants (even over Dual-Div) across most experimental configurations robustly validates our central hypothesis: explicitly incorporating diversity considerations at different stages of ICL yields tangible performance benefits. However, a closer examination reveals an important trade-off. Despite their strong accuracy, these variants often exhibit lower macro-F1 scores, indicating a tendency to favor majority-class predictions while being less effective on queries associated with infrequent, minority labels.

This observation highlights both the strengths and limitations of the Div variants in comparison to our proposed Dual-Div: although they improve overall correctness by emphasizing representative and frequent patterns, their performance can diminish on rarer yet semantically important cases. This is precisely why we retain the diversity regularization in Stage 2. The finding further justifies our use of macro-F1 as the primary evaluation metric, since it more accurately reflects performance on minority classes—which often carry greater practical significance.

Visualization of diversity

To better illustrate the effect of the diversity-enhanced technique, we also visualize the semantic representations of the 100 candidate queries from the first retrieval stage. Specifically, we use UMAP18 to project the high-dimensional semantic embedding vectors from the retriever models onto a 2D plane. We then apply MinMaxScaler for normalization to facilitate visualization. The results, summarized in Fig. 2, show that queries retrieved using the diversity metric exhibit greater dispersion in their vector representations compared to those retrieved without it. This difference is particularly pronounced on the DDI dataset for the relation extraction task. This outcome aligns with our intuition of D(S) regarding the determinant of the Gram matrix of these vectors. By maximizing the “volume” of the space spanned by the semantic vectors, we encourage the model to avoid selecting queries clustered in a small neighborhood, which are likely to represent the same biomedical terminology or phenomenon.

Fig. 2. Visualization of retrieved queries in S* after Stage 1.

Fig. 2

Semantic embedding vectors from various retriever models on a ChemProt w/o D(S), b DDI w/o D(S), c HealthAdvice w/o D(S), d ChemProt w/ D(S), e DDI w/ D(S), and f HealthAdvice w/ D(S) during optimization are reduced and projected by UMAP.

In the comparison of the three retriever models, we observe that without diversity-enhancing techniques, the embeddings generated by BGE-Large and MedCPT exhibit a more scattered and uniform distribution compared to those from BMRetriever. This pattern may indicate a stronger capability of these two models in shaping the embedding space and facilitating instruction-agnostic retrieval. However, when diversity enhancement is applied, the distributions of demonstrations retrieved by all three models become considerably more competitive.

Sensitivity Analysis

In order to investigate the sensitivity of our Dual-Div algorithm, we mainly study the effect of internal ordering of final demonstration sets, as well as the value of the budget constraint k, which limits the number of examples in the prompts.

Previous studies19–21 have shown that the ICL performance of LLMs is highly sensitive to the ordering of demonstrations within input prompts. To investigate this effect, we evaluate the performance distributions across all possible permutations of the examples in the final set T (default cardinality 3). Fig. 3 presents the results for our Dual-Div method and all baselines, using BGE-Large as the retriever. Results for other retriever models are provided in Supplementary Section 4.2.

Fig. 3. Sensitivity analysis regarding the permutation ordering in the LLM’s prompts of the final demonstration set.

Fig. 3

Given BGE-Large as the retriever model, for each set of 3 demonstrations from different methods, we plot the violin graphics of macro-F1 scores of all 6 possible permutations on the a ChemProt, b DDI, and c HealthAdvice datasets.

Overall, Dual-Div exhibits the strongest robustness against prompt permutations. On ChemProt and HealthAdvice, Dual-Div not only achieves the highest median macro-F1 scores but also shows the tightest performance distribution (narrowest violin shape), indicating minimal sensitivity to permutation order. Conversely, Random-Similar displays the broadest performance spread across all datasets, likely due to the inherent randomness introduced in its first stage.

On the DDI dataset, Dual-Div shows a much wider distribution than Div-S3 and Div-S3*, but achieves superior average performance. This suggests that prioritizing high diversity in candidate queries during the first stage may reduce stability when using BGE-Large. However, we posit that this effect is both task-specific and retriever-dependent. Supplemental experiments confirm that incorporating diversity significantly improves stability intervals: for ChemProt using BMRetriever (Supplementary Fig. 1a) and for DDI using MedCPT (Supplementary Fig. 2b).

Figure 4 and Figure 5 illustrate the trend of macro-F1 scores as the budget limit k for the second stage varies. Overall, Qwen 2.5 consistently outperforms Llama 3.1 across all datasets and retriever models, indicating superior inference capabilities from in-context information for biomedical tasks. This finding aligns with our previous observations. Regarding specific tasks, the performance improvement is most pronounced on ChemProt (approximately 9.1% at the optimal k) and HealthAdvice (approximately 47.5%). Concerning the choice of k, increasing its value generally enhances performance across all LLMs and tasks. The optimal macro-F1 performance is typically achieved at k = 3 or 5. However, performance gains diminish beyond k = 3, becoming marginal. Notably, further increasing k from 5 to 10 often results in negligible or even negative gains across nearly all experiments. This pattern suggests that the quality of demonstrations is more critical than quantity. An excessive number of examples may potentially impair LLM performance by introducing redundant or irrelevant knowledge.

Fig. 4. Sensitivity analysis of the budget constraint in the second stage using Qwen 2.5 as the inference LLM.

Fig. 4

(a) Macro-F1 performance on the ChemProt dataset under different values of K. (b) Macro-F1 performance on the DDI dataset under different values of K. (c) Macro-F1 performance on the HealthAdvice dataset under different values of K. Symbols: red circles = BGE-Large; blue squares = BMRetriever; green triangles = MedCPT.

Fig. 5. Sensitivity analysis of the budget constraint in the second stage using Llama 3.1 as the inference LLM.

Fig. 5

(a) Macro-F1 performance on the ChemProt dataset under different values of K. (b) Macro-F1 performance on the DDI dataset under different values of K. (c) Macro-F1 performance on the HealthAdvice dataset under different values of K. Symbols: red circles = BGE-Large; blue squares = BMRetriever; green triangles = MedCPT.

Furthermore, regarding the fluctuation of macro-F1 scores with varying k across different retriever models, BGE-Large generally exhibits greater robustness compared to BMRetriever and MedCPT. This robustness likely stems from BGE-Large’s training on general-domain data, rather than being specialized solely for biomedical contexts.

Case Study

We further provide case studies to demonstrate the enhanced ability in extracting diverse examples from training corpora. The comparison in Figure 6 showcases that the previous Div-S3 method is more likely to select top examples with the same NER tags, while our proposed Dual-Div can greatly alleviate this issue by reducing the similarity within the ranked examples. See more comparisons regarding the other two tasks in Supplementary Fig. 3 and Supplementary Fig. 4.

Fig. 6.

Fig. 6

Case study of ranked demonstrations on the ChemProt dataset for NER task from (diversity-enhanced) Dual-Div and Div-S3, with BGE-Large as the retriever and Qwen 2.5 as the inference LLM.

Discussion

In this work, we introduce Dual-Div, a two-stage framework that optimizes demonstration selection for biomedical ICL by jointly maximizing semantic coverage and diversity through submodular optimization. Leveraging a determinantal point process (DPP) for diversity and a facility location function for representativeness, Dual-Div first retrieves a diverse candidate set from large corpora, then ranks examples conditioned on test queries to minimize redundancy. Evaluated across three biomedical NLP tasks (NER, RE, TC) using multiple LLMs (Llama 3.1, Qwen 2.5) and retrievers (BGE-Large, BMRetriever, MedCPT), Dual-Div consistently outperforms baselines—achieving higher macro-F1 scores (up to +5%) — while proving robust to prompt permutations and class imbalance. Key insights reveal that diversity in initial retrieval (Stage 1) is more critical than ranking (Stage 2), and limiting demonstrations to 3-5 examples optimizes performance, establishing Dual-Div as a data-efficient solution for biomedical ICL.

For future research, we plan to explore the dynamic adjustment of the trade-off parameter λ in Eq. (10). Another important direction is improving scalability to extremely large corpora, which could be addressed by developing distributed variants of the lazy greedy algorithm. Furthermore, as indicated in 22,23, a more principled approach to balancing similarity metrics and word frequency in queries involves using discounted cosine similarity, where the discounting parameters can be optimized via Monte Carlo Bayesian optimization. Applying this framework may lead to further performance improvements.

Methods

As model and data scales increase, ICL emerges as a distinctive capability of LLMs4,8. This paradigm enables LLMs to perform unseen biomedical tasks by learning directly from demonstration examples provided within their input context. Mirroring human analogical reasoning, ICL offers an interpretable interaction mechanism: it explicitly activates domain-specific knowledge encoded within LLM parameters through natural language demonstrations, bypassing traditional parameter updates.

Unlike retrieval-augmented generation (RAG)24, which dynamically retrieves external, up-to-date knowledge relevant to a query, ICL primarily queries the LLM’s internal knowledge. Its efficacy hinges on the model’s ability to infer task requirements solely from the provided context demonstrations. Consequently, ICL performance is highly sensitive to the quality, relevance, and composition of these selected demonstrations.

Crucially, while ICL was originally conceived as a training-free method to reduce computational cost, many current approaches incur significant auxiliary costs during demonstration selection. For instance, some methods require data preparation and supervised training to learn specialized retrievers25,26, while others leverage the LLM’s inference capabilities during selection (e.g., using confidence scores)27, adding latency and compute overhead. This paper specifically focuses on learning-free, end-to-end ICL frameworks. We prioritize methods where the LLM itself is not utilized within the demonstration selection algorithm; its role is confined to the final inference step after demonstrations are fully determined.

Retrieving effective demonstrations from large corpora (labeled or unlabeled) often starts with simple heuristics like cosine similarity, which remains surprisingly effective and widely used28–30. Moving beyond basic similarity, research has explored optimization objectives like the facility location function31, which emphasizes the coverage or representativeness of the selected set. While Div-S310 offers a theoretically grounded framework using this function, it primarily addresses coverage and neglects diversity. The more recent diversity-guided search32 addresses this gap by incorporating heuristic beam search to promote diverse examples. Finally, it is essential to note that once demonstrations are selected, factors like prompt formatting and the permutation order of examples within the context significantly influence the accuracy of the LLM’s response19–21.

Mathematical Formulation

ICL is a kind of few-shot learning method enabling LLMs to adapt to new tasks with only a small number of task-specific demonstration examples4. Let V denote the corpus, with or without task-specific labels, composed of N = ∣V∣ datapoints (xi,yi,ei)i=1N, where xi is the input sentence, yi is the label, and ei is the representation vector from a retriever model. Here, we denote this retriever model by g( ⋅ ), which means ei = g(xi).

Given a test query set Q that includes samples (xtest, ytest), the target is to select a subset T* ⊆ V such that the likelihood of the target answer ytest from the scoring distribution outputted by LLMs, denoted by Π(xtest,ytest)∈QP(ytest∣xtest), is maximized. During this process, a major destination is to limit the cardinality of T*, i.e., ensuring that ∣T*∣≤k with k < < N. Equivalently, this constraint can also be considered as the maximal sequence length of the prompt instruction33.

In general, this process can be further split into two procedures, as illustrated in Figure 1. For the first one, a limited number (namely k1, k < k1 < < N) of candidate examples, denoted by S*, are retrieved from the corpus V, in terms of the coverage and diversity. Intuitively, what we expect is to select a subset S that contains the most relevant and non-redundant examples from the corpus set V. As a result, the two metrics for coverage and diversity are critical. Let C(S) and D(S) be the corresponding notations. The final utility function that is composed of these two perspectives can be written as

f(S)=C(S)+λD(S), 1

where λ is a trade-off parameter. With f(S) as our optimization objective, we can conclude the first procedure as

S*=argmaxS⊆V,∣S∣≤k1f(S). 2

If the samples in S* are unlabeled, an extra annotation step is required. However, this approach has already reduced costs by avoiding annotation of the entire corpus V.

In the second stage, given the test query set Q, the informative examples in the subset S* are ranked using the same metrics — now applied conditionally as f(T ∣ Q) with T ⊆ S*. The optimization problem associated with this step can be written as

T*=argmaxT⊆S*,∣T∣≤kf(T∣Q). 3

Finally, the selected demonstrations in T* and the test query xtest in Q will be used to construct the prompt for LLM inference. The evaluation is based on the likelihood

∏(xtest,ytest)∈QP(ytest∣xtest)=∏(xtest,ytest)∈Qf(ytest∣T(T*,xtest)), 4

where T denotes the instruction prompt template regarding the examples, test query, and task descriptions.

Again, our proposed framework aims to address two important issues associated with efficient in-context learning:

  1. Given a large corpus V, how can we identify a subset with cardinality constraint to keep the most information while ensuring the internal diversity at the same time?

  2. Given a query Q, how can we decide the most relevant and non-redundant demonstrations from S*?

Metric Selection

Ideally, we desire to find suitable monotone submodular functions to describe diversity and coverage such that the weighted form f(S) still belongs to monotone submodular functions. With this assumption, we can obtain a theoretically guaranteed optimal solution.

To seek an elegant probabilistic model to describe the probabilities of subsets S, we leverage the algebraic properties of determinantal point processes (DPPs)34. Specifically, we select the L-ensemble representation of DPPs with a semidefinite matrix L. Given a subset S ⊆ V, the probability that it would be selected is

P(S)=det(LS)det(L+I) 5

where LS=Liji,j∈S denotes the restriction of L to the entries indexed by elements in S, det( ⋅ ) denotes the determinant of a matrix, and I represents the identity matrix. Note that P(S) is normalized because of the equation ∑S⊆Vdet(LS) = det(L + I) according to Theorem 2.1 in 34.

Similar to other scenarios like recommendation35, we can construct the kernel matrix in the context of ICL by the Gram matrix L = ETE, where the i-th columns of E corresponds the vector ei representing the query i in V. Intuitively, the determinant of the gram matrix L describes the “volume” of the space spanned by all query representations in E. Therefore, if the queries in S are more various, the spanned space will be greater, resulting in a greater determinant of LS. Furthermore, if we rewrite ei as the product of the L2 norm ri≥0 and a normalized vector e¯i with ∣∣e¯i∣∣2=1. The elements of kernel L can be written as

Lij=Ei,Ej=rie¯i,rje¯j=rirje¯i,e¯j=rirjwi,j. 6

Here, the term wi,j is exactly the cosine similarity of query i and j. Due to the equation L=Diagr⋅W⋅Diagr, the problem of maximizing P(S) can be thought of as

maxS⊆VlogP(S)=maxS⊆VlogdetLS=maxS⊆V∑i∈Slogri2+logdetWS. 7

This decomposition reveals that the probabilities of DPP models can be further split into a diversity-related factor logdetWS and a norm-related factor ∑i∈Slogri2. Meanwhile, prior studies36,37 have established that the L2 norm of word embeddings exhibits a linear correlation with the word’s log-frequency in the training corpus. To mitigate the influence of word frequency inherent in the queries, we therefore propose to define the diversity of a subset S independent of the L2 norms of the corresponding query embeddings {ei}i∈S, i.e.,

D(S)=logdetWS, 8

which also aligns with the definition of C(S) below.

To describe the coverage, we follow the previous studies10,31 and define the coverage of a subset S regarding the entire set V as the facility location function

C(S)= ∑i∈Vmaxj∈Swi,j= ∑i∈Vmaxj∈Se¯i,e¯j. 9

As such, the ultimate objective is a balance of diversity and coverage metric as

f(S)=C(S)+λD(S)=∑i∈Vmaxj∈Swi,j+λlogdetWS, 10

where WSij=wi,j for i, j ∈ S. We can prove that f(S) is monotone and submodular for a small trade-off constant λ (See Below).

Submodularity and Monotonicity of f(S)

Proof

First of all, C(S) and D(S) are both submodular functions. C(S) is submodular because the linear combinations of submodular functions are still submodular. D(S) is submodular because WS is positive semi-definite and the determinant of positive semi-definite matrices is log-submodular38. It can also be observed that C(S) is monotonically increasing and D(S) is monotonically decreasing. The former holds because if we add a new element x′∉S into S, we have ∀ i ∈ V, maxj∈S∪{x′}wi,j≥maxj∈Swi,j. Therefore C(S∪{x′})≥C(S). Next, we consider the latter. Again, det(WS) is non-negative since WS is positive semi-definite. If we add a new element x′∉S into S, we denote qS(e¯x′) by the orthogonal projection residual of the normalized embedding vector e¯x′ from the retriever model g(x′) in terms of the space spanned by vectors {e¯i}i∈S, namely e¯x′−projspan(S)(e¯x′). It holds that ∣∣qS(e¯x′)∣∣2≤1 because ∣∣e¯x′∣∣=1. Hence, we have det(WS∪{x′})=det(WS)⋅∣∣qS(e¯x′)∣∣2≤det(WS), leading to D(S∪{x′})≤D(S).

Intuitively, during the balance of the coverage and diversity of S, if we ensure that λ is small enough that 0<λ≤C(S∪{x′})−C(S)D(S)−D(S∪{x′}) for all subset S and x′∉S, we can obtain a monotonically increasing submodular objective function f(S)=C(S)+λD(S). In our context, if we assume the space is high-dimensional and all vectors in E are linearly independent, there exists a constant ε=1−mini,j∈Vwi,j>0 such that C(S∪{x′})−C(S)≤ε for all subset S and x′∉S. Since the upper bound of D(S)−D(S∪{x′}) is also limited, we can guarantee the existence of the constant λ. □

Diversity-Enhanced Retrieval

With the utility function f(S) defined, we first obtain the embedding vector for each sentence in V using retriever models, and compute the pairwise cosine similarity scores to instantiate the value of f(S) for every singleton set S={xi}xi∈V, as specified in Eq. (10). Subsequently, we iteratively select the sample that yields the highest marginal gain f(S ∪ {x}) − f(S), adding it to the set S until the cardinality constraint k1 is met. During this process, the lazy greedy39 strategy is applied to improve computational efficiency.

Since we only consider the special cardinality constraint so far, where the costs of all elements are identical, it has been shown in 40 that the greedy optimization provides a (1 − 1/e)-approximation guarantee. In other words, we can make sure the f(S*)≥(1 − 1/e)f(Sopt) where Sopt denotes the ideal optimal subset. For a more general case where the constraint ∣S∣≤k1 in Eq. (2) is replaced with ∑x∈Sc(s)≤k1, a modified greedy algorithm proposed in 41 alternatively offer a (1−1/e) performance guarantee factor.

Diversity-Enhanced Ranking

Once we have retrieved a subset S that covers as many semantic meanings of V as possible, we now consider the conditional ranking process given the test query set Q. At a high level, we expect to generate a subset with the largest marginal advantage given the query set Q, where the marginal advantage of a subset T ⊆ S* is denoted by

f(T∣Q):=f(T∪Q)−f(Q). 11

A higher value of f(T ∣ Q) indicates a higher utility gain if we combine the samples in T with those in Q, where the utility depends on our objective, such as the relevance or non-redundancy. Here, we use the same definition of f(T) as used in the previous stage – a balance of coverage and diversity.

In practice, we utilize a modular approximation ∑xi∈Tf{xi}∣Q as the upper bound of f(T ∣ Q), resulting the final objective of this stage to be

T*=argmaxT⊆S*,∣T∣≤k∑xi∈Tf({xi}∣Q)=argmaxT⊆S*,∣T∣≤k∑xi∈Tf({xi}∪Q)−f(Q). 12

We also conclude the detailed algorithms for the entire process, including the retrieval and ranking in Supplementary Algorithm 1.

Supplementary information

Acknowledgements

This work was supported by the National Institutes of Health’s National Center for Complementary and Integrative Health under grant number R01AT009457 and U01AT012871, National Institute on Aging under grant number R01AG078154, National Cancer Institute under grant number R01CA287413, National Institute of Diabetes and Digestive and Kidney Diseases under grant number R01DK115629, and National Institute on Minority Health and Health Disparities under grant number 1R21MD019134-01. The content is solely the responsibility of the authors and does not represent the official views of the National Institutes of Health. We also acknowledge the support from the Center for Learning Health System Sciences.

Author contributions

J.W. and Q.Z. conceptualized and designed the study. J.W. and Z.Z. curated the data and executed the experiments. J.W. drafted the initial manuscript. Z.Z., Q.Z., M.L., M.S. and R.Z. reviewed and finalized the manuscript. R.Z. supervised the whole project.

Data availability

The datasets involved in this study are publicly available from the following links: ChemProt (https://huggingface.co/datasets/bigbio/chemprot), DDI (https://github.com/isegura/DDICorpus), and HealthAdvice (https://huggingface.co/datasets/medalpaca/medical_meadow_health_advice).

Code availability

The code is publicly available on GitHub at https://github.com/huoxusg/submodular_icl.

Competing interests

Author Rui Zhang is Editorial Board Members of Journal of Biomedical Informatics and npj Health Systems. Author Rui Zhang is also Associate Editor of npj Digital Medicine. Rui Zhang was not involved in the journal’s review of, or decisions related to, this manuscript.

Footnotes

Publisher’s note Springer Nature remains neutral with regard to jurisdictional claims in published maps and institutional affiliations.

Supplementary information

The online version contains supplementary material available at https://doi.org/10.1038/s44387-026-00123-0.

References

  • 1.Achiam, J. et al. Gpt-4 technical report. arXiv preprint arXiv:2303.08774 (2023).
  • 2.Grattafiori, A. et al. The llama 3 herd of models. arXiv preprint arXiv:2407.21783 (2024).
  • 3.Yang, A. et al. Qwen2. 5 technical report. arXiv preprint arXiv:2412.15115 (2024).
  • 4.Brown, T. et al. Language models are few-shot learners. Adv. Neural Inf. Process. Syst.33, 1877–1901 (2020). [Google Scholar]
  • 5.Chen, X. et al. Rarebench: Can llms serve as rare diseases specialists? In Proceedings of the 30th ACM SIGKDD Conference on Knowledge Discovery and Data Mining, 4850–4861 (2024).
  • 6.Ullah, E., Parwani, A., Baig, M. M. & Singh, R. Challenges and barriers of using large language models (llm) such as chatgpt for diagnostic medicine with a focus on digital pathology–a recent scoping review. Diagnostic Pathol.19, 43 (2024). [DOI] [PMC free article] [PubMed] [Google Scholar]
  • 7.Ge, Y., Guo, Y., Das, S., Al-Garadi, M. A. & Sarker, A. Few-shot learning for medical text: A review of advances, trends, and opportunities. J. Biomed. Inform.144, 104458 (2023). [DOI] [PMC free article] [PubMed] [Google Scholar]
  • 8.Dong, Q. et al. A survey on in-context learning. In Al-Onaizan, Y., Bansal, M. & Chen, Y.-N. (eds.) Proceedings of the 2024 Conference on Empirical Methods in Natural Language Processing, 1107–1128 (Association for Computational Linguistics, Miami, Florida, USA, 2024). https://aclanthology.org/2024.emnlp-main.64/.
  • 9.Kossen, J., Gal, Y. & Rainforth, T. In-context learning learns label relationships but is not conventional learning. In International Conference on Learning Representations (ICLR) (2024).
  • 10.Kumari, L., Wang, S., Das, A., Zhou, T. & Bilmes, J. An end-to-end submodular framework for data-efficient in-context learning. In Duh, K., Gomez, H. & Bethard, S. (eds.) Findings of the Association for Computational Linguistics: NAACL 2024, 3293–3308 (Association for Computational Linguistics, Mexico City, Mexico, 2024). https://aclanthology.org/2024.findings-naacl.209/.
  • 11.Krallinger, M. et al. Overview of the biocreative vi chemical-protein interaction track. In Proceedings of the sixth BioCreative challenge evaluation workshop, vol. 1, 141-146 (2017).
  • 12.Segura-Bedmar, I., Martínez, P. & Herrero-Zazo, M. Semeval-2013 task 9: Extraction of drug-drug interactions from biomedical texts (ddiextraction 2013). In Second Joint Conference on Lexical and Computational Semantics (* SEM), Volume 2: Proceedings of the Seventh International Workshop on Semantic Evaluation (SemEval 2013), 341–350 (2013).
  • 13.Yu, B., Li, Y. & Wang, J. Detecting causal language use in science findings. In Proceedings of the 2019 Conference on Empirical Methods in Natural Language Processing and the 9th International Joint Conference on Natural Language Processing (EMNLP-IJCNLP), 4656–4666 (2019). https://www.aclweb.org/anthology/D19-1473.pdf.
  • 14.Hassan, E., Abd El-Hafeez, T. & Shams, M. Y. Optimizing classification of diseases through language model analysis of symptoms. Sci. Rep.14, 1507 (2024). [DOI] [PMC free article] [PubMed] [Google Scholar]
  • 15.Xu, R. et al. Bmretriever: Tuning large language models as better biomedical text retrievers. In Proceedings of the 2024 Conference on Empirical Methods in Natural Language Processing, 22234–22254 (2024). [DOI] [PMC free article] [PubMed]
  • 16.Jin, Q. et al. Medcpt: Contrastive pre-trained transformers with large-scale pubmed search logs for zero-shot biomedical information retrieval. Bioinformatics39, btad651 (2023). [DOI] [PMC free article] [PubMed] [Google Scholar]
  • 17.Xiao, S. et al. C-pack: Packed resources for general chinese embeddings. In Proceedings of the 47th international ACM SIGIR conference on research and development in information retrieval, 641–649 (2024).
  • 18.McInnes, L., Healy, J., Saul, N. & Großberger, L. Umap: uniform manifold approximation and projection. J. open source soft.3, 861 (2018).
  • 19.Zhao, Z., Wallace, E., Feng, S., Klein, D. & Singh, S. Calibrate before use: Improving few-shot performance of language models. In International conference on machine learning, 12697–12706 (PMLR, 2021).
  • 20.Liu, J. et al. What makes good in-context examples for gpt-3? In Proceedings of Deep Learning Inside Out (DeeLIO 2022): The 3rd Workshop on Knowledge Extraction and Integration for Deep Learning Architectures, 100–114 (2022).
  • 21.Lu, Y., Bartolo, M., Moore, A., Riedel, S. & Stenetorp, P. Fantastically ordered prompts and where to find them: Overcoming few-shot prompt order sensitivity. In Proceedings of the 60th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers), 8086–8098 (2022).
  • 22.Zhou, K., Ethayarajh, K., Card, D. & Jurafsky, D. Problems with cosine as a measure of embedding similarity for high frequency words. In Proceedings of the 60th Annual Meeting of the Association for Computational Linguistics (Volume 2: Short Papers), 401–423 (2022).
  • 23.Wannasuphoprasit, S., Zhou, Y. & Bollegala, D. Solving cosine similarity underestimation between high frequency words by ℓ2 norm discounting. In Findings of the Association for Computational Linguistics: ACL 2023, 8644–8652 (2023).
  • 24.Lewis, P. et al. Retrieval-augmented generation for knowledge-intensive nlp tasks. Adv. neural Inf. Process. Syst.33, 9459–9474 (2020). [Google Scholar]
  • 25.Rubin, O., Herzig, J. & Berant, J. Learning to retrieve prompts for in-context learning. In Carpuat, M., de Marneffe, M.-C. & Meza Ruiz, I. V. (eds.) Proceedings of the 2022 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies, 2655-2671 (Association for Computational Linguistics, Seattle, United States, 2022). https://aclanthology.org/2022.naacl-main.191/.
  • 26.Ye, J., Wu, Z., Feng, J., Yu, T. & Kong, L. Compositional exemplars for in-context learning. In International Conference on Machine Learning, 39818–39833 (PMLR, 2023).
  • 27.Su, H. et al. Selective annotation makes language models better few-shot learners. In The Eleventh International Conference on Learning Representations (2023). https://openreview.net/forum?id=qY1hlv7gwg.
  • 28.Wu, Z., Wang, Y., Ye, J. & Kong, L. Self-adaptive in-context learning: An information compression perspective for in-context example selection and ordering. In Proceedings of the 61st Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers), 1423–1436 (2023).
  • 29.Margatina, K., Schick, T., Aletras, N. & Dwivedi-Yu, J. Active learning principles for in-context learning with large language models. In Findings of the Association for Computational Linguistics: EMNLP 2023, 5011–5034 (2023).
  • 30.Zhan, Z., Wang, J., Zhou, S., Deng, J. & Zhang, R. MMRAG: multi-mode retrieval-augmented generation with large language models for biomedical in-context learning. J. Am Med. Inform. Assoc.32, 1505–1516 (2025). [DOI] [PMC free article] [PubMed]
  • 31.Lin, H., Bilmes, J. & Xie, S. Graph-based submodular selection for extractive summarization. In 2009 IEEE Workshop on Automatic Speech Recognition & Understanding, 381–386 (IEEE, 2009).
  • 32.Li, X. & Qiu, X. Finding supporting examples for in-context learning. CoRR (2023).
  • 33.Lin, H. & Bilmes, J. Multi-document summarization via budgeted maximization of submodular functions. In Human Language Technologies: The 2010 Annual Conference of the North American Chapter of the Association for Computational Linguistics, 912–920 (2010).
  • 34.Kulesza, A., Taskar, B. et al. Determinantal point processes for machine learning. Found. Trends® Mach. Learn.5, 123–286 (2012). [Google Scholar]
  • 35.Chen, L., Zhang, G. & Zhou, E. Fast greedy map inference for determinantal point process to improve recommendation diversity. Advances in Neural Information Processing Systems 31 (2018).
  • 36.Arora, S., Li, Y., Liang, Y., Ma, T. & Risteski, A. A latent variable model approach to pmi-based word embeddings. Trans. Assoc. Comput. Linguist.4, 385–399 (2016). [Google Scholar]
  • 37.Bollegala, D., Yoshida, Y. & Kawarabayashi, K.-i. Using k-way co-occurrences for learning word embeddings. In Proceedings of the AAAI Conference on Artificial Intelligence, vol. 32 (2018).
  • 38.Gillenwater, J., Kulesza, A. & Taskar, B. Near-optimal map inference for determinantal point processes. Advances in Neural Information Processing Systems 25 (2012).
  • 39.Minoux, M. Accelerated greedy algorithms for maximizing submodular set functions. In Optimization Techniques: Proceedings of the 8th IFIP Conference on Optimization Techniques Würzburg, September 5–9, 1977, 234–243 (Springer, 2005).
  • 40.Nemhauser, G. L., Wolsey, L. A. & Fisher, M. L. An analysis of approximations for maximizing submodular set functions—i. Math. Program.14, 265–294 (1978). [Google Scholar]
  • 41.Lin, H. & Bilmes, J. A class of submodular functions for document summarization. In Proceedings of the 49th annual meeting of the association for computational linguistics: human language technologies, 510–520 (2011).

Associated Data

This section collects any data citations, data availability statements, or supplementary materials included in this article.

Supplementary Materials

Data Availability Statement

The datasets involved in this study are publicly available from the following links: ChemProt (https://huggingface.co/datasets/bigbio/chemprot), DDI (https://github.com/isegura/DDICorpus), and HealthAdvice (https://huggingface.co/datasets/medalpaca/medical_meadow_health_advice).

The code is publicly available on GitHub at https://github.com/huoxusg/submodular_icl.


Articles from Npj Artificial Intelligence are provided here courtesy of Nature Publishing Group

RESOURCES