• About
  • Advertise
  • Privacy & Policy
  • Contact
Ai News
Advertisement
  • Home
    • Home – Layout 1
    • Home – Layout 2
    • Home – Layout 3
    • Home – Layout 4
    • Home – Layout 5
    • Home – Layout 6
  • News
    • All
    • Business
    • Politics
    • Science
    • World
    Hillary Clinton in white pantsuit for Trump inauguration

    Hillary Clinton in white pantsuit for Trump inauguration

    Amazon has 143 billion reasons to keep adding more perks to Prime

    Amazon has 143 billion reasons to keep adding more perks to Prime

    Shooting More than 40 Years of New York’s Halloween Parade

    Shooting More than 40 Years of New York’s Halloween Parade

    These Are the 5 Big Tech Stories to Watch in 2017

    These Are the 5 Big Tech Stories to Watch in 2017

    Why Millennials Need to Save Twice as Much as Boomers Did

    Why Millennials Need to Save Twice as Much as Boomers Did

    Doctors take inspiration from online dating to build organ transplant AI

    Doctors take inspiration from online dating to build organ transplant AI

    Trending Tags

    • Trump Inauguration
    • United Stated
    • White House
    • Market Stories
    • Election Results
  • Tech
    • All
    • Apps
    • Gadget
    • Mobile
    • Startup
    The Legend of Zelda: Breath of the Wild gameplay on the Nintendo Switch

    The Legend of Zelda: Breath of the Wild gameplay on the Nintendo Switch

    Shadow Tactics: Blades of the Shogun Review

    Shadow Tactics: Blades of the Shogun Review

    macOS Sierra review: Mac users get a modest update this year

    macOS Sierra review: Mac users get a modest update this year

    Hands on: Samsung Galaxy A5 2017 review

    Hands on: Samsung Galaxy A5 2017 review

    The Last Guardian Playstation 4 Game review

    The Last Guardian Playstation 4 Game review

    These Are the 5 Big Tech Stories to Watch in 2017

    These Are the 5 Big Tech Stories to Watch in 2017

    Trending Tags

    • Nintendo Switch
    • CES 2017
    • Playstation 4 Pro
    • Mark Zuckerberg
  • Entertainment
    • All
    • Gaming
    • Movie
    • Music
    • Sports
    The Legend of Zelda: Breath of the Wild gameplay on the Nintendo Switch

    The Legend of Zelda: Breath of the Wild gameplay on the Nintendo Switch

    macOS Sierra review: Mac users get a modest update this year

    macOS Sierra review: Mac users get a modest update this year

    Hands on: Samsung Galaxy A5 2017 review

    Hands on: Samsung Galaxy A5 2017 review

    Heroes of the Storm Global Championship 2017 starts tomorrow, here’s what you need to know

    Heroes of the Storm Global Championship 2017 starts tomorrow, here’s what you need to know

    Harnessing the power of VR with Power Rangers and Snapdragon 835

    Harnessing the power of VR with Power Rangers and Snapdragon 835

    So you want to be a startup investor? Here are things you should know

    So you want to be a startup investor? Here are things you should know

  • Lifestyle
    • All
    • Fashion
    • Food
    • Health
    • Travel
    Shooting More than 40 Years of New York’s Halloween Parade

    Shooting More than 40 Years of New York’s Halloween Parade

    Heroes of the Storm Global Championship 2017 starts tomorrow, here’s what you need to know

    Heroes of the Storm Global Championship 2017 starts tomorrow, here’s what you need to know

    Why Millennials Need to Save Twice as Much as Boomers Did

    Why Millennials Need to Save Twice as Much as Boomers Did

    Doctors take inspiration from online dating to build organ transplant AI

    Doctors take inspiration from online dating to build organ transplant AI

    How couples can solve lighting disagreements for good

    How couples can solve lighting disagreements for good

    Ducati launch: Lorenzo and Dovizioso’s Desmosedici

    Ducati launch: Lorenzo and Dovizioso’s Desmosedici

    Trending Tags

    • Golden Globes
    • Game of Thrones
    • MotoGP 2017
    • eSports
    • Fashion Week
  • Review
    The Legend of Zelda: Breath of the Wild gameplay on the Nintendo Switch

    The Legend of Zelda: Breath of the Wild gameplay on the Nintendo Switch

    Shadow Tactics: Blades of the Shogun Review

    Shadow Tactics: Blades of the Shogun Review

    macOS Sierra review: Mac users get a modest update this year

    macOS Sierra review: Mac users get a modest update this year

    Hands on: Samsung Galaxy A5 2017 review

    Hands on: Samsung Galaxy A5 2017 review

    The Last Guardian Playstation 4 Game review

    The Last Guardian Playstation 4 Game review

    Intel Core i7-7700K ‘Kaby Lake’ review

    Intel Core i7-7700K ‘Kaby Lake’ review

No Result
View All Result
  • Home
    • Home – Layout 1
    • Home – Layout 2
    • Home – Layout 3
    • Home – Layout 4
    • Home – Layout 5
    • Home – Layout 6
  • News
    • All
    • Business
    • Politics
    • Science
    • World
    Hillary Clinton in white pantsuit for Trump inauguration

    Hillary Clinton in white pantsuit for Trump inauguration

    Amazon has 143 billion reasons to keep adding more perks to Prime

    Amazon has 143 billion reasons to keep adding more perks to Prime

    Shooting More than 40 Years of New York’s Halloween Parade

    Shooting More than 40 Years of New York’s Halloween Parade

    These Are the 5 Big Tech Stories to Watch in 2017

    These Are the 5 Big Tech Stories to Watch in 2017

    Why Millennials Need to Save Twice as Much as Boomers Did

    Why Millennials Need to Save Twice as Much as Boomers Did

    Doctors take inspiration from online dating to build organ transplant AI

    Doctors take inspiration from online dating to build organ transplant AI

    Trending Tags

    • Trump Inauguration
    • United Stated
    • White House
    • Market Stories
    • Election Results
  • Tech
    • All
    • Apps
    • Gadget
    • Mobile
    • Startup
    The Legend of Zelda: Breath of the Wild gameplay on the Nintendo Switch

    The Legend of Zelda: Breath of the Wild gameplay on the Nintendo Switch

    Shadow Tactics: Blades of the Shogun Review

    Shadow Tactics: Blades of the Shogun Review

    macOS Sierra review: Mac users get a modest update this year

    macOS Sierra review: Mac users get a modest update this year

    Hands on: Samsung Galaxy A5 2017 review

    Hands on: Samsung Galaxy A5 2017 review

    The Last Guardian Playstation 4 Game review

    The Last Guardian Playstation 4 Game review

    These Are the 5 Big Tech Stories to Watch in 2017

    These Are the 5 Big Tech Stories to Watch in 2017

    Trending Tags

    • Nintendo Switch
    • CES 2017
    • Playstation 4 Pro
    • Mark Zuckerberg
  • Entertainment
    • All
    • Gaming
    • Movie
    • Music
    • Sports
    The Legend of Zelda: Breath of the Wild gameplay on the Nintendo Switch

    The Legend of Zelda: Breath of the Wild gameplay on the Nintendo Switch

    macOS Sierra review: Mac users get a modest update this year

    macOS Sierra review: Mac users get a modest update this year

    Hands on: Samsung Galaxy A5 2017 review

    Hands on: Samsung Galaxy A5 2017 review

    Heroes of the Storm Global Championship 2017 starts tomorrow, here’s what you need to know

    Heroes of the Storm Global Championship 2017 starts tomorrow, here’s what you need to know

    Harnessing the power of VR with Power Rangers and Snapdragon 835

    Harnessing the power of VR with Power Rangers and Snapdragon 835

    So you want to be a startup investor? Here are things you should know

    So you want to be a startup investor? Here are things you should know

  • Lifestyle
    • All
    • Fashion
    • Food
    • Health
    • Travel
    Shooting More than 40 Years of New York’s Halloween Parade

    Shooting More than 40 Years of New York’s Halloween Parade

    Heroes of the Storm Global Championship 2017 starts tomorrow, here’s what you need to know

    Heroes of the Storm Global Championship 2017 starts tomorrow, here’s what you need to know

    Why Millennials Need to Save Twice as Much as Boomers Did

    Why Millennials Need to Save Twice as Much as Boomers Did

    Doctors take inspiration from online dating to build organ transplant AI

    Doctors take inspiration from online dating to build organ transplant AI

    How couples can solve lighting disagreements for good

    How couples can solve lighting disagreements for good

    Ducati launch: Lorenzo and Dovizioso’s Desmosedici

    Ducati launch: Lorenzo and Dovizioso’s Desmosedici

    Trending Tags

    • Golden Globes
    • Game of Thrones
    • MotoGP 2017
    • eSports
    • Fashion Week
  • Review
    The Legend of Zelda: Breath of the Wild gameplay on the Nintendo Switch

    The Legend of Zelda: Breath of the Wild gameplay on the Nintendo Switch

    Shadow Tactics: Blades of the Shogun Review

    Shadow Tactics: Blades of the Shogun Review

    macOS Sierra review: Mac users get a modest update this year

    macOS Sierra review: Mac users get a modest update this year

    Hands on: Samsung Galaxy A5 2017 review

    Hands on: Samsung Galaxy A5 2017 review

    The Last Guardian Playstation 4 Game review

    The Last Guardian Playstation 4 Game review

    Intel Core i7-7700K ‘Kaby Lake’ review

    Intel Core i7-7700K ‘Kaby Lake’ review

No Result
View All Result
Ai News
No Result
View All Result
Home Machine Learning

Declarative and Imperative Prompt Engineering for Generative AI

AiNEWS2025 by AiNEWS2025
2025-07-26
in Machine Learning
0
Declarative and Imperative Prompt Engineering for Generative AI
0
SHARES
0
VIEWS
Share on FacebookShare on Twitter


refers to the careful design and optimization of inputs (e.g., queries or instructions) for guiding the behavior and responses of generative AI models. Prompts are typically structured using either the declarative or imperative paradigm, or a mixture of both. The choice of paradigm can have a big impact on the accuracy and relevance of the resulting model output. This article provides a conceptual overview of declarative and imperative prompting, discusses advantages and limitations of each paradigm, and considers the practical implications.

The What and the How

In simple terms, declarative prompts express what should be done, while imperative prompts specify how something should be done. Suppose you are at a pizzeria with a friend. You tell the waiter that you will have the Neapolitan. Since you only mention the type of pizza you want without specifying exactly how you want it prepared, this is an example of a declarative prompt. Meanwhile, your friend — who has some very particular culinary preferences and is in the mood for a bespoke pizza alle quattro stagioni — proceeds to tell the waiter exactly how she would like it made; this is an example of an imperative prompt.

Declarative and imperative paradigms of expression have a long history in computing, with some programming languages favoring one paradigm over the other. A language such as C tends to be used for imperative programming, while a language like Prolog is geared towards declarative programming. For example, consider the following problem of identifying the ancestors of a person named Charlie. We happen to know the following facts about Charlie’s relatives: Bob is Charlie’s parent, Alice is Bob’s parent, Susan is Dave’s parent, and John is Alice’s parent. Based on this information, the code below shows how we can identify Charlie’s ancestors using Prolog.

parent(alice, bob).
parent(bob, charlie).
parent(susan, dave).
parent(john, alice).

ancestor(X, Y) :- parent(X, Y).
ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y).

get_ancestors(Person, Ancestors) :- findall(X, ancestor(X, Person), Ancestors).

?- get_ancestors(charlie, Ancestors).

Although the Prolog syntax may seem strange at first, it actually expresses the problem we wish to solve in a concise and intuitive way. First, the code lays out the known facts (i.e., who is whose parent). It then recursively defines the predicate ancestor(X, Y), which evaluates to true if X is an ancestor of Y. Finally, the predicate findall(X, Goal, List) triggers the Prolog interpreter to repeatedly evaluate Goal and store all successful bindings of X in List. In our case, this means identifying all solutions to ancestor(X, Person) and storing them in the variable Ancestors. Notice that we do not specify the implementation details (the “how”) of any of these predicates (the “what”).

In contrast, the C implementation below identifies Charlie’s ancestors by describing in painstaking detail exactly how this should be done.

#include 
#include 

#define MAX_PEOPLE 10
#define MAX_ANCESTORS 10

// Structure to represent parent relationships
typedef struct {
    char parent[20];
    char child[20];
} ParentRelation;

ParentRelation relations[] = {
    {"alice", "bob"},
    {"bob", "charlie"},
    {"susan", "dave"},
    {"john", "alice"}
};

int numRelations = 4;

// Check if X is a parent of Y
int isParent(const char *x, const char *y) {
    for (int i = 0; i 

Nowadays, the increasing availability of feature-rich libraries and APIs, and the consolidation of architectural best practices (e.g., programming to interfaces, using configuration over code), allows us to focus on the “what” without having to worry about the “how” in many cases. Indeed, as suggested in the short video below, a lot of code can — and perhaps should — be written in a declarative manner.

From Programming to Prompting

If we think of prompt engineering as the next evolutionary step in programming — tracing the journey from machine-centric languages such as Assembly and C to more human-centric languages like Java and Python — then the relevance of declarative and imperative paradigms to prompt engineering becomes apparent. Although we can now instruct a generative AI model simply using natural language, we still need to decide on the extent to which the instruction should express what is to be achieved versus how it is to be achieved.

Consider the scenario of a developer who would like to use an AI assistant to generate code — specifically, Python code to compute the cosine distance between two vectors. Following are three versions of a prompt that the developer might write to achieve her goal. Version 1 is declarative, Version 2 is imperative, and Version 3 is a hybrid of the two. The code generated by OpenAI’s GPT-4 model in response to each of these prompts is also provided.

Prompt Version 1 (Declarative)

Write a short and fast function in Python to compute the cosine distance between two input vectors.

Result:

import numpy as np

def cosine_distance(u, v):
    u = np.array(u)
    v = np.array(v)
    return 1 - np.dot(u, v) / (np.linalg.norm(u) * np.linalg.norm(v))

Prompt Version 2 (Imperative)

Write a Python function cosine_distance(v1, v2) that computes the cosine distance between two vectors. Follow these steps using only built-in functions:

1. Raise a ValueError exception if v1 or v2 are empty, or if v1 and v2 are of different lengths.

2. Compute the dot product of v1 and v2.

3. Compute the magnitude of each vector and raise a ValueError if either magnitude is zero.

4. Divide the dot product by the product of the two magnitudes to get the cosine similarity.

5. Do 1 minus the cosine similarity to obtain cosine distance.

6. Return the resulting distance.

Result:

import math

def cosine_distance(v1, v2):
    # Step 1: Input validation
    if not v1 or not v2:
        raise ValueError("Input vectors must not be empty.")
    if len(v1) != len(v2):
        raise ValueError("Vectors must be of the same length.")

    # Step 2: Compute dot product
    dot_product = sum(a * b for a, b in zip(v1, v2))

    # Step 3: Compute magnitudes
    mag_v1 = math.sqrt(sum(a ** 2 for a in v1))
    mag_v2 = math.sqrt(sum(b ** 2 for b in v2))

    if mag_v1 == 0 or mag_v2 == 0:
        raise ValueError("Vectors must not have zero magnitude.")

    # Step 4 & 5: Compute cosine similarity and distance
    cosine_similarity = dot_product / (mag_v1 * mag_v2)
    cosine_distance = 1 - cosine_similarity

    # Step 6: Return result
    return cosine_distance

Prompt Version 3 (Hybrid)

Write a fast Python function that computes the cosine distance between two vectors v1 and v2 using relevant optimized functions from numpy and scipy. The function should validate the input vectors (non-empty, equal length) and gracefully handle invalid inputs with exceptions.

Result:

import numpy as np
from scipy.spatial.distance import cosine

def cosine_distance(v1, v2):
    # Convert inputs to NumPy arrays
    v1 = np.asarray(v1, dtype=np.float64)
    v2 = np.asarray(v2, dtype=np.float64)

    # Validation checks
    if v1.size == 0 or v2.size == 0:
        raise ValueError("Input vectors must be non-empty.")
    if v1.shape != v2.shape:
        raise ValueError("Vectors must have the same shape.")
    if not np.any(v1) or not np.any(v2):
        raise ValueError("Vectors must not be zero vectors.")

    # Compute cosine distance using optimized function
    return cosine(v1, v2)

Horses for Courses

The three prompts and their respective AI-generated implementations shown above imply different trade-offs and may be suited to different personas and scenarios in practice.

The declarative prompt (Version 1) is short and simple. It does not specify details of the exact algorithmic approach to be taken, expressing instead the high-level task only. As such, it promotes creativity and flexibility in implementation. The downside of such a prompt, of course, is that the result may not always be reproducible or robust; in the above case, the code generated by the declarative prompt could vary significantly across inference calls, and does not handle edge cases, which could be a problem if the code is intended for use in production. Despite these limitations, typical personas who may favor the declarative paradigm include product managers, UX designers, and business domain experts who lack coding expertise and may not need production-grade AI responses. Software developers and data scientists may also use declarative prompting to quickly generate a first draft, but they would be expected to review and refine the code afterward. Of course, one must keep in mind that the time needed to improve AI-generated code may cancel out the time saved by writing a short declarative prompt in the first place.

By contrast, the imperative prompt (Version 2) leaves very little to chance — each algorithmic step is specified in detail. Dependencies on non-standard packages are explicitly avoided, which can sidestep certain problems in production (e.g., breaking changes or deprecations in third-party packages, difficulty debugging strange code behavior, exposure to security vulnerabilities, installation overhead). But the greater control and robustness come at the cost of a verbose prompt, which may be almost as effort-intensive as writing the code directly. Typical personas who opt for imperative prompting may include software developers and data scientists. While they are quite capable of writing the actual code from scratch, they may find it more efficient to feed pseudocode to a generative AI model instead. For example, a Python developer might use pseudocode to quickly generate code in a different and less familiar programming language, such as C++ or Java, thereby reducing the likelihood of syntactic errors and the time spent debugging them.

Finally, the hybrid prompt (Version 3) seeks to combine the best of both worlds, using imperative instructions to fix key implementation details (e.g., stipulating the use of NumPy and SciPy), while otherwise employing declarative formulations to keep the overall prompt concise and easy to follow. Hybrid prompts offer freedom within a framework, guiding the implementation without completely locking it in. Typical personas who may lean toward a hybrid of declarative and imperative prompting include senior developers, data scientists, and solution architects. For example, in the case of code generation, a data scientist may wish to optimize an algorithm using advanced libraries that a generative AI model might not select by default. Meanwhile, a solution architect may need to explicitly steer the AI away from certain third-party components to comply with architectural guidelines.

Ultimately, the choice between declarative and imperative prompt engineering for generative AI should be a deliberate one, weighing the pros and cons of each paradigm in the given application context.

Source link

#Declarative #Imperative #Prompt #Engineering #Generative

Tags: Declarative Programminggenerative AIImperative ProgrammingLarge Language ModelsPrompt Engineering
Previous Post

After BlackSuit is taken down, new ransomware group Chaos emerges

Next Post

The Download: gas and oil’s role in climate tech, and using AI to decipher ancient Latin

AiNEWS2025

AiNEWS2025

Next Post
The Download: gas and oil’s role in climate tech, and using AI to decipher ancient Latin

The Download: gas and oil’s role in climate tech, and using AI to decipher ancient Latin

Stay Connected test

  • 23.9k Followers
  • 99 Subscribers
  • Trending
  • Comments
  • Latest
A tiny new open source AI model performs as well as powerful big ones

A tiny new open source AI model performs as well as powerful big ones

0
Water Cooler Small Talk: The Birthday Paradox 🎂🎉 | by Maria Mouschoutzi, PhD | Sep, 2024

Water Cooler Small Talk: The Birthday Paradox 🎂🎉 | by Maria Mouschoutzi, PhD | Sep, 2024

0
Ghost of Yōtei: The acclaimed Ghost of Tsushima is getting a sequel

Ghost of Yōtei: The acclaimed Ghost of Tsushima is getting a sequel

0
Best Headphones for Working Out (2024): Bose, Shokz, JLab

Best Headphones for Working Out (2024): Bose, Shokz, JLab

0
Google Cloud and Palo Alto Networks sign deal worth nearly  billion

Google Cloud and Palo Alto Networks sign deal worth nearly $10 billion

2025-12-22
Bio-hybrid robots turn food waste into functional machines

Bio-hybrid robots turn food waste into functional machines

2025-12-22
This company is developing gene therapies for muscle growth, erectile dysfunction, and “radical longevity”

This company is developing gene therapies for muscle growth, erectile dysfunction, and “radical longevity”

2025-12-22
Understanding Vibe Proving | Towards Data Science

Understanding Vibe Proving | Towards Data Science

2025-12-22

Recent News

Google Cloud and Palo Alto Networks sign deal worth nearly  billion

Google Cloud and Palo Alto Networks sign deal worth nearly $10 billion

2025-12-22
Bio-hybrid robots turn food waste into functional machines

Bio-hybrid robots turn food waste into functional machines

2025-12-22
This company is developing gene therapies for muscle growth, erectile dysfunction, and “radical longevity”

This company is developing gene therapies for muscle growth, erectile dysfunction, and “radical longevity”

2025-12-22
Understanding Vibe Proving | Towards Data Science

Understanding Vibe Proving | Towards Data Science

2025-12-22
Footer logo

We bring you the best Premium WordPress Themes that perfect for news, magazine, personal blog, etc. Check our landing page for details.

Follow Us

Browse by Category

  • AI & Cloud Computing
  • AI & Cybersecurity
  • AI & Sentiment Analysis
  • AI Applications
  • AI Ethics
  • AI Future Predictions
  • AI in Education
  • AI in Fintech
  • AI in Gaming
  • AI in Healthcare
  • AI in Startups
  • AI Innovations
  • AI News
  • AI Research
  • AI Tools & Automation
  • Apps
  • AR/VR & AI
  • Business
  • Deep Learning
  • Emerging Technologies
  • Entertainment
  • Fashion
  • Food
  • Gadget
  • Gaming
  • Health
  • Lifestyle
  • Machine Learning
  • Mobile
  • Movie
  • Music
  • News
  • Politics
  • Review
  • Robotics & Smart Systems
  • Science
  • Sports
  • Startup
  • Tech
  • Travel
  • World

Recent News

Google Cloud and Palo Alto Networks sign deal worth nearly  billion

Google Cloud and Palo Alto Networks sign deal worth nearly $10 billion

2025-12-22
Bio-hybrid robots turn food waste into functional machines

Bio-hybrid robots turn food waste into functional machines

2025-12-22
  • About
  • Advertise
  • Privacy & Policy
  • Contact

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.

No Result
View All Result

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.