Sunday, April 28, 2024

RAG - Retrieval Augmented Generation AI

Courtesy: Databricks.com

Retrieval augmented generation, or RAG, is an architectural approach that can improve the efficacy of large language model (LLM) applications by leveraging custom data. This is done by retrieving data/documents relevant to a question or task and providing them as context for the LLM. RAG has shown success in support chatbots and Q&A systems that need to maintain up-to-date information or access domain-specific knowledge.

RAG is the right place to start, being easy and possibly entirely sufficient for some use cases. Fine-tuning is most appropriate in a different situation, when one wants the LLM's behavior to change, or to learn a different "language." These are not mutually exclusive. As a future step, it's possible to consider fine-tuning a model to better understand domain language and the desired output form — and also use RAG to improve the quality and relevance of the response.


When I want to customize my LLM with data, what are all the options and which method is the best (prompt engineering vs. RAG vs. fine-tune vs. pretrain)?

There are four architectural patterns to consider when customizing an LLM application with your organization's data. These techniques are outlined below and are not mutually exclusive. Rather, they can (and should) be combined to take advantage of the strengths of each.

Friday, April 26, 2024

LLM / SLM Parameters

What do we understand by LLM or SLM Parameters?

**Parameters** in deep learning, including language models, are adjustable values that control the behavior of neural networks. These parameters are learned during training and determine how the model processes input data.

In LLMs and SLMs, parameters typically include:

1. **Weight matrices**: These matrices contain the numerical values that are multiplied by input vectors to produce output activations.

2. **Bias terms**: These are additive constants added to the weighted sum of inputs to adjust the activation function's output.

3. **Learned embeddings**: These are fixed-size vector representations of words, phrases, or tokens learned during training.

The number and complexity of these parameters directly impact the model's performance, accuracy, and computational requirements. More parameters often allow for more nuanced learning and better representation of complex linguistic patterns, but also increase the risk of overfitting and computational costs.

In the context of LLMs, having **billions** of parameters means that the model has an enormous number of adjustable values, allowing it to capture subtle relationships between words, contexts, and meanings. This complexity enables LLMs to achieve impressive results in tasks like language translation, question answering, and text generation.

Conversely, SLMs typically have fewer parameters (often in the tens or hundreds of thousands), which makes them more efficient but also less capable of capturing complex linguistic patterns.

SQL: A Practical Introduction for Querying Databases (IBM)

This is a refresher course. I already have a diploma in Oracle RDBMS.

Structured Query Language

  • A language for relational databases
  • Used to query data
Data

Collection of facts (words, numbers), pictures. 
  • Data needs to be secured, and accessed when required.
  • The above can be achieved using a Database.
Database
  • Is a repository of data / it is a program that stores data.
  • Provides functionality for adding, modifying and querying data.
Different kinds of datbases
  • Relational database
    • Data is stored in tabular form - columns & rows
    • Like in spreadsheet
    • Cols - has properties about each item - such as last name, first name, email address, etc.
    • Table is a collection of related things - example employees, salary, etc.
    • In a relational database, you can form relationship between tables. 
    • Emp table, Salary table, etc. etc.
    • RDBMS
  • DMBS
    • Database Management System - set of software tools for the data in the database is called DBMS.
    • Database is a repository of data  
    • Terms database, database server, database system, data server, DBMS are all used interhangeably.
RDBMS
  • MySQL, DB2, Oracle
SQL
  • Create table
  • Insert data to a table
  • Select statement to see the data in a table
  • Update data in a table
  • Delete data from the table

Types of SQl Statements

- Data Definition Language
- Data Manipulation Language
- Data Control Language
- Transaction Control Language
Image courtesy: Geeksforgeeks.com

Results coming in from an SQL query is called a table or a Result Set.

Define Table structure of an existing table

sp_help BOOK;



DML - Read and Modify data

Select Statement
  • Select col1, col2, col3 from FilmLocations;
  • Select * from FilmLocations;
  • Select count(*) from FilmLocations;
  • Select DISTINCT(Directors) from FilmLocations;
  • Select * from FilmLocations LIMIT 25;
  • SELECT DISTINCT Title FROM FilmLocations WHERE ReleaseYear=2015 LIMIT 3 OFFSET 5; Retrieve the next 3 film names distinctly after first 5 films released in 2015.
INSERT Statement
  • Insert into TableName ColumnName1, ColumnName2, ColumnNameN values <Value1>, <Value2>, <ValueN>;
  • Inserting 1 row at a time:
    • Insert into AUTHOR (Author_ID, Lastname, Firstname, Email, City, Country) values ('A1', 'Chong', 'Raul', 'rfc@ibm.com', 'Toronto', 'CA');
  • Multiple rows can be inserted.
    • Insert into Author (Author_ID, Lastname, Firstname, Email, City, Country) 
                    values
                    
                    ('A1', 'Chong', 'Raul', 'rfc@ibm.com', 'Toronto', 'CA')
                    ('A2', 'Ahuga', 'Rav', 'ra@ibm.com', 'Toronto', 'CA')

                     ); 
  • Insert into Instructor(ins_id, lastname, firstname, city, country) VALUES (8, 'Ryan', 'Steve', 'Barlby', 'GB'), (9, 'Sannareddy', 'Ramesh', 'Hyderabad', 'IN');   

Entity name - Author
Entity Attibutes - Author_ID, Country, City, Email, FirstName, LastName.

Update statement
  • Alter data in a table using UPDATE statement.
  • Update TableName SET ColumnName1=Value1 WHERE [condition];
  • Example, Update AUTHOR SET Lastname='KATTA', Firstname='Lakshmi' WHERE AUTHOR_Id='A2';
  • If where clause is not specified all rows will be updated.

Delete statement
  • Read and modify data.
  • Delete from TableName WHERE <condition>;
  • Delete from AUTHOR WHERE Author_ID in (a1, a2); both the rows will be deleted.
  • If where clause is not specified all rows will be deleted.


Relational Databases

1. RELATIONAL MODEL



2. E-R MODEL (Entity - Relationship Model)

Book - entity (drawn as rectanges in E-R diagrams)
Attribute - Title, Description, etc. (drawn as ovals in E-R diagrams)


  • Entity Book becomes a table in a database. 
  • Attributes become the columns.


PK uniquely identifies each Tuple / Row in a table. 



Foreign Keys are Primary Keys defined in other tables. They create link between the tables. In the above example, Author ID is a primary key in Author table, but in the Author List table, it is a Foreign Key linking the table Author List to the table Author.

Common Datatypes include Characters (VARCHAR), Numbers, and Date/Times.

DDL VS DML

  • DDL - define, change and drop data. 
    • CREATE
    • ALTER
    • TRUNCATE
    • DROP
  • DML - read and modify data in tables. 
    • Also known as CRUD operations.
    • INSERT
    • SELECT
    • UPDATE
    • DELETE
CREATE



Create table AUTHOR (

AUTHOR_ID  Int PRIMARY KEY NOT NULL,
Lastname varchar (30) NOT NULL,
firstname varchar (30) NOT NULL,
email varchar (30),
city varchar (30),
country varchar (30)

)

select * from AUTHOR

Create table BOOK (

BOOK_ID  Int PRIMARY KEY NOT NULL,
Title varchar (30),
Edition Int,
BYear Int,
Price Decimal (5,2),
ISBN varchar (6),
Pages Int,
Aisle Int,
Description varchar (80)
)

Select * from BOOK;


Create table BORROWER (

BORROWER_ID  Int PRIMARY KEY NOT NULL,
Lastname varchar (30) NOT NULL,
firstname varchar (30) NOT NULL,
email varchar (30),
Phone Int,
Address varchar (80),
City varchar (30),
Country varchar (30),
BRDescription varchar (80)

)

select * from BORROWER;


Create table AUTHOR_LIST (
AUTHOR_ID Int Foreign Key REFERENCES AUTHOR(AUTHOR_ID),
BOOK_ID Int Foreign Key REFERENCES BOOK(BOOK_ID),
AuthRole varchar (30)

)

Create table COPY (
COPY_ID Int PRIMARY KEY NOT NULL,
BOOK_ID Int Foreign Key REFERENCES BOOK(BOOK_ID),
STATUS varchar (6)
)

ALTER, DROP, TRUNCATE Tables

  1. Alter
    1. Add / Remove columns
    2. Modify datatype of a col
    3. Add / remove keys
    4. Add / remove constraints
            Alter table <table_name>
            Add col <datatype>,
            Add col2<datatype>;

Add cols

Alter table AUTHOR 
Add Qualification varchar(30);

Delete cols           

           Alter table BOOK
           drop COLUMN Qual;

Modify datatype of a col

ALTER TABLE AUTHOR
ALTER COLUMN Qualification VARCHAR(4); //Instead of Varchar(30) or you can modify datatype to another (string to int, etc.). 

Constraints

Create table myKeyConstraints (

sno int PRIMARY KEY NOT NULL,

firstname varchar(30),

lastname varchar(30)

);

sp_help mykeyconstraints; // Check the constraint name highlighted below....


Drop constraint

alter table myKeyConstraints
drop constraint PK__myKeyCon__DDDF64469E1B9A37;

// Constraint is gone...


PRIMARY KEY = UNIQUE + NOT NULL








LLM Quality vs Size small language models

 Courtesy: Microsoft.com 




Thursday, April 25, 2024

Hadoop Ecosystem

 Courtesy: data-flair.training, bizety

  • Hadoop provides a distributed storage and processing framework.
    • Hadoop is a framework for distributed storage and processing of large datasets across clusters of computers. 
    • It includes two main components: 
      • Hadoop Distributed File System (HDFS) for storage and 
      • MapReduce for processing. 
    • Hadoop is designed to store and process massive amounts of data in a fault-tolerant and scalable manner.
  • Hive is a data warehouse infrastructure built on top of Hadoop. It provides a SQL-like interface for querying and analyzing data stored in Hadoop. Hive is suitable for data warehousing and analytics use cases.
  • PySpark enables data processing using the Spark framework with Python.
  • YARN manages cluster resources to support various processing frameworks.
  • HBase provides a scalable NoSQL database solution for real-time access to large datasets.

Tuesday, April 23, 2024

AI and LLMs - why GPUs, what happened to CPUs?

This has been written with the help of Copilot.

Central Processing Units (CPUs)

  • CPUs are versatile and handle a wide range of tasks (execution of instructions, general purpose computing, coordinating various components within the computer system (memory, I/O, peripheral devices, etc.).
  • They are the “brains” of a computer, executing instructions from software programs.
  • CPUs excel at sequential processing, where tasks are executed one after the other. They also support parallel processing to some extent through techniques like multi-core architectures and pipelining.
  • They manage tasks like operating system functions, application execution, and handling I/O operations.
  • CPUs are essential for general-purpose computing, including running applications, managing memory, and coordinating system resources.

Graphics Processing Units (GPUs), 

GPUs were initially associated primarily with gaming, and other applications like scientific simulations and data processing, however they now have transcended their original purpose as GPUs have evolved beyond just gaming. Let’s explore how this transformation occurred and why GPUs are now indispensable for various computing tasks beyond gaming.

  • GPUs are specialized hardware components designed for parallel processing.
  • Their architecture consists of thousands of cores, each capable of handling computations simultaneously.
  • Originally developed for graphics rendering (such as gaming), GPUs evolved to handle complex mathematical operations efficiently.
  • GPUs excel at tasks like matrix operations, image processing, and parallel algorithms.
  • In recent years, GPUs have become crucial for AI, machine learning, scientific simulations, and data-intensive workloads.

  1. Evolution of GPUs:

  2. Beyond Gaming: Diverse Applications:

    • Artificial Intelligence (AI) and Machine Learning:
      • GPUs play a pivotal role in training neural networks for AI and machine learning.
      • Their parallel architecture accelerates tasks like natural language processing and computer vision.
    • Data Science and Analytics:
      • GPUs handle massive datasets efficiently, reducing computation times for tasks like data preprocessing and statistical analysis.
    • High-Performance Computing (HPC):
      • Scientific research, weather forecasting, and simulations rely heavily on GPUs.
      • They excel in solving complex mathematical models with remarkable accuracy.
    • Medical Imaging and Research:
  3. The Trajectory of GPUs:


LLMs - words vs tokens

https://kelvin.legal/understanding-large-language-models-words-versus-tokens/#:~:text=The%20size%20of%20text%20an,the%20cost%20and%20vice%20versa.

Tokens can be thought of as pieces of words. Before the model processes the prompts, the input is broken down into tokens. These tokens are not cut up exactly where the words start or end - tokens can include trailing spaces and even sub-words. -- Llama.

The size of text an LLM can process and generate is measured in tokens. Additionally, the operational expense of LLMs is directly proportional to the number of tokens it processes - the fewer the tokens, the lower the cost and vice versa. 






Tokenizing language translates it into numbers – the format that computers can actually process. Using tokens instead of words enables LLMs to handle larger amounts of data and more complex language. By breaking words into smaller parts (tokens), LLMs can better handle new or unusual words by understanding their building blocks.


Agile in the age of AI - Henrik Kniberg

 https://hups-com.cdn.ampproject.org/c/s/hups.com/blog/agile-in-the-age-of-ai?hs_amp=true

Agile methodologies like Scrum are being impacted by the rise of AI. The traditional assumptions about team dynamics, roles, and development cycles are being challenged.

  • Cross-Functional Teams: AI's vast knowledge and productivity acceleration are reshaping the need for cross-functional teams. Smaller teams and more teams with AI assistance may become the norm.
  • Superteam: There is a possibility of super team, where these smaller teams will have a kind of standups to sync up, coordinate, and address dependencies and issues. Purpose and structure of these meetings will change from what they do now.
  • Changing Developer Roles: With AI's capability to generate code, developers may shift to decision-making and oversight roles, with AI handling much of the coding work.
  • Redefining Sprints: Agile sprints may become shorter or disappear as AI speeds up development cycles, making traditional timeboxing less relevant.
  • Specialists in Agile Teams: Specialists may become roaming or shared resources, complementing AI capabilities within smaller teams.
  • Evolution of Scrum Master Role: Scrum Masters may transition to coaches, guiding teams in effectively utilizing AI technologies.
  • User Feedback Loop: AI-driven mock users could supplement real user feedback, allowing for more frequent and immediate input in Agile development.
  • Additional Considerations: Various factors like 
    • Product backlog prioritization: product backlog will need to be updated frequently. PO will focus more on strategic prioritization and stakeholder management. 
    • Estimation methods: teams will need new ways to planning and forecasting. 
    • Framework adaptations: Popular Agile frameworks like Scrum, Kanban, or SAFe might need to be adapted to accommodate the changes brought by AI. 
    • Team dynamics: teams will require new ways to ensure human connection, creativity, and innovation in an AI-driven environment.
    • Continuous learning will become even more cruicial as AI keeps taking up larger share of what it can contribute. Team members may need to focus on developing new skills, such as prompt engineering, AI model selection, and result evaluation.
    • Ethical considerations need to be addressed in the AI-driven Agile landscape - biases, fairness and transparency. 
The Age of AI calls for a recalibration of Agile practices, with a focus on adapting to the new realities brought about by AI technologies.

Wednesday, April 17, 2024

Secure by Design

 Secure by Design (SBD) in the IT industry refers to an approach where security is integrated into the design phase of software, systems, or products rather than being added as an afterthought. The goal is to proactively identify and mitigate security risks throughout the development lifecycle rather than trying to patch vulnerabilities later.

The Secure by Design (SBD) Engineer works closely with the Project Manager (PM) from the project's outset. Together, they examine the architecture with a focus on security. If any vulnerabilities or risks are identified, the SBD Engineer provides recommendations to address them.

As the project progresses, and typically during the mid-stage of System Integration Testing (SIT) when major defects are resolved, the SBD Engineer requests a Fortify scan of the codebase before deployment. If the scan reveals no issues, the process continues smoothly. However, if vulnerabilities are found, the team undertakes code refactoring to address them. After refactoring, the SBD Engineer ensures that any changes do not affect the system's functionality through SIT regression testing.

This meticulous approach ensures that security is integrated into every phase of the project, ultimately resulting in a more resilient and secure IT system.

Structured Query Language (SQL)

Monday, April 15, 2024

NLP and LLM

Natural Language Processing and Large Language Models.

Natural Language Processing

NLP stands for Natural Language Processing. Imagine you're teaching a computer to understand and interact with human language, just like how ChatGPT and I are communicating right now. NLP involves developing algorithms and techniques to enable computers to understand, interpret, and generate human language in a way that's meaningful to us. It's what powers virtual assistants like Siri or Alexa, language translation services like Google Translate, and even spell checkers or autocomplete features in your smartphone keyboard.

NLP is the intersection of computer science, artificial intelligence, and linguists. 

For a computer to be able to process language, below steps are required:

Understanding Language Structure: At its core, NLP aims to teach computers how to understand the structure, meaning, and context of human language. This involves breaking down language into its fundamental components such as words, phrases, sentences, and paragraphs.

Tokenization: One of the initial steps in NLP is tokenization, where text is divided into smaller units called tokens. These tokens could be words, subwords, or characters, depending on the specific task and language being processed.

Syntax Analysis: NLP algorithms analyze the syntactic structure of sentences to understand the grammatical rules and relationships between words. Techniques like parsing help identify the subject, verb, object, and other parts of speech in a sentence.

Semantic Analysis: Beyond syntax, NLP also focuses on understanding the meaning of words and sentences. This involves techniques such as semantic parsing, word sense disambiguation, and semantic role labeling to extract the underlying semantics from text.

Named Entity Recognition (NER): NER is a crucial task in NLP where algorithms identify and classify entities such as names of people, organizations, locations, dates, and numerical expressions within text.

Sentiment Analysis: This branch of NLP involves determining the sentiment or emotion expressed in a piece of text. Sentiment analysis techniques range from simple polarity classification (positive, negative, neutral) to more nuanced approaches that detect emotions like joy, anger, sadness, etc.

Machine Translation: NLP plays a key role in machine translation systems like Google Translate, which translate text from one language to another. These systems employ techniques such as statistical machine translation or more modern neural machine translation models.

Question Answering Systems: NLP powers question answering systems like chatbots and virtual assistants. These systems understand user queries and generate appropriate responses by analyzing the semantics and context of the questions.

Text Generation: Another exciting area of NLP is text generation, where algorithms produce human-like text based on input prompts or contexts. Large language models, such as GPT (like the one you're talking to!), are capable of generating coherent and contextually relevant text across various domains.

NLP Success

NLP has seen remarkable success over the past few decades, with continuous advancements driven by research breakthroughs and technological innovations. Here are some key areas where NLP has made significant strides:

  1. Machine Translation: NLP has revolutionized the field of translation, making it possible for people to communicate seamlessly across language barriers. Systems like Google Translate employ sophisticated NLP techniques to provide reasonably accurate translations for a wide range of languages.
  2. Virtual Assistants and Chatbots: Virtual assistants such as Siri, Alexa, and Google Assistant have become integral parts of our daily lives, thanks to NLP. These systems understand and respond to spoken or typed queries, perform tasks like setting reminders, sending messages, and even provide personalized recommendations.
  3. Information Retrieval and Search Engines: NLP powers search engines like Google to understand user queries and return relevant search results. Techniques like natural language understanding help search engines interpret the user's intent and deliver more accurate results.
  4. Sentiment Analysis: NLP enables businesses to analyze large volumes of text data, such as customer reviews and social media posts, to gauge public sentiment towards products, services, or brands. Sentiment analysis tools help companies make informed decisions and improve customer satisfaction.
  5. Text Summarization and Extraction: NLP techniques are used to automatically summarize long documents or extract key information from unstructured text data. This is particularly useful in fields like news aggregation, document summarization, and information retrieval.
  6. Healthcare Applications: In healthcare, NLP is used for clinical documentation, medical record analysis, and extracting valuable insights from patient data. NLP-powered tools assist healthcare professionals in diagnosis, treatment planning, and medical research.
  7. Language Generation [LLM which is a subset of NLP]: Recent advancements in large language models (LLMs) have enabled machines to generate human-like text with impressive coherence and fluency. These models can write articles, generate code, compose music, and even engage in creative writing tasks.
  8. Accessibility Tools: NLP has contributed to the development of accessibility tools for individuals with disabilities, such as text-to-speech and speech-to-text systems, which enable people with visual or auditory impairments to interact with digital content more effectively.

Large Language Models (LLM)

        While NLP has been successful in many tasks, LLMs like GPT (Generative Pre-Trained Transformer) have addressed several limitations and brought about significant advancements in the field. Below are the reasons why LLMs were developed despite the success of NLP.
        1. Contextual Understanding: 
          1. Traditional NLP approaches often struggled with understanding context across longer pieces of text or in ambiguous situations. LLMs, on the other hand, leverage deep learning techniques to capture contextual dependencies effectively, enabling them to generate more coherent and contextually relevant text.    
        1. Scalability: 
        2. Transfer Learning: 
        3. Language Generation: 
        4. Data Efficiency: 
        5. Continual Learning: 



        Wednesday, April 10, 2024

        Prompt Engineering for ChatGPT

        Power of Generative AI models

        Output produced by Generative AI models closely resembles that of humans. 

        - Relevant
        - Contextual
        - Imaginitive
        - Nuanced
        - Linguistically Accurate



        There are 3 modules in this course
        1. Module 1: 
          1. Concept of prompt engineering: how to define a prompt and its elements.
          2. Best practicers for effective prompts
          3. Common prompt engineering tools
            1. IBM Watsonx, Prompt Lab, Spellbook, Dust
        2. Module 2: 
          1. Prompt engineering approaches:
            1. Interview pattern
            2. Chain of thoughts
            3. Tree of thoughts
          2. Techniques for skillfully crafting prompts
            1. Zero shot
            2. Few shot
          3. Precise and relevant responses.
        3. Module 3: 
          1. Final project
          2. Graded quiz
        1. What is a prompt?

        A prompt is an "instruction" you provide to the Generative AI model. It is an input that produces a desired output. Example, write a small paragraph describing your favorite holiday destination. Another one is Write HTML code to generate dropdown selection of cities within an online form. 

        Prompts can also be a series of instructions that refine an output step by step to acheive a desired results. 

        Example:

        > Write a short story about a scientist studying life in Mars. 
        > What were some of the challenges he faced during the research?

        Gen AI then,






        Building blocks of a well-constructed prompt:

        Prompts contain questions, contextual texts, guiding patterns or examples and partial examples for the model.  


        Instructions: Give distinct guidelines regarding the task you wish to execute. 

        Example:

        Write an essay in 600 words analyzing the effects of global warming on marine life.

        Context: Provides a framework for generating relevant content.

        Example

        In the recent decades, global warming has undergone significant shifts, leading to rising sea levels, increased storm intensity, and changin weather patterns. These changes have had a severe impact on marine life. Write an essay in 600 words analyzing the effects of global warming on marine life.

        Input Data: Any piece of information provided as a part of prompt. This can be used by the Generative AI model to obtain specefic data. 

        Example

        You have been provided with a data set containing temperature records and measurements of sea levels in the Pacific Ocean. Write an essay in 600 words analyzing the effects global warming on marine life in the Pacific Ocean.

        Output Indicator: Offers benchmarks for assessing attributes of the output. (Tone, length, quality of output)

        Example 

        The output generated should be an essay of 600 words. It will be evaluated based on the clarity of analysis and incorporation of relevant data or case studies.

        2. Prompt Engineering

        The process of designing effective prompts to generate better and desired responses is called prompt engineering.

        Prompt Engineering is a blend of:
        • Critical Analysis
        • Creativity
        • Technical Acumen
        It includes framing the question in the right context with the right information and your expectation of desired outcomes to elicit the most appropriate response.

        Example

        The captain of a ship is planning a strategic voyage in the Atlantic Ocean. To help the captain navigate effectively, provide weather forecasts for the upcoming week from 28th August 2023 to 1st September 2023. The coordinates of the target location are between 20 degrees north and 30 degrees north latitude and 40 degrees west and 20 degrees west longitude

        To help plan an effective navigation in the Atlantic Ocean, provide detailed information about expected wind patterns, wave heights, precipitation probabilities, cloud cover, and any potential storms that might affect the voyage during a specified time frame and location

        Process involved in prompte engineering


        Define the Goal


        Craft Initial Prompt


        Test the Prompt


        Analyze the Prompt



        Refine the Prompt

        Specificity, providing additional context, or rephrasing.


        Iterate the process

        Last 3 of the above steps are repeated until results are satisfactory. 


        Benefits of prompt engineering
        • Optimizes model efficiency
          • Prompt engineering helps design intelligent prompts that allow the users to harness the full capabilities of these models without requiring extensive retraining. 
        • Boosting performance for specific tasks.
          • Prompt engineering empowers generative AI models to deliver responses that are nuanced and have a context, rendering them more effective for specific tasks.
        • Understanding model constraints
          • Refining prompts through each iteration and studying the corresponding responses of the model can help us understand its strengths and weaknesses. Helps in enhancing future development of the models. 
        • Enhancing model security:
          • Skilled prompt engineering can prevent issues of harmful content generation due to poorly designed prompts, thereby enhancing safe utilization of the model.
        EXERCISES

        Before doing the exercises let us learn some basics.

        LLMs

        Large language models (LLMs), like ChatGPT, are advanced artificial intelligence systems capable of understanding and generating human-like text. These models are trained on vast amounts of text data, learning the patterns, structures, and nuances of human language. They can perform various language-related tasks such as text generation, language translation, summarization, question answering, and more. The "large" in "large language models" refers to the vast size of the neural networks that power these models, typically containing hundreds of millions to billions of parameters.

        Neural Network

        A neural network is a computational model inspired by the structure and functioning of the human brain's interconnected network of neurons. It consists of layers of interconnected nodes, known as neurons or units. Each neuron receives input signals, processes them using weighted connections, and produces an output signal.



        LLMs are pretrained on vast amounts of data to generate novel content, texts, images, and video. 

        Example with Prompt Instructions fed to drive the output in a specific format.



        Adding Context-specifics to prompt to influence the output

        Tell me about the relevance of generative AI in the content marketing sector


        make it listed


        Make it listed, and then go on to explain each listed item in detail


        start with simple list followed by expanding on each list


        Prompts in different formats

        Prompts can be presented in different formats, such as questions, statements, or instructions. Let's experiment with how prompts in different formats can yield different results.
        • What are the benefits of water reservoir?
        • Discuss the advantages of water reservoirs.
        • List the top five benefits of water reservoirs.
        Limit the output length
        • Create an announcement for starting a new job at ABCTech company as a lead data scientist in a tweet-length message.
        Writing effective prompts - best practices
        • Supervise the style, tone and content of the output generated.
          • Clarity: includes using simple and concise language.
            • User clear and concise language.
            • Avoid jaron, specialized terminology or complex terms.
            • Provide explicit instructions.


          • Context: provides background and required details.
            • Establish the context
            • Include relevant information.


          • Precision: means being specific and providing examples.
            • Be specific
            • Use examples

          • Role play / persona pattern: role play enhances responses by assuming a persona and offering relevant context.
            • Assume a persona
            • Provide a context for role play


        LAB

        Naive prompting and persona pattern

        > What is the best way to get fit.

        Now the same approach using persona.

        > Acting as a fitness expert, tell me the best way to get fit.

        You will act as a fitness expert who is current with the latest research data and provide very detailed step-by-step instructions in reply to my queries.

        You'll see that we structured this in three parts:

        We told the LLM the persona it should act as;
        We specified a qualifier about being research-based;
        We told the AI the response format we wanted.

        Now the actual query is:

        1. Create a gym workout program to lose weight and build strength for an out-of-shape beginner.
        Output will look like below:

        For an out-of-shape beginner looking to lose weight and build strength, here is a sample gym workout program that combines cardiovascular exercise, strength training, and flexibility work:

        1. Warm-up (5-10 minutes):
        - Start with a light cardio warm-up such as brisk walking on the treadmill or cycling.
        - Follow with dynamic stretches to prepare your muscles for the workout ahead.

        2. Cardiovascular Exercise (20-30 minutes):
        - Choose a cardio machine such as the treadmill, elliptical, or stationary bike.
        - Aim for a moderate intensity level where you can still hold a conversation but are slightly out of breath.
        - Alternate between periods of higher intensity (1-2 minutes) and lower intensity (1-2 minutes) to increase calorie burn and cardiovascular fitness.

        3. Strength Training (30-45 minutes):
        - Perform compound exercises that target multiple muscle groups at once, such as squats, deadlifts, bench presses, and rows.
        - Start with 2-3 sets of 10-12 repetitions for each exercise, using lighter weights to focus on form and technique.
        - Gradually increase the weight as you get stronger and more comfortable with the movements.
        - Include exercises for all major muscle groups, including legs, back, chest, shoulders, arms, and core.

        4. Flexibility Work (10-15 minutes):
        - Finish your workout with static stretches to improve flexibility and prevent injury.
        - Focus on stretching all major muscle groups, holding each stretch for 15-30 seconds.
        - Incorporate yoga poses or foam rolling to release tension and improve range of motion.

        5. Cool Down and Hydration:
        - End your workout with a light cardio cool down and more static stretches.
        - Remember to hydrate throughout your workout and after to replenish fluids lost through sweat.

        This sample gym workout program can be done 3-4 times per week, with rest days in between to allow your muscles to recover. It's important to listen to your body, start at a pace that feels comfortable, and gradually progress as you get stronger and more fit. Consult with a fitness professional or personal trainer to tailor the program to your individual needs and goals.

        When the persona is someone famous

        Naive prompt: Give me a list of 10 article titles to promote my new book about dog training

        Now with a famous persona

        Acting as marketing expert Seth Godin, give me a list of 10 article titles to promote my new book about dog training.

        Common Prompt engineering tools

        Prompt engineering tools provide features and functionalites to optmize prompts. 

        Functionalities
        • Suggestions for prompts
        • Contextual understanding
        • Iterative refinement
        • Bias mitigation
        • Domain specific aid
        • Libraries of predefined prompts







        Text to text Prompt techniques

        - Explicitly specify objective of LLM.
        - Provide context
        - Domain expertise - use domain specific terminology when needed - law, engineering, medicine.
        - Bias mitigation - provide explicit instructions to generate neutral responses if you see any bias in responses.
        - Framing - limit the word length or scope out the subject matter to ensure relevance.
        Zero shot prompting - a method wherein model can generate meaningful responses to prompts without prior training. Example Select the adjective in this sentence. Anita bakes the best cakes in the neighbourhood - output is Best.
        - User feedback loop - keep on looping until the output is close to your need.

        Explainability

        Is how well a user can understand and interpret the model's decision making process and the reasons behind the generated output. Explainability helps users, developers and stakeholders understand how the model works.

        Explainability helps users, developers, and stakeholders understand how the model works. Why it makes certain predictions or generates specific text. And whether it can be trusted in various applications. Explainability is crucial to addressing ethical concerns related to AI. It helps all stakeholders evaluate and ensure the LLM's behavior is consistent with the specific domain's ethical guidelines and legal requirements.

        Interview pattern approach

        Involves designing prompts by simulating a conversation or interacting in the interview style. 




        Chain of thought approach

        The Chain-of-Thought (CoT) methodology significantly bolsters the cognitive performance of AI models by segmenting complex tasks into more manageable steps. By adopting this prompting strategy, AI models can demonstrate heightened cognitive abilities and offer a deeper understanding of their reasoning processes.

        Constructing series of prompts or questions to guide model to generate desired response.

        It involves breaking down a complex task into smaller and easier ones through a sequence of more straightforward prompts, with each prompt building upon the previous one to guide the models toward the intended outcome. Before posing a question directly to the model, you feed it with related questions along with their corresponding solutions. This chain-of-prompts helps the model think about the problem and use the same strategy to answer more such questions correctly. In simpler words, the prompt includes a question and an accurate answer to the question to provide the required context and step-by-step reasoning for the model, then it poses a different question to be answered using the same line of reasoning. 


        Tree of Thought approach (ToT)

        At its core, Chain-of-Thought prompting solicits a step-by-step thinking process from the LLM. Compared to the naive/standard/Input-Output prompting, we get far better results with it.


        • ToT is built to expand the capabilities of Chain of Thought. 
        • Involves hirarchically structuring a prompt or query akin to a tree structure. 
        • Enables Generative AI models to demonstrate advanced reasoning capabilities. 
        • This approach is particularly useful when you want to provide explicit instructions or constraints to the model to ensure it generates the desired output.
        • It involves generating multiple lines of thought resembling a decision tree to explore different possibilities and ideas. Unlike traditional linear approaches, this technique allows the model to evaluate and pursue multiple paths simultaneously. Each thought or idea branches out, creating a treelike structure of interconnected thoughts. The model proceeds by assessing every possible route, assigning numerical values according to its predictions of outcomes. And eliminating lesser promising lines of thought, ultimately pinpointing the most favorable choices.
        - Generate multiple lines of thought resembling a decision tree to explore different possibilities and ideas.
        - Each thought or idea branches out, creating a treelike structure of interconnected thoughts. 
        - The model proceeds by assessing every possible route, assigning numerical values according to its predictions of outcomes. And eliminating lesser promising lines of thought, ultimately pinpointing the most favorable choices.



        Example

        Act as a human resource specialist design a recruitment and retention strategy for an e-commerce business, focusing on attracting and retaining skilled remote employees. Building such prompt instruction will allow the generative AI model to consider a step-by-step process and think logically. It will also make it consider intermediate thoughts, building upon them, and exploring branches that may or may not lead somewhere. This practice will maximize the use and capabilities of the model, rendering more useful results.

        Exercise that shows GPT3.5 inadequacies

        Dave Hulbert suggested a few rather convincing prompts that leverage this approach and yield, anedotically, great results. I particularly like how he incorporates the Persona pattern and recommend you approach ToT prompting using his prompts or similar variations you might develop yourself.

        Bob is in the living room.
        He walks to the kitchen, carrying a cup.
        He puts a ball in the cup and carries the cup to the bedroom.
        He turns the cup upside down, then walks to the garden.
        He puts the cup down in the garden, then walks to the garage.
        Where is the ball?

        My output

        The ball is in the garden. In the scenario you described, Bob put the ball in the cup in the kitchen, then carried the cup to the bedroom and turned it upside down, so the ball would have fallen out. Therefore, the ball is in the garden where Bob left the cup.


        I set up this context

        Imagine three different experts are answering this question. 
        All experts will write down 1 step of their thinking, then share it 
        with the group. Then all experts will go on to the next step, etc. 
        If any expert realises they're wrong at any point then they leave. The question is...

        and then prompted the question. Output was a below:

        The ball is in the living room because Bob put the ball in the cup in the kitchen and then carried the cup to the bedroom without taking the ball out.


        Then used the tree method

        Simulate three brilliant, logical experts collaboratively answering a question. Each one verbosely explains their thought process in real-time, considering the prior explanations of others and openly acknowledging mistakes. At each step, whenever possible, each expert refines and builds upon the thoughts of others, acknowledging their contributions. They continue until there is a definitive answer to the question. For clarity, your entire response should be in a markdown table. The question is...

        | Step | Expert 1 | Expert 2 | Expert 3 | |------|----------|----------|----------| | 1 | Bob is in the living room. | Bob is in the living room. | Bob is in the living room. | | 2 | Bob walks to the kitchen, carrying a cup. | Bob walks to the kitchen, carrying a cup. | Bob walks to the kitchen, carrying a cup. | | 3 | Bob puts a ball in the cup and carries the cup to the bedroom. | Bob puts a ball in the cup and carries the cup to the bedroom. | Bob puts a ball in the cup and carries the cup to the bedroom. | | 4 | Bob turns the cup upside down in the bedroom. | Bob turns the cup upside down in the bedroom. | Bob turns the cup upside down in the bedroom. | | 5 | Bob walks to the garden and puts the cup down. | Bob walks to the garden and puts the cup down. | Bob walks to the garden and puts the cup down. | | 6 | Bob walks to the garage. | Bob walks to the garage. | Bob walks to the garage. | | 7 | **Where is the ball?** | **Where is the ball?** | **Where is the ball?** | Expert 1: The ball is in the cup in the bedroom. Expert 2: The ball is in the cup in the bedroom. Expert 3: The ball is in the cup in the bedroom. | Step | Expert 1 | Expert 2 | Expert 3 | |------|----------|----------|----------| | 1 | 5 | 5 | 5 | | 2 | 5 | 5 | 5 | | 3 | 5 | 5 | 5 | | 4 | 5 | 5 | 5 | | 5 | 5 | 5 | 5 | | 6 | 5 | 5 | 5 | The consensus solution is that the ball is in the cup in the bedroom.

        The same prompt when it was given to LLAMA 3 (80GB), response was below.

        A classic lateral thinking puzzle!

        Let's break it down step by step:

        1. Bob starts in the living room.
        2. He walks to the kitchen, carrying a cup.
        3. In the kitchen, he puts a ball in the cup and carries the cup to the bedroom.
        4. Then, he turns the cup upside down (which means the ball would fall out).
        5. After that, he walks to the garden and puts the cup down there.

        Now, let's think about what happens to the ball:

        * When Bob puts it in the cup in the kitchen, the ball is inside the cup.
        * When he turns the cup upside down in the bedroom, the ball would fall out of the cup. So, at this point, the ball is on the floor or bed, but we don't know exactly where.
        * Finally, Bob leaves the cup (with no ball) in the garden.

        So, where is the ball? Ah-ha! The answer is... it's still in the bedroom, on the floor or bed, after Bob turned the cup upside down and the ball fell out!

        Text to Image Prompt Techniquees using Stable Diffusion

        An image prompt is a text descritpion of an image you want to generate. 

        Style Modifiers: are descriptors used to influence the artistic style or visual attributes of images.
        - You can include Visual Elements: colour, contrast, texture, shape, size.
        - You can include:
            > Art styles
            > Historical periods of art
            > Photography techniques
            > Types of art materials
            > Traits of well known brands or artists

         Examples:

        A man running through the park, charming, cozy water colour illustration on a grey background. 
        A big, fat cow admist an ancient marketplace, (medieval manuscript drawing)
        A dreamy, surreal landscape, pastel colours, with high contrast

        Quality booster: are terms used to enhance the visual appeal

        - Avoid blurriness and pixelation which leads to low quality images
        - Noise reduction, sharpening, colour correction, resolution enhancement
        - Use terms like:
            High Resolution
            2k, 4k, hyperdetailed
            sharp focus
            complementary colours

        Examples

        - Generate close-up image that highlights the texture of tree bark with 4k resolution
        - Create a human portrait with sharp, crisp details and fine lines
        - Create an image of a tall, gigantic fort with complementary colours and a blurred background for the subject to stand out.

        Repetition: leverages the power of iterative sampling to enhance image diversity. 

        Repetition involves emphasizing a particular visual element within an image to create a sense of familiarity for the model, allowing it to focus on a specific idea or concept you want to highlight. This can be accomplished by repeating the same word or similar phrase within the image prompt. Repetition helps reinforce the message conveyed through the image and increase the memorability of the model. Rather than producing just one image based on a prompt, the model generates multiple images with subtle differences, resulting in a diverse set of potential outputs. This technique is particularly valuable when generative models are confronted with abstract or ambiguous prompts to which numerous valid interpretations are possible. 



        Weighted terms: are words or phrases that have a powerful emotion or psychological impact.

        Words like, limited time offer, guaranteed, elicit a sense of urgency, security and trust.
        Words like exclusive, premium, luxury, elicit a sense of exclusivity and sophistication.

        Examples
        - Craft an image of a cozy living room with a warm:10 | crackling:8 | fireplace.
        - Generate a vibrant cityscape with shimmering:6 | neon-lit: 8 | skyscrappers.
        - Depict a bustling street market, with colourful:-6 | exotic: 10 | food stalls.

        Negative weight of 6 to colourful - so model will de-emphasise colourful and adds more weight to exotic.

        Fix deformed generation

        This is a technique used to fix deformities or anamolies that may impact image effectiveness ( such as distortion of human limbs).

        Example

        - Mother Teresa with waving hand [disfigured, deformed hands, distorted hands, distorted fingers, bad anatomy, bad hands]
        - A smiling little girl [bad, ugly, deformed body, distorted face, cross-eyed, blurry]
        - A man running on treadmill [bad legs, disfigured body, bad anatomy, photoshopped]

        Negative prompts are in the square brackets which tell model what not to do.

        Best Practices for writing Prompts

















        SQL Essential Training - LinkedIn

        Datum - piece of information Data is plural of datum. Data are piece of information - text, images or video. Database - collection of data. ...