Understanding In-Context Learning for Language Models

In-context learning for RAG (Retrieval Augmented Generation) pipeline

Shivam Solanki
Towards Generative AI
3 min readAug 11, 2023

--

As AI continues to evolve, the ability of models to learn and understand context is becoming increasingly important. In the field of Natural Language Processing (NLP), one of the most exciting developments is the capacity for Language Models (LMs) to grasp and generate responses based on context. This concept, known as “In-context Learning,” is revolutionizing the way we interact with AI.

In-context Learning (Source — Papers with Code)

What is In-Context Learning?

In-context learning is the ability of an AI model to generate responses or make predictions based on the specific context provided to it. This context is usually in the form of preceding text or a prompt. In the realm of LMs, this context comes in the form of a text passage or a conversation history, which the model uses to generate appropriate responses.

For instance, if you feed the model a context about DataOps and ask it a question within that context, the model will generate a response that fits within the given context.

How does In-context learning fits in the RAG pipeline?

At a high level, RAG involves two key steps. First, given an input prompt, it uses a retriever to fetch relevant documents from a corpus of knowledge. Then, it feeds these documents, along with the original prompt, to a Large Language model which generates a response. This allows the model to pull in relevant information from the document to pass it as a context generating its response. The In-context learning component of the RAG pipeline is shown below.

RAG pipeline

Implementing In-Context Learning with a Language Model

To illustrate how in-context learning works, let’s look at a simple Python implementation. The script prompts the user for a context and a question based on that context, and then uses an AI model to generate an answer to the question.

Here’s a snippet of the Python code used:

def get_input(prompt="Please enter your question: "):
return input(prompt)
def get_context(prompt="Please enter the context: "):
return input(prompt)
def process_watsonx_request():
question = get_input()
context = get_context()
#…

You can find the full script on GitHub.

Seeing It In Action

Here’s an example of how it works:

>>> process_bam_request()
Please enter the context: DataOps is a collaborative data management discipline that focuses on end-to-end data management and the elimination of data silos…
Please enter your question: What are the benefits of DataOps?

And the script will generate an answer like:

“DataOps offers several benefits including decreasing the cycle time in deploying analytical solutions, lowering data defects, reducing the time required to resolve data defects, and minimizing data silos.”

Conclusion

In-context learning for LLMs is a powerful tool in the field of AI. It allows models to generate more relevant and accurate responses by considering the context in which a question or prompt is given. This leads to more natural and meaningful interactions between humans and AI, opening up exciting possibilities for the future of AI communication.

Follow Towards Generative AI for more on the latest in AI.

--

--

Shivam Solanki
Towards Generative AI

Sr. Data Scientist | Living at the interstice of business, data and technology