Langchain Concepts — Part 4— Prompts

Shishir Singh
4 min readJun 11, 2023

This article is part of a series explaining langchain concepts with simple intuitive examples.

Part 3 covered Models. Another critical component of Langchain is the concept of prompts, which serve as the input to the model. Prompts are rarely hard-coded; instead, they are dynamically constructed from multiple components, providing a flexible and powerful mechanism for model interaction.

In this article, we’ll delve deeper into the four primary facets of prompts in LangChain: PromptValue, PromptTemplate, ExampleSelectors, and OutputParsers. We’ll explore how each of these components contributes to the overall functionality of LangChain and how they can be effectively leveraged in real-life scenarios.

PromptValue

In the realm of LangChain, a PromptValue is what is eventually passed to the underlying model. This concept is vital to understanding how LangChain interfaces with different AI models, particularly because different models may expect different data formats. The PromptValue class exposes methods that can be converted to the exact input types expected by each model type, such as Text or ChatMessages.

For instance, let’s consider a language translation model that translates English text to Spanish. The English text that we want to translate would be wrapped in a PromptValue object, which is then passed to the model. The model processes the PromptValue and returns the translated text as output.

PromptTemplate

While PromptValue represents the input passed to a model, it’s often dynamically created based on user input, other non-static information, and a fixed template string. That’s where PromptTemplate comes into play. This object is responsible for creating the PromptValue, taking in input variables, and returning a PromptValue.

Let’s consider an example of a chatbot that provides movie recommendations based on user preferences. The PromptTemplate would take in various user inputs such as preferred genre, favorite actors, and desired movie length, and dynamically construct a PromptValue that the movie recommendation model can interpret.

ExampleSelectors

When creating prompts, it’s often useful to include examples. These examples can be hard-coded, but dynamically selected examples are often more powerful. ExampleSelectors are objects that take in user input and then return a list of examples to use

For example, suppose we’re working with a language model to generate sales emails. We could use an ExampleSelector to provide the model with examples of high-performing sales emails. These examples would guide the model in generating effective email content.

OutputParsers: Structuring the Model Responses

Once the model has processed the PromptValue, it returns a text response. However, we often want more structured information than just text. This is where OutputParsers step in. They are responsible for instructing the model how output should be formatted and parsing the output into the desired format

Consider a scenario where we’re using a model to extract key information from a legal document. The model’s raw output might be a block of text, but an OutputParser could parse this text and extract the desired pieces of information, such as the parties involved, the contract date, and key clauses.

Real-World examples

let’s apply the concepts of PromptValue, PromptTemplate, ExampleSelectors, and OutputParsers to the examples of “Hiking trip to Colorado” and “Trading bot”.

Example 1: Hiking Trip to Colorado

For planning a hiking trip to Colorado, we use a travel recommendation AI model. Here’s how you could utilize the different components of LangChain:

PromptValue: The user’s preferences for the trip, such as desired difficulty level of the hike, preferred scenery (like mountains or forests), and preferred duration of the hike, would be passed as the PromptValue to the travel recommendation AI model.

PromptTemplate: This would take in the user’s inputs and dynamically construct the PromptValue. For instance, if the user specifies that they want a moderate hike in the Rocky Mountains that lasts for three days, the PromptTemplate would construct a PromptValue representing these preferences.

ExampleSelectors: If the AI model learns from examples, ExampleSelectors could be used to provide examples of ideal hiking trips based on the user’s preferences. These examples could guide the model in generating suitable recommendations.

OutputParsers: The model’s output, perhaps a list of recommended hiking trails, would be parsed by an OutputParser. The OutputParser could structure this output in a user-friendly format, such as an itinerary with details of each recommended trail, including its location, difficulty level, and unique features.

Example 2: Trading Bot

In the case of a trading bot, you might be using a financial prediction AI model. Here’s how the different components of LangChain could be used:

PromptValue: The PromptValue might include the user’s trading preferences, such as risk tolerance, investment horizon, and preferred asset classes. It could also include current market data.

PromptTemplate: The PromptTemplate would take the user’s preferences and the current market data to dynamically construct the PromptValue that the financial prediction model can process.

ExampleSelectors: The ExampleSelectors could provide examples of successful trades or investment strategies that align with the user’s preferences. These examples would guide the model in generating effective trading strategies.

OutputParsers: Once the model processes the PromptValue, it returns a prediction or a trading strategy. The OutputParser would parse this output into a structured format that the user can easily understand and act upon. For example, it could parse the output into a list of recommended trades, with details like the asset to trade, the type of trade (buy or sell), and the recommended timing of the trade.

Conclusion

The versatility and power of prompts in LangChain make them a crucial component of the platform. By dynamically constructing inputs and parsing outputs, they provide a flexible and robust framework for interacting with a variety of AI models.

In the next set of articles, we will cover other key concepts that include Part 5— Indexes, Memory, Chains, and Agents.

GitHub Python code after the conclusion of the series.

--

--

Shishir Singh

Digital Assets, Blockchains, DLTs, Tokenization & Protocols & AI Intersection