LangChain & The Principle Of Composition

In order to use Large Language Models (LLMs) effectively, organisations need to accurately retrieve contextual data, with the correct context window size and at the right time for prompt injection; at scale.

Cobus Greyling
4 min readJul 17, 2023

--

I’m currently the Chief Evangelist @ HumanFirst. I explore & write about all things at the intersection of AI and language; ranging from LLMs, Chatbots, Voicebots, Development Frameworks, Data-Centric latent spaces & more.

Below is complete working code which you can copy and paste in a notebook and run. You only need to add your OpenAI API Key.

This notebook illustrates how a contextual prompt is composed or constituted, with the different elements of the prompts having placeholders as templates.

Hence part of a prompt can be reused. In the words of LangChain:

A PipelinePrompt consists of two main parts:

Final prompt: This is the final prompt that is returned

Pipeline prompts: This is a list of tuples, consisting of a string name and a prompt template. Each prompt template will be formatted and then passed to future prompt templates as a variable with the same name.

From the example below, composing a prompt manually and running it against a LLM is a straightforward and simple process.

But, compiling contextual prompts at scale and in an enterprise setting will require automation of the prompt creation process. Retrieving the right information at the right time to put the prompt together; ensuring the prompt is contextual.

Looking at the diagram below, the red arrow shows the point where this automation is required.

Based on the question of the user, a semantic search needs to be performed, and the relevant piece of text needs to be retrieved from a Knowledge Store. Again, this text snippet will act as the contextual reference for the prompt.

Read more about intelligent prompt pipelines here.

pip install langchain
pip install openai

import os
import openai
os.environ['OPENAI_API_KEY'] = str("xxxxxxxxxxxxxxxxxxxxxx")

from langchain.prompts.pipeline import PipelinePromptTemplate
from langchain.prompts.prompt import PromptTemplate

full_template = """{introduction}
{example}
{start}"""
full_prompt = PromptTemplate.from_template(full_template)

introduction_template = """You are impersonating {person}."""
introduction_prompt = PromptTemplate.from_template(introduction_template)

example_template = """Here's an example of an interaction:
Q: {example_q}
A: {example_a}"""
example_prompt = PromptTemplate.from_template(example_template)


start_template = """Now, do this for real!
Q: {input}
A:"""
start_prompt = PromptTemplate.from_template(start_template)


input_prompts = [
("introduction", introduction_prompt),
("example", example_prompt),
("start", start_prompt)
]
pipeline_prompt = PipelinePromptTemplate(final_prompt=full_prompt, pipeline_prompts=input_prompts)

pipeline_prompt.input_variables

print(pipeline_prompt.format(
person="Elon Musk",
example_q="What's your favorite car?",
example_a="Tesla",
input="What's your favorite social media site?"
))

And when the code is run, here is the output:

You are impersonating Elon Musk.

Here's an example of an interaction:

Q: What's your favorite car?
A: Tesla

Now, do this for real!

Q: What's your favorite social media site?
A:

Prompt Pipelines extend prompt templates by automatically injecting contextual reference data for each prompt. Read more here.

⭐️ Follow me on LinkedIn for updates on Conversational AI ⭐️

I’m currently the Chief Evangelist @ HumanFirst. I explore & write about all things at the intersection of AI and language; ranging from LLMs, Chatbots, Voicebots, Development Frameworks, Data-Centric latent spaces & more.

LinkedIn

--

--