from langchain import PromptTemplate
= "Based on your preference for {genre} movies and your interest in {actor}, I would recommend {movie_title}."
template_movie_recomendation
= PromptTemplate(input_variables=["genre", "actor", "movie_title"], template=template_movie_recomendation) prompt_template_movie_recomendation
Basic Usage LangChain
3 Basic Things to know
Before we jump into the deep of the LangChain Framework, there are 3 basic things to know:
- Prompt Template
- Chat Models
- Chains
Prompt Template
Prompt Templates are predefined structures used to guide the responses of language models.
Why Prompt Templates?
Consistency
By using a set structure, prompt templates ensure consistent responses from the language model.
Relevance
Templates can be designed to elicit specific types of responses, ensuring the output is relevant to the user’s needs.
Example Prompt Template
Example 1
Prompt Template for a Movie Recommendation
"Based on your preference for {genre} movies and your interest in {actor}, I would recommend 5 Films:"
In this template: * {genre} would be replaced by the user’s preferred movie genre (e.g., action, comedy, drama). * {actor} would be replaced by the user’s favorite actor.
The final prompt would look something like this:
"Based on your preference for comedy movies and your interest in Will Ferrell, I would recommend 5 Film:"
Example 2
Prompt Template for a Restaurant Recommendation
"Considering your love for {cuisine_type} and your location in {city}, I suggest you try the following restaurant:"
In this template: * {cuisine_type} would be replaced by the user’s preferred cuisine (e.g., Italian, Chinese, Mexican). * {city} would be replaced by the user’s current city.
The final prompt would look something like this:
"Considering your love for ramen and your location in Bandung, I suggest you try the following restaurant:"
Creating a Prompt Template
Prompt templates are created using the PromptTemplate
from the langchain
library.
Chat Models
Chat Models are specialized language models designed for conversational interactions.
Why Chat Models?
- Natural Interaction
Chat models are trained to respond in a conversational manner, providing a more engaging and natural user experience.
- Context Awareness
Unlike traditional models, chat models can maintain context over a series of exchanges, allowing for more coherent and meaningful conversations.
Note: In this course, we will be using chat models from OpenAI’s
Declaring a Chat Model
Chat model is declared using the ChatOpenAI
from the langchain.chat_models
.
from langchain.chat_models import ChatOpenAI
= ChatOpenAI(openai_api_key="...", model_name="gpt-3.5-turbo", temperature=0) chat
Chains
Chains in LangChain allow for sequential interactions with the language model, enabling more complex and contextual conversations.
Why Chains?
Multiple Steps
Chains allow for multi-step interactions, where each step can influence the next, creating a dynamic conversation flow.
Context Maintenance
Chains keep track of context over multiple exchanges, ensuring continuity in conversations.
Declaring & Running a Chain
Chains are declared using the LLMChain
from the langchain.chains
library.
from langchain.chains import LLMChain
= LLMChain(llm=llm, prompt=prompt_template_movie_recomendation)
chain
print(chain.run(genre="action", actor="Tom Cruise"))
We already created a basic and simple application using the LangChain Framework. Let’s dive more with the LangChain Framework in the next section.