FUNDAMENTAL OF GENERATIVE AI UNIT III.pptx

MalathyN5 1 views 15 slides Oct 30, 2025
Slide 1
Slide 1 of 15
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15

About This Presentation

FUNDAMENTAL OF GENERATIVE AI UNIT III.pptx


Slide Content

Fundamentals of Generative AI and Applications UNIT 3 Prompting for Problem-Solving & Code Generation Dr. Malathy N Assistant Professor J.N.N Institute of Engineering

UNIT 3 Prompting for Problem-Solving & Code Generation Debugging and code explanation using LLMs - Algorithm design and optimization-Simulation automation – MATLAB-Python prompts Hands on Practice 1: Debugging and explaining code with prompts. 2: Algorithm design & optimization using LLMs. 3: Engineering simulation prompts (MATLAB/Python).

Understanding Code Generation Large Language Models (LLMs) are increasingly utilized in software development for tasks such as debugging and code explanation. Large Language Models (LLMs) have revolutionized many aspects of technology. One of the most exciting is their ability to generate code. These models, trained on vast datasets of text and code, can produce snippets, functions, or even entire programs based on natural language prompts. This capability democratizes software development, allowing individuals with limited coding experience to automate tasks or prototype applications.

Code generation : You can use LLMs for tasks like code generation, debugging, refactoring, and even simulating servers. Reduce complexity : AI chatbots can reduce time spent on repetitive tasks, such as adding comments or optimizing inefficient scripts. Memory and multi-file handling : Chatbots can handle conversations and code from multiple files, simulating databases, web servers, and more. Step-by-step help : This article walks you through using LLMs for code-related tasks with practical examples.

Step-by-Step Debugging Process 1. Initial Code Generation and Testing Start by generating the code using a clear and concise prompt. After obtaining the code, test it thoroughly with various inputs to identify any issues. 2. Identifying Errors When the code doesn’t behave as expected, identify the type of error. Common errors include: Syntax Errors Incorrect use of the programming language’s syntax. Runtime Errors Errors that occur during the execution of the code (e. G. , division by zero, index out of bounds). Logical Errors The code runs without crashing but produces incorrect results.

Step-by-Step Debugging Process 3. Refining the Prompt Often, the easiest way to fix errors is to refine the prompt. Be more specific about the desired behavior and constraints. Original Prompt “Write a Python function to calculate the factorial of a number.” Refined  Prompt “Write a Python function to calculate the factorial of a non-negative integer. Handle invalid inputs by returning an error message.” Regenerate the code using the refined prompt:

Step-by-Step Debugging Process 4. Analyzing the Generated Code Carefully review the generated code to grasp its logic. Use debugging tools (e. G. , print statements, debuggers) to trace the execution and identify the source of errors. 5. Iterative Refinement Debugging code generated by LLMs is often an iterative process. You may need to refine the prompt, review the code. Test it multiple times to achieve the desired result. Document your changes and observations to track your progress. 6. Using Constraints and Examples in Prompts Adding constraints and examples to your prompts can significantly improve the quality of the generated code. Example Prompt Write a Python function called  calculate_sum  that takes a list of numbers as input and returns the sum of the numbers. The function should handle empty lists by returning 0. Provide an example:  calculate_sum ([1, 2, 3]) should return 6

Real-World Applications and Use Cases Automating Repetitive Tasks Generating scripts to automate tasks such as data processing, file manipulation, or system administration. Prototyping Applications Quickly creating prototypes of software applications to test ideas and gather feedback. Code Completion and Suggestion Assisting developers by suggesting code snippets or completing entire functions. Educational Purposes Helping students learn programming by generating code examples and providing explanations. Generating Test Cases Automating the creation of test cases to ensure code quality.

Code Generation PROMPT Act as a junior Python programmer. Please write the code to generate madlibs from 3 arrays of 6 items each: prepositions, verbs, and nouns, with the nouns also being used as direct objects. After writing it, execute the code. AI OUTPUT import random prepositions = ['at', 'in', 'on', 'with', 'under', 'over'] verbs = ['run', 'jump', 'dance', 'sing', 'laugh', 'cry'] nouns = ['cat', 'dog', 'tree', 'flower', 'book', 'computer'] preposition = random.choice (prepositions) verb = random.choice (verbs) noun = random.choice (nouns) madlib = f"I {verb} {preposition} the {noun}." print( madlib )

Debugging and explaining code with prompts - Hands on Practice "Write a C program to add two integers entered by the user." "Generate a C program that calculates the area of a circle given its radius." "Create a C program to convert Celsius to Fahrenheit. "Write a C program to print the first 10 natural numbers using a for loop." "Generate a C program to check if a number is prime." "Create a C program to print the multiplication table of a given number." "Write a C program to find the largest element in an array." "Generate a C program to reverse a string entered by the user." "Create a C program to count the number of vowels in a string." "Write a C program to calculate factorial using recursion." "Generate a C program with a function that checks if a number is even or odd." "Create a C program to find the GCD of two numbers using a function." "Write a C program to read and display the contents of a text file." "Generate a C program to write user input to a file." "Create a C program to count the number of lines in a file."

🐞 Debugging Practice Snippets in C #include < stdio.h > int main() { int a, b, c; float average; printf ("Enter three numbers: "); scanf ("%d %d %d", &a, &b, &c); average = a + b + c / 3; printf ("Average = %f\n", average); return 0; }

2. 🔁 Prime Number Check (Logic + Syntax Error) #include < stdio.h > int main() { int num, i , flag = 0; printf ("Enter a number: "); scanf ("%d", &num); for ( i = 2; i <= num/2; i ++) { if (num % i == 0) flag = 1; break; } if (flag == 0) printf ("%d is a prime number.\n", num); else printf ("%d is not a prime number.\n", num); return 0; }

3. 📊 Reverse a String (Runtime Error) #include < stdio.h > #include < string.h > int main() { char str[10]; int i , len ; printf ("Enter a string: "); gets(str); len = strlen (str); for ( i = len ; i >= 0; i --) printf ("%c", str[ i ]); return 0; }

4. 📁 File Read (File Handling Error) #include < stdio.h > int main() { FILE * fp ; char ch ; fp = fopen ("data.txt", "r"); while ( ch != EOF) { ch = fgetc ( fp ); printf ("%c", ch ); } fclose ( fp ); return 0; }

5. 🧮 Factorial Using Recursion (Base Case Error) #include < stdio.h > int factorial(int n) { if (n == 1) return 1; else return n * factorial(n - 1); } int main() { int num; printf ("Enter a number: "); scanf ("%d", &num); printf ("Factorial = %d\n", factorial(num)); return 0; }
Tags