# Domain-Specific Few-Shot Table Prompt Question Answering via Contrastive Exemplar Selection
## 3 Methodology
### Exemplar Retrieval Approach  
To inject training exemplars targeted to assist the model in understanding the task and standardize model outputs, this paper proposes an exemplar injection prompt template retrieval strategy, which retrieves relevant exemplars to guide model outputs, as shown in Algorithm 2. ```
1:Input: Training dataset \(\mathcal{D}=\{(x_{i},y_{i})\}_{i=1}^{n}\), test set \(x_{test}\). 2:Output: Relevant subset \(\mathcal{P}=\left\{\left(x_{j},y_{j}\right)\right\}_{j=1}^{m}\subset\mathcal{D}\) for the test set. 3: (1). Initialize a candidate set of training exemplars \(\varepsilon=\mathcal{R}_{u}((x,y),\mathcal{D})\). 4: (2). Use the large language model \(g\) to independently score each \(e_{l}\in\varepsilon\) as \(s(e_{l})=\mathrm{Prob}_{g}(y|e_{l},x)\). 5: (3). Based on the scores \(s(e_{l})\) from (2), define a positive exemplar set \(\varepsilon_{pos}\) and a negative exemplar set \(\varepsilon_{neg}\) for each \(e_{l}\). 6: (4). Construct contrastive learning data \(\left\langle x_{i},e_{i}^{+},e_{i,1}^{-},\cdots,e_{i,2B-1}^{-}\right\rangle\). 7: (5). Compute the inner product \(\sin(x,e)=E_{X}(x)^{T}E_{P}(e)\). 8: (6). Minimize \(\mathrm{L}\!\left(x_{i},e_{i}^{+},e_{i,1}^{-},\cdots,e_{i,B-1}^{-}\right)=- \mathrm{log}\frac{\mathrm{\phi}^{\mathrm{min}(x_{i},e_{i}^{+})}}{\mathrm{\phi} ^{\mathrm{min}(x_{i},e_{i}^{+})}+\sum_{j=1}^{B-1}\varepsilon^{\mathrm{min}(x_ {i},e_{i}^{+})}}\). ```  
**Algorithm 2** The Prompt Template Retrieval Strategy  
The algorithm describes the prompt template retrieval strategy for exemplar injection, where the input consists of a training dataset \(\mathcal{D}=\{(x_{i},y_{i})\}_{i=1}^{n}\) and a test set \(x_{test}\), and the output is the relevant subset \(\mathcal{P}=\left\{\left(x_{j},y_{j}\right)\right\}_{j=1}^{m}\) of test exemplars, to aggregate exemplar prompts to guide the large language model. The steps of the algorithm are as follows: first, initialize the candidate training dataset \(\varepsilon=\mathcal{R}_{u}((x,y),\mathcal{D})\); then, use the large language model \(g\) to independently calculate the relevance probability score \(s(e_{l})\) of each \(e_{l}\in\varepsilon\) with the test set \(x_{test}\). Specifically, we utilize the output of the large model \(g\) to compute \(y|e_{l}\). The computed result is then used to calculate the relevant probability with the test set, yielding the probability of \(y|e_{l}\) being \(x_{test}\). Because we used \(x_{test}\) in the input, \(s(e_{l})\), indicating how helpful each candidate object is for decoding the target exemplar. Next, apply \(s(e_{l})\) to all training samples and define a positive instance set \(\varepsilon_{pos}\) and a negative instance set \(\varepsilon_{neg}\) for each \(e_{l}\), where \(\varepsilon_{pos}\) and \(\varepsilon_{neg}\) respectively consist of the top \(k\) candidate instances and the bottom \(k\) candidate instances based on the calculated scores \(s(e_{l})\). Then, construct contrastive learning training data in the form of \(\left\langle x_{i},e_{i}^{+},e_{i,1}^{-},\cdots,e_{i,2B-1}^{-}\right\rangle\), where the positive instance \(e_{i}^{+}\) is a randomly sampled instance from \(e_{pos}^{(i)}\), while the negative instance is composed of a randomly sampled instance from \(e_{neg}^{(i)}\), along with \(B-1\) positive instances sampled from the same batch and \(B-1\) negative instances sampled from the same batch. Finally, compute the inner product \(\sin(x,e)=E_{X}(x)^{T}E_{P}(e)\) and minimize the loss to train the exemplar retriever. Here, \(E_{X}(\cdot)\) and \(E_{P}(\cdot)\) represent the input encoder and prompt encoder as described in reference [44]. Based on the detailed explanation of the prompt template generation strategy and the prompt template retrieval algorithm, the exemplar-based injection approach based on large language model prompting engineering is further demonstrated through specific application examples.