Objective Driven Prompt System

Dr. Manoj Kumar Yadav
5 min readJun 13, 2024

--

To implement a system using “PROMPTS” Java library that takes an objective, generates questions based on that objective, retrieves answers from an API, and checks if all topics are covered, we need to follow these steps:

  1. Define the Objective and Create Prompts:
  • Start with an objective for data collection.
  • Generate a chain of questions (prompts) based on this objective.

2. Handle the Response:

  • Use an API to get answers to the generated questions.
  • Modify variables and update prompts as necessary based on responses.

3. Check Coverage:

  • Ensure that all topics related to the objective are covered by the questions and responses.

Here’s a step-by-step implementation:

Step 1: Define the Objective and Create Prompts

First, we’ll define the objective and generate initial prompts.

Step 2: Handle the Response

We’ll simulate getting answers from an API and modify variables accordingly.

Step 3: Check Coverage

Ensure all topics are covered based on the provided objective.

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class ObjectiveDrivenPromptSystem {

public static void main(String[] args) {
// Define the objective
String objective = "Collect data on gear tooth calculations.";

// Generate initial prompts based on the objective
List<Prompt> prompts = generatePromptsBasedOnObjective(objective);

// Initialize the PROMPTS repository
PROMPTS promptRepository = new PROMPTS();
for (int i = 0; i < prompts.size(); i++) {
Prompt prompt = prompts.get(i);
String promptId = "Q" + (i + 1);
String nextPromptId = (i < prompts.size() - 1) ? "Q" + (i + 2) : null;
promptRepository.addPrompt(promptId, prompt.getText(), nextPromptId, prompt.getDefaultVariables(), prompt.getVersion(), prompt.getModelId());
}

// Variables to replace in prompts
Map<String, String> initialVariables = new HashMap<>();
initialVariables.put("gear_ratio", "4");
initialVariables.put("module", "2");

// Initialize response handler
PromptResponseHandler responseHandler = new ExampleHandler();

// Iterate through the chain of prompts starting from Q1
Map<String, Map<String, String>> promptResponses = promptRepository.iterateThroughChain("Q1", initialVariables, responseHandler);

// Check if all topics are covered
checkCoverage(promptResponses, objective);
}

private static List<Prompt> generatePromptsBasedOnObjective(String objective) {
List<Prompt> prompts = new ArrayList<>();

if (objective.toLowerCase().contains("gear tooth calculations")) {
prompts.add(new Prompt("Step 1: Calculate the Number of Teeth\nGiven a desired gear ratio of {gear_ratio} and a module of {module} mm:\n- Formula for number of teeth on the driver gear (N1): N1 = {number_of_teeth_driver}\n- Formula for number of teeth on the driven gear (N2): N2 = N1 * {gear_ratio}\nCalculate N1 and N2.", "1.0", "ModelA"));
prompts.add(new Prompt("Step 2: Calculate the Module\nGiven a gear with a pitch circle diameter of {pitch_circle_diameter} mm and a number of teeth of {number_of_teeth}:\n- Formula: Module (m) = {pitch_circle_diameter} / {number_of_teeth}\nCalculate the module.", "1.0", "ModelA"));
prompts.add(new Prompt("Step 3: Calculate Addendum and Dedendum\nGiven a module of {module} mm:\n- Addendum (a) = module (m)\n- Dedendum (d) = 1.25 * module (m)\nCalculate the addendum and dedendum.", "1.0", "ModelA"));
prompts.add(new Prompt("Step 4: Calculate Bending Stress Using the Lewis Formula\nGiven a face width of {face_width} mm, module of {module} mm, and a load of {load} N:\n- Lewis formula: σ = (W * P) / (F * Y * m)\nWhere:\n - σ = Bending stress\n - W = Load ({load} N)\n - P = Circular pitch (π * m)\n - F = Face width ({face_width} mm)\n - Y = Lewis form factor (depends on the number of teeth)\nCalculate the bending stress.", "1.0", "ModelA"));
prompts.add(new Prompt("Step 5: Calculate Hertzian Contact Stress\nGiven a module of {module} mm and material properties {material_properties}:\n- Formula: σ_H = sqrt[(P * (1 - ν^2) / (π * E)) * ((1 - ν1^2) / E1 + (1 - ν2^2) / E2)]\nWhere:\n - P = Load per unit length\n - ν = Poisson's ratio\n - E = Young's modulus\n - ν1, ν2 = Poisson's ratio of materials 1 and 2\n - E1, E2 = Young's modulus of materials 1 and 2\nCalculate the Hertzian contact stress.", "1.0", "ModelA"));
}

return prompts;
}

private static void checkCoverage(Map<String, Map<String, String>> promptResponses, String objective) {
// Here we would check if all topics related to the objective are covered
// This can be customized based on specific requirements

System.out.println("Checking coverage for objective: " + objective);
for (Map.Entry<String, Map<String, String>> entry : promptResponses.entrySet()) {
System.out.println("Prompt ID: " + entry.getKey());
System.out.println("Variables: " + entry.getValue());
}

System.out.println("All topics covered.");
}
}

class ExampleHandler extends PromptResponseHandler {
public ExampleHandler() {
super("127.0.0.1"); // Example IP address
}

@Override
public Map<String, String> modifyVariables(String promptId, Map<String, String> currentVariables) {
// Simulate calling an API to get new variables
Map<String, String> newVariables = new HashMap<>();

// Mock API responses
switch (promptId) {
case "Q1":
newVariables.put("number_of_teeth_driver", "20");
newVariables.put("gear_ratio", "4");
break;
case "Q2":
newVariables.put("pitch_circle_diameter", "40");
newVariables.put("number_of_teeth", "20");
break;
case "Q3":
newVariables.put("module", "2");
break;
case "Q4":
newVariables.put("face_width", "10");
newVariables.put("load", "1000");
break;
case "Q5":
newVariables.put("material_properties", "Steel");
newVariables.put("module", "2");
break;
}

return newVariables;
}

@Override
public Object handleResponse(String promptId, Map<String, String> variables) {
// Handle the response (e.g., print it, send it to an API, etc.)
System.out.println("Handling response for prompt ID: " + promptId);
return variables;
}
}

// The rest of the classes (Prompt, PROMPTS, etc.) would be the same as defined in the p
revious parts of the conversation.

Explanation:

  1. Objective Definition and Prompt Generation:
  • We define the objective and generate a list of prompts based on the objective.

2. Prompt Repository Initialization:

  • We initialize the PROMPTS repository and add prompts to it.

3. Initial Variables:

  • Define initial variables for replacing placeholders in the prompts.

4. Response Handling:

  • Use the ExampleHandler class to simulate API responses and modify variables accordingly.

5. Iterate Through Chain:

  • Iterate through the chain of prompts, updating variables and handling responses.

6. Check Coverage:

  • Check if all topics related to the objective are covered by the questions and responses.

Running the Code:

To run this code, ensure you have all the necessary classes (e.g., Prompt, PROMPTS, PromptResponseHandler) defined as discussed previously. This code provides a simple command-line interface to simulate the prompt-response workflow based on a defined objective.

Prompt: Step 1: Calculate the Number of Teeth
Given a desired gear ratio of 4 and a module of 2 mm:
- Formula for number of teeth on the driver gear (N1): N1 =
- Formula for number of teeth on the driven gear (N2): N2 = N1 * 4
Calculate N1 and N2.
Handling response for prompt ID: Q1
Prompt: Step 2: Calculate the Module
Given a gear with a pitch circle diameter of mm and a number of teeth of :
- Formula: Module (m) = /
Calculate the module.
Handling response for prompt ID: Q2
Prompt: Step 3: Calculate Addendum and Dedendum
Given a module of 2 mm:
- Addendum (a) = module (m)
- Dedendum (d) = 1.25 * module (m)
Calculate the addendum and dedendum.
Handling response for prompt ID: Q3
Prompt: Step 4: Calculate Bending Stress Using the Lewis Formula
Given a face width of mm, module of 2 mm, and a load of N:
- Lewis formula: ? = (W * P) / (F * Y * m)
Where:
- ? = Bending stress
- W = Load ( N)
- P = Circular pitch (? * m)
- F = Face width ( mm)
- Y = Lewis form factor (depends on the number of teeth)
Calculate the bending stress.
Handling response for prompt ID: Q4
Prompt: Step 5: Calculate Hertzian Contact Stress
Given a module of 2 mm and material properties :
- Formula: ?_H = sqrt[(P * (1 - ?^2) / (? * E)) * ((1 - ?1^2) / E1 + (1 - ?2^2) / E2)]
Where:
- P = Load per unit length
- ? = Poisson's ratio
- E = Young's modulus
- ?1, ?2 = Poisson's ratio of materials 1 and 2
- E1, E2 = Young's modulus of materials 1 and 2
Calculate the Hertzian contact stress.
Handling response for prompt ID: Q5
Checking coverage for objective: Collect data on gear tooth calculations.
Prompt ID: Q1
Variables: {module=2, gear_ratio=4}
Prompt ID: Q2
Variables: {module=2, gear_ratio=4}
Prompt ID: Q3
Variables: {module=2, gear_ratio=4}
Prompt ID: Q4
Variables: {module=2, gear_ratio=4}
Prompt ID: Q5
Variables: {module=2, gear_ratio=4}
All topics covered.
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time: 6.044 s
Finished at: 2024-06-13T11:30:50+05:30
------------------------------------------------------------------------

Reference:

Design Pattern For AI Backend: Chain of Responsibility Pattern & Strategy Pattern

True Geometry Blog

--

--

Dr. Manoj Kumar Yadav

Doctor of Business Administration | VP - Engineering at redBus | Data Engineering | ML | Servers | Serverless | Java | Python | Dart | 3D/2D