Basic setup

import openai
import os
 
from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv()) # read local .env file
openai.api_key = os.environ['OPENAI_API_KEY']

Note: LLM’s do not always produce the same results. When executing the code in your notebook, you may get slightly different answers that those in the video.

# account for deprecation of LLM model
import datetime
# 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"

Using helper function

helper function

def get_completion(prompt, model=llm_model):
	messages = [{"role": "user", "content" : prompt}]
	response = openai.ChatCompletion.create(
		model = model,
		messages = messages,
		temperature=0,
	)
	return response.choces[0].message["content"]
	
get_completion("What is 1+1?") # '1+1 equals 2.'

using a custom prompt

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!
"""
 
style = """American English in a calm and respectful tone
"""
 
prompt = f"""Translate the text that is delimited by triple backticks 
into a style that is {style}. text: ```{customer_email}```
"""
 
response = get_completion(prompt) 
# "I am really frustrated that my blender lid flew off and splattered my kitchen walls with smoothie! And to make matters worse, the warranty doesn't cover the cost of cleaning up my kitchen. I could really use your help right now, friend."
  • (output) parser
    • a tool that takes the raw, unstructured text output generated by an LLM & transforms it into a predictable and usable structured format

using ChatPromptTemplate

  • ChatPromptTemplate
  • we use prompt templates instead of just f strings in python because:
    • as u build sophisticated apps, prompts will be quite long and detailed, so this lets you reuse prompts
from langchain.prompts import ChatPromptTemplate
 
# 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)
  • we make a chat
    • setting temperature to 0 to make the response less random
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)
prompt_template.messages[0].prompt
# PromptTemplate(input_variables=['style', 'text'], output_parser=None, partial_variables={}, template='Translate the text that is delimited by triple backticks into a style that is {style}. text: ```{text}```\n', template_format='f-string', validate_template=True)
prompt_template.messages[0].prompt.input_variables
# ['style', 'text']
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,
	test=customer_email)
print(type(customer_messages)) # <class 'list'>
print(type(customer_messages[0])) # length is 1
print(customer_messages[0])
# content="Translate the text that is delimited by triple backticks into a style that is American English in a calm and respectful tone\n. text: ```\nArrr, 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!\n```\n" additional_kwargs={} example=False
  • customer_messages is just the entire prompt_template with the embedded customer_style and customer_email
    • access only the string using customer_messages.conent
customer_response = chat(customer_messages)
# Oh man, I'm really frustrated that my blender lid flew off and made a mess of my kitchen walls with smoothie! And on top of that, the warranty doesn't cover the cost of cleaning up my kitchen. I could really use your help right now, buddy!
  • pass the customer_message to chat (openai)

output parsing

  • outputting json
    • extract info from a product review and format to a json
customer_review = """\
This leaf blower is pretty amazing. \
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.
"""
 
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}
"""
from langchain.prompts import ChatPromptTemplate
 
prompt_template = ChatPromptTemplate.from_template(review_template)
messages = prompt_template.format_messages(text=customer_review)
chat = ChatOpenAI(temperature=0.0, model=llm_model)
response = chat(messages)
print(response.content)
"""
{ 
	"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."] 
}
"""
type(response.content) # str
  • the response is a str

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.")
# putting all schemas into a 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()
"""
The output should be a markdown code snippet formatted in the following schema, including the leading and trailing "\`\`\`json" and "\`\`\`":
 
```json
{
	"gift": string  // Was the item purchased                             as a gift for someone else?                              Answer True if yes,                             False if not or unknown.
	"delivery_days": string  // How many days                                      did it take for the product                                      to arrive? If this                                       information is not found,                                      output -1.
	"price_value": string  // Extract any                                    sentences about the value or                                     price, and output them as a                                     comma separated Python list.
}
"""

trying again

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)
                                
                                
response = chat(messages)
"""
```json
{
	"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 = output_parser.parse(response.content)
type(output_dict) # 2
  • now you can access the fields inside the json output since it’s a python dictionary now (and not a string)