Lab2 Text Models
Lab2 Text Models
Ex 1: Text Summarisation
Problem Statement: Summarise the minutes of the meeting given below in 4 bullet points:
Car Manufacturing Company
Agenda:
1. Call to Order
2. Approval of Previous Meeting Minutes
3. CEO's Report
4. Financial Report
Meeting Minutes:
1. Call to Order:
- The meeting was called to order by Bharath Thippireddy.
3. CEO's Report:
- Bharath Thippireddy presented an overview of the company's performance, highlighting key
achievements and challenges. Key points discussed include:
- Sales figures for the last quarter.
- Progress on cost reduction initiatives.
- Highlights from recent industry events.
- The CEO answered questions from board members.
4. Financial Report:
- The Chief Financial Officer ([CFO's Name]) presented the financial report. Key financial metrics
discussed include:
- Revenue and profit margins.
- Budget vs. actual expenditure.
- Cash flow analysis.
- The board discussed financial performance and the impact on shareholder value.
Page 1 of 8
Program :
instruction = """ Summarise the minutes of the meeting given below in 4 bullet points:
Car Manufacturing Company
Agenda:
1. Call to Order
2. Approval of Previous Meeting Minutes
3. CEO's Report
4. Financial Report
Meeting Minutes:
1. Call to Order:
- The meeting was called to order by Bharath Thippireddy.
3. CEO's Report:
- Bharath Thippireddy presented an overview of the company's performance, highlighting key
achievements and challenges. Key points discussed include:
- Sales figures for the last quarter.
- Progress on cost reduction initiatives.
- Highlights from recent industry events.
- The CEO answered questions from board members.
4. Financial Report:
- The Chief Financial Officer ([CFO's Name]) presented the financial report. Key financial metrics
discussed include:
- Revenue and profit margins.
- Budget vs. actual expenditure.
- Cash flow analysis.
- The board discussed financial performance and the impact on shareholder value.
"""
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "user",
"content": [
{
Page 2 of 8
"type": "text",
"text": instruction
}
]
},
],
temperature=1,
max_tokens=256,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
#print(response)
print(response.choices[0].message.content)
--------------------------------------------------------------------------------------------------------------------------------------
EX2: TEXT CLASSIFICATION
PROGRAM:
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": "Categorise the companies given : "
"Microsoft Corporation, Roche Holding AG, Apple Inc"
"Amazon.com, Inc,Pfizer Inc, JPMorgan Chase & Co."
"Johnson & Johnson, Bank of America Corporation, Industrial and Commercial Bank of China ."
}
]
},
],
max_tokens=300,
)
print(response.choices[0].message.content)
--------------------------------------------------------------------------------------------------------------------------------------
EX3: SENTIMENT ANALYSIS
PROBLEM: Given a set of Social Media Posts below, analyse the sentiment of each and classify it as
either positive, negative, or neutral. Provide a brief explanation for your classification
Page 3 of 8
1. Just got my hands on the new XPhone - absolutely loving the camera and battery life! 📸🔋
#TechLove
2. Disappointed with the XPhone. Its pricey and not much different from the last model. Expected
more. 😕 #TechTalk
3. XPhones latest update is a game-changer for mobile gaming. The graphics are just incredible! 🎮💯
4. Cant believe I waited this long for the XPhone... its underwhelming and overpriced. Back to my old
phone, I guess. 😒
5. The XPhone has exceeded my expectations. Fast, sleek, and the new AI features are a standout! 🚀
#Innovation
PROGRAM:
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": """Below are several social media posts. For each post, please analyze the sentiment
expressed and classify it as either positive, negative, or neutral. Provide a brief explanation for your
classification"
"1. Just got my hands on the new XPhone - absolutely loving the camera and battery life!
📸🔋 #TechLove
2. Disappointed with the XPhone. Its pricey and not much different from the last model. Expected
more. 😕 #TechTalk
3. XPhones latest update is a game-changer for mobile gaming. The graphics are just incredible! 🎮💯
4. Cant believe I waited this long for the XPhone... its underwhelming and overpriced. Back to my old
phone, I guess. 😒
5. The XPhone has exceeded my expectations. Fast, sleek, and the new AI features are a standout! 🚀
#Innovation"""
}
]
},
],
)
print(response.choices[0].message.content)
Page 4 of 8
{
"type": "text",
"text": "You are a cake baker"
}
]
},
{
"role": "user",
"content": [
{
"type": "text",
"text": "Get me a recipe to bake a cake"
}
]
},
],
n=3,
max_tokens=300,
)
#print(response)
for i in range(3):
print("Recipe : ", i+1)
print("**************")
print(response.choices[i].message.content)
_------------------------------------------------------------------------------------------------------------------------------------
Page 5 of 8
"type": "text",
"text": instruction
}
]
},
],
max_tokens=300,
)
print(response.choices[0].message.content)
{
"role": "user",
"content": [
{
"type": "text",
"text": "What is OpenAI"
}
]
},
]
)
except OpenAI().AuthenticationError as e:
print(f"AuthenticationError : {e}")
Page 6 of 8
print(response.choices[0].message.content)
Page 7 of 8
EX10: CREATING PROMPTS WITH MULTIPLE INPUT VARIABLES
PROBLEM: GENERATE A SUITABLE NAME FOR A COMPANY THAT MAKES A PARTICULAR PRODUCT
(USE ANY PRODUCT NAME) IN A SPECIFIC LANGUAGE. INPUTS ARE THE PRODUCT AND THE
LANGUAGE.
PROGRAM:
from langchain_openai import OpenAI
from langchain import PromptTemplate
prompt = PromptTemplate(
template = """/You are a naming consultant, give responses to the following/
question: {question}. Do not use technical words, give easy/
to understand responses. Give your response in {language}""",
input_variables = ["question", "language"]
)
#format the prompt to add variable’s values
prompt_formatted_str : str = prompt.format(
question = "Suggest a good name for a company that makes socks?",
language = "English"
)
# instantiate the OpenAI instance
llm = OpenAI(temperature = 0.9)
# make a prediction
prediction = llm.predict(prompt_formatted_str)
# print the prediction
print(prediction)
Page 8 of 8