Exercise 7
: Creating applications using different types of LangChains – Simple Sequential, Sequential and map reduce
Ex1 : Simple Sequential LLM CHains - Chains with single input and single output
#Using LLM Chains Suggest a good name for a company that makes a particular product
#Also generate a catchy phrase or a logo for this company
###############################################################################
from langchain_openai import OpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
from langchain.chains import SimpleSequentialChain
llm = OpenAI(temperature = 0.9)
template = "Suggest a good name for a company that makes {product}"
prompt1 = PromptTemplate.from_template(template)
chain1 = LLMChain(llm=llm, prompt = prompt1)
template = "Generate a catchy phrase for this company {company}"
prompt2 = PromptTemplate.from_template(template)
chain2 = LLMChain(llm=llm, prompt = prompt2)
final_chain = SimpleSequentialChain(
chains = [chain1,chain2],
verbose = True)
response = final_chain.run("Noodles")
print(response)
SEQUENTIAL CHAIN
EX2 : USING A SEQUENTIAL LANGCHAIN, FIND THE YEAR WHEN “FORD” WAS LAUNCHED AND LIST
THE TOP 5 CAR MODELS OF THAT YEAR
#Sequential chain with single input and single output variable
from langchain_openai import OpenAI
from langchain import PromptTemplate
from langchain.chains import LLMChain
from langchain.chains import SequentialChain
llm = OpenAI(temperature = 0.5)
prompt1 = PromptTemplate(
input_variables = ['name'],
template = "Tell me when the car model {name} was launched"
chain1 = LLMChain(llm = llm, prompt = prompt1, verbose = True, output_key = 'year')
prompt2 = PromptTemplate(
input_variables = ["year"],
template = "Tell me the top 5 car model in the year {year}"
chain2 = LLMChain(llm = llm, prompt = prompt2, verbose = True, output_key = 'top 5')
final_chain = SequentialChain(
chains = [chain1,chain2],
input_variables = ['name'],
output_variables = ['year', 'top 5'], verbose = True)
response = final_chain({"name": "Ford"})
print(response)
****************************************************************************
Ex3 FIND THE YEAR WHEN “FORD” LAUNCHED “ECO-SPORT”. FIND SIMILAR PRODUCTS LAUNCHED
IN THAT YEAR
# With multiple input Variables
from langchain_openai import OpenAI
from langchain import PromptTemplate
from langchain.chains import LLMChain
from langchain.chains import SequentialChain
llm = OpenAI(temperature = 0.5)
prompt1 = PromptTemplate(
input_variables = ['company', 'product_name'],
template = "Tell me when the {company} launched the product {product_name}?"
chain1 = LLMChain(llm = llm, prompt = prompt1, verbose = True, output_key = 'year')
prompt2 = PromptTemplate(
input_variables = ["year"],
template = "List four similar products launched in the year {year}"
chain2 = LLMChain(llm = llm, prompt = prompt2, verbose = True, output_key = 'four similar')
final_chain = SequentialChain(
chains = [chain1,chain2],
input_variables = ['company', 'product_name'],
output_variables = ['year', 'four similar'], verbose = True)
response = final_chain({"company": "Ford", "product_name":"Eco_sport" })
print(response)
Ex4 : Sequential Chains - Chains with multiple inputs and Outputs
PROBLEM: TRANSLATE THE GIVEN REVIEW To ENGLISH. SUMMARIZE IT. FIND THE LANGUAGE IN
WHICH THE REVIEW IS WRITTEN. WRITE A FOLLOW UP RESPONSE TO THE SUMARY IN A GIVEN
LANGUAGE.
from langchain_openai import OpenAI
from langchain.prompts import PromptTemplate
from langchain.prompts import ChatPromptTemplate
from langchain.chains import LLMChain
from langchain.chains import SimpleSequentialChain
from langchain.chains import SequentialChain
llm = OpenAI(temperature = 0.8)
Review = """Les ordinateurs portables GamersTech impressionne par ses
performances exceptionnelles et son design élégant. De sa configuration
matérielle robuste à un clavier RVB personnalisable et un système de
refroidissement efficace, il établit un équilibre parfait entre prouesses
de jeu et portabilité."""
#Promp Template 1 : Translate the review to English
prompt1 = ChatPromptTemplate.from_template(
"Translate the following to English:"
"\n\n{Review}"
#Chain 1 : Input : Review Output : Eng_review
chain1 = LLMChain(llm = llm, prompt = prompt1, output_key = "Eng_review")
#Promp Template 2 : Summarize the translated review
prompt2 = ChatPromptTemplate.from_template(
"Summarize the following review in 1 sentence"
"\n\n{Eng_review}"
#Chain 2 : Input : Eng_review Output : Summary
chain2 = LLMChain(llm = llm, prompt = prompt1, output_key = "Summary")
#Promp Template 3 : Find the language in which the review is written
prompt3 = ChatPromptTemplate.from_template(
"What is the language in which the review is written"
"\n\n{Review}"
)
#Chain 3 : Input : Review Output : language
chain3 = LLMChain(llm = llm, prompt = prompt3, output_key = "language")
#Promp Template 4 : Write a follow up response to the summary in the language given
prompt4 = ChatPromptTemplate.from_template(
"Write a follow up response to the following summary"
"In the specified language"
"\n\nSummary : {Summary}\n\n Language : {language}"
#Chain 4 : Input : Summary, Language, Output : followup_message
chain4 = LLMChain(llm = llm, prompt = prompt4, output_key = "followup_message")
final_chain = SequentialChain(
chains = [chain1,chain2,chain3,chain4],
input_variables = ["Review"],
output_variables = ["Eng_review", "Summary", "language", "followup_message"],
verbose = True)
response = final_chain(Review)
print("Language of the given review is", response['language'])
print("\nReview translated to English is:", response['Eng_review'])
print("\nSummary of the review is", response['Summary'])
print("\nA follow-up message of the review summary is", response['followup_message'])