This sample demonstrates how to use DotPrompt files with Genkit Java for building AI applications with complex inputs and outputs.
- DotPrompt Files: Load and use
.promptfiles with Handlebars templating - Complex Input Schemas: Handle nested objects, arrays, and optional fields
- Complex Output Schemas: Parse structured JSON responses into Java objects
- Prompt Variants: Use different prompt variations (e.g.,
recipe.robot.prompt) - Partials: Include reusable template fragments (e.g.,
_style.prompt)
Located in src/main/resources/prompts/:
recipe.prompt- Generate recipes with structured outputrecipe.robot.prompt- Robot-themed recipe generationstory.prompt- Story generation with personality optionstravel-planner.prompt- Complex travel itinerary generationcode-review.prompt- Code analysis with detailed output_style.prompt- Partial template for personality styling
- Java 21+
- Maven 3.6+
- OpenAI API key
# Set your OpenAI API key
export OPENAI_API_KEY=your-api-key-here
# Navigate to the sample directory
cd java/samples/dotprompt
# Run the sample
./run.sh
# Or: mvn compile exec:java# Set your OpenAI API key
export OPENAI_API_KEY=your-api-key-here
# Navigate to the sample directory
cd java/samples/dotprompt
# Run with Genkit CLI
genkit start -- ./run.shThe Dev UI will be available at http://localhost:4000
curl -X POST http://localhost:8080/chefFlow \
-H 'Content-Type: application/json' \
-d '{"food":"pasta carbonara","ingredients":["bacon","eggs","parmesan"]}'curl -X POST http://localhost:8080/robotChefFlow \
-H 'Content-Type: application/json' \
-d '{"food":"pizza"}'curl -X POST http://localhost:8080/tellStory \
-H 'Content-Type: application/json' \
-d '{"subject":"a brave knight","personality":"dramatic","length":"short"}'curl -X POST http://localhost:8080/planTrip \
-H 'Content-Type: application/json' \
-d '{
"destination": "Tokyo",
"duration": 5,
"budget": "$3000",
"interests": ["food", "culture", "technology"],
"travelStyle": "adventure"
}'curl -X POST http://localhost:8080/reviewCode \
-H 'Content-Type: application/json' \
-d '{
"code": "function add(a, b) { return a + b; }",
"language": "javascript",
"analysisType": "best practices"
}'{
"title": "Pasta Carbonara",
"ingredients": [
{"name": "spaghetti", "quantity": "400g"},
{"name": "bacon", "quantity": "200g"}
],
"steps": ["Step 1...", "Step 2..."],
"prepTime": "10 minutes",
"cookTime": "20 minutes",
"servings": 4
}{
"tripName": "Tokyo Adventure",
"destination": "Tokyo",
"duration": 5,
"dailyItinerary": [
{
"day": 1,
"title": "Arrival & Exploration",
"activities": [
{
"time": "9:00 AM",
"activity": "Visit Senso-ji Temple",
"location": "Asakusa",
"estimatedCost": "$10",
"tips": "Go early to avoid crowds"
}
]
}
],
"estimatedBudget": {
"accommodation": "$800",
"food": "$500",
"activities": "$400",
"transportation": "$300",
"total": "$2000"
},
"packingList": ["Comfortable shoes", "Power adapter"],
"travelTips": ["Get a JR Pass", "Learn basic Japanese phrases"]
}Access the Genkit Development UI at http://localhost:3100 to:
- Browse available flows and prompts
- Test flows interactively
- View execution traces