[Course Notes] LangChain for LLM Application Development: Part 2

Chanan
5 min readMar 4, 2024

--

Discover how to utilize LangChain Models, Prompts, and Parsers with your application.

LangChain for LLM Application Development by DeepLearning.AI

Table of Contents

  1. Introduction
  2. Models, Prompts, and Parsers (this part)
  3. Memory
  4. Chains
  5. Question and Answer
  6. Evaluation
  7. Agents

Models, Prompts and Parsers

This section includes two parts

  1. Directly use the OpenAI API interface to make calls
  2. Using LangChain’s API, which includes 3 aspects:
  • Prompts: Inputs to the model used to convey user intent to the model
  • Models: Language models that support most of the work
  • Output parsers: Processes the model’s output into a more structured format

Direct API calls to OpenAI

import os
import openai
import datetime
from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv()) # read local .env file
openai.api_key = os.environ['OPENAI_API_KEY']
# Get the current date
current_date = datetime.datetime.now().date()
# Define the date after which the model should be set to "gpt-3.5-turbo"
target_date = datetime.date(2024, 6, 12)
# Set the model variable based on the current date
if current_date > target_date:
llm_model = "gpt-3.5-turbo"
else:
llm_model = "gpt-3.5-turbo-0301"def get_completion(prompt, model=llm_model):
messages = [{"role": "user", "content": prompt}]
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=0,
)
return response.choices[0].message["content"]
def get_completion(prompt, model=llm_model):
messages = [{"role": "user", "content": prompt}]
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=0,
)
return response.choices[0].message["content"]
get_completion("What is 1+1?")

This looks nice. But, if you have different customer reviews in different languages, not just English pirate, but French, German, Japanese, and so on, you can imagine having to generate a whole sequence of prompt to generate such translations. Using LangChain would be more convenient.

API calls through LangChain

Model

from langchain.chat_models import ChatOpenAI# To control the randomness and creativity of the generated
# text by an LLM, use temperature = 0.0
chat = ChatOpenAI(temperature=0.0, model=llm_model)

Prompt Template

from langchain.prompts import ChatPromptTemplate
template_string = """Translate the text \
that is delimited by triple backticks \
into a style that is {style}. \
text: ```{text}```
"""
prompt_template = ChatPromptTemplate.from_template(template_string)customer_style = """American English \
in a calm and respectful tone
"""
customer_email = """
Arrr, I be fuming that me blender lid \
flew off and splattered me kitchen walls \
with smoothie! And to make matters worse, \
the warranty don't cover the cost of \
cleaning up me kitchen. I need yer help \
right now, matey!
"""
customer_messages = prompt_template.format_messages(
style=customer_style,
text=customer_email)
# Call the LLM to translate to the style of the customer message
customer_response = chat(customer_messages)
print(customer_response.content)
service_reply = """Hey there customer, \
the warranty does not cover \
cleaning expenses for your kitchen \
because it's your fault that \
you misused your blender \
by forgetting to put the lid on before \
starting the blender. \
Tough luck! See ya!
"""
service_style_pirate = """\
a polite tone \
that speaks in English Pirate\
"""
service_messages = prompt_template.format_messages(
style=service_style_pirate,
text=service_reply)
print(service_messages[0].content)

Prompt:

Translate the text that is delimited by triple backticks into a style that is a polite tone that speaks in English Pirate. text: ```Hey there customer, the warranty does not cover cleaning expenses for your kitchen because it’s your fault that you misused your blender by forgetting to put the lid on before starting the blender. Tough luck! See ya!
```

service_response = chat(service_messages)
print(service_response.content)

Output:

Ahoy there, matey! I must kindly inform ye that the warranty be not coverin’ the expenses o’ cleaning yer galley, as ’tis yer own fault fer misusin’ yer blender by forgettin’ to put the lid on afore startin’ it. Aye, tough luck! Farewell and may the winds be in yer favor!

Why use prompt template?

  • Prompts can be long and detailed.
  • Reuse good prompts when you can!
  • LangChain also provides prompts for common operation.

Output Parsers

Example: Extracting information from the product review and format the output in the JSON format.

{
"gift": False,
"delivery_days": 5,
"price_value": "pretty affordable!"
}
from langchain.prompts import ChatPromptTemplate
customer_review = """\
This leaf blower is pretty amazing. It has four settings:\
candle blower, gentle breeze, windy city, and tornado. \
It arrived in two days, just in time for my wife's \
anniversary present. \
I think my wife liked it so much she was speechless. \
So far I've been the only one using it, and I've been \
using it every other morning to clear the leaves on our lawn. \
It's slightly more expensive than the other leaf blowers \
out there, but I think it's worth it for the extra features.
"""
# Prompt template
review_template = """\
For the following text, extract the following information:
gift: Was the item purchased as a gift for someone else? \
Answer True if yes, False if not or unknown.
delivery_days: How many days did it take for the product \
to arrive? If this information is not found, output -1.
price_value: Extract any sentences about the value or price,\
and output them as a comma separated Python list.
Format the output as JSON with the following keys:
gift
delivery_days
price_value
text: {text}
"""
prompt_template = ChatPromptTemplate.from_template(review_template)
print(prompt_template)
messages = prompt_template.format_messages(text=customer_review)
chat = ChatOpenAI(temperature=0.0, model=llm_model)
response = chat(messages)
print(response.content)

Parse the LLM output string into a Python dictionary

from langchain.output_parsers import ResponseSchema
from langchain.output_parsers import StructuredOutputParser
gift_schema = ResponseSchema(name="gift",
description="Was the item purchased\
as a gift for someone else? \
Answer True if yes,\
False if not or unknown.")
delivery_days_schema = ResponseSchema(name="delivery_days",
description="How many days\
did it take for the product\
to arrive? If this \
information is not found,\
output -1.")
price_value_schema = ResponseSchema(name="price_value",
description="Extract any\
sentences about the value or \
price, and output them as a \
comma separated Python list.")
response_schemas = [gift_schema,
delivery_days_schema,
price_value_schema]
output_parser = StructuredOutputParser.from_response_schemas(response_schemas)
format_instructions = output_parser.get_format_instructions()
review_template_2 = """\
For the following text, extract the following information:
gift: Was the item purchased as a gift for someone else? \
Answer True if yes, False if not or unknown.
delivery_days: How many days did it take for the product\
to arrive? If this information is not found, output -1.
price_value: Extract any sentences about the value or price,\
and output them as a comma separated Python list.
text: {text}{format_instructions}
"""
prompt = ChatPromptTemplate.from_template(template=review_template_2)messages = prompt.format_messages(text=customer_review,
format_instructions=format_instructions)
print(messages[0].content)
response = chat(messages)
print(response.content)
output_dict = output_parser.parse(response.content)
output_dict

Output:

{‘gift’: True,
‘delivery_days’: ‘2’,
‘price_value’: [“It’s slightly more expensive than the other leaf blowers out there, but I think it’s worth it for the extra features.”]}

output_dict.get('delivery_days')

Output:

‘2’

That wraps up our discussion on Models, Prompts, and Parsers in LangChain! Next, we’ll delve into Memory, exploring how it enhances our LLM applications.

Stay tuned! 😄

Complete series here: https://medium.com/@parnchananchida/list/langchain-for-llm-application-development-2a43c740ad64

--

--

Chanan

Book and Coffee enthusiast | Data Scientist | Python, NLP, AI