Large Language Models Can Learn Representation in Natural Language (2024)

Abstract: One major challenge for Large Language Models (LLMs) is completing complex tasks involving multiple entities, such as tool APIs. To tackle this, one approach is to retrieve relevant entities to enhance LLMs in task completion. A crucial issue here is obtaining accurate natural language representations for each entity to aid in retriever precision. In this paper, we propose the Natural Language Representation Optimization Problem, which aims to refine entity descriptions for improved retrieval and LLM utilization. We introduce the Learning to Represent with Natural Language method, which utilizes LLMs to optimize entity representations consisting of text patterns based on environmental feedback. We iteratively prompt LLMs to enhance or adjust patterns based on entity samples and evaluate their effectiveness through environmental feedback. Our method successfully learns human-understandable representations for classification tasks (e.g., instructions and documents) and API call tasks (e.g., APIbench and Virtual Home), significantly improving GPT-4’s task performance.

heading: 1 Introduction
text: Large Language Models (LLMs), such as GPT4 (OpenAI, 2023), LLaMa-2 (Touvron et al., 2023), and Gemini (Team et al., 2023), successfully achieved strong zero-shot/few-shot performance on various natural language tasks (Qin et al., 2023a; Frieder et al., 2023; Kojima et al., 2022; Zhong et al., 2023; Hendrycks et al., 2020). To complete more challenging tasks that requires operating multiple tools (Guo et al., 2023c; Zhou et al., 2023; Liu et al., 2023; Mao et al., 2023), LLMs need to understand and use these new entities (e.g., classes and tools). To address these tasks, a straightforward way is to fine-tune LLMs on the training corpus (Patil et al., 2023; Qin et al., 2023b), which needs many GPUs. (Ovadia et al., 2023; Zhang et al., 2023) retrieves relevant entities from all en-
tities and then uses the retrieved entities to augment the LLM for task completion (Ovadia et al., 2023; Zhang et al., 2023). However, plain natural language representations of entities do not always make the retriever capture the complex relation between entities and the task instruction, causing inaccurate retrieved results.
To address the poor entity representation, we propose the Natural Language Representation Optimization Problem, which seeks the best representations of entities for complex task completion. Cur-
9145
rently, users must experiment with various representations to find the most suitable, lacking knowledge of compatibility with specific retrievers and LLMs. To streamline this process, we introduce the Learning to Represent with Natural Language method, leveraging LLMs to automatically optimize representations. Our method aims to learn entity representations comprising multiple patterns, each describing a unique property. It operates in two main stages: (1) Multi-step iterations to refine entity representations. (2) Within each iteration, LLMs are prompted to add new patterns and edit existing ones based on entity samples, evaluating effectiveness through feedback.
We demonstrate our method’s learned representation alongside typical elements of the API call task in Table 1. Compared to plain natural language representation, our learned representation offers 4 distinct patterns for describing API usage, each encapsulating general API usage scenarios with typical examples. These patterns, derived from user instructions, aid in improved retrieval and usage of the Language Model (LLM). Additionally, the learned representation is human-readable and amenable to expert editing. Experimental results on classification and API call tasks show that our method significantly enhances the retrieval performance and the GPT-4’s task performance.

heading: 2 Related Work
text: Large Language Models and Prompt Optimization (e.g., GPT3 (Brown et al., 2020), GPT-4 (OpenAI, 2023), Gemini (Team et al., 2023), and LLaMA (Touvron et al., 2023)) can solve various basic natural language tasks (Qin et al., 2023a), write mathematical proof draft (Jiang et al., 2022), and pass human standard examinations (Zhong et al., 2023). Furthermore, with the help of tools (e.g., APIs (Liang et al., 2023) and search engine (Schick et al., 2023)), LLMs can complete PPT tasks (Guo et al., 2023c) and control operating system (Liu et al., 2023). Prompting optimization methods further improve the LLM’s performance by optimizing the input prompt based on feedback. APE (Zhou et al., 2022) and Instruction Induction (Honovich et al., 2022) update task instruction by selecting the best instruction candidate based on the validation performance. Learning to plan (Guo et al., 2023b) optimizes the task plan consisting of step-by-step solutions based on the error made by LLMs. EvoPrompt (Guo et al., 2023a) iteratively
optimizes the prompts based on the evolutionary algorithms.
Representation Learning in Pretrained Language Models The recent mainstream paradigm for natural language representation learning is to first pre-train the language model on a large-scale corpus with self-supervised losses like auto-regressive loss and masked language modeling loss (Devlin et al., 2019). Analysis experiments show that the pretrained models (e.g., GPT (Radford and Narasimhan, 2018), RoBERTa (Liu et al., 2019b), and T5 (Raffel et al., 2019)) can learn rich linguistic knowledge (Tenney et al., 2019b; Liu et al., 2019a; Tenney et al., 2019a) and world knowledge (Petroni et al., 2019; Bouraoui et al., 2019; Feldman et al., 2019) during the pre-training process. However, learning natural language representations of entities of the task has not been investigated.

heading: 3 Natural Language Representation Optimization Using LLMs
text: We start with a task defined by a dataset Dtrain = (Q,A), where Q represents the input query and A the corresponding answer containing entities e (e.g., a class or API sequence). Typically, to tackle this task, we employ a retriever R to select an entity e from a set S that relates to the query Q. This selection is based on ranking the similarity between the query and the natural language descriptions pe of entities in S. This process can be formalized as ê = R(Q,P ), where P comprises the natural language representations of all entities, and ê is the retrieved entity. Subsequently, this approach enriches the Language Model (LM) M with the retrieved ê to generate an answer M(ê) for completing Q. The objective of optimizing natural language representations is to find a set P such that R can accurately retrieve relevant entities based on P , and M(ê) = A. Formally, we formulate this task as an optimization problem aiming to maximize the expectation of a per-sample score f(M(ê), Q,A) = f(M(R(Q,P )), Q,A) over possible (Q,A).

heading: P ∗ = argmax
text: P E(Q,A)[f(M(R(Q,P )), Q,A)]
(1)

heading: 4 Method
text: In this section, we present our method, "Learning to Represent with Natural Language," for automatically optimizing the natural language representa-
tion P . Our method features two key aspects: (1) Each entity’s representation is managed as a list of patterns describing its properties, which are updated via add and edit operations. (2) Inspired by APE (Zhou et al., 2022), our approach employs an iteration-based optimization strategy to discover the optimal representation. Each iteration involves the following three steps.

heading: 4.1 Collecting Incorrect Samples Based on Current Representation
text: To iteratively optimize the representation, our method first identifies incorrect samples unresolved by the current iteration’s representation. Specifically, at the t-th training iteration, for each entity e, we collect its false negative samples SeFN = {(Q,A)|e /∈ M(ê) ∩ e ∈ A} from the training dataset D = (Q,A), where the retriever and LLM generate answers lacking necessary entity e based on the current representation Pt. Similarly, we gather false positive samples SeFP = {(Q,A)|e ∈ M(ê) ∩ e /∈ A} from the training dataset, where the retriever R and LLM M mistakenly include e in the generated answer M(ê) for query Q based on representation Pt.

heading: 4.2 Adding New Patterns and Editing Existing Pattern
text: For a given entity e and its current representation P et , the retriever R might not capture the relevance between P et and some samples of e, resulting in false positives, SeFN. To address this, we prompt the LLM to derive new patterns, pet , from S e FN, aiming to enhance the relevance between SeFN and P e t by adding pet to P e t . We illustrate this addition prompt in Figure 1. Additionally, the retriever R may incorrectly assign high relevance between samples of other entities and P et , leading to false positive samples, SeFP. To mitigate this, we prompt the LLM to edit the existing patterns of P et , resulting in P e t , to decrease the relevance between P et and SFP e. We demonstrate this editing prompt in Figure 2, where we request the LLM to modify words and phrases in the patterns to reduce erroneous high relevance while preserving the representativeness of e’s properties.

heading: 4.3 Verifying the Effectiveness of Patterns Based on Feedback
text: To assess the effectiveness of newly generated patterns pet and edited patterns P e t , we evaluate whether updating Pt with pet and SFPe improves the average validation score f(M(R(Q,P )), Q,A). Specifically, We first insert pet into the original representation P e t of entity e and compare the average validation scores before and after the update. If the updated score is higher than a threshold, we consider pet an effective update to Pt; otherwise, we maintain P et as the original representation. Similarly, we evaluate the effectiveness of P et by replacing P e t with P e t and comparing validation performance. After verification, we obtain the new representation P et+1 updated with the new patterns pet and edited patterns P et if they demonstrate an improvement in validation performance.

heading: 4.4 Put it together
text: At iteration t, our method applies the three steps to each entity in set S one by one to obtain new entities’ representation Pt+1. Optimization continues until reaching the maximum iteration or no improvement in validation performance over the last three iterations. We illustrate our method’s algorithm procedure in Appendix B.

heading: 5 Experiments
text: Datasets For Classification Tasks, their entities are classes and we consider (1) the banking intent classification task Casanueva et al. (2020) (2) the web of science document classification task Kowsari et al. (2017) (WOS5736). For API call Tasks, their entities are APIs and we consider (1) the APIbench benchmark (Patil et al., 2023). It has three sub-tasks consisting of APIs extracted from huggingface, torchhub, and tensor-
flow respectively. (2) the virtual-home task (Puig et al., 2018) where the LLM needs to call and generate the API sequence to finish the instruction. In the test process, we follow Patil et al. (2023); Xu et al. (2023) and report the Longest Common Sub-sequence (LCS) score for VirtualHome and accuracy performance for other tasks. More details are in Appendix C
Model and retrievers: We utilize the GPT-4 model (GPT-4-turbo) as the LLM to balance performance and the inference cost. For the virtual home task, following Xu et al. (2023), we employ the BM25 retriever, treating each API as a separate document. Retrieval involves searching the index and fetching the most relevant APIs based on instructions. For other tasks, each API/class is indexed as an individual document and embedded using the openai text-embedding-ada-002 API. Relevant APIs/classes are retrieved based on cosine similarity between their embeddings and the user instruction or passage.
Baselines and Hyper-parameters (1) ’Retriever-only’, which only uses the retriever to retrieve the most relevant entities for the instruction. Each entity uses its plain natural language representation as its document. (2) ’Retriever top k + LLM selection’, which uses the retriever to retrieve the most relevant top k entities for the instruction and then prompts the LLM to select the necessary entities from the k entities (See the prompt in Figure 3). (3) Automatic Prompt Engineer Liu et al. (2021b), which iteratively updates the representations by generating representation candidates of each entity and then selecting the candidate with the best validation performance. (4) ’Retriever+learning to represent’, which follows (1) but replaces plain representations with learned representations by our method. (5) ’Retriever top k + learning to represent+ LLM selection’, which follows (2) but using the learned representations by our method. Details and hyperparameters are in Appendix D.

heading: 5.1 Main results
text: The results in Table 2 show: (1) Our method significantly enhances the retriever’s performance, e.g., from 35 (plain representation in ’Retriever only’) to 68.4 (learned representation in ’Retriever+learning to present’) on the Hugging Face API task. (2) The LLM can utilize the learned representation to make decisions, achieving the best performance
across all baselines on the 6 tasks (’Retriever top k + learning to represent+ LLM selection’). Interestingly, despite the web-of-science task providing only class indices (e.g., 1, 2, 3, ...), our method still can learn general representations from samples to improve task performance.
Finetuning VS Learning to represent Patil et al. (2023) fine-tunes the LLaMa-7b model on the APIbench training set, achieving accuracy performances of 55.6, 67.3, and 61.8 on the Hugging Face, TensorFlow, and TorchHub APIbench tasks, respectively. Additionally, the fine-tuned RoBERT model in Shao et al. (2023) attains 88.6 accuracy in the banking77 task. In comparison, our method achieves superior performance with humanunderstandable natural language representation.

heading: 5.2 Analysis
text: Case study of the learned represetations We compare the representations directly generated from GPT-4 and the learned representation in Table 4. Our learned representation uses more detailed words and aligns closely with the task samples. More examples are in Table 5.
Ablation study We conduct the ablation study on each component of our method as shown in Table 3. Our findings are: (1) Addition and edition operations are crucial. (2) Increased iterations improve performance but escalate inference cost; thus, we set the iteration number as 10. (3) A higher threshold filters out noisy updates but risks losing useful ones; hence, we set the threshold at 0.01.

heading: 6 Conclusion
text: This paper focuses on challenging tasks requiring processing multiple entities for completion. We propose the natural language representation optimization problem to find the optimal entity representation for improved retrieval and LLM utilization. We introduce the learning to represent with natural language method, which automatically optimizes representation based on feedback. Experiments demonstrate our method boosts the LLM’s task performance significantly, along with highquality entity natural language representation.

heading: 7 Limitations and potential risks
text: While our method shows notable enhancements in tasks like classification and API calls, it does have limitations:
It assumes access to a pre-existing training set, making deployment in open-domain settings challenging. We plan to address this in future research.
Theoretical guarantees and convergence analysis for our method are lacking. We aim to investigate this in future studies.
We do not foresee significant risks, as our experiments utilize public datasets and our method is tailored to classification and API call tasks.

heading: A Prompts of our method
text: We illustrate the addition prompt and the edition prompt of our method in Figure 1 and Figure 1 respectively.

heading: B Algorithm procedure
text: We illustrate our method in Algorithm.1.

heading: C Task details
text: For Classification Tasks, their entities are classes and we consider (1) the banking intent classification task Casanueva et al. (2020) where the LLM needs to classify instructions (sentences) belonging to 77 different intents. (2) the web of science task Kowsari et al. (2017) (WOS5736) where the LLM needs to classify passages belonging to 11 different science categories. For API call Tasks, their entities are APIs and we consider (1) the APIbench benchmark Patil et al. (2023) where the LLM needs to call the correct API to finish the instruction. It has three sub-tasks consisting of APIs extracted
Algorithm 1 Our Method procedure OPTIMIZE(P : Entities’ representations)
P0 ← plain natural language representations fval ← validation performance with P0 iteration t← 0 while iteration t < maximum iteration do
for each entity e ∈ S do false negative samples SeFN, false positive samples S e FP ← collecting incorrect samples from
dataset D with Pt new pattern pet ← LLMM,SeFN Edited patterns P et ← LLMM,P et , SeFP fadd ← validation performance with Pt + pet (add pet to P et ) if fadd > fval+threshold then
Pt ← Pt + pet fval ← fadd
end if fedit ← validation performance with Pt edited by P et (replace P et with P e t ) if fedit > fval+threshold then Pt ←Pt edited by P et fval ← fedit
end if end for iteration t← iteration t+ 1
end while return Pt
end procedure
from huggingface, torchhub, and tensorflow respectively. (2) the virtual-home task Puig et al. (2018) where the LLM needs to call and generate the API sequence to finish the instruction.

heading: D Details of baselines and hyper-parameters
text: In (1) ’retriever-only’, for the virtual home task, we follow Xu et al. (2023) and retrieve the top 10 relevant APIs to augment the LLM. For other tasks, we choose the most relevant API as the final answer. In (2) ’retriever top k + LLM selection’, the selected entities by the LLM can be used to augment the LLM (virtual home) or as the final answer (classification task). We set k as 5 here. In (3) kNN selection method Liu et al. (2021a), for the virtual home task, APIs from the labels of these samples augment LLM; for other tasks, the majority class of labels from k samples is chosen as the final answer. This baseline utilizes the whole training set. We set k as 5 here. (4) Automatic Prompt Engineer (APE) Liu et al. (2021b) that iteratively updates the representations by generating representation candidates of each entity and then selecting the candidate with the best validation performance as the entity’s representation. In the test process, we follow (2) except for replacing the plain natural language representation of each entity with the learned representation by the APE method for retrieval. This baseline utilizes the training set to iteratively optimize the representations.
Hyperparamters: In the learning process, we randomly split the original training set into 4:1 for constructing the training set and the validation set respectively. We use the training dataset D to find the optimal representations and test methods’ performance on the original test dataset. We use the average F1 score over all entities as the score f in the verification process and set the threshold as 0.01 (Sec. 4.4). We set the maximum iteration as 10. We use the 1201 version of GPT-4-turbo and set its temperature as 0. We follow the official code of APE Liu et al. (2021b) to conduct our experiment. It also has 10 iterations. At each iteration, we select the best candidate from 5 candidates for each entity.
E Case studies

heading: Future Work
text: We plan to address this in future research. Theoretical guarantees and convergence analysis for our method are lacking. We aim to investigate this in future studies. We do not foresee significant risks, as our experiments utilize public datasets and our method is tailored to classification and API call tasks