Friday, 2 May 2025

Introducing Prompts, Responses, and the Art of Prompt Engineering

What is a Prompt?

A prompt is the input or instruction that you provide to a Generative AI model (like ChatGPT) to generate a response. Think of it as the starting point for the AI's task. Prompts can range from simple questions or statements to more complex queries or tasks, depending on what you want the model to do.

 

Example: If you want to get a summary of a text, your prompt might be: "Summarize the following article about climate change."

 

In this case, the AI uses the prompt to understand the context of the request and generate a relevant response.

 

What is a Response?

A response is the output or result generated by the AI after processing the prompt. It is the AI's interpretation and response to the instructions or query you provided. The response is based on the model's knowledge, training data, and the prompt's specificity.

 

Example: Given the prompt above, the response from the AI could be: "Climate change refers to long-term shifts in temperatures and weather patterns, primarily caused by human activity, such as burning fossil fuels."

 

The response is how the AI answers or interacts based on your input.

 

What is Prompt Engineering?

Prompt Engineering is the practice of designing and structuring prompts to get the most accurate, relevant, or useful responses from the AI. Since Generative AI models interpret prompts in specific ways, writing effective prompts requires an understanding of how the AI functions. The better the prompt, the better the response.

 

It involves techniques such as:

1.   Clarity: Being precise in your language and request.

2.   Context: Providing background information or examples to guide the AI.

3.   Iteration: Refining the prompt based on the initial response to get more detailed or specific results.

4.   Tone/Style: Structuring prompts to guide the AI on the tone (e.g., formal, casual) or format (e.g., bullet points, essay) of the response.

 

Example: Instead of just saying "Tell me about climate change," you could craft a more specific prompt like: "Can you provide a 200-word summary on the causes of climate change, focusing on human activities?"

 

 

Example of Good and Bad Prompts

 

1. Generating a Creative Story

 

Good Prompt
"Write a short, imaginative story about a robot who discovers an ancient book in a futuristic city. Include an emotional twist at the end."

 

Why it’s good?

·      Clear intent: Tells the model what type of text to generate ("a short, imaginative story").

·      Context: Includes specific details (robot, ancient book, futuristic city).

·      Expectation: Adds an extra challenge ("include an emotional twist").

 

Bad Prompt
"Write a story."

 

Why it’s bad?

·      Vague: Doesn't specify the length, topic, or tone.

·      Lack of context: The model may produce a generic or irrelevant story.

·      No direction for creativity.

 

2. Explaining a Complex Topic

 

Good Prompt
"Explain quantum computing in simple terms suitable for a 10-year-old. Use a fun example involving cookies."

Why it’s good?

·      Target audience: Specifies "suitable for a 10-year-old."

·      Simplification: Asks for an explanation in simple terms.

·      Creativity: Incorporates an example (cookies) to make the explanation engaging.

 

Bad Prompt
"What is quantum computing?"

 

Why it’s bad?

·      Too broad: Doesn’t specify the audience or tone.

·      Generic: The response might be overly technical or unhelpful for beginners.

 

 

3. Generating a Recipe

Good Prompt
"Create a vegan pancake recipe using simple ingredients. Include step-by-step instructions and tips for achieving the best texture."

 

Why it’s good?

·      Specific: Focuses on "vegan pancakes" and asks for detailed steps.

·      Adds value: Requests tips to improve results.

 

Bad Prompt
"How do I cook pancakes?"

 

Why it’s bad?

·      Ambiguous: Doesn’t specify the type of pancakes (vegan, gluten-free, etc.).

·      Lack of clarity: Results in a generic response.

 

4. Asking for Code

Good Prompt:
"Write a Python script to fetch current weather data using an API. Ensure the script includes error handling for invalid API responses."

 

Why it’s good?

·      Clear task: Specifies "fetch current weather data."

·      Adds requirements: Includes error handling as an essential feature.

 

Bad Prompt:
"Write a Python program."

 

Why it’s bad?

·      Too broad: The model doesn’t know what kind of program to write.

·      No constraints or focus: Response could be irrelevant to the user’s needs.


Previous                                                    Next                                                    Home

Thursday, 1 May 2025

Javalin: Validate path parameter

Using Context.pathParamAsClass("name", clazz), we can validate the path parameter.

Example 

Integer age = ctx
.pathParamAsClass("age", Integer.class)
.check(myAge -> myAge >= 18, "age is less than 18")
.get();

Find the below working application.

 

Step 1: Create new maven project ‘javalin-validate-path-parameter’.

 

Step 2: Update pom.xml with maven dependencies.

 

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.sample.app</groupId>
	<artifactId>javalin-validate-path-parameter</artifactId>
	<version>1.0.0</version>

	<properties>
		<packaging>jar</packaging>
		<jdk.version>17</jdk.version>
		<release.version>17</release.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>io.javalin</groupId>
			<artifactId>javalin</artifactId>
			<version>5.3.2</version>
		</dependency>

		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-simple</artifactId>
			<version>2.0.3</version>
		</dependency>


		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.14.0</version>
		</dependency>

		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-core</artifactId>
			<version>2.14.0</version>
		</dependency>


	</dependencies>

	<build>
		<plugins>

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.10.1</version>
				<configuration>
					<source>17</source>
					<target>17</target>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-assembly-plugin</artifactId>
				<version>2.6</version>
				<configuration>
					<descriptorRefs>
						<descriptorRef>jar-with-dependencies</descriptorRef>
					</descriptorRefs>
					<archive>
						<manifest>
							<addClasspath>true</addClasspath>
							<mainClass>com.sample.app.App</mainClass>
						</manifest>
					</archive>
				</configuration>
				<executions>
					<execution>
						<id>assemble-all</id>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>

Step 3: Define main application class.

 

App.java

package com.sample.app;

import io.javalin.Javalin;
import io.javalin.http.HttpStatus;

public class App {

	public static void main(String[] args) {

		Javalin javalin = Javalin.create();

		javalin.get("/", (ctx) -> {
			ctx.result("Welcome to Javalin programming!!!!");
		});

		javalin.get("/register-me/{name}/{age}", (ctx) -> {

			String name = ctx.pathParam("name");
			Integer age = ctx
					.pathParamAsClass("age", Integer.class)
					.check(myAge -> myAge >= 18, "age is less than 18")
					.get();

			ctx.result("Hello " + name + ", you are " + age + " years old").status(HttpStatus.OK);
		});

		javalin.start(8080);
	}

}

 Total project structure looks like below.


 

Build the project

Navigate to the folder, where pom.xml is located and execute the command ‘mvn package’.

 

Upon successful command execution, you can see a ‘javalin-validate-path-parameter-1.0.0-jar-with-dependencies.jar’ file.

$ls ./target/
archive-tmp
classes
generated-sources
generated-test-sources
javalin-validate-path-parameter-1.0.0-jar-with-dependencies.jar
javalin-validate-path-parameter-1.0.0.jar
maven-archiver
maven-status
test-classes

Run the application

Execute below command to run the application.

java -jar ./target/javalin-validate-path-parameter-1.0.0-jar-with-dependencies.jar

$java -jar ./target/javalin-validate-path-parameter-1.0.0-jar-with-dependencies.jar
[main] INFO io.javalin.Javalin - Starting Javalin ...
[main] INFO org.eclipse.jetty.server.Server - jetty-11.0.13; built: 2022-12-07T20:47:15.149Z; git: a04bd1ccf844cf9bebc12129335d7493111cbff6; jvm 17.0.5+9-LTS-191
[main] INFO org.eclipse.jetty.server.session.DefaultSessionIdManager - Session workerName=node0
[main] INFO org.eclipse.jetty.server.handler.ContextHandler - Started i.j.j.@3911c2a7{/,null,AVAILABLE}
[main] INFO org.eclipse.jetty.server.AbstractConnector - Started ServerConnector@5bcea91b{HTTP/1.1, (http/1.1)}{0.0.0.0:8080}
[main] INFO org.eclipse.jetty.server.Server - Started Server@42607a4f{STARTING}[11.0.13,sto=0] @400ms
[main] INFO io.javalin.Javalin - 
       __                  ___          ______
      / /___ __   ______ _/ (_)___     / ____/
 __  / / __ `/ | / / __ `/ / / __ \   /___ \
/ /_/ / /_/ /| |/ / /_/ / / / / / /  ____/ /
\____/\__,_/ |___/\__,_/_/_/_/ /_/  /_____/

       https://javalin.io/documentation

[main] INFO io.javalin.Javalin - Listening on http://localhost:8080/
[main] INFO io.javalin.Javalin - You are running Javalin 5.3.2 (released January 22, 2023).
[main] INFO io.javalin.Javalin - Javalin started in 266ms \o/

 

Test the API

Example 1: With age < 18.
curl --location --request GET 'http://localhost:8080/register-me/krishna/5'

$curl --location --request GET 'http://localhost:8080/register-me/krishna/5'
{"age":[{"message":"age is less than 18","args":{},"value":5}]}

Example 2: With age >= 18.

curl --location --request GET 'http://localhost:8080/register-me/krishna/18'

$curl --location --request GET 'http://localhost:8080/register-me/krishna/18'
Hello krishna, you are 18 years old

 You can download this application from this link.



Previous                                                    Next                                                    Home

Javalin: Get the value mapped to this path parameter

Context#pathParam("name") method return the value mapped to this path parameter.

Example 

javalin.get("/hello/{name}", (ctx) -> {
	String name = ctx.pathParam("name");
	ctx.result("Hello " + name + ", how are you?").status(HttpStatus.OK);
});

Find the below working application.

 

Step 1: Create new maven project ‘javalin-path-parameter-by-name’.

 

Step 2: Update pom.xml with maven dependencies.

 

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.sample.app</groupId>
	<artifactId>javalin-path-parameter-by-name</artifactId>
	<version>1.0.0</version>

	<properties>
		<packaging>jar</packaging>
		<jdk.version>17</jdk.version>
		<release.version>17</release.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>io.javalin</groupId>
			<artifactId>javalin</artifactId>
			<version>5.3.2</version>
		</dependency>

		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-simple</artifactId>
			<version>2.0.3</version>
		</dependency>


		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.14.0</version>
		</dependency>

		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-core</artifactId>
			<version>2.14.0</version>
		</dependency>


	</dependencies>

	<build>
		<plugins>

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.10.1</version>
				<configuration>
					<source>17</source>
					<target>17</target>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-assembly-plugin</artifactId>
				<version>2.6</version>
				<configuration>
					<descriptorRefs>
						<descriptorRef>jar-with-dependencies</descriptorRef>
					</descriptorRefs>
					<archive>
						<manifest>
							<addClasspath>true</addClasspath>
							<mainClass>com.sample.app.App</mainClass>
						</manifest>
					</archive>
				</configuration>
				<executions>
					<execution>
						<id>assemble-all</id>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>

Step 3: Define main application class.

 

App.java

package com.sample.app;

import io.javalin.Javalin;
import io.javalin.http.HttpStatus;

public class App {

	public static void main(String[] args) {

		Javalin javalin = Javalin.create();

		javalin.get("/", (ctx) -> {
			ctx.result("Welcome to Javalin programming!!!!");
		});

		javalin.get("/hello/{name}", (ctx) -> {
			
			String name = ctx.pathParam("name");
		

			ctx.result("Hello " + name + ", how are you?").status(HttpStatus.OK);
		});

		javalin.start(8080);
	}

}

Total project structure looks like below.


 

Build the project

Navigate to the folder, where pom.xml is located and execute the command ‘mvn package’.

 

Upon successful command execution, you can see a ‘javalin-path-parameter-by-name-1.0.0-jar-with-dependencies.jar’ file.

$ls ./target/
archive-tmp
classes
generated-sources
generated-test-sources
javalin-path-parameter-by-name-1.0.0-jar-with-dependencies.jar
javalin-path-parameter-by-name-1.0.0.jar
maven-archiver
maven-status
test-classes

 

Run the application

Execute below command to run the application.

 

java -jar ./target/javalin-path-parameter-by-name-1.0.0-jar-with-dependencies.jar

$java -jar ./target/javalin-path-parameter-by-name-1.0.0-jar-with-dependencies.jar
[main] INFO io.javalin.Javalin - Starting Javalin ...
[main] INFO org.eclipse.jetty.server.Server - jetty-11.0.13; built: 2022-12-07T20:47:15.149Z; git: a04bd1ccf844cf9bebc12129335d7493111cbff6; jvm 17.0.5+9-LTS-191
[main] INFO org.eclipse.jetty.server.session.DefaultSessionIdManager - Session workerName=node0
[main] INFO org.eclipse.jetty.server.handler.ContextHandler - Started i.j.j.@3911c2a7{/,null,AVAILABLE}
[main] INFO org.eclipse.jetty.server.AbstractConnector - Started ServerConnector@5bcea91b{HTTP/1.1, (http/1.1)}{0.0.0.0:8080}
[main] INFO org.eclipse.jetty.server.Server - Started Server@42607a4f{STARTING}[11.0.13,sto=0] @484ms
[main] INFO io.javalin.Javalin - 
       __                  ___          ______
      / /___ __   ______ _/ (_)___     / ____/
 __  / / __ `/ | / / __ `/ / / __ \   /___ \
/ /_/ / /_/ /| |/ / /_/ / / / / / /  ____/ /
\____/\__,_/ |___/\__,_/_/_/_/ /_/  /_____/

       https://javalin.io/documentation

[main] INFO io.javalin.Javalin - Listening on http://localhost:8080/
[main] INFO io.javalin.Javalin - You are running Javalin 5.3.2 (released January 22, 2023).
[main] INFO io.javalin.Javalin - Javalin started in 311ms \o/

 

Test the api

Execute below command to run the application.

curl --location --request GET 'http://localhost:8080/hello/krishna'

$curl --location --request GET 'http://localhost:8080/hello/krishna'
Hello krishna, how are you?

You can download this application from this link.

 

Previous                                                    Next                                                    Home