Artificial intelligence and machine learning are transforming the way we approach problem-solving. However with an overwhelming increased adoption of LLMs, diving into classical machine learning models can sometimes feel like stepping into a labyrinth. Enter prompt engineering – a technique that allows us to give specific instructions to language models like GPT models by Open AI, Llama models by Meta, Gemini by Google and many more. It’s a powerful and accessible way to leverage AI for various tasks, from sentiment analysis to code generation. This blog will introduce you to the basics of prompt engineering, complete with Python code samples, and show how it can be applied to classical machine learning and general understanding tasks in software applications.
A prompt is essentially a piece of text or instruction that you provide to a language model to generate a response. Think of it as a starting point for the AI to understand what you want it to do. For example, if you ask, “What’s the weather like today?” you’re giving a prompt that the AI will use to generate a relevant response. Prompts can be simple questions, incomplete sentences, or more complex instructions, depending on the task at hand.
Prompt engineering involves designing and refining these prompts to guide language models in generating the desired outputs. By carefully crafting specific prompts, we can instruct the model to perform a variety of tasks without needing to build complex algorithms or train models from scratch.
To get started with prompt engineering, you’ll need access to a language model API like OpenAI’s GPT-4o. For the Python examples below, we’ll use the OpenAI API. Make sure you have the openai Python package installed and an API key.
First, let’s set up our Python environment. Install the openai package if you haven’t already:
pip install openai
Then, import the necessary libraries and set your API key:
from openai import OpenAI
# Set your OpenAI API key here
client = OpenAI(api_key="Your API key")
client.chat.completions.create()
function is part of the OpenAI Python client library and is used to interact with chat-based models like ChatGPT.
Before diving into examples, let’s understand the key parameters used in this function. These parameters help fine-tune how the AI generates responses, making it a versatile tool for various applications.
Example:
system_message = { "role": "system", "content": f"You are a translator that translates text into {target_language}." } # User message with the text to be translated user_message = { "role": "user", "content": f"Translate the following text into {target_language}:\n\n{text}" } messages=[system_message, user_message]
For this blog, I’ll focus on using simple user messages so that we stick to the objective of keeping things simple for everyone.
One common machine learning task is data classification. With prompt engineering, we can classify text data without explicitly training a model.
Let’s start with a simple sentiment analysis task where we classify text as positive, negative, or neutral.
def classify_sentiment(text):
prompt = f"Classify the sentiment of the following text as positive, negative, or neutral:\n\nText: \"{text}\"\nSentiment:"
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{
"role": "user",
"Content":prompt,
}
],
max_tokens=10,
temperature=0
)
sentiment = response.choices[0].text.strip()
return sentiment
# Example usage
text = "I love this product! It has made my life so much easier."
sentiment = classify_sentiment(text)
print(f"Sentiment: {sentiment}")
In this example, we provide the model with a prompt that asks it to classify the sentiment of a given text. The model generates the sentiment based on its training data.
Text summarization is another common task. By using prompt engineering, we can generate concise summaries of longer texts.
def summarize_text(text):
prompt = f"Summarize the following text in a few sentences:\n\nText: \"{text}\"\nSummary:"
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{
"role": "user",
"Content":prompt,
}
],
max_tokens=50,
temperature=0.5
)
summary = response.choices[0].text.strip()
return summary
# Example usage
text = "Artificial intelligence is transforming industries across the globe. By automating processes, improving decision-making, and enhancing customer experiences, AI is driving innovation and growth in ways previously unimaginable."
summary = summarize_text(text)
print(f"Summary: {summary}")
Here, the prompt instructs the model to generate a summary of the given text. The model’s output provides a concise version of the original text.
Example 3: Named Entity Recognition
Named Entity Recognition (NER) involves identifying and classifying key elements in text, such as names of people, organizations, dates, etc.
def extract_entities(text):
prompt = f"Extract the entities from the following text and list them under categories such as Person, Organization, Date, etc.:\n\nText: \"{text}\"\nEntities:"
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{
"role": "user",
"Content":prompt,
}
],
max_tokens=100,
temperature=0.3
)
entities = response.choices[0].text.strip()
return entities
# Example usage
text = "Elon Musk, the CEO of SpaceX, announced that the company would send humans to Mars by 2024."
entities = extract_entities(text)
print(f"Entities: {entities}")
In this example, the model identifies and categorizes entities within the text based on the provided prompt.
Prompt engineering can also be used to generate code snippets, helping developers save time and improve productivity.
def generate_code(task_description):
prompt = f"Generate a Python function to {task_description}.\n\nFunction:"
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{
"role": "user",
"Content":prompt,
}
],
max_tokens=150,
temperature=0.2
)
code = response.choices[0].text.strip()
return code
# Example usage
task_description = "sort a list of integers in ascending order"
code_snippet = generate_code(task_description)
print(f"Generated Code:\n{code_snippet}")
Here, we prompt the model to generate a Python function for a specified task. The model produces a code snippet based on the task description.
Prompt engineering is a powerful technique that allows you to harness the capabilities of advanced language models for a variety of tasks without the need for extensive machine learning expertise. By crafting specific prompts, you can leverage AI to perform data classification, text summarization, named entity recognition, code generation, and more. The examples provided here demonstrate how to get started with prompt engineering using Python and the OpenAI API. With these techniques, you can unlock the full potential of AI and enhance your software applications.
At IDEA Foundation, we’re constantly expanding our GenAI capabilities. This is our first blog in IDEA GenAI series, stay tuned as we deep dive into prompt engineering advance techniques and other GenAI technologies. Until then – Happy prompting!
This blog is authored by Kanu Malviya, Manager & AI Lead, IDEA Foundation