Augmenting Language Models with Private Data.

May 16, 2023


Language models are powerful tools for a variety of applications such as chatbots, summarization, translation, and others. Most existing language models, however, are trained on large-scale public datasets, which may not capture the specific domain knowledge or style of our private data. For example, if we want to use a language model to write a personalised email to a customer, we may need to include data from our own database or previous interactions. How can we do this without compromising our data's privacy or security?

In this blog post, we will look at how we can use our private data to augment popular language models like GPT and BERT. We will concentrate on three main techniques: in-context learning, semantic search, and fine-tuning. We will also discuss some of the benefits and drawbacks of each technique, as well as provide some examples and resources for further reading.


There are several techniques available to help us add new knowledge to our model and adapt it to specific tasks or domains. We will look at three popular techniques for augmenting language models with private data in this section: in-context learning, semantic search, and fine-tuning. Let's take a look at each technique and see how it can help our language model.

In-context learning

In-context learning is a technique in which LLMs make predictions based on contexts augmented with a small number of training examples as input. LLMs can extract patterns from context examples and use them to perform many complex NLP tasks without changing model parameters. It can be applied to a wide range of tasks and domains, including image generation, code writing, and natural language generation. Here are some benefits of in-context learning:

  • It doesn't require any additional training or adjustments to the language model. We can simply use the existing pre-trained models as input and provide our own data.

  • It preserves the privacy and security of our data since we do not need to share or upload it anywhere.

  • It enables the language model to locate latent concepts learned from pretraining data and apply them to tasks.

  • It enables zero-shot task generalization for both pretrained and instruction-fine-tuned models.

Tools that use in-context learning include DALL-E, a system that can generate images from text captions using a large-scale generative model, and Codex, a system that can write code in a variety of programming languages based on natural language descriptions or examples. In-context learning is also used by GPT-3, a system that can produce natural language texts for a variety of purposes and domains.

To use in-context learning effectively, it is important to choose high-quality examples that are:

  • Semantically similar to the test input

  • Diverse and representative of the task

  • Clear and consistent in formatting

  • Correct and informative in content

However, there are some drawbacks to in-context learning that limit its applicability and reliability:

  • While in-context learning is a simple method that does not require fine-tuning or transfer learning, it is not very efficient or scalable because it requires examples for every input. Additionally, if the examples are noisy, incomplete, or biased, the output of the model may be inaccurate or unreliable. There is also a limit to how much information we can provide.

  • In-context learning is not resistant to changes in input distribution or task definition. Since the model's parameters are not updated during in-context learning, it cannot adapt to new or unknown inputs that differ from the examples. It is incapable of handling tasks requiring multiple steps or complex reasoning because it may lose track of the context or the goal.

  • In-context learning doesn't have an explicit feedback or explanation mechanism. This lack of transparency can make it challenging to verify or debug the outputs of the system, especially in scenarios where interpretability and accountability are essential, such as safety-critical or high-stakes situations. As a result, the applicability of in-context learning may be limited in these contexts.

These disadvantages indicate that in-context learning is not a cure-all for natural language processing, but rather a useful tool that should be used with caution and supervision.

Semantic search

One approach to overcoming challenges with in-context learning when augmenting LLMs with private data is to use semantic search, a technique that aims to understand the meaning and intent of a query rather than just matching keywords.

Here are some key points to consider when using semantic search:

  • Semantic search uses various indices that measure semantic similarity, such as vector store, tree store, list, or keyword table indices. These indices represent the relationships between words or documents in numbers, which help to find more accurate search results. Semantic search can also use embeddings, which are numerical representations of words or documents that capture their meaning in context. Embeddings are based on machine learning models and are very effective for understanding words in different situations. With these techniques, semantic search can offer more relevant and interactive search results that match the user’s intent better.

  • With semantic search, LLMs can retrieve relevant information from private data sources, such as databases, APIs, or cloud services, and extract answers from various sources of knowledge, such as the Google Knowledge Graph. This improves the ability of LLMs to understand complex queries and generate more accurate responses.

  • Semantic search provides greater control over the access and use of private data, improving the security and privacy of LLMs. Users can control who has access to their data and how it is used, minimizing the risk of data breaches or unauthorized access.

  • By enabling LLMs to understand the meaning and context of private data, such as documents, emails, or chats, semantic search can improve the accuracy and efficiency of LLMs. This reduces the need for manual data processing and labeling, enabling LLMs to process data more quickly and accurately.

When utilizing semantic search for augmenting LLMs with private data, it's important to follow these guidelines:

  • Establish the scope, purpose, and desired outcomes of the semantic search task.

  • Identify relevant private data sources that comply with data protection and privacy regulations.

  • Enrich private data with metadata using a semantic annotation tool and link it to a common ontology or schema.

  • Train an LLM model on the annotated private data and assess its performance on semantic search queries.

  • Integrate the LLM model with a semantic search engine and provide a user-friendly interface for querying and browsing results.

Several tools can be used to perform semantic search are:

  • Elastic Search: a distributed, open-source search engine that supports various data types and queries.

  • Haystack: a transformer and neural network-based framework for scalable and flexible semantic search.

  • Vespa: a cloud-native platform for large-scale information retrieval and ranking using indexing, serving, and machine learning.

  • Llama Index: a lightweight and fast semantic search engine using approximate nearest neighbor search and vector embeddings.

There are also several tools that use semantic search for augmenting LLMs with private data, including:

  • Open Semantic Search: an open-source search engine for easy searching, monitoring, and text mining of large document sets and news.

  • Azure Cognitive Search: a cloud-based service with semantic search capabilities for text-based queries.

  • an AI platform with semantic search solutions for discovering relevant data and improving access to information.

  • Deepset: a company offering semantic search products like Haystack and FARM for unstructured textual data matching based on semantics rather than lexical overlap.

While there are many benefits to using semantic search, there are also some potential drawbacks to consider. These include:

  • Understanding searcher intent and contextual meaning may be challenging in private data sources, impacting the effectiveness of semantic search.

  • Natural language algorithms and concept matching used in semantic search may not capture the nuances or variations of unstructured or domain-specific private data.

  • Implementing semantic search may require significant resources and expertise, especially for large and complex datasets.

  • The quality and relevance of search results are heavily dependent on the quality and quantity of annotated metadata and semantic models used.

  • Semantic search algorithms may be prone to biases and inaccuracies in the data and models used to train them.

In conclusion, semantic search can improve LLMs' accuracy and efficiency by understanding the meaning and context of private data. However, it requires careful consideration of potential drawbacks and proper implementation to achieve the desired outcomes.


A third technique to augment LLMs with private data is fine-tuning. It involves training a pre-trained language model on a smaller dataset that is relevant to our specific task or domain. The idea is to adjust or update some of the parameters of the language model so that it can better capture the characteristics and nuances of our private data. For example, if we have a private dataset of customer reviews, we can fine-tune an LLM on a public dataset of product reviews, and then use the fine-tuned model to generate summaries or sentiment analysis for the private reviews. Another way is to fine-tune an LLM on a subset of the private data that is anonymized or encrypted, and then use the fine-tuned model on the rest of the private data. For example, if we have a private dataset of financial transactions, we can fine-tune an LLM on a subset of the transactions that are masked or encrypted, and then use the fine-tuned model to detect fraud or anomalies for the unmasked transactions.

Before we move on, here are some common misconceptions that need to be debunked about fine-tuning a language model:

  • Fine-tuning a LLM does not make it learn new data. It only adapts the existing knowledge of the LM to a specific task or domain.

  • Fine-tuning a LLM does not guarantee better performance than training a LM from scratch. It depends on the similarity between the pre-training and fine-tuning data, the size and quality of the fine-tuning data, and the hyperparameters of the fine-tuning process.

  • Fine-tuning a LLM does not mean that the LM forgets its previous knowledge. It only modifies some of the weights of the LM to fit the fine-tuning task better, while retaining most of the general linguistic knowledge.

Some examples of tools and projects that use fine-tuning to augment an LLM with private data:

  • Microsoft T-NLG is a tool that uses fine-tuning to generate natural language summaries from structured data sources.

  • OpenAI Codex is fine-tuned on a large corpus of public and private code repositories to generate code snippets from natural language queries.

  • Hugging Face Transformers library provides a unified interface for accessing and fine-tuning various pre-trained LLMs, including the ability to upload and share fine-tuned models on the Hugging Face Hub.

Fine-tuning LLMs has several advantages, including:

  • Fine-tuning can improve the performance and accuracy of LLMs on downstream applications such as text classification, sentiment analysis, question answering, etc.

  • Fine-tuning saves time and resources by avoiding the need to train LMs from scratch or to collect large amounts of public data

To effectively fine-tune an LLM to teach it a new task, you should follow these guidelines:

  • Identify the target domain and task that you want the LLM to learn.

  • Collect or curate a high-quality dataset that is relevant and representative of the target domain and task.

  • Preprocess the dataset to ensure it is compatible with the LLM's input and output formats and vocabularies.

  • Choose an appropriate fine-tuning objective and loss function that aligns with the target task and measures the LLM's performance.

  • Select a suitable fine-tuning strategy and hyperparameters that balance the trade-off between overfitting and underfitting the dataset.

  • Evaluate the fine-tuned LLM on a held-out test set or a real-world application to assess its generalization and robustness.

  • Iterate on the previous steps until you achieve the desired level of performance and quality for the new task.

However, fine-tuning LLMs also has some drawbacks, including:

  • May compromise privacy and introduce inconsistencies in LLM outputs

  • Can be costly and requires re-training on each private dataset and task

  • Strategies may differ based on LLM architecture, pre-training data, and target task/domain

  • Requires understanding of LLM and data, including hyperparameters, data selection and augmentation, evaluation metrics, and error analysis

  • Does not guarantee LLM performance and may introduce new problems and ethical considerations.

Fine-tuning can introduce privacy risks if the additional data contains sensitive or personal information that can be leaked by the fine-tuned LLM. To effectively use fine-tuning to augment LMs with private data, the following guidelines are recommended:

  • Add noise to the fine-tuning process and limit the information exposure of the private data

  • Distribute the fine-tuning across multiple devices or servers and avoid centralizing the private data

  • Compress the fine-tuned LM into a smaller model that retains the essential knowledge but reduces the memorization of the private data

Choosing the Best Method for Augmenting a Language Model with Private Data

Each method has its own advantages and limitations, and choosing the right one depends on our specific needs and goals:

  • We can use in-context learning when we want to leverage the general knowledge of a large pre-trained model and adapt it to our specific task without fine-tuning. This technique is fast and flexible, but it may not produce the best results for complex tasks or domains. For example, we can use in-context learning to generate content, summarize text, or translate natural language to code.

  • We can use semantic search when we want to find relevant information from a large collection of unstructured data based on the meaning and intent of our query. This technique is fast and reliable, but it does not provide direct answers to our questions. For example, we can use semantic search to improve the performance of a job search engine or a question answering system.

  • We can use fine-tuning when we want to improve the performance of a pre-trained model on a specific task or domain by further training it on a new dataset. This technique can produce high-quality results, but it is slow and expensive, and it may cause the model to forget or confabulate information. For example, we can use fine-tuning to improve the performance of a sentiment analysis or a named entity recognition system.


In this blog, we have discussed some popular techniques to augment language models with private data, such as in-context learning, semantic search, and fine-tuning. These techniques can help leverage the power of large pre-trained models without compromising data privacy or quality. We have also explored examples and applications of these techniques in different domains and tasks, providing insights and inspiration on enhancing language models for better results.

References, learning resources and further readings