Prompt Engineering - NLP and ML Foundations

NLP and ML Foundations

Introduction to Prompt Engineering

  • Large Language Models (LLMs) have revolutionized Natural Language Processing (NLP).

  • LLMs can generate human-like text and answer questions with accuracy.

  • Techniques for writing prompts are crucial for effective LLM interaction, guiding the model’s output and enabling relevant responses.

What is NLP?

  • Natural Language Processing (NLP) is a branch of AI that enables computers to understand, generate, and manipulate human language.

  • NLP is the core technology behind virtual assistants like Oracle Digital Assistant (ODA), Siri, Cortana, or Alexa.

  • NLP enables virtual assistants to understand user requests and respond in natural language i.e., "language in."

  • Examples of NLP-powered tools include:

    • Web search

    • Email spam filtering

    • Automatic translation of text or speech

    • Document summarization

    • Sentiment analysis

    • Grammar/spell checking

    • Email programs suggesting replies based on content.

Text Generation

  • Text Generation is a popular NLP task for automatically generating natural language texts like sentences and documents.

  • Generative AI and Large Language Models (LLMs) have significantly advanced text generation.

  • LLMs are trained on vast datasets and generate coherent and contextually relevant text.

  • Popular LLMs for text generation:

    • OpenAI’s GPT series (GPT-3.5, GPT-4, etc.)

    • Google’s BERT and its variants (T5 - Text-To-Text Transfer Transformer).

  • GPT-4 excels in generating human-like text and is useful for content creation, conversation simulation, and coding.

  • GPT models predict the next word in a sentence to generate coherent passages.

Text Classification

  • Text classification in NLP involves categorizing text documents by assigning predefined labels based on content.

  • It automatically determines the class or category to which a piece of text belongs.

  • Applications include:

    • Sentiment analysis

    • Spam detection

    • Topic labeling

    • Language identification

  • Text classification algorithms analyze text features to make predictions about its category.

The Bag of Words Representation
  • Example:

    • Review : I love this movie! It's sweet, but with satirical humor. The dialogue is great and the adventure scenes are fun…. It manages to be whimsical and romantic while laughing at the conventions of the fairy tale genre. I would recommend it to just about anyone. I've seen it several times, and I'm always happy to see it again whenever I have a friend who hasn't seen it yet!

    • Word Frequency:

      • it: 6

      • I: 5

      • the: 4

      • to: 3

      • and: 3

      • seen: 2

      • are, happy, dialogue, adventure, recommend, whimsical, times, sweet, several, anyone, yet, again, humor, satirical, manages, genre, fun, about, while, whenever, have, conventions, with, great, movie, but, romantic, would, fairy, always, love, scenes, friend: 1

Named Entity Recognition

  • Named Entity Recognition (NER) is an NLP task identifying and classifying named entities in text into categories:

    • Persons

    • Organizations

    • Locations

    • Times

    • Quantities

    • Monetary values

    • Percentages

  • Example Sentence: “Pravin visited IBM in New York on Tuesday.”

    • NER would identify:

      • “Pravin” as a person

      • “IBM” as an organization

      • “New York” as a location

      • “Tuesday” as a time expression.

  • Another example:

    • Text: “Ramesh was born on January 5th, 1980 in New Delhi City. He graduated from Shivaji University in 2002 with a degree in Computer Science.”

      • Extracted Information:

        • Name: Ramesh

        • Date of Birth: January 5th, 1980

        • Place of Birth: New Delhi City

        • Education: Shivaji University, Computer Science (2002)

Question Answering

  • Question answering involves providing direct answers to questions posed by users in natural language format.

  • Virtual assistants like Siri or Alexa use question answering techniques to provide relevant responses based on user queries.

  • Example:

    • Context: The COVID-19 pandemic has affected countries worldwide, leading to widespread lockdowns and economic disruption. Vaccines have been developed and distributed to combat the virus. BNT162b2 vaccine was developed by Pfizer-BioNTech

    • Question: What is the name of the vaccine that was developed by Pfizer-BioNTech?

    • Answer: The vaccine that was developed by Pfizer-BioNTech is called BNT162b2.

  • An NLP system needs to understand the context, identify key information, and match the question with the appropriate answer.

Text Summarization

  • Text summarization condenses large amounts of text into shorter summaries while retaining important information.

  • The goal is to provide an efficient way to understand the main points of a longer piece of text.

  • Two types of text summarization:

    • Extractive: Identifying and using the most important sentences or phrases from the original text.

      • Preserves the original wording and phrasing, but can lack coherence.

    • Abstractive: Generating new sentences that convey the same meaning as the original text in a more concise manner.

      • Requires advanced NLP techniques like natural language generation and deep learning models.

  • Example of extractive summarization:

    • Original text: The COVID-19 pandemic has had a significant impact on the global economy. Many businesses have been forced to close their doors due to lockdowns and social distancing measures. The travel and hospitality industries have been hit particularly hard, with many airlines and hotels experiencing massive losses. Governments around the world have implemented stimulus packages to help support affected businesses and individuals.

    • Summary: COVID-19 has significantly impacted the global economy, with many businesses closing due to lockdowns and social distancing measures. The travel and hospitality industries have been hit hard, with airlines and hotels experiencing huge losses. Governments have implemented stimulus packages to support affected businesses and individuals.

Speech to Text

  • Speech-to-text, also known as automatic speech recognition (ASR), converts spoken language into written text format.

  • This technology is used by virtual assistants like Siri, Alexa, and Google Assistant.

  • These assistants interpret user commands or queries spoken aloud and process these verbal inputs to provide appropriate responses or actions.

Text-to-Speech

  • Text-to-speech converts written text into spoken words using computer-generated voices.

  • Example: audiobooks use NLP algorithms to convert written text into audio format.

Translation

  • Translation involves converting text from one language to another.

  • Google Translate uses NLP algorithms to translate web pages or documents in real-time.

  • Example:

    • Source Text: “Bonjour, comment ça va?”

    • Target Text: “Hello, how are you?”

  • Translation preserves the meaning and context of the original text and can be used for translating documents, websites, and social media posts.

Sentiment Analysis

  • Sentiment analysis identifies and extracts opinions and emotions from text data.

  • Example: analyzing customer reviews to determine overall satisfaction levels.

  • Example:

    • Review comment: I absolutely loved the new restaurant in town. The food was delicious and the service was excellent!

    • Sentiment Analysis Result: Positive

  • Sentiment analysis determines the emotional tone or attitude conveyed by the words used, and it can be used for understanding customer feedback, monitoring brand reputation, and analyzing social media trends.

AI Prompts

  • Well-crafted AI prompts evoke desired information, maintain context, and steer the conversation.

  • They help users harness the full potential of LLMs, leading to enhanced performance and user satisfaction.

Five Essential Techniques for Writing Prompts in LLMs:
  • Role Prompting

  • Few-Shot Prompting

  • Chain of Thought Prompting

  • Generated Knowledge Prompting

  • Self-Consistency

Role Prompting

  • Role prompting assigns a specific role to the AI model.

  • Prompts start with a role instruction like “You are a prompt engineer” or “You are a physics professor”.

  • It gives an AI model additional context to help it better understand the question.

  • It is used to obtain more efficient and accurate answers.

  • Example: Assign the role of a Gen AI professor named Dr. Pravin Mane to the language model, providing background information about Pravin's knowledge and role, goals, communication style, and behavior.

  • The language model responds to user queries as if it were Pravin, drawing upon the provided knowledge and communication guidelines.

Benefits of Role Prompting
  • Defining a clear role, persona, knowledge base, and objectives for the AI assistant can significantly enhance the quality and relevance of its outputs for the given task.

  • Role prompting helps ensure the AI responds in a consistent manner that aligns with the defined persona.

  • Careful role prompting can embed guidelines for ethics, safety, truthfulness, and alignment with human values into the AI's core persona, promoting safer and more trustworthy AI assistants.

  • Role-based AI assistants can provide tailored support and expertise in specialized fields such as healthcare, legal services, and engineering by pre-loading domain- specific knowledge and goals.

Drawbacks of Role Prompting
  • Restrictive role prompts could limit the AI's ability to engage broadly and draw upon its full knowledge and capabilities.

  • It does not provide foolproof behavioural control over LLMs. Some sufficiently capable AI prompts may find clever workarounds to satisfy the literal prompt while still engaging in unintended ways.

  • Prompts that contain factually incorrect information about the AI's characteristics could lead to inconsistencies in confusion in its responses as it attempts to fulfil that role. There is also a risk of misuse, such as writing prompts that portray the AI in deceptive or dangerous roles.

  • Heavy reliance on prompting could lead to brittleness, where the AI fails gracelessly outside of narrowly defined roles. Robust general intelligence is still needed.

Few-Shot Prompting

  • Few-shot prompting provides an AI model with a few examples of a task to learn from before generating a response.

  • The language model uses examples to improve its performance on similar tasks.

  • It showcase the desired task or output.

  • This enables the LLMs to grasp the expected behaviour or outcome, even when confronted with new, similar inputs, despite having limited context.

  • It is a valuable technique in NLP, particularly when labelled data is scarce or when quick adaptation to new tasks is required.

  • You insert examples in your prompt, training the model on what you want the output to look and sound like.

  • This method builds on LLM’s ability to learn and generalize information from a small amount of data. This makes it particularly helpful when you don’t have enough data to fine-tune a model.

  • Example: the goal of the prompt is for the LLM to determine the sentiment of the movie review.

    • The movie was good // positive

    • The movie was quite bad // negative

    • I really like the movie, but the ending was lacking // neutral

    • I LOVED the movie //

  • The approach helps the model learn what is positive, negative, or neutral.

  • It also shows our desired output format is a single word, all lowercase.

  • Generally called “N-shot prompting,” where N is the number of examples (or shots) provided to the model.

  • If we provided a single example, it would be called “one-shot prompting.”

Benefits of Few-Shot Prompting
  • It enables models to learn from minimal examples, often just a few, rather than needing extensive datasets. This enhances sample efficiency and enables quick adaptation to new tasks.

  • With few-shot prompting, the same base model can be used for many different tasks just by providing a few examples in the prompt. This flexibility means models can be rapidly adapted to novel domains and applications.

  • The few-shot technique for writing prompts improves performance on unseen tasks, known as zero-shot learning, by incorporating relevant examples in the prompt. This helps models better understand tasks and make accurate predictions for new data.

  • Humans can learn from a few examples and generalize to new tasks rapidly. Few- shot prompting helps narrow the gap between AI and human-like task flexibility and efficiency.

Drawbacks of Few-Shot Prompting
  • The quality and clarity of the writing prompts are crucial. If the examples are ambiguous, inconsistent, or fail to cover the edge cases, the result of the model will be poor.

  • There is limited ability to control the model's behaviour beyond the provided examples, which may lead to generalization.

  • It relies heavily on the base model's existing capabilities. It doesn't fundamentally expand what the model can do; it just recombines it in new ways.

  • For complex tasks, a few demonstrations may not be sufficient to fully specify the behaviour. The model can fail to infer the full scope and nuance of the task.

Chain of Thought Prompting (CoT)

  • Chain-of-thought prompting aims to improve language models' performance on tasks requiring logic, calculation and decision-making by structuring the input prompt in a way that mimics human reasoning.

  • Example:

    • A farmer with wolf, Goat and cabbage is crossing the river by boat. Boat can carry farmer and single item at a time. if left unattended wolf eat goat or goat eats cabbage. How they can cross river without eaten anything. Describe reasoning step by step.