ChatGPT Prompt Engineering for Developers | Inferring

preview_player
Показать описание
Inferring

In this lesson, you will infer sentiment and topics from product reviews and news articles.
Setup

import openai

import os



from dotenv import load_dotenv, find_dotenv

_ = load_dotenv(find_dotenv()) # read local .env file



def get_completion(prompt, model="gpt-3.5-turbo"):

messages = [{"role": "user", "content": prompt}]

model=model,

messages=messages,

temperature=0, # this is the degree of randomness of the model's output

)

Product review text

lamp_review = """

Needed a nice lamp for my bedroom, and this one had \

additional storage and not too high of a price point. \

Got it fast. The string to our lamp broke during the \

transit and the company happily sent over a new one. \

Came within a few days as well. It was easy to put \

together. I had a missing part, so I contacted their \

support and they very quickly got me the missing piece! \

Lumina seems to me to be a great company that cares \

about their customers and products!!

"""

Sentiment (positive/negative)

prompt = f"""

What is the sentiment of the following product review,

which is delimited with triple backticks?



Review text: '''{lamp_review}'''

"""

response = get_completion(prompt)

print(response)

prompt = f"""

What is the sentiment of the following product review,

which is delimited with triple backticks?



Give your answer as a single word, either "positive" \

or "negative".



Review text: '''{lamp_review}'''

"""

response = get_completion(prompt)

print(response)
Рекомендации по теме